Skip to content

Commit d9cf060

Browse files
committed
review code.
1 parent d9e508e commit d9cf060

2 files changed

Lines changed: 79 additions & 98 deletions

File tree

10.microbit/light.py

Lines changed: 62 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,85 @@
11
from time import sleep_ms
22

33

4-
class LightIntensity():
4+
class Intensity():
5+
dither = 5
6+
57
def __init__(self, pin):
8+
self.old, self.new, self.eliminate = 0, 0, 0
9+
610
from machine import ADC, Pin
711
self.adc = ADC(Pin(pin, Pin.IN))
8-
self.adc.atten(ADC.ATTN_11DB) # 0-3.9V
12+
self.adc.atten(ADC.ATTN_11DB) # 0-3.9V
913

1014
def read(self):
11-
self.last_val = self.new_val
12-
self.new_val = self.adc.read()/5
13-
return int(self.new_val*5/40.95)
15+
self.old = self.new
16+
self.new = self.adc.read()
17+
return int(self.new / 4.095)
1418

19+
# result > 0 to state up, < 0 to state down.
20+
def get_state(self):
21+
# print(self.new, self.old)
22+
tmp = self.new - self.old
23+
return 0 if abs(tmp) < self.eliminate else tmp
1524

16-
class Sensegesture():
17-
idle, running, finish = 0, 1, 2
25+
def calibrate(self):
26+
self.eliminate = self.read() / Intensity.dither
1827

19-
def __init__(self, leftPin=36, rightpin=39):
20-
from light1 import LightIntensity
21-
self.LightL = LightIntensity(leftPin)
22-
self.LightR = LightIntensity(rightpin)
28+
class Gesture(object):
29+
idle, ing, end, = 0, 1, 2
2330

24-
def get_brightness(self):
25-
self.LightR.read()
26-
self.LightL.read()
31+
def __init__(self, PinLeft=36, PinRight=39, dither=5):
32+
Intensity.dither = dither
33+
self.l, self.r = Intensity(PinLeft), Intensity(PinRight)
34+
self.l_state, self.r_state = Gesture.idle, Gesture.idle
2735

28-
def set_threshold(self, val): # 设置阈值
29-
self.threshold = self.LightR.new_val*val
36+
def get_brightness(self):
37+
self.r.read()
38+
self.l.read()
3039

31-
def is_right(self, runtime=10, interval=0.5):
32-
status = 0
33-
self.get_brightness()
34-
self.set_threshold(0.1)
35-
for t in range(runtime*40):
36-
if status == Sensegesture.idle:
37-
count = 0
38-
self.get_brightness()
39-
# print(self.threshold)
40-
# print('right_new=%d' % self.LightR.new_val)
41-
sleep_ms(20)
42-
if self.LightL.last_val-self.LightL.new_val > self.threshold and self.LightR.new_val-self.LightL.new_val > self.threshold:
43-
status = self.running
44-
elif status == Sensegesture.running:
45-
self.get_brightness()
46-
# print(self.threshold)
47-
# print(self.LightL.last_val)
48-
# print('left_new=%d' % self.LightL.new_val)
49-
sleep_ms(20)
50-
count += 1
51-
if count > interval*40:
52-
status = Sensegesture.idle
53-
if self.LightR.last_val-self.LightR.new_val > self.threshold:
54-
status = self.finish
55-
elif status == Sensegesture.finish:
56-
return True
57-
return False
58-
59-
def is_left(self, runtime=10, interval=0.5):
60-
status = 0
40+
def get_gesture(self, delay=30):
41+
sleep_ms(delay)
6142
self.get_brightness()
62-
self.set_threshold(0.1)
63-
for t in range(runtime*40):
64-
if status == Sensegesture.idle:
65-
count = 0
66-
self.get_brightness()
67-
# print('right_new=%d' % self.LightR.new_val)
68-
sleep_ms(20)
69-
if self.LightR.last_val-self.LightR.new_val > self.threshold and self.LightL.new_val-self.LightR.new_val > self.threshold:
70-
status = Sensegesture.running
71-
elif status == Sensegesture.running:
72-
self.get_brightness()
73-
# print(self.threshold)
74-
# print(self.LightL.last_val)
75-
# print('left_new=%d' % self.LightL.new_val)
76-
sleep_ms(20)
77-
count += 1
78-
if count > interval*40:
79-
status = Sensegesture.idle
80-
if self.LightL.last_val-self.LightL.new_val > self.threshold:
81-
status = Sensegesture.finish
82-
83-
elif status == Sensegesture.finish:
84-
return True
85-
return False
43+
l_state, r_state = self.l.get_state(), self.r.get_state()
44+
result = []
45+
46+
if l_state < 0 and r_state < 0:
47+
self.l_state, self.r_state = Gesture.ing, Gesture.ing
8648

49+
if self.l_state == Gesture.ing and l_state == 0 and r_state < 0:
50+
self.l_state = Gesture.end
51+
52+
if self.l_state == Gesture.end and l_state > 0 and r_state == 0:
53+
self.l_state = Gesture.idle
54+
result.append('left')
55+
56+
if self.r_state == Gesture.ing and l_state < 0 and r_state == 0:
57+
self.r_state = Gesture.end
58+
59+
if self.r_state == Gesture.end and l_state == 0 and r_state > 0:
60+
self.r_state = Gesture.idle
61+
result.append('right')
62+
63+
if l_state == 0 and r_state == 0:
64+
self.l_state = self.r_state = Gesture.idle
65+
self.l.calibrate()
66+
self.r.calibrate()
67+
68+
return None if len(result) != 1 else result[0]
8769

8870
def unit_test():
8971
print('\n\
90-
from machine import Pin\n\
91-
t = Sensegesture()\n\
92-
LED = Pin(18, Pin.OUT)\n\
93-
LED.value(0)\n\
94-
if(t.is_right(runtime=15)):\n\
95-
LED.value(1)\n\
72+
g = Gesture()\n\
73+
while True:\n\
74+
res = g.get_gesture()\n\
75+
if res != None:\n\
76+
print(res)\n\
9677
')
97-
from machine import Pin
98-
t = Sensegesture()
99-
LED = Pin(18, Pin.OUT)
100-
LED.value(0)
101-
if(t.is_right(runtime=15)):
102-
LED.value(1)
103-
78+
g = Gesture()
79+
while True:
80+
res = g.get_gesture()
81+
if res != None:
82+
print(res)
10483

10584
if __name__ == '__main__':
10685
unit_test()

11.music/music.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Author: qiren123
2-
# This file is part of MicroPython MIDI Music
3-
# Copyright (c) 2018 qiren123
4-
#
5-
# Licensed under the MIT license:
6-
# http://www.opensource.org/licenses/mit-license.php
7-
#
8-
1+
# Author: qiren123
2+
# This file is part of MicroPython MIDI Music
3+
# Copyright (c) 2018 qiren123
4+
#
5+
# Licensed under the MIT license:
6+
# http://www.opensource.org/licenses/mit-license.php
7+
#
8+
99
DADADADUM = ['r4:2', 'g', 'g', 'g', 'eb:8', 'r:2', 'f', 'f', 'f', 'd:8']
1010

1111
ENTERTAINER = [
@@ -58,7 +58,7 @@
5858
]
5959

6060
BIRTHDAY = [
61-
'c5:4', 'c:1', 'd:4', 'c:4', 'f', 'e:8', 'c:3', 'c:1', 'd:4', 'c:4', 'g',
61+
'c4:4', 'c:1', 'd:4', 'c:4', 'f', 'e:8', 'c:3', 'c:1', 'd:4', 'c:4', 'g',
6262
'f:8', 'c:3', 'c:1', 'c5:4', 'a4', 'f', 'e', 'd', 'a#:3', 'a#:1', 'a:4',
6363
'f', 'g', 'f:8'
6464
]
@@ -108,11 +108,11 @@
108108
normal_tone = {
109109
'A1': 55, 'B1': 62, 'C1': 33, 'D1': 37, 'E1': 41, 'F1': 44, 'G1': 49,
110110

111-
'A2': 110, 'B2': 123, 'C2': 65, 'D2': 73, 'E2': 82, 'F2': 87, 'G2': 98,
111+
'A2': 110, 'B2': 123, 'C2': 65, 'D2': 73, 'E2': 82, 'F2': 87, 'G2': 98,
112112

113113
'A3': 220, 'B3': 247, 'C3': 131, 'D3': 147, 'E3': 165, 'F3': 175, 'G3': 196,
114114

115-
'A4': 440, 'B4': 494, 'C4': 262, 'D4': 294, 'E4': 330, 'F4': 349, 'G4': 392,
115+
'A4': 440, 'B4': 494, 'C4': 262, 'D4': 294, 'E4': 330, 'F4': 349, 'G4': 392,
116116

117117
'A5': 880, 'B5': 988, 'C5': 523, 'D5': 587, 'E5': 659, 'F5': 698, 'G5': 784,
118118

@@ -204,6 +204,7 @@ def parse(self, tone, dict):
204204
elif tone_size == 2:
205205
freq = dict[tone]
206206
self.set_octave(tone[1:])
207+
# print(int(freq), int(time))
207208
return int(freq), int(time)
208209

209210
def midi(self, tone):
@@ -221,16 +222,17 @@ def set_default(self, tone):
221222
if pos != -1:
222223
self.set_duration(int(tone[(pos + 1):]))
223224
tone = tone[:pos]
224-
if len(tone) == 2:
225-
self.set_octave(tone[1:])
226225

227-
def play(self, tune, pin=25):
226+
def play(self, tune, pin=25, duration=None):
228227
from machine import Pin, PWM
229228
from utime import sleep_ms
230229

231230
try:
232231
pwm = PWM(Pin(pin))
233-
self.set_default(tune[0])
232+
if duration is None:
233+
self.set_default(tune[0])
234+
else:
235+
self.set_duration(duration)
234236
for tone in tune:
235237
tone = tone.upper() # all to upper
236238
if tone[0] not in Letter:

0 commit comments

Comments
 (0)