-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_finallized.py
More file actions
313 lines (295 loc) · 10.8 KB
/
Copy pathcode_finallized.py
File metadata and controls
313 lines (295 loc) · 10.8 KB
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
# Lab 02 - Group 1X
# Name: XXX, YYY
# SID: 5XXXXXXX, 5XXXXXXX
import random
import time
import music
from microbit import *
pin0.set_pull(pin0.PULL_DOWN)
pin1.set_pull(pin1.PULL_DOWN)
pin2.set_pull(pin2.PULL_DOWN)
# Returns a picture from microbit built-in class
def random_pictures():
"""Returns a random picture from microbit built-in class"""
try:
# Getting all images
images = [Image.__dict__[i] for i in Image.__dict__ if isinstance(Image.__dict__[i], Image) and Image.__dict__[i] != Image.YES and Image.__dict__[i] != Image.NO]
return random.choice(images)
except RuntimeError:
return Image.SKULL # Return something
class MagicSwitchboard:
def __init__(self):
self.switch_to_led = {} # Initialization
# Logging the switches during initialization
def switch_init(self):
"""Logging the switches with their corresponding LED light bulb"""
if self.switch_to_led != {}:
self.switch_to_led = {} # Initialization
# Mapping switches (buttons) to LEDs using string names
while not len(self.switch_to_led) >= 3: # There are 3 switches in total
# Turn on all LEDs to lead to confusion
if pin13.read_digital() and "pin13" not in self.switch_to_led: # Button 13 pressed (pulled to GND)
if len(self.switch_to_led) == 0:
self.switch_to_led["pin13"] = "pin0"
pin0.write_digital(1)
sleep(200)
pin0.write_digital(0)
elif len(self.switch_to_led) == 1:
self.switch_to_led["pin13"] = "pin1"
pin1.write_digital(1)
sleep(200)
pin1.write_digital(0)
else:
self.switch_to_led["pin13"] = "pin2"
pin2.write_digital(1)
sleep(200)
pin2.write_digital(0)
if pin14.read_digital() and "pin14" not in self.switch_to_led: # Button 14 pressed (pulled to GND)
if len(self.switch_to_led) == 0:
self.switch_to_led["pin14"] = "pin0"
pin0.write_digital(1)
sleep(200)
pin0.write_digital(0)
elif len(self.switch_to_led) == 1:
self.switch_to_led["pin14"] = "pin1"
pin1.write_digital(1)
sleep(200)
pin1.write_digital(0)
else:
self.switch_to_led["pin14"] = "pin2"
pin2.write_digital(1)
sleep(200)
pin2.write_digital(0)
if pin15.read_digital() and "pin15" not in self.switch_to_led: # Button 15 pressed (pulled to GND)
if len(self.switch_to_led) == 0:
self.switch_to_led["pin15"] = "pin0"
pin0.write_digital(1)
sleep(200)
pin0.write_digital(0)
elif len(self.switch_to_led) == 1:
self.switch_to_led["pin15"] = "pin1"
pin1.write_digital(1)
sleep(200)
pin1.write_digital(0)
else:
self.switch_to_led["pin15"] = "pin2"
pin2.write_digital(1)
sleep(200)
pin2.write_digital(0)
return
# Toggle the RGB lights - determined by sound level
def sound_level_detector(self):
"""Toggle the RGB lights by sound level"""
display.show(Image.YES)
time.sleep(0.5)
pin0.write_digital(0)
pin1.write_digital(0)
pin2.write_digital(0)
while not pin_logo.is_touched():
if microphone.sound_level() <= 70:
display.show("0", wait=False)
# Turn off all LEDs
pin0.write_digital(0)
pin1.write_digital(0)
pin2.write_digital(0)
if microphone.sound_level() > 70:
# Turn on the left LED
display.show("1", wait=False)
pin0.write_digital(1)
pin1.write_digital(0)
pin2.write_digital(0)
if microphone.sound_level() > 180:
# Turn on the left LED and the middle LED
display.show("2", wait=False)
pin0.write_digital(1)
pin1.write_digital(1)
pin2.write_digital(0)
if microphone.sound_level() > 225:
# Turn on all LEDs
display.show("3", wait=False)
pin0.write_digital(1)
pin1.write_digital(1)
pin2.write_digital(1)
time.sleep(0.1)
return
# Function to convert a string pin name to the actual pin object
def get_pin(self, pin_name):
"""Convert a string pin name to the actual pin object."""
pin_map = {
"pin0": pin0,
"pin1": pin1,
"pin2": pin2,
"pin3": pin3,
"pin4": pin4,
"pin5": pin5,
"pin6": pin6,
"pin7": pin7,
"pin8": pin8,
"pin9": pin9,
"pin10": pin10,
"pin11": pin11,
"pin12": pin12,
"pin13": pin13,
"pin14": pin14,
"pin15": pin15,
"pin16": pin16,
"pin19": pin19,
"pin20": pin20
}
return pin_map.get(pin_name)
# Function to play some music
def music_box(self):
"""Play some music"""
# Play Marry have a little lamb
music.play(["E4:3", "D4:3", "C4:3", "D4:3", "E4:3", "E4:3", "E4:6",
"D4:3", "D4:3", "D4:6", "E4:3", "G4:3", "G4:6",
"E4:3", "D4:3", "C4:3", "D4:3", "E4:3", "E4:3", "E4:6",
"D4:3", "D4:3", "E4:3", "D4:3", "C4:6"],
wait=False, pin=None)
# Pin toggling sequence fine-tuned to the song's rhythm
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin1.write_digital(1)
sleep(350)
pin1.write_digital(0)
pin0.write_digital(1)
sleep(350)
pin0.write_digital(0)
pin1.write_digital(1)
sleep(350)
pin1.write_digital(0)
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin2.write_digital(1)
sleep(800)
pin2.write_digital(0)
pin1.write_digital(1)
sleep(350)
pin1.write_digital(0)
pin1.write_digital(1)
sleep(350)
pin1.write_digital(0)
pin1.write_digital(1)
sleep(750)
pin1.write_digital(0)
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin2.write_digital(1)
sleep(1350)
pin2.write_digital(0)
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin1.write_digital(1)
sleep(350)
pin1.write_digital(0)
pin0.write_digital(1)
sleep(350)
pin0.write_digital(0)
pin1.write_digital(1)
sleep(350)
pin1.write_digital(0)
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin2.write_digital(1)
sleep(800)
pin2.write_digital(0)
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin1.write_digital(1)
sleep(350)
pin1.write_digital(0)
pin1.write_digital(1)
sleep(350)
pin1.write_digital(0)
pin2.write_digital(1)
sleep(350)
pin2.write_digital(0)
pin1.write_digital(1)
sleep(800)
pin1.write_digital(0)
pin0.write_digital(1)
sleep(800)
pin0.write_digital(0)
# Update the RGB LED light bulbs status
def update_leds(self):
"""Update the RGB LED light bulbs status"""
# Check each switch and set the corresponding LED state based on the switch state
for switch_name, led_name in self.switch_to_led.items():
switch_pin = self.get_pin(switch_name)
led_pin = self.get_pin(led_name)
if switch_pin is None or led_pin is None:
continue
# If the switch is pressed (pin reads 0), turn on the LED, otherwise turn it off
if switch_pin.read_digital(): # Button pressed (pulled to GND)
led_pin.write_digital(1) # Turn on LED
else:
led_pin.write_digital(0) # Turn off LED
return
switchboard = MagicSwitchboard()
# Run the program asynchronously
def asynchronous_run(seconds):
"""Run the program as a 'couroutine'"""
end_time = time.ticks_add(time.ticks_ms(), int(seconds * 1000))
reset = True
display.show(random_pictures())
while time.ticks_diff(end_time, time.ticks_ms()) > 0:
switchboard.update_leds()
if accelerometer.is_gesture("shake"):
switchboard.sound_level_detector()
if button_b.is_pressed() or pin_logo.is_touched():
# Check if both buttons are pressed
time.sleep(0.1)
if button_a.is_pressed():
#raise NotImplementedError("Unsupported opeartion for pressing both buttons simultaneously.")
switchboard.music_box()
reset = False
return reset # Terminates the program
# Check if any button is pressed and cancel the reset if so
if pin13.read_digital() or pin14.read_digital() or pin15.read_digital():
reset = False
return reset
# Callback for function main()
def after_serving():
"""Callback for function main()"""
music.play(music.BA_DING, pin=None, wait=False)
display.show(Image('09000:99990:09009:00009:99990:')) # Custom back arrow
terminate = button_b.is_pressed()
if terminate: # Terminates the program
display.show(Image.NO)
time.sleep(1)
display.clear()
if not terminate:
main() # Call main to soft reset
return
# Main function
def main():
"""Main program loop"""
display.on()
while not button_a.is_pressed():
sleep(0.001)
if button_b.is_pressed() or pin_logo.is_touched():
break
if button_a.is_pressed():
switchboard.switch_init()
while button_a.was_pressed:
if asynchronous_run(5):
break
after_serving() # callback
# For 1st time startup only
if __name__ == "__main__":
# The following executes for the 1st time only
music.play(music.NYAN, pin=None, wait=False)
display.scroll("Press A to start, tap the logo to reset, or press B to stop.", delay=60)
main()