Skip to content

Commit 883ce96

Browse files
committed
events.EButton: fix iss 101. Queues: allow int list size arg.
1 parent 0270af5 commit 883ce96

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

v3/docs/EVENTS.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,10 @@ Attributes of `RingbufQueue`:
493493
4. It has an "overwrite oldest data" synchronous write mode.
494494

495495
Constructor mandatory arg:
496-
* `buf` Buffer for the queue, e.g. list `[0 for _ in range(20)]` or array. A
497-
buffer of size `N` can hold a maximum of `N-1` items.
496+
* `buf` Buffer for the queue, e.g. list, bytearray or array. If an integer is
497+
passed, a list of this size is created. A buffer of size `N` can hold a
498+
maximum of `N-1` items. Note that, where items on the queue are suitably
499+
limited, bytearrays or arrays are more efficient than lists.
498500

499501
Synchronous methods (immediate return):
500502
* `qsize` No arg. Returns the number of items in the queue.

v3/docs/THREADING.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ Attributes of `ThreadSafeQueue`:
317317
raises an `IndexError`.
318318

319319
Constructor mandatory arg:
320-
* `buf` Buffer for the queue, e.g. list `[0 for _ in range(20)]` or array. A
321-
buffer of size `N` can hold a maximum of `N-1` items.
320+
* `buf` Buffer for the queue, e.g. list, bytearray or array. If an integer is
321+
passed, a list of this size is created. A buffer of size `N` can hold a
322+
maximum of `N-1` items. Note that, where items on the queue are suitably
323+
limited, bytearrays or arrays are more efficient than lists.
322324

323325
Synchronous methods.
324326
* `qsize` No arg. Returns the number of items in the queue.

v3/primitives/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ 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(): # Don't retrigger long timer if already running
126+
if not (self._ltim() or self._dtim()): # Don't retrigger long timer if already running
127127
self._ltim.trigger()
128128
if self._dtim(): # Press occurred while _dtim is running
129129
self.double.set() # User event

v3/primitives/ringbuf_queue.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ringbuf_queue.py Provides RingbufQueue class
22

3-
# Copyright (c) 2022 Peter Hinch
3+
# Copyright (c) 2022-2023 Peter Hinch
44
# Released under the MIT License (MIT) - see LICENSE file
55

66
# API differs from CPython
@@ -13,7 +13,7 @@
1313

1414
class RingbufQueue: # MicroPython optimised
1515
def __init__(self, buf):
16-
self._q = buf
16+
self._q = [0 for _ in range(buf)] if isinstance(buf, int) else buf
1717
self._size = len(buf)
1818
self._wi = 0
1919
self._ri = 0

v3/primitives/tests/event_test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def expect(v, e):
108108
if v == e:
109109
print("Pass")
110110
else:
111-
print(f"Fail: expected {e} got {v}")
111+
print(f"Fail: expected 0x{e:04x} got 0x{v:04x}")
112112
fail = True
113113

114114
async def btest(btn, verbose, supp):
@@ -142,6 +142,16 @@ async def btest(btn, verbose, supp):
142142
verbose and print("Double press", hex(val))
143143
exp = 0x48 if supp else 0x52
144144
expect(val, exp)
145+
val = 0
146+
await asyncio.sleep(1)
147+
print("Start double press, 2nd press long, test")
148+
await pulse()
149+
await asyncio.sleep_ms(100)
150+
await pulse(2000)
151+
await asyncio.sleep(4)
152+
verbose and print("Double press", hex(val))
153+
exp = 0x48 if supp else 0x52
154+
expect(val, exp)
145155
for task in tasks:
146156
task.cancel()
147157

v3/threadsafe/threadsafe_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ThreadSafeQueue: # MicroPython optimised
1313
def __init__(self, buf):
14-
self._q = buf
14+
self._q = [0 for _ in range(buf)] if isinstance(buf, int) else buf
1515
self._size = len(buf)
1616
self._wi = 0
1717
self._ri = 0

0 commit comments

Comments
 (0)