-
Notifications
You must be signed in to change notification settings - Fork 30
/
boot_switch.py
75 lines (66 loc) · 1.87 KB
/
boot_switch.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
import pyb
# This script is intended to be installed as boot.py. It will flash the
# Red LED for 2 seconds. If you press the USR switch during those 2
# seconds then it will cycle between the blue, yellow, and green LEDs to
# allow the USB mode to b eslected.
#
# Blue - CDC+MSC
# Yellow - CDC+HID
# Green - CDC only
#
# When you release the USR switch then it will flash the selected mode
# a couple of times and persist the choice.
#
# If you boot up without pressing the USR switch then it will use the
# currently persisted USB mode (default is USB+MSC)
sw = pyb.Switch()
# 1 - Red
# 2 - Green
# 3 - Yellow
# 4 - Blue
pyb.LED(2).off() # Turn Greem LED off since normal boot turns it on
led = pyb.LED(1)
leds = (pyb.LED(4), pyb.LED(3), pyb.LED(2))
try:
import boot_mode
persisted_mode = boot_mode.mode
mode = boot_mode.mode
except:
persisted_mode = -1
mode = 0
def mode_led(mode):
for led in leds:
led.off()
if mode >= 0:
leds[mode].on()
for i in range(10):
led.on()
pyb.delay(100)
led.off()
pyb.delay(100)
if sw():
while True:
mode_led(mode)
pyb.delay(500)
if not sw():
mode_led(-1)
break
mode = (mode + 1) % 3
break
for i in range(3):
mode_led(mode)
pyb.delay(100)
mode_led(-1)
pyb.delay(100)
usb_mode = ('CDC+MSC', 'CDC+HID', 'CDC')[mode]
if mode != persisted_mode:
with open('/flash/boot_mode.py', 'w') as f:
f.write('mode = %d\n' % mode)
f.write("usb_mode = '%s'\n" % usb_mode)
pyb.sync()
pyb.usb_mode(usb_mode)
# Note: prints which occur before the call pyb.usb_mode() will not show on the
# USB serial port.
print('usb_mode = %s' % usb_mode)
# Cleanup the namespace (since anything from boot.py shows up in the REPL)
del led, leds, mode, usb_mode, persisted_mode, i, sw, mode_led, boot_mode