-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbutton.py
203 lines (176 loc) · 6.37 KB
/
button.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import machine
import micropython
import time
PRESS = const(0x01)
RELEASE = const(0x02)
LONGPRESS = const(0x04)
DOUBLEPRESS = const(0x08)
MULTIPRESS = const(0x10)
PRESSWAIT = const(0x20)
state_list = [PRESS, RELEASE, LONGPRESS, DOUBLEPRESS]
class BtnChild:
def __init__(self, pin, dbtime=20):
self.pin = machine.Pin(pin, mode=machine.Pin.IN, pull=machine.Pin.PULL_UP)
self._event = 0
self._eventLast = 0
self._valueLast = 1
self._pressTime = 0
self._releaseTime = 0
self._doubleTime = 220
self._dbtime = dbtime
self._holdTime = 1000.0
self._cbState = PRESS | RELEASE
self._eventTime = {PRESS:0, RELEASE:0, LONGPRESS:0, DOUBLEPRESS:0}
self.cb = {PRESS: None, RELEASE:None, LONGPRESS:None, DOUBLEPRESS:None}
def wasDoublePress(self, callback=None):
self._cbState |= DOUBLEPRESS
if callback:
self.cb[DOUBLEPRESS] = callback
elif self._event & DOUBLEPRESS:
self._event -= DOUBLEPRESS
return True
else:
return False
def pressFor(self, holdTime=1.0, callback=None):
self._holdTime = holdTime * 1000
self._cbState |= LONGPRESS
if callback:
self.cb[LONGPRESS] = callback
elif self._event & LONGPRESS:
self._event -= LONGPRESS
return True
else:
return False
def wasReleased(self, callback=None):
if callback:
self.cb[RELEASE] = callback
elif self._event & RELEASE:
self._event -= RELEASE
return True
else:
return False
def wasPressed(self, callback=None):
if callback:
self.cb[PRESS] = callback
elif self._event & PRESS:
self._event -= PRESS
return True
else:
return False
def isPressed(self):
return not self.pin.value()
def isReleased(self):
return self.pin.value()
def restart(self):
self._cbState = PRESS | RELEASE
self.cb = {PRESS: None, RELEASE:None, LONGPRESS:None, DOUBLEPRESS:None}
self._eventTime = {PRESS:0, RELEASE:0, LONGPRESS:0, DOUBLEPRESS:0}
self._event = 0
self._pressTime = 0
self._releaseTime = 0
self._valueLast = 1
def deinit(self):
pass
def upDate(self):
value = self.pin.value()
state = self._eventLast ^ self._event
if state:
for i in state_list:
if state & i:
if self._event & i:
self._eventTime[i] = 40
else:
self._eventTime[i] = 0
self._eventLast = self._event
for i in state_list:
if self._eventTime[i] > 1:
self._eventTime[i] -= 1
elif self._eventTime[i] == 1:
self._eventTime[i] = 0
self._event &= ~i
if value ^ self._valueLast:
nowTime = time.ticks_ms()
self._valueLast = value
if not value:
if nowTime - self._pressTime > self._dbtime:
if self._cbState & DOUBLEPRESS and nowTime - self._pressTime < self._doubleTime:
self._event |= DOUBLEPRESS
self._event |= PRESSWAIT
self._pressTime = nowTime
else:
if nowTime - self._releaseTime> self._dbtime:
if self._cbState & LONGPRESS and nowTime - self._pressTime > self._holdTime:
self._event |= LONGPRESS
else:
self._event |= RELEASE
self._releaseTime = nowTime
class Btn:
def __init__(self, multiTime=50):
self.timer = machine.Timer(1)
self.timer.init(period=10, mode=self.timer.PERIODIC, callback=self.timerCb)
self.btn = []
self.multiList = []
self._multiTime = multiTime
def attach(self, pin):
self.btn.append(BtnChild(pin))
return self.btn[-1]
def detach(self, btnIn):
if btnIn in self.btn:
self.btn.remove(btnIn)
def restart(self):
self.multiList = []
self.btn = self.btn[:3]
for i in self.btn:
i.restart()
def multiBtnCb(self, btn1, btn2, callback=None):
self.multiList.append([btn1, btn2, callback])
btn1._cbState |= MULTIPRESS
btn2._cbState |= MULTIPRESS
def timerCb(self, arg):
nowTime = time.ticks_ms()
for i in self.btn:
i.upDate()
for i in self.multiList:
if i[0]._event & PRESSWAIT and i[1]._event & PRESSWAIT:
if abs(i[0]._pressTime - i[1]._pressTime) < self._multiTime:
i[0]._event &= ~PRESSWAIT
i[1]._event &= ~PRESSWAIT
i[2]()
for i in self.btn:
if i._event & DOUBLEPRESS:
if i.cb[DOUBLEPRESS]:
i.cb[DOUBLEPRESS]()
i._event &= ~PRESSWAIT
i._event &= ~DOUBLEPRESS
if i._event & PRESSWAIT:
if i._cbState & DOUBLEPRESS:
if nowTime - i._pressTime > i._doubleTime:
i._event &= ~PRESSWAIT
if i.cb[PRESS]:
i.cb[PRESS]()
else:
i._event |= PRESS
i._eventTime[0] = 40
elif i._cbState & MULTIPRESS:
if nowTime - i._pressTime > self._multiTime:
i._event &= ~PRESSWAIT
if i.cb[PRESS]:
i.cb[PRESS]()
else:
i._event |= PRESS
else:
i._event &= ~PRESSWAIT
if i.cb[PRESS]:
i.cb[PRESS]()
else:
i._event |= PRESS
if i._event & LONGPRESS:
if i.cb[LONGPRESS]:
i._event &= ~LONGPRESS
i.cb[LONGPRESS]()
if i._event & RELEASE:
if i.cb[RELEASE]:
i._event &= ~RELEASE
i.cb[RELEASE]()
def deinit(self):
pass