-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaperture.py
More file actions
86 lines (58 loc) · 1.88 KB
/
aperture.py
File metadata and controls
86 lines (58 loc) · 1.88 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
import pcd8544_fb
import machine
import time
import math
from registry import registry
import apertureset
apertures = [cls() for cls in registry]
spi = machine.SPI(1, sck=machine.Pin(26), mosi=machine.Pin(27))
spi.init(baudrate=2000000, polarity=0, phase=0)
cs = machine.Pin(29)
dc = machine.Pin(28)
rst = machine.Pin(25)
lcd = pcd8544_fb.PCD8544_FB(spi, cs, dc, rst)
buttons = [
machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_UP), # sw2
machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_UP), # sw3
machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_UP), # sw4
]
clicked = [False, False, False]
pressed = [False, False, False]
debounce = [0, 0, 0]
def read_buttons():
for b, button in enumerate(buttons):
clicked[b] = False
if debounce[b] > 0:
debounce[b] -= 1
else:
if pressed[b] != (button.value() == 0):
pressed[b] = not pressed[b]
clicked[b] = pressed[b]
debounce[b] = 2
FRAME_PERIOD = 33
def main():
#lcd.contrast(0x3f)
lcd.invert(True)
lcd.clear()
aperture = 0
apertures[aperture].activate(lcd)
prev_tick = time.ticks_ms()
next_tick = time.ticks_add(prev_tick, FRAME_PERIOD)
while True:
tick = time.ticks_ms()
dt = time.ticks_diff(tick, prev_tick)
prev_tick = tick
read_buttons()
if clicked[0]:
aperture = (aperture + 1) % len(apertures)
apertures[aperture].activate(lcd)
apertures[aperture].update(lcd, dt, clicked)
tick = time.ticks_ms()
pad_tick = time.ticks_diff(next_tick, tick)
if pad_tick > 0:
time.sleep_ms(pad_tick)
next_tick = time.ticks_add(next_tick, FRAME_PERIOD)
else:
next_tick = time.ticks_add(tick, FRAME_PERIOD)
if __name__ == '__main__':
main()