Skip to content

Commit 0339d72

Browse files
committed
events.Ebutton: Simplify code. Improve test script.
1 parent 883ce96 commit 0339d72

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

v3/primitives/events.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,12 @@ async def _poll(self, dt): # Poll the button
123123
def _pf(self): # Button press
124124
if not self._supp:
125125
self.press.set() # User event
126-
if not (self._ltim() or self._dtim()): # Don't retrigger long timer if already running
127-
self._ltim.trigger()
128126
if self._dtim(): # Press occurred while _dtim is running
129127
self.double.set() # User event
130128
self._dtim.stop() # _dtim's Event is only used if suppress
131-
else:
129+
else: # Single press or 1st of a double pair.
132130
self._dtim.trigger()
131+
self._ltim.trigger() # Trigger long timer on 1st press of a double pair
133132

134133
def _rf(self): # Button release
135134
self._ltim.stop()
@@ -142,11 +141,12 @@ async def _ltf(self): # Long timeout
142141
self._ltim.clear() # Clear the event
143142
self.long.set() # User event
144143

145-
async def _dtf(self): # Double timeout (runs if suppress is set)
144+
# Runs if suppress set. Delay response to single press until sure it is a single short pulse.
145+
async def _dtf(self):
146146
while True:
147-
await self._dtim.wait()
147+
await self._dtim.wait() # Double click has timed out
148148
self._dtim.clear() # Clear the event
149-
if not self._ltim(): # Button was released
149+
if not self._ltim(): # Button was released: not a long press.
150150
self.press.set() # User events
151151
self.release.set()
152152

v3/primitives/tests/event_test.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,15 @@ async def btest(btn, verbose, supp):
116116
val = 0
117117
events = btn.press, btn.release, btn.double, btn.long
118118
tasks = []
119-
for n, evt in enumerate(events):
119+
for n, evt in enumerate(events): # Each event has a 3-bit event counter
120120
tasks.append(asyncio.create_task(monitor(evt, 1 << 3 * n, verbose)))
121121
await asyncio.sleep(1)
122122
print("Start short press test")
123123
await pulse()
124124
await asyncio.sleep(1)
125125
verbose and print("Test of short press", hex(val))
126126
expect(val, 0x09)
127+
127128
val = 0
128129
await asyncio.sleep(1)
129130
print("Start long press test")
@@ -132,6 +133,7 @@ async def btest(btn, verbose, supp):
132133
verbose and print("Long press", hex(val))
133134
exp = 0x208 if supp else 0x209
134135
expect(val, exp)
136+
135137
val = 0
136138
await asyncio.sleep(1)
137139
print("Start double press test")
@@ -142,6 +144,7 @@ async def btest(btn, verbose, supp):
142144
verbose and print("Double press", hex(val))
143145
exp = 0x48 if supp else 0x52
144146
expect(val, exp)
147+
145148
val = 0
146149
await asyncio.sleep(1)
147150
print("Start double press, 2nd press long, test")
@@ -163,9 +166,11 @@ async def stest(sw, verbose):
163166
for n, evt in enumerate(events):
164167
tasks.append(asyncio.create_task(monitor(evt, 1 << 3 * n, verbose)))
165168
asyncio.create_task(pulse(2000))
169+
print("Switch closure")
166170
await asyncio.sleep(1)
167171
expect(val, 0x08)
168172
await asyncio.sleep(4) # Wait for any spurious events
173+
print("Switch open")
169174
verbose and print("Switch close and open", hex(val))
170175
expect(val, 0x09)
171176
for task in tasks:
@@ -177,12 +182,15 @@ async def switch_test(pol, verbose):
177182
pin = Pin('Y1', Pin.IN)
178183
pout = Pin('Y2', Pin.OUT, value=pol)
179184
print("Testing EButton.")
180-
print("suppress == False")
185+
print("Testing with suppress == False")
181186
btn = EButton(pin)
182187
await btest(btn, verbose, False)
183-
print("suppress == True")
188+
print()
189+
print("Testing with suppress == True")
190+
btn.deinit()
184191
btn = EButton(pin, suppress=True)
185192
await btest(btn, verbose, True)
193+
print()
186194
print("Testing ESwitch")
187195
sw = ESwitch(pin, pol)
188196
await stest(sw, verbose)

0 commit comments

Comments
 (0)