-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2040.py
269 lines (238 loc) · 5.98 KB
/
2040.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import board as bd
import digitalio as dio
from analogio import AnalogIn
from time import sleep
import supervisor as sv
import busio as bus
from adafruit_debouncer import Debouncer
import microcontroller as uC
from adafruit_pn532.uart import PN532_UART
import adafruit_bitbangio as bang
MAXTEMP = 65.5
MAXVOL = 5.15
__loop = True
jmpr_first_removal = False
slider = bd.A3
mov = bd.A0
mov_pwr = dio.DigitalInOut(bd.GP11)
rx = bd.GP0
tx = bd.GP1
sda = bd.GP14
scl = bd.GP28
pn532_baud = 115200 #9600
uart_timeout = 2
uart = bus.UART(tx, rx, baudrate=baud)
green = dio.DigitalInOut(bd.GP20)
red = dio.DigitalInOut(bd.GP19)
yellow = dio.DigitalInOut(bd.GP17)
yellow_gnd = dio.DigitalInOut(bd.GP15)
grounds = [yellow_gnd]
jmpr = dio.DigitalInOut(bd.GP8)
jmpr.direction = dio.Direction.INPUT
#en = dio.DigitalInOut(bd.GP26)
#en.pull = dio.Pull.DOWN
jmpr_pwr = dio.DigitalInOut(bd.GP6)
outputs = [mov_pwr, jmper_pwr]
leds = [red, green, yellow]
inputs = [slider, mov]
analogs = {}
pn532 = None
samconf = True
tag_listen_timeout = 1
def set_pin_directions():
for out in outputs:
out.direction = dio.Direction.OUTPUT
out.value = True
for led in leds:
led.direction = dio.Direction.OUTPUT
for inp in inputs:
analogs[inp] = AnalogIn(inp)
for g in grounds:
ground(g)
def ground(pin):
pin.direction = dio.Direction.OUTPUT
pin.value = False
def get_voltage(pin):
return (pin.value * 3.3) / 65536
def ledson():
yellow.value = True
red.value = True
green.value = True
def ledsoff():
yellow.value = False
red.value = False
green.value = False
def blink_all():
ledson()
sleep(1.50)
ledsoff()
def blink(led, n):
initial_state = led.value
i = 0
while i <= n:
led.value = True
sleep(0.5)
i += 1
led.value = False
led.value = initial_state
def blink_fast(led, n):
initial_state = led.value
i = 0
while i <= n:
led.value = True
sleep(0.23)
i+=1
led.value = False
led.value = initial_state
def blink_slow(led, n):
initial_state = led.value
i = 0
while i <= n:
led.value = True
sleep(0.95)
i+=1
led.value = False
led.value = initial_state
def blink_one(led, n):
for l in leds:
if l != led:
l.value = False
blink_fast(led, n)
def rainbow(repeats):
for r in range(0,repeats):
ledsoff()
for led in leds:
led.value = True
sleep(0.25)
led.value = False
sleep(0.7)
def get_mov():
if analogs[mov].value > 36000:
return True
#if analogs[mov].value != init_mov_val:
if abs(analogs[mov].value - init_mov_val) > 25000:
return True
return False
def check_uart():
if uart.read(32) is not None:
blink_one(green,5)
green.value = True
return green.value
sleep(1)
blink_one(red, 5)
return False
def check_usb():
blink(green,2)
if sv.runtime.usb_connected:
blink_long(yellow, 1)
return True
else:
blink_long(green, 1)
return False
def reboot():
blink_fast(red, 5)
uC.on_next_reset(uC.RunMode.NORMAL)
uC.reset()
def enter_uf2():
if not sv.runtime.usb_connected:
uC.on_next_reset(uC.RunMode.UF2)
uC.reset()
def check_reset_reason():
pass
def diagnostics():
phases = [check_usb,
check_reset_reason,
check_temp_and_volt]
ledson()
sleep(1.3)
ledsoff()
phaseno = 1
for p in phases:
p()
rainbow(phaseno)
phaseno += 1
ledsoff()
#change_percent = ((float(current)-previous)/previous)*100
def debounce(pin):
pin.pull = dio.Pull.UP
button = Debouncer(pin)
while True:
button.update()
if button.fell:
return True
if button.rose:
return False
def check_movement():
red.value = False
while not red.value:
sleep(15)
jmpr_first_removal = True
if get_mov():
red.value = True
break
def check_temp_and_volt():
# . . .
second_cpu_check = False
for cpunit in uC.cpus:
if second_cpu_check:
yellow.value = True
if cpunit.temperature > MAXTEMP:
ledsoff()
red.value = True
blink_fast(red, 3)
reboot()
if cpunit.voltage > MAXVOLT:
yellow.value = True
blink_fast(red, 3)
reboot()
second_cpu_check = True
def no_jmpr_mode():
if not jmpr_first_removal:
print("[&] Launching diagnostics . . .")
diagnostics()
ledsoff()
check_movement()
def initialize():
set_pin_directions()
check_usb()
check_temp_and_volt()
if not jmpr.value:
check_reset_reason()
init_mov_val = analogs[mov].value
yellow.value = False
green.value = False
def pn532_init():
try:
uart = busio.UART(rx, tx, baudrate=pn532_baud, timeout=uart_timeout)
pn532 = PN532_UART(uart, debug=False)
ic, ver, rev, support = pn532.firmware_version
print("Found PN532 with firmware version: {0}.{1} -> 0x{2} [IC: {3}]".format(ver, rev, support, ic))
if samconf:
pn532.SAM_configuration()
blink(green, 2)
green.value = True
except:
blink(yellow, 2)
yellow.value = True
def uid_detection_mode():
while True:
if not jmpr.value:
break
if pn532 == None:
break
listener = pn532.listen_for_passive_target(timeout=tag_listen_timeout)
if pn532.uid_4b():
print("[*] 4 bytes UID")
blink_long(green, 2)
if pn532.uid_7b():
print("[*] 7 bytes UID")
blink_long(yellow, 2)
def core():
while __loop:
if not jmpr.value:
no_jmpr_mode()
else:
pn532_init()
uid_detection_mode()
initialize()
core()