-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.py
79 lines (70 loc) · 2.15 KB
/
test2.py
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
from machine import Pin
from utime import ticks_us, ticks_diff
from array import array
class RFBtn:
def parseData(data):
key = ''
binKey = ''
charKey = ''
for i in data:
if i > 900:
key = key + "1"
else:
key = key + "0"
if(len(key)%8==0):
c = chr(int(key, 2))
charKey = charKey + c
binKey = binKey + hex(int(key, 2))[2:]
key = ''
return binKey
def listener(p):
nedges = 80
state = 0
while state < 2:
x = 0
state = 0
diffs = []
cycle = []
times = array('I', (0 for _ in range(nedges)))
# 1.findout every change point
while x < nedges:
v = p()
while v == p():
pass
times[x] = ticks_us()
x += 1
# 2.calculate diffTime & boundle
for x in range(nedges - 2):
diffs.append(times[x + 1]- times[x])
maxTime = max(diffs)*0.8
if(maxTime >7000 and maxTime < 9000):
#print("trigger:",maxTime)
for i in diffs:
if(i > maxTime*0.8 and state == 0):
state = 1
#print("start:",i)
elif (i < maxTime and state == 1):
cycle.append(i)
elif (i > maxTime and state == 1):
state = 2
#print("end:",i)
break
#print("state:",state)
if(state==2):
return RFBtn.parseData(cycle)
print("start...")
btn1 = "5556565a5556"
btn2 = "5566959a5556"
btn3 = "5559565a5556"
btn4 = "555aa5aa5556"
while True:
data = RFBtn.listener(Pin(5,Pin.IN))
if(len(data)==12):
if(data==btn1):
print("Btn 1:",data)
if(data==btn2):
print("Btn 2:",data)
if(data==btn3):
print("Btn 3:",data)
if(data==btn4):
print("Btn 4:",data)