-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (115 loc) · 2.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import machine
import time
import uos
from machine import I2C, Pin
# Third-Party
import sdcard
from eeprom import EEPROM
from epaper import EPD_5in65
# Local
import bitmap
# Fns
#
def display_bytes(epd, bytes_gen):
epd.send_command(0x61) # Set Resolution setting
epd.send_data(0x02)
epd.send_data(0x58)
epd.send_data(0x01)
epd.send_data(0xC0)
epd.send_command(0x10)
for b in bytes_gen:
epd.digital_write(epd.dc_pin, 1)
epd.digital_write(epd.cs_pin, 0)
epd.spi.write(b)
epd.digital_write(epd.cs_pin, 1)
epd.send_command(0x04) # 0x04
epd.BusyHigh()
epd.send_command(0x12) # 0x12
epd.BusyHigh()
epd.send_command(0x02) # 0x02
epd.BusyLow()
epd.delay_ms(200)
def clean_display(epd):
epd.EPD_5IN65F_Clear(epd.White)
epd.EPD_5IN65F_Clear(epd.Clean)
def poweroff(i):
pin = Pin(i, Pin.OUT)
while 1:
pin.high()
time.sleep_ms(500)
pin.low()
time.sleep_ms(500)
def is_switch_on(i):
pin = Pin(i, mode=Pin.IN, pull=Pin.PULL_UP)
return bool(1 - pin.value())
def read_rom(eeprom):
rom_data = eeprom.read(1, 3)
try:
i = int(rom_data)
except ValueError:
i = 0
return i
def write_rom(eeprom, i):
eeprom.write(1, f"{i:03d}")
def debug():
led = Pin(25, Pin.OUT)
led.toggle()
# Constants
#
IMAGE_DIR = "/imgs"
DONE_PIN = 17
SWITCH_PIN = 20
time.sleep(2)
if not is_switch_on(SWITCH_PIN):
poweroff(DONE_PIN)
# EEPROM
#
I2C_ADDR = 0x50 # DEC 80, HEX 0x50
EEPROM_SIZE = 16 # AT24C16 on 0x50
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400_000)
eeprom = EEPROM(addr=I2C_ADDR, at24x=EEPROM_SIZE, i2c=i2c)
image_i = read_rom(eeprom)
# SD Card Reader
#
spi = machine.SPI(
0,
baudrate=1_000_000,
polarity=0,
phase=0,
bits=8,
firstbit=machine.SPI.MSB,
sck=machine.Pin(2),
mosi=machine.Pin(3), # TX
miso=machine.Pin(4) # RX
)
cs = machine.Pin(5, machine.Pin.OUT)
for _ in range(3):
try:
sd = sdcard.SDCard(spi, cs)
except OSError:
continue
break
vfs = uos.VfsFat(sd)
uos.mount(vfs, IMAGE_DIR)
images = [item for item in sorted(uos.listdir(IMAGE_DIR)) if item.endswith(".bmp")]
if image_i >= len(images):
image_i = 0
bmp_image = images[image_i]
else:
bmp_image = images[image_i]
image_i += 1
for _ in range(3):
write_rom(eeprom, image_i)
if read_rom(eeprom) == image_i:
break
time.sleep(1)
# ePaper Display
#
epd = EPD_5in65()
clean_display(epd)
with open(f"{IMAGE_DIR}/{bmp_image}", "rb") as img:
bytestream = bitmap.as_bytes(img)
display_bytes(epd, bytestream)
# Shutdown
#
poweroff(DONE_PIN)