|
1 | 1 | from time import sleep_ms |
2 | 2 |
|
3 | 3 |
|
4 | | -class LightIntensity(): |
| 4 | +class Intensity(): |
| 5 | + dither = 5 |
| 6 | + |
5 | 7 | def __init__(self, pin): |
| 8 | + self.old, self.new, self.eliminate = 0, 0, 0 |
| 9 | + |
6 | 10 | from machine import ADC, Pin |
7 | 11 | 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 |
9 | 13 |
|
10 | 14 | 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) |
14 | 18 |
|
| 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 |
15 | 24 |
|
16 | | -class Sensegesture(): |
17 | | - idle, running, finish = 0, 1, 2 |
| 25 | + def calibrate(self): |
| 26 | + self.eliminate = self.read() / Intensity.dither |
18 | 27 |
|
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 |
23 | 30 |
|
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 |
27 | 35 |
|
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() |
30 | 39 |
|
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) |
61 | 42 | 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 |
86 | 48 |
|
| 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] |
87 | 69 |
|
88 | 70 | def unit_test(): |
89 | 71 | 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\ |
96 | 77 | ') |
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) |
104 | 83 |
|
105 | 84 | if __name__ == '__main__': |
106 | 85 | unit_test() |
0 commit comments