|
| 1 | +from time import sleep_ms |
| 2 | + |
1 | 3 |
|
2 | 4 | class LightIntensity(): |
3 | 5 | def __init__(self, pin): |
4 | 6 | from machine import ADC, Pin |
5 | 7 | self.adc = ADC(Pin(pin, Pin.IN)) |
6 | | - self.adc.atten(ADC.ATTN_11DB) # 0-3.9V |
| 8 | + self.adc.atten(ADC.ATTN_11DB) # 0-3.9V |
| 9 | + |
7 | 10 | def read(self): |
8 | | - self.last_val=self.new_val |
| 11 | + self.last_val = self.new_val |
9 | 12 | self.new_val = self.adc.read()/5 |
10 | 13 | return int(self.new_val*5/40.95) |
11 | 14 |
|
| 15 | + |
| 16 | +class Sensegesture(): |
| 17 | + idle, running, finish = 0, 1, 2 |
| 18 | + |
| 19 | + def __init__(self, leftPin=36, rightpin=39): |
| 20 | + from light1 import LightIntensity |
| 21 | + self.LightL = LightIntensity(leftPin) |
| 22 | + self.LightR = LightIntensity(rightpin) |
| 23 | + |
| 24 | + def get_brightness(self): |
| 25 | + self.LightR.read() |
| 26 | + self.LightL.read() |
| 27 | + |
| 28 | + def set_threshold(self, val): # 设置阈值 |
| 29 | + self.threshold = self.LightR.new_val*val |
| 30 | + |
| 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 |
| 61 | + 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 |
| 86 | + |
| 87 | + |
12 | 88 | def unit_test(): |
13 | 89 | print('\n\ |
14 | | - from time import sleep\n\ |
15 | | - LightL = LightIntensity(36)\n\ |
16 | | - LightR = LightIntensity(39)\n\ |
17 | | - while True:\n\ |
18 | | - print("LightL", LightL.read())\n\ |
19 | | - print("LightR", LightR.read())\n\ |
20 | | - sleep(0.5)\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\ |
21 | 96 | ') |
22 | | - from time import sleep |
23 | | - LightL = LightIntensity(36) |
24 | | - LightR = LightIntensity(39) |
25 | | - while True: |
26 | | - print('LightL', LightL.read()) |
27 | | - print('LightR', LightR.read()) |
28 | | - sleep(0.5) |
| 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 | + |
29 | 104 |
|
30 | 105 | if __name__ == '__main__': |
31 | 106 | unit_test() |
0 commit comments