-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
551 lines (490 loc) · 18.8 KB
/
code.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
## Adafruit Feather nRF52840 Sense all-the-devices demo
##
## https://www.adafruit.com/product/4516
## https://learn.adafruit.com/adafruit-feather-sense/
## https://circuitpython.org/board/feather_bluefruit_sense/
##
## Board has 6 i2c addresses in use:
##
## * 0x1C / 28 : LIS3MDL Magnetometer https://github.com/adafruit/Adafruit_CircuitPython_LIS3MDL
## * 0x39 / 57 : APDS9960 Light/Gesture/Proximity (IRQ on D36) https://github.com/adafruit/Adafruit_CircuitPython_APDS9960
## * 0x44 / 68 : SHT30 Humidity https://learn.adafruit.com/adafruit-sht31-d-temperature-and-humidity-sensor-breakout
## * 0x6A / 106 : LSM6DS33 Gyro + Accel (IRQ on D3) https://github.com/adafruit/Adafruit_CircuitPython_LSM6DS
## * 0x77 / 119 : BMP280 Temp/Pressure https://github.com/adafruit/Adafruit_CircuitPython_BMP280
##
## Other Peripherals
##
## Sound Sensor (PDM): MP34DT01-M: board.MICROPHONE_DATA (D34) is PDM Data, board.MICROPHONE_CLOCK (D35) is PDM clock
##
##
## External Peripherals in Dev Setup
##
## Adafruit FeatherWing Tripler https://www.adafruit.com/product/3417
##
## Adafruit 128x64 OLED FeatherWing https://www.adafruit.com/product/4650
##
## * 0x3C / 60 : OLED Display
## * Pin 9: Button A
## * Pin 6: Button B
## * Pin 5: Button C
##
## Adafruit NeoKey FeatherWing https://www.adafruit.com/product/4979
##
## Rewired the pins on my dev board so that they don't collide with the display's buttons or an additional NeoKey wing
##
## * Pin 10: NeoPixel
## * Pin 11: Switch B
## * Pin 12: Switch A
##
import board
import displayio
# import time
from neopixel import NeoPixel
# import countio
from adafruit_lis3mdl import LIS3MDL
from adafruit_lsm6ds.lsm6ds33 import LSM6DS33
from adafruit_apds9960.apds9960 import APDS9960
import adafruit_bmp280
from adafruit_sht31d import SHT31D
from adafruit_displayio_sh1107 import SH1107
import audiobusio
import array
import math
import microcontroller
import terminalio
from adafruit_display_text.label import Label
from adafruit_display_shapes.sparkline import Sparkline
import keypad
import gc
# print("Startup mem_free: {}".format(gc.mem_free()))
i2c = board.I2C()
## Mag
mag = LIS3MDL(i2c)
## Gyro/Accel
acc = LSM6DS33(i2c)
# acc_irq_counter = countio.Counter(board.ACCELEROMETER_GYRO_INTERRUPT)
## Light/Gesture/Color
apds = APDS9960(i2c)
# apds.proximity_interrupt_threshold = (0, 100, 4)
# apds_irq_counter = countio.Counter(board.PROXIMITY_LIGHT_INTERRUPT)
## Temperature/Pressure/Altitude BMP280
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
bmp280.mode = adafruit_bmp280.MODE_SLEEP # Start up in sleep mode
## Humidity/Temperature
sht30 = SHT31D(i2c)
## Microphone
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, bit_depth=16)
mic_samples = array.array('H', [0] * 160)
def mean(values):
return sum(values) / len(values)
def normalized_rms(values):
minbuf = int(mean(values))
samples_sum = sum(
float(sample - minbuf) * (sample - minbuf)
for sample in values
)
return math.sqrt(samples_sum / len(values))
## Helper: IRQ pulse count checker
# def count_checker(pulse_counter: countio.Counter) -> int:
# if pulse_counter.count > 0:
# current_count = pulse_counter.count
# pulse_counter.reset
# return current_count
# else:
# return 0
## Buttons
buttons = (
board.SWITCH, # Built-in user button
board.D9, # Display button A
board.D6, # Display button B
board.D5, # Display button C
board.D12, # Neokey A (left)
board.D11 # Neokey B (right)
)
keys = keypad.Keys(buttons, value_when_pressed=False, pull=True)
def retrieve_key_events(keys: keypad.Keys):
new_events = True
key_events = []
while new_events:
event = keys.events.get()
if event:
key_events.append(event)
else:
new_events = False
return key_events
## Neopixels
pixel_board = NeoPixel(board.NEOPIXEL, 1, brightness=0.1)
pixel_board[0] = (0, 0, 50)
pixels_keys = NeoPixel(board.D10, 2, brightness=0.1)
pixels_keys[0] = (50, 50, 50)
pixels_keys[1] = (50, 50, 50)
# print("Device setup complete, mem_free: {}".format(gc.mem_free()))
## Display
displayio.release_displays()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)
WIDTH = 128
HEIGHT = 62
BORDER = 2
display = SH1107(display_bus, width=WIDTH, height=HEIGHT, rotation=0)
font = terminalio.FONT
splash = displayio.Group()
display.show(splash)
# print("Display init complete, mem_free: {}".format(gc.mem_free()))
# Set up Page Numbers text area
page_text = "[{}/{}]"
page_label = Label(font, text=page_text.format(0,0), padding_top=0, padding_bottom=0)
page_label.anchor_point = (1.0, 0.0)
page_label.anchored_position = (128, 0)
splash.append(page_label)
# Set up Page Title text area
title_text = " "*16
title_label = Label(font, text=title_text, padding_top=0, padding_bottom=0)
title_label.anchor_point = (0.0, 0.0)
title_label.anchored_position = (0, 0)
splash.append(title_label)
# Set up Mag Data text area
mag_text = "Magnetometer\nX: {:>+9.4f}\nY: {:>+9.4f}\nZ: {:>+9.4f}"
mag_label = Label(font, text=mag_text.format(0.0, 0.0, 0.0), padding_top=0, padding_bottom=0, line_spacing=0.7)
mag_label.anchor_point = (0.0, 1.0)
mag_label.anchored_position = (0, 60)
mag_label.hidden = True
splash.append(mag_label)
# Set up Gyro Data text area
gyro_text = "Gyro\nX: {:>+6.2f}\nY: {:>+6.2f}\nZ: {:>+6.2f}"
gyro_label = Label(font, text=gyro_text.format(0.0, 0.0, 0.0), padding_top=0, padding_bottom=0, line_spacing=0.7)
gyro_label.anchor_point = (1.0, 1.0)
gyro_label.anchored_position = (128, 60)
gyro_label.hidden = True
splash.append(gyro_label)
# Set up Accel
acc_text = "Accel\nX: {:>+7.2f}\nY: {:>+7.2f}\nZ: {:>+7.2f}"
acc_label = Label(font, text=acc_text.format(0.0, 0.0, 0.0), padding_top=0, padding_bottom=0, line_spacing=0.7)
acc_label.anchor_point = (0.0, 1.0)
acc_label.anchored_position = (0, 60)
acc_label.hidden = True
splash.append(acc_label)
# Set up color/prox/gesture
color_text = "Color\nR: {:4d}\nG: {:4d}\nB: {:4d}\nC: {:4d}"
color_label = Label(font, text=color_text.format(0, 0, 0, 0), padding_top=0, padding_bottom=0, line_spacing=0.7)
color_label.anchor_point = (0.0, 1.0)
color_label.anchored_position = (0, 60)
color_label.hidden = True
splash.append(color_label)
prox_text = "Prox: {:6d}\nGesture: {:1d}"
prox_label = Label(font, text=prox_text.format(0, 0), padding_top=0, padding_bottom=0, line_spacing=0.7)
prox_label.anchor_point = (0.0, 1.0)
prox_label.anchored_position = (55, 60)
prox_label.hidden = True
splash.append(prox_label)
# Set up temp/pressure/altitude
temp_text = "Temp: {:6.2f} C\nPressure: {:7.2f} hPa\nAltitude: {:7.2f} m\nHumidity: {:7.2f} %"
temp_label = Label(font, text=temp_text.format(0.0, 0.0, 0.0, 0.0), padding_top=0, padding_bottom=0, line_spacing=0.7)
temp_label.anchor_point = (0.0, 1.0)
temp_label.anchored_position = (0, 60)
temp_label.hidden = True
splash.append(temp_label)
# Set up PDM microphone label
mic_text = "Audio Level: {:7.2f}"
mic_label = Label(font, text=mic_text.format(0.0, 0.0, 0.0, 0.0), padding_top=0, padding_bottom=0, line_spacing=0.7)
mic_label.anchor_point = (0.0, 1.0)
mic_label.anchored_position = (0, 60)
mic_label.hidden = True
splash.append(mic_label)
## Set up PDM microphone sound level sparkline
mic_sparkline_max_entries = 16
mic_sparkline = Sparkline(
width=126, height=30,
max_items=mic_sparkline_max_entries,
y_min=0, y_max=400,
x=1,
y=16)
mic_sparkline.hidden = True
mic_sparkline._spark_list = [1] * mic_sparkline_max_entries
splash.append(mic_sparkline)
mic_cycle_count = 0
gc.collect()
# Set up internals label
cpu_text = "CPU\nFreq: {:9.1f} MHz\nTemp: {:9.1f} C\nVolts: {:8.3} V\nMem Free: {:5d} bytes"
cpu_label = Label(font, text=cpu_text.format(0.0, 0.0, 0.0, 0), padding_top=0, padding_bottom=0, line_spacing=0.7)
cpu_label.anchor_point = (0.0, 1.0)
cpu_label.anchored_position = (0, 60)
cpu_label.hidden = True
splash.append(cpu_label)
# print("Display label setup complete, mem_free: {}".format(gc.mem_free()))
# Prep for Loop and Loop
reverse = False
pause = False
page = 1
page_max = 6
pagechange = True
page_label.text = page_text.format(page, page_max)
gc.collect()
while True:
## Handle IRQ events
# acc_irq_events = count_checker(acc_irq_counter)
# apds_irq_events = count_checker(apds_irq_counter)
# if acc_irq_events:
# print("{} accelerometer IRQ events detected".format(acc_irq_events))
# if apds_irq_events:
# print("{} APDS9960 IRQ events detected, threshold: {}, prox: {}".format(apds_irq_events, apds.proximity_interrupt_threshold, apds.proximity))
## Handle button/key inputs for page changes and animation pauses
should_pause = False
should_page = False
should_page_to = 0
should_print_mem = False
new_events = retrieve_key_events(keys)
if len(new_events) > 0:
for e in new_events:
# print(e)
if e.pressed: # We only care about pressed keys, not released ones
# Pause animations
if e.key_number is 0:
if not should_pause: # Only act on the first event
should_pause = True
pixel_board[0] = (40, 40, 40)
# Increment page
elif e.key_number is 1 or e.key_number is 4:
if not should_page: # Only act on the first event
should_page = True
should_page_to = page + 1
if should_page_to > page_max:
should_page_to = page_max
pagechange = False
else:
pagechange = True
pixel_board[0] = (0, 0, 50)
# Decrement page
elif e.key_number is 3 or e.key_number is 5:
if not should_page: # Only act on the first event
should_page = True
should_page_to = page - 1
if should_page_to <= 0:
should_page_to = 1
pagechange = False
else:
pagechange = True
pixel_board[0] = (0, 0, 50)
elif e.key_number is 2:
if not should_print_mem:
should_print_mem = True
if should_print_mem:
print("mem_free: {}".format(gc.mem_free()))
if should_pause:
pause = not pause
if should_page and pagechange:
if page != should_page_to:
page = should_page_to
else:
pagechange = False
## Handle page changes
if pagechange:
# print("Page changing to page #{}, mem free: {}".format(page, gc.mem_free()))
page_label.text = page_text.format(page, page_max)
if page == 1:
title_label.text = "LIS3MDL"
# Show page 1
mag_label.hidden = False
# Hide page 2
gyro_label.hidden = True
acc_label.hidden = True
color_label.hidden = True
# Hide page 3
color_label.hidden = True
prox_label.hidden = True
# Hide page 4
temp_label.hidden = True
# Hide page 5
mic_label.hidden = True
mic_sparkline.hidden = True
# Hide page 6
cpu_label.hidden = True
# Disable page 3 unused sensors/features
apds.enable_color = False
apds.enable_proximity = False
apds.enable_proximity_interrupt = False
apds.enable_gesture = False
# Disable page 4 unused sensors/features
bmp280.mode = adafruit_bmp280.MODE_SLEEP
elif page == 2:
title_label.text = "LSM6DS33"
# Hide page 1
mag_label.hidden = True
# Show page 2
gyro_label.hidden = False
acc_label.hidden = False
# Hide page 3
color_label.hidden = True
prox_label.hidden = True
# Hide page 4
temp_label.hidden = True
# Hide page 5
mic_label.hidden = True
mic_sparkline.hidden = True
# Hide page 6
cpu_label.hidden = True
# Disable page 3 unused sensors/features
apds.enable_color = False
apds.enable_proximity = False
apds.enable_proximity_interrupt = False
apds.enable_gesture = False
# Disable page 4 unused sensors/features
bmp280.mode = adafruit_bmp280.MODE_SLEEP
elif page == 3:
title_label.text = "APDS9960"
# Hide page 1
mag_label.hidden = True
# Hide page 2
gyro_label.hidden = True
acc_label.hidden = True
# Show page 3
color_label.hidden = False
prox_label.hidden = False
# Hide page 4
temp_label.hidden = True
# Hide page 5
mic_label.hidden = True
mic_sparkline.hidden = True
# Hide page 6
cpu_label.hidden = True
# Enable Page 3 sensors/features
apds.enable_color = True
apds.enable_proximity = True
# apds.enable_proximity_interrupt = True
apds.enable_gesture = True
# Disable page 4 unused sensors/features
bmp280.mode = adafruit_bmp280.MODE_SLEEP
elif page == 4:
title_label.text = "BMP280/SHT30"
# Hide page 1
mag_label.hidden = True
# Hide page 2
gyro_label.hidden = True
acc_label.hidden = True
# Hide page 3
color_label.hidden = True
prox_label.hidden = True
# Show page 4
temp_label.hidden = False
# Hide page 5
mic_label.hidden = True
mic_sparkline.hidden = True
# Hide page 6
cpu_label.hidden = True
# Disable page 3 unused sensors/features
apds.enable_color = False
apds.enable_proximity = False
apds.enable_proximity_interrupt = False
apds.enable_gesture = False
# Enable page 4 unused sensors/features
bmp280.mode = adafruit_bmp280.MODE_NORMAL
elif page == 5:
title_label.text = "PDM Microphone"
# Hide page 1
mag_label.hidden = True
# Hide page 2
gyro_label.hidden = True
acc_label.hidden = True
# Hide page 3
color_label.hidden = True
prox_label.hidden = True
# Hide page 4
temp_label.hidden = True
# Show page 5
mic_label.hidden = False
mic_sparkline.hidden = False
# Hide page 6
cpu_label.hidden = True
# Disable page 3 unused sensors/features
apds.enable_color = False
apds.enable_proximity = False
apds.enable_proximity_interrupt = False
apds.enable_gesture = False
# Disable page 4 unused sensors/features
bmp280.mode = adafruit_bmp280.MODE_SLEEP
elif page == 6:
title_label.text = "Internals"
# Hide page 1
mag_label.hidden = True
# Hide page 2
gyro_label.hidden = True
acc_label.hidden = True
# Hide page 3
color_label.hidden = True
prox_label.hidden = True
# Hide page 4
temp_label.hidden = True
# Hide page 5
mic_label.hidden = True
mic_sparkline.hidden = True
# Show page 6
cpu_label.hidden = False
# Disable page 3 unused sensors/features
apds.enable_color = False
apds.enable_proximity = False
apds.enable_proximity_interrupt = False
apds.enable_gesture = False
# Disable page 4 unused sensors/features
bmp280.mode = adafruit_bmp280.MODE_SLEEP
gc.collect()
# print("Page changed to page #{}, mem free: {}".format(page, gc.mem_free()))
pagechange = False
# Update sensor data and display only if sensors are on the page
if page == 1:
# Mag Update
mag_x, mag_y, mag_z = mag.magnetic
# print(mag_text.format(mag_x, mag_y, mag_z))
mag_label.text = mag_text.format(mag_x, mag_y, mag_z)
elif page == 2:
# Gyro/Accel Update
gyro_x, gyro_y, gyro_z = acc.gyro
# print(gyro_text.format(gyro_x, gyro_y, gyro_z))
gyro_label.text = gyro_text.format(gyro_x, gyro_y, gyro_z)
acc_x, acc_y, acc_z = acc.acceleration
# print(acc_text.format(acc_x, acc_y, acc_z))
acc_label.text = acc_text.format(acc_x, acc_y, acc_z)
# Test Gyro/Accel Interrupt Status
# print("Acc IRQ: Value: {}, Rose: {}, Fell: {}".format(acc_irq_sw.value, acc_irq_sw.rose, acc_irq_sw.fell))
elif page == 3:
# Color Update
color_r, color_g, color_b, color_c = apds.color_data
color_label.text = color_text.format(color_r, color_g, color_b, color_c)
# Prox update
prox_label.text = prox_text.format(apds.proximity, apds.gesture())
# Test APDS Interrupt Status
# print("APDS IRQ: Value: {}, Rose: {}, Fell: {}".format(apds_irq_sw.value, apds_irq_sw.rose, apds_irq_sw.fell))
elif page == 4:
# Temp/Pressure/Altitude Update
temp_label.text = temp_text.format(bmp280.temperature, bmp280.pressure, bmp280.altitude, sht30.relative_humidity)
elif page == 5:
mic_cycle_count += 1
if mic_cycle_count > 3:
mic_cycle_count = 0
# PDM Microphone updates
mic.record(mic_samples, len(mic_samples))
magnitude = normalized_rms(mic_samples)
# print("Mic RMS: {}".format(magnitude))
mic_label.text = mic_text.format(magnitude)
# Handle sparklist with care, only updating every x cycles to avoid display issues
if len(mic_sparkline._spark_list) > mic_sparkline_max_entries:
mic_sparkline._spark_list.pop(0)
mic_sparkline._spark_list.append(magnitude)
if mic_cycle_count is 3:
mic_sparkline.update()
elif page == 6:
cpu_label.text = cpu_text.format(microcontroller.cpu.frequency / 1000 / 1000, microcontroller.cpu.temperature, microcontroller.cpu.voltage, gc.mem_free())
## Handle scrolling loop indicator
### Move that little box
# if not pause:
# if reverse:
# new_x_2 = sm_square.x + 1
# sm_square.x = new_x_2
# if new_x_2 > 125:
# reverse = False
# else:
# new_x_2 = sm_square.x - 1
# sm_square.x = new_x_2
# if new_x_2 < 0:
# reverse = True
gc.collect()
# time.sleep(0.005)
# time.sleep(0.2)