Skip to content

Commit c505e6a

Browse files
committed
fix
1 parent d9cf060 commit c505e6a

1 file changed

Lines changed: 47 additions & 21 deletions

File tree

10.microbit/light.py

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33

44
class Intensity():
5-
dither = 5
5+
dither = 10
66

77
def __init__(self, pin):
8-
self.old, self.new, self.eliminate = 0, 0, 0
8+
self.old, self.new, self.eliminate = 0, 0, 100
99

1010
from machine import ADC, Pin
1111
self.adc = ADC(Pin(pin, Pin.IN))
12-
self.adc.atten(ADC.ATTN_11DB) # 0-3.9V
12+
self.adc.atten(ADC.ATTN_11DB) # 0-3.9V
1313

1414
def read(self):
1515
self.old = self.new
16-
self.new = self.adc.read()
17-
return int(self.new / 4.095)
16+
self.new = self.adc.read()/4.095
17+
return int(self.new)
1818

1919
# result > 0 to state up, < 0 to state down.
2020
def get_state(self):
@@ -23,63 +23,89 @@ def get_state(self):
2323
return 0 if abs(tmp) < self.eliminate else tmp
2424

2525
def calibrate(self):
26-
self.eliminate = self.read() / Intensity.dither
26+
self.eliminate = 2+self.read() / Intensity.dither
27+
2728

2829
class Gesture(object):
2930
idle, ing, end, = 0, 1, 2
3031

31-
def __init__(self, PinLeft=36, PinRight=39, dither=5):
32+
def __init__(self, PinLeft=36, PinRight=39, dither=10):
3233
Intensity.dither = dither
3334
self.l, self.r = Intensity(PinLeft), Intensity(PinRight)
3435
self.l_state, self.r_state = Gesture.idle, Gesture.idle
36+
self.updata=0
3537

3638
def get_brightness(self):
3739
self.r.read()
3840
self.l.read()
3941

40-
def get_gesture(self, delay=30):
42+
def get_gesture(self, delay=25):
4143
sleep_ms(delay)
4244
self.get_brightness()
4345
l_state, r_state = self.l.get_state(), self.r.get_state()
4446
result = []
45-
46-
if l_state < 0 and r_state < 0:
47-
self.l_state, self.r_state = Gesture.ing, Gesture.ing
48-
49-
if self.l_state == Gesture.ing and l_state == 0 and r_state < 0:
47+
if self.l_state == Gesture.idle and r_state > 0 and self.r.new-self.l.new > self.l.eliminate:
48+
self.count_l = 0
49+
self.l_state = Gesture.ing
50+
51+
if self.r_state == Gesture.idle and l_state > 0 and self.l.new-self.r.new > self.r.eliminate:
52+
self.count_2 = 0
53+
self.r_state = Gesture.ing
54+
55+
if self.l_state == Gesture.ing:
56+
# print(self.l.eliminate)
57+
self.count_l += 1
58+
if self.count_l > 20:
59+
self.l_state = Gesture.idle
60+
elif l_state > 0 and self.r.new-self.l.new < self.l.eliminate:
5061
self.l_state = Gesture.end
5162

52-
if self.l_state == Gesture.end and l_state > 0 and r_state == 0:
63+
if self.l_state == Gesture.end:
5364
self.l_state = Gesture.idle
5465
result.append('left')
5566

56-
if self.r_state == Gesture.ing and l_state < 0 and r_state == 0:
57-
self.r_state = Gesture.end
67+
if self.r_state == Gesture.ing:
68+
# print(self.r.eliminate)
69+
self.count_2 += 1
70+
if self.count_2 > 20:
71+
self.r_state = Gesture.idle
72+
elif r_state > 0 and self.l.new-self.r.new < self.r.eliminate:
73+
self.r_state = Gesture.end
5874

59-
if self.r_state == Gesture.end and l_state == 0 and r_state > 0:
75+
if self.r_state == Gesture.end:
6076
self.r_state = Gesture.idle
6177
result.append('right')
6278

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()
79+
if l_state == 0 and r_state == 0 and self.l_state == Gesture.idle and self.r_state == Gesture.idle:
80+
self.updata +=1
81+
if self.updata>20:
82+
self.updata=0
83+
self.l.calibrate()
84+
self.r.calibrate()
6785

6886
return None if len(result) != 1 else result[0]
6987

88+
7089
def unit_test():
7190
print('\n\
7291
g = Gesture()\n\
92+
count = 0\n\
7393
while True:\n\
7494
res = g.get_gesture()\n\
7595
if res != None:\n\
7696
print(res)\n\
97+
count = 1+count\n\
98+
print(count)\n\
7799
')
78100
g = Gesture()
101+
count = 0
79102
while True:
80103
res = g.get_gesture()
81104
if res != None:
82105
print(res)
106+
count = 1+count
107+
print(count)
108+
83109

84110
if __name__ == '__main__':
85111
unit_test()

0 commit comments

Comments
 (0)