Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·219 lines (185 loc) · 5.21 KB
/
Copy pathcode.py
File metadata and controls
executable file
·219 lines (185 loc) · 5.21 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import alarm
import microcontroller
import time
import board
import busio
import displayio
from digitalio import DigitalInOut, Direction, Pull
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
from fourwire import FourWire
import adafruit_ssd1680
displayio.release_displays()
#vars
counterLock = False
needsRefresh = True
pressed = False
locking = False
resetting = False
#init time
pressStart = float(0)
#init button
btn = DigitalInOut(board.BUTTON)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
#set display vars
spi = busio.SPI(board.EPD_SCK, MOSI=board.EPD_MOSI, MISO=None)
epdCs = board.EPD_CS
epdDc = board.EPD_DC
epdReset = board.EPD_RESET
epdBusy = board.EPD_BUSY
displayBus = FourWire(spi, command=epdDc, chip_select=epdCs, reset=epdReset, baudrate=1000000)
display = adafruit_ssd1680.SSD1680(
displayBus,
width=250,
height=122,
busy_pin=epdBusy,
rotation=270,
colstart = 8,
seconds_per_frame=2.0
)
group = displayio.Group()
#init fonts
big = bitmap_font.load_font("fonts/AtkinsonHyperlegibleMono-Medium-35.bdf")
small = bitmap_font.load_font("fonts/AtkinsonHyperlegibleMono-MediumItalic-15.bdf")
#init background
backgroundBitmap = displayio.Bitmap(250,122,1)
backgroundPalette = displayio.Palette(1)
backgroundPalette[0] = 0xFFFFFF
background = displayio.TileGrid(backgroundBitmap, pixel_shader=backgroundPalette, x =0, y=0)
group.append(background)
#init text
dispText = "days since fire alarm"
botText = label.Label(font = small, text = dispText, scale = 1, color = 0x000000)
botText.anchor_point = (0,0)
botText.anchored_position = (10, 105)
group.append(botText)
def saveLock(lock):
microcontroller.nvm[3] = lock
def loadLock():
lock = microcontroller.nvm[3]
return lock
def saveCount(count):
microcontroller.nvm[0:3] = count.to_bytes(3, 'big')
def loadCount():
val = int.from_bytes(microcontroller.nvm[0:3], 'big')
if val > 99999:
return 0
return val
print(loadCount())
#init counter
count = loadCount()
countText = str(count)
counter = label.Label(font= big, text = countText, scale = 2, color = 0x000000)
counter.anchor_point = (0,0)
counter.anchored_position =(10,30)
group.append(counter)
#init lock
lockText = ""
lockDisp = label.Label(font = small, text = lockText, scale = 1, color = 0x000000)
lockDisp.anchor_point = (0,0)
lockDisp.anchored_position = (240, 105)
group.append(lockDisp)
#init sleeping
sleepText = ""
sleepDisp = label.Label(font = small, text = sleepText, scale = 1, color = 0x000000)
sleepDisp.anchor_point = (0,0)
sleepDisp.anchored_position = (0, 0)
group.append(sleepDisp)
#display group
display.root_group = group
display.refresh()
#init sleep feature
btn.deinit()
pin_alarm = alarm.pin.PinAlarm(pin=board.BUTTON, value=False, pull=True)
def sleep():
if (time.monotonic() - pressStart) > 60:
sleepDisp.text = "zzz...sleeping...huhmgh...zzz"
time.sleep(display.time_to_refresh)
while display.busy:
time.sleep(0.01)
display.refresh()
print("sleeping")
alarm.exit_and_deep_sleep_until_alarms(pin_alarm)
if alarm.wake_alarm:
sleepDisp.text = ""
pressStart = time.monotonic()
counterLock = loadLock()
needsRefresh = True
#init button again
btn = DigitalInOut(board.BUTTON)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
def counterIncrement():
global count
global needsRefresh
if not counterLock:
count += 1
counter.text = str(count)
needsRefresh = True
saveCount(count)
print(count)
def counterDecrement():
global count
global needsRefresh
if not counterLock:
count -= 1
counter.text = str(count)
needsRefresh = True
saveCount(count)
print(count)
def reset():
global resetting
global count
if (time.monotonic() - pressStart) > 10 and pressed and not resetting and not counterLock:
resetting = True
print("resetting")
count = 0
counter.text = str(count)
saveCount(count)
print("Reset!")
refresh()
def refresh():
global needsRefresh
time.sleep(display.time_to_refresh)
while display.busy:
time.sleep(0.01)
display.refresh()
print("refreshed count")
needsRefresh = False
def lockUnlock():
global locking
global counterLock
counterLock = loadLock()
if counterLock:
lockDisp.text = "μ"
if (time.monotonic() - pressStart) > 4.5 and pressed and not locking:
locking = True
if counterLock:
print("unlocking")
counterLock = False
saveLock(counterLock)
lockDisp.text = ""
refresh()
else:
print("locking")
counterDecrement()
counterLock = True
saveLock(counterLock)
lockDisp.text = "μ"
refresh()
while True:
if not btn.value and not pressed:
pressed = True
counterIncrement()
pressStart = time.monotonic()
if btn.value:
pressed = False
locking = False
resetting = False
lockUnlock()
reset()
if ((time.monotonic() - pressStart) > 1.0 and needsRefresh and not pressed):
refresh()
sleep()
time.sleep(0.1)