-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
114 lines (104 loc) · 3.31 KB
/
main.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
# main.py -- put your code here!
import pyb
import dcfurs
import badge
import emote
import micropython
import settings
import ubinascii
print("Booting...")
import animations
## Handle events from the BLE module.
def blerx(args):
for x in args:
## Locate an optional value given by an '='
nv = x.split('=', 1)
name = nv[0]
value = nv[1] if len(nv) > 1 else None
## Handle the received stuff.
if name == 'emote':
## Select a random emote.
if (not value) or (value == 'random'):
emote.random()
pyb.delay(2500)
## Parse a specific emote to draw.
else:
emstr = ubinascii.unhexlify(value).decode("ascii")
emote.render(emstr)
pyb.delay(2500)
if name == 'awoo':
## Someone started a howl
msg = animations.scroll(" AWOOOOOOOOOOOOOO")
delay = 0
while delay < 5000:
msg.draw()
pyb.delay(msg.interval)
delay += msg.interval
def ble():
line = badge.ble.readline().decode("ascii")
if settings.debug:
print(line)
try:
event, x = line.split(':', 1)
args = x.rstrip().split()
if (event == 'rx'):
blerx(args)
except Exception:
return
## Program the serial number into the BLE module, which ought
## to have finished booting by now.
if badge.ble:
badge.ble_set("serial", "0x%04x" % dcfurs.serial())
badge.ble_set("cooldown", "%d" % settings.blecooldown)
## Select the user's preferred boot animation.
available = animations.all()
selected = 0
if settings.bootanim:
try:
selected = available.index(getattr(animations, settings.bootanim))
except Exception:
pass
anim = available[selected]()
while True:
anim.draw()
ival = anim.interval
while ival > 0:
## Change animation on button press, or emote if both pressed.
if badge.right.event():
if badge.left.value():
emote.random()
else:
selected = (selected + 1) % len(available)
anim = available[selected]()
elif badge.left.event():
if badge.right.value():
emote.random()
elif selected == 0:
selected = len(available)-1
anim = available[selected]()
else:
selected = selected - 1
anim = available[selected]()
# Service events.
elif badge.ble.any():
ble()
elif badge.boop.event():
if hasattr(anim, 'boop'):
anim.boop()
else:
if settings.debug:
micropython.mem_info()
emote.boop()
ival = 1000
## Pause for as long as long as both buttons are pressed.
if badge.right.value() and badge.left.value():
ival += 50
## Run the animation timing
if ival > 50:
pyb.delay(50)
ival -= 50
else:
pyb.delay(ival)
ival = 0
## Attempt to suspend the badge between animations
badge.trysuspend()