-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprac_mon_feather.py
444 lines (326 loc) · 13 KB
/
prac_mon_feather.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
"""MIDI Practice Monitor
a.k.a. MIDI-bit - A fitbit for your MIDI keyboard
(c)2024 Rob Cranfill
See https://github.com/RobCranfill/MIDI-bit
Version 1 - Minimum Viable Product - Just keeps track of elapsed time spent practicing.
For CircuitPython, on the device known as
"Adafruit Feather RP2040 with USB Type A Host" (whew!)
(Adafruit Product ID: 5723)
To force startup mode,
import microcontroller
microcontroller.nvm[0] = 0x12 # for run
microcontroller.nvm[0] = 0x34 # for dev
"""
# stdlibs
import board
import digitalio
import microcontroller
import neopixel
import supervisor
import time
import usb.core
# adafruit libs
import adafruit_datetime as datetime
import adafruit_midi
import adafruit_midi.midi_message
# FIXME: this seems clunky. can't we do this more succinctly?
from adafruit_midi.control_change import ControlChange
from adafruit_midi.note_off import NoteOff
from adafruit_midi.note_on import NoteOn
from adafruit_midi.pitch_bend import PitchBend
import adafruit_usb_host_midi
# Our libs
import one_line_oled
import two_line_oled
import midi_state_machine
import midibit_defines as DEF
# TODO: how does this affect responsiveness? buffering? what-all??
MIDI_TIMEOUT = .1
# Timeouts, in seconds.
# Defaults will be changed if dev mode
SESSION_TIMEOUT = 15
DISPLAY_IDLE_TIMEOUT = 60 # for display blanking
SETTINGS_NAME = "pm_settings.text"
# Keyboard "attention" sequence MIDI notes: G G G Eb F F F D
MIDI_TRIGGER_SEQ_PREFIX = (67, 67, 67, 63, 65, 65, 65, 62)
MIDI_TRIGGER_SEQ_RESET = MIDI_TRIGGER_SEQ_PREFIX + (60,) # middle C
MIDI_TRIGGER_SEQ_TOGGLE_BOOT = MIDI_TRIGGER_SEQ_PREFIX + (62,) # D above middle C
MIDI_TRIGGER_SEQ_TEST = MIDI_TRIGGER_SEQ_PREFIX + (64,) # E
neopixel_ = neopixel.NeoPixel(board.NEOPIXEL, 1)
def flash_led(seconds):
neopixel_.fill(flash_color_)
time.sleep(seconds)
neopixel_.fill((0,0,0))
def set_run_or_dev():
'''Set the NeoPixel state and some other globals; return dev mode flag'''
global SESSION_TIMEOUT
global DISPLAY_IDLE_TIMEOUT
global flash_color_
RUN_MODE_COLOR = (128, 0, 0)
DEV_MODE_COLOR = (0, 128, 0)
flash_color_ = RUN_MODE_COLOR
# Read the non-volatile memory for the dev mode set by boot.py.
is_dev_mode = False
if microcontroller.nvm[0] == DEF.MAGIC_NUMBER_DEV_MODE:
is_dev_mode = True
# print(f"{microcontroller.nvm[0]=} -> {is_dev_mode=} ({DEF.MAGIC_NUMBER_DEV_MODE=})")
if is_dev_mode:
flash_color_ = DEV_MODE_COLOR
neopixel_.fill(flash_color_)
SESSION_TIMEOUT = 5
DISPLAY_IDLE_TIMEOUT = 10
print(f"\nDEV MODE: Setting timeouts to {SESSION_TIMEOUT=}, {DISPLAY_IDLE_TIMEOUT=}\n")
else:
neopixel_.fill(flash_color_)
print(f"\nRUN MODE")
return is_dev_mode
SPINNER = "|/-\\"
spinner_index_ = 0
def spin():
'''Return the next wiggling text characater.'''
global spinner_index_
spinner_index_ = (spinner_index_+1) % len(SPINNER)
return SPINNER[spinner_index_]
def as_hms(seconds):
return str(datetime.timedelta(0, int(seconds)))
def show_total_time(disp, seconds):
disp.set_text_1(as_hms(seconds))
def write_session_data(session_seconds):
'''Writes a string-ified version of the integer value.
This will throw an exception if the filesystem isn't writable. Catch it higher up.'''
print(f"write_session_data: {int(session_seconds)}")
with open(SETTINGS_NAME, "w") as f:
f.write(str(int(session_seconds)))
def read_session_data():
result = "0"
try:
with open(SETTINGS_NAME, "r") as f:
result = f.read()
except:
print("No old session data? Continuing....")
if len(result) == 0:
result = "0"
# print(f"read_session_data: returning '{result}'")
return result
def find_midi_device(disp):
"""Does not return until it finds a (suitable?) MIDI device"""
# does this help weird startup behavior? No,
# time.sleep(1)
print("\nLooking for MIDI devices...")
# display_message_for_a_bit(disp, "Looking for MIDI", delay=1)
disp.set_text_2("Looking for MIDI!....")
raw_midi = None
attempt = 1
no_midi_idle_start_time = time.monotonic()
while raw_midi is None:
all_devices = usb.core.find(find_all=True)
# no can do
# print(f" Found {len(all_devices)} devices?")
for device in all_devices:
# FIXME: this is not useful?
print(f" - looking at USB device vendor 0x{device.idVendor:04x}, product 0x{device.idProduct:04x}")
# I guess this is how we find a MIDI device: try it; if not MIDI, will throw exception.
try:
raw_midi = adafruit_usb_host_midi.MIDI(device, timeout=MIDI_TIMEOUT)
print(f" ^ MIDI OK for {device.product=}")
if device.product is None:
print("FUNNY DEVICE; SKIPPING!")
continue
else:
break
except ValueError:
print(" * adafruit_usb_host_midi.MIDI: ValueError?")
continue
except Exception as e:
print(f" * adafruit_usb_host_midi.MIDI: {e}")
print(f" Done iterating MIDI devices, found {raw_midi}")
# Looked at all devices, didn't find MIDI. Try again.
if raw_midi is None:
print(f"No MIDI device found on try #{attempt}. Sleeping....")
time.sleep(1)
# No-MIDI timeout; flash LED twice
if time.monotonic() - no_midi_idle_start_time > DISPLAY_IDLE_TIMEOUT:
# print("no-MIDI idle timeout!")
disp.blank_screen()
flash_led(0.01)
time.sleep(0.1)
flash_led(0.01)
attempt += 1
try:
midi_device = adafruit_midi.MIDI(midi_in=raw_midi)
except Exception as e:
print(f" * adafruit_midi.MIDI: {e}")
print(f" returning {midi_device=}")
disp.set_text_2(f"Found {device.product}")
# time.sleep(2)
return midi_device
def try_write_session_data(dev_mode, disp, seconds):
'''Write the given elapsed time to the data file. Display errors as needed.'''
try:
write_session_data(seconds)
display_message_for_a_bit(disp, "DATA SAVED")
except Exception as e:
# we expect write errors in dev mode.
if dev_mode:
print("Can't write, as expected")
display_message_for_a_bit(disp, "FAILED TO SAVE - OK")
else:
print(f"Can't write! {e}")
display_message_for_a_bit(disp, "FAILED TO SAVE!")
def toggle_boot_mode(disp):
nvm_dev_mode = microcontroller.nvm[0] == DEF.MAGIC_NUMBER_DEV_MODE
nvm_dev_mode = not nvm_dev_mode
microcontroller.nvm[0] = DEF.MAGIC_NUMBER_DEV_MODE if nvm_dev_mode else DEF.MAGIC_NUMBER_RUN_MODE
print(f"Setting {microcontroller.nvm[0]=} -> {nvm_dev_mode=}")
display_message_for_a_bit(disp, f"Dev: {nvm_dev_mode}")
def display_message_for_a_bit(disp, text, delay=2):
disp.set_text_2(str(text))
time.sleep(delay)
disp.set_text_2("")
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# turn off auto-reload, cuz it's a pain
supervisor.runtime.autoreload = False
print(f"{supervisor.runtime.autoreload=}")
# Are we running in dev mode? Set some stuff.
in_dev_mode = set_run_or_dev()
# Load previous total time from text file.
total_seconds = int(read_session_data())
print(f"read_session_data: {total_seconds=}")
# The display.
#
display = None
try:
display = two_line_oled.two_line_oled(0x3d, 64)
print("Created two-line display OK!")
except:
display = one_line_oled.one_line_oled()
print("Created one-line display OK!")
if display == None:
print("Can't init display??")
while True:
pass
last_event_time = time.monotonic()
in_session = False
session_start_time = 0
show_total_time(display, total_seconds)
# last_displayed_time is the (integer) time we last displayed; only update if changed.
# (The time itself is a float that's always changing.)
#
last_displayed_time = int(total_seconds)
idle_start_time = time.monotonic()
idle_led_blip_time = idle_start_time
# A state machine to watch for the "reset" sequence.
msm_reset = midi_state_machine.midi_state_machine(MIDI_TRIGGER_SEQ_RESET)
# A state machine to watch for the "toggle boot mode" sequence.
msm_toggle_boot = midi_state_machine.midi_state_machine(MIDI_TRIGGER_SEQ_TOGGLE_BOOT)
# # For testing shit
# msm_test = midi_state_machine.midi_state_machine(MIDI_TRIGGER_SEQ_TEST)
# wait for USB ready??? nope
# time.sleep(2)
# Main event loop. Does not exit.
#
midi_device = None
msg_number = 0
while True:
# This doesn't return until we have a MIDI device.
# TODO: Is it always a *usable* device? No. Something funny here.
#
if midi_device is None:
print("MEL loking for MIDI....")
# TODO: check for None?
midi_device = find_midi_device(display)
print(" back from find_midi_device")
# stop screen timeout immediately after finding ?
last_event_time = time.monotonic()
# print(f"waiting for event; {in_session=}")
# TODO: remove this as 'else' to fix one-off error?
# else:
try:
msg = midi_device.receive()
except usb.core.USBError as e:
print(f" ** midi_device.receive: usb.core.USBError: '{e}'")
# Assume this is a MIDI disconnect?
if in_session:
total_seconds_temp = total_seconds + session_length
print(f"* Force write: {total_seconds=}, {session_length=}")
try_write_session_data(in_dev_mode, display, total_seconds+session_length)
# TODO: end the session?
last_event_time = time.monotonic()
midi_device = None
continue
event_time = time.monotonic()
# TODO: This acts on *any* kind of MIDI message - on, off, CC, etc.
# Should we only pay attention to NoteOn events?
#
if msg:
msg_number += 1
if not isinstance(msg, NoteOn):
# print(f"Not a MIDI ON message! ({msg_number})")
# print(f" > midi msg: {msg} @ {event_time:.1f}")
continue
# print(f"midi msg: {msg} @ {event_time:.1f}")
last_event_time = time.monotonic()
display.set_text_2(spin())
if not in_session:
print("\nStarting session")
session_start_time = time.monotonic()
in_session = True
# This would only be missing for <1 sec, but hey.
show_total_time(display, total_seconds)
# Look for command sequences.
if isinstance(msg, NoteOn):
# Could be a zero-velocity NoteOn which is really a "note off".
if msg.velocity == 0:
# print("note off!")
continue
if msm_reset.note(msg.note):
print(f"* Got {MIDI_TRIGGER_SEQ_RESET=}")
total_seconds = 0
last_displayed_time = 0
session_length = 0
session_start_time = time.monotonic()
show_total_time(display, total_seconds)
try_write_session_data(in_dev_mode, display, total_seconds)
if msm_toggle_boot.note(msg.note):
print(f"* Got {MIDI_TRIGGER_SEQ_TOGGLE_BOOT=}")
toggle_boot_mode(display)
# if msm_force_write.note(msg.note):
# # don't update total_seconds yet, but write the new value
# total_seconds_temp = total_seconds + session_length
# print(f"* Force write: {total_seconds=}, {total_seconds_temp=}")
# try_write_session_data(display, total_seconds_temp)
# else:
# # print(" empty message")
# pass
# We have handled the event/note. Now do other stuff.
#
if in_session:
# Session timeout?
if event_time - last_event_time > SESSION_TIMEOUT:
# print("\nSESSION_TIMEOUT!")
in_session = False
display.set_text_2("")
total_seconds += session_length
try_write_session_data(in_dev_mode, display, total_seconds)
# For idle screen timeout
idle_start_time = time.monotonic()
else:
# Update current session info
session_length = time.monotonic() - session_start_time
# print(f" Session now {as_hms(session_length)}")
new_total = total_seconds + session_length
if last_displayed_time != int(new_total):
last_displayed_time = int(new_total)
# print(f" updating at {last_displayed_time}")
show_total_time(display, new_total)
else:
# print(" not in session...")
# With-MIDI display timeout
if time.monotonic() - idle_start_time > DISPLAY_IDLE_TIMEOUT:
# print("idle timeout!")
display.blank_screen()
# Single flash of LED, once per second.
if time.monotonic() - idle_led_blip_time > 1:
flash_led(0.01)
idle_led_blip_time = time.monotonic()