@@ -41,7 +41,7 @@ def ticks_ms() -> int:
41
41
except ImportError :
42
42
pass
43
43
44
- __version__ = "2 .0.0"
44
+ __version__ = "3 .0.0"
45
45
__repo__ = "https://github.com/EGJ-Moorington/CircuitPython_Button_Handler.git"
46
46
47
47
_TICKS_PERIOD = 1 << 29
@@ -73,22 +73,22 @@ def __init__(
73
73
) -> None :
74
74
"""
75
75
:param bool enable_multi_press: Sets :attr:`.enable_multi_press`
76
- (whether to track multi presses).
76
+ (whether to track multi- presses).
77
77
:param float multi_press_interval: Sets :attr:`.multi_press_interval`
78
- (the time frame within which two presses should occur to count as a multi press).
78
+ (the time frame within which two presses should occur to count as a multi- press).
79
79
:param float long_press_threshold: Sets :attr:`.long_press_threshold`
80
80
(the minimum length of a press to count as a long press).
81
81
:param int max_multi_press: Sets :attr:`.max_multi_press`
82
- (the maximum amount of presses a multi press can have).
82
+ (the maximum amount of presses a multi- press can have).
83
83
84
84
.. attribute:: enable_multi_press
85
85
:type: bool
86
86
:value: enable_multi_press = True
87
87
88
88
Whether to account for the possibility of another short press
89
- following a short press and counting as a multi press.
89
+ following a short press and counting as a multi- press.
90
90
If set to false, :meth:`ButtonHandler.update`
91
- returns ``SHORT_PRESS`` immediately after a short press.
91
+ returns a short press :class:`ButtonInput` object immediately after a short press.
92
92
93
93
.. attribute:: long_press_threshold
94
94
:type: float
@@ -101,16 +101,16 @@ def __init__(
101
101
:type: int
102
102
:value: max_multi_press = 2
103
103
104
- The maximum amount of button presses that a multi press can be.
105
- :meth:`ButtonHandler.update` returns the appropiate ``_MULTI_PRESS`` immediaetly after
106
- the button has been pressed this many times.
104
+ The maximum amount of button presses that a multi- press can be.
105
+ :meth:`ButtonHandler.update` returns the appropiate multi-press :class:`ButtonInput`
106
+ object immediaetly after the button has been pressed this many times.
107
107
108
108
.. attribute:: multi_press_interval
109
109
:type: float
110
110
:value: multi_press_interval = 175
111
111
112
112
The time frame from a button release within which
113
- another release should occur to count as a multi press.
113
+ another release should occur to count as a multi- press.
114
114
"""
115
115
self .enable_multi_press = enable_multi_press
116
116
self .long_press_threshold = long_press_threshold
@@ -137,8 +137,9 @@ def __init__(
137
137
:value: config.enable_multi_press = True
138
138
139
139
Whether to account for the possibility of another short press
140
- following a short press and counting that as a multi press. If set to false,
141
- :meth:`ButtonHandler.update` returns ``SHORT_PRESS`` immediately after a short press.
140
+ following a short press and counting that as a multi-press. If set to false,
141
+ :meth:`ButtonHandler.update` returns a short press :class:`ButtonInput`
142
+ object immediately after a short press.
142
143
143
144
.. attribute:: long_press_threshold
144
145
:type: float
@@ -151,16 +152,16 @@ def __init__(
151
152
:type: int
152
153
:value: config.max_multi_press = 2
153
154
154
- The maximum amount of button presses that a multi press can be.
155
- :meth:`ButtonHandler.update` returns the appropiate ``_MULTI_PRESS`` immediaetly after
156
- the button has been pressed this many times.
155
+ The maximum amount of button presses that a multi- press can be.
156
+ :meth:`ButtonHandler.update` returns the appropiate multi-press :class:`ButtonInput`
157
+ object immediaetly after the button has been pressed this many times.
157
158
158
159
.. attribute:: multi_press_interval
159
160
:type: float
160
161
:value: config.multi_press_interval = 175
161
162
162
163
The time frame from a button release within which
163
- another release should occur to count as a multi press.
164
+ another release should occur to count as a multi- press.
164
165
165
166
.. caution:: Attributes with a *leading underscore (_)* are meant for **internal use only**,
166
167
and accessing them may cause **unexpected behaviour**. Please consider accessing
@@ -192,15 +193,15 @@ def __init__(
192
193
:value: None
193
194
194
195
The time (in miliseconds, tracked by :meth:`supervisor.ticks_ms`) that has passed since
195
- the start of the previous press of a multi press. It is set to :type:`None`
196
+ the start of the previous press of a multi- press. It is set to :type:`None`
196
197
after the time specified by :attr:`.multi_press_interval` has passed.
197
198
198
199
.. attribute:: _press_count
199
200
:type: int
200
201
:value: 0
201
202
202
203
The amount of times the button has been pressed since the last
203
- multi press ended. It is set to 0 if the time set
204
+ multi- press ended. It is set to 0 if the time set
204
205
by :attr:`.multi_press_interval` passes after a short press.
205
206
206
207
.. attribute:: _press_start_time
@@ -257,12 +258,12 @@ def _check_multi_press_timeout(self, current_time: int) -> Union[int, None]:
257
258
.. caution:: Methods with a *leading underscore (_)* are meant for **internal use only**,
258
259
and calling them may cause **unexpected behaviour**. Please refrain from using them.
259
260
260
- Check whether a multi press has ended.
261
- If it has, return the amount of times the button was pressed in that multi press.
261
+ Check whether a multi- press has ended.
262
+ If it has, return the amount of times the button was pressed in that multi- press.
262
263
263
264
:param int current_time: The current time, provided by :meth:`supervisor.ticks_ms`.
264
- :return: The amount of times the button was pressed in a multi press,
265
- if a multi press has ended.
265
+ :return: The amount of times the button was pressed in a multi- press,
266
+ if a multi- press has ended.
266
267
:rtype: int or None
267
268
"""
268
269
if (
@@ -301,9 +302,14 @@ def _is_held(self, current_time: int) -> bool:
301
302
class ButtonInput :
302
303
"""Defines a button's input's characteristics."""
303
304
305
+ SHORT_PRESS = 1
306
+ DOUBLE_PRESS = 2
307
+ HOLD = "H"
308
+ LONG_PRESS = "L"
309
+
304
310
def __init__ (
305
311
self ,
306
- action : Union [Literal [ "SHORT_PRESS" , "LONG_PRESS" , "HOLD" , "DOUBLE_PRESS" ] , str ],
312
+ action : Union [int , str ],
307
313
button_number : int = 0 ,
308
314
callback : Callable [[], None ] = lambda : None ,
309
315
timestamp : int = 0 ,
@@ -317,7 +323,13 @@ def __init__(
317
323
:param int timestamp: Sets :attr:`timestamp` (the time at which the input was performed).
318
324
319
325
.. type:: InputAction
320
- :canonical: Literal["SHORT_PRESS", "LONG_PRESS", "HOLD", "DOUBLE_PRESS"] | str
326
+ :canonical: int | str
327
+
328
+ Represents the action the :class:`ButtonInput` object represents.
329
+ Using a constant defined by :class:`ButtonInput` when available is recommended.
330
+ To represent a multi-press, use the number of presses in that multi-press.
331
+ Available constants are :const:`SHORT_PRESS`, :const:`DOUBLE_PRESS`,
332
+ :const:`HOLD` and :const:`LONG_PRESS`.
321
333
322
334
.. attribute:: button_number
323
335
:type: int
@@ -339,6 +351,37 @@ def __init__(
339
351
The timestamp (in milliseconds, provided by :meth:`supervisor.ticks_ms`)
340
352
at which the input was performed.
341
353
354
+ .. warning:: Variables written in *upper case with underscores* are constants and
355
+ should not be modified. Doing so may cause **unexpected behaviour**.
356
+
357
+ .. data:: SHORT_PRESS
358
+ :type: int
359
+ :value: 1
360
+
361
+ Represents a short press to pass as an argument to
362
+ parameter `action` in :class:`ButtonInput`.
363
+
364
+ .. data:: DOUBLE_PRESS
365
+ :type: int
366
+ :value: 2
367
+
368
+ Represents a double press to pass as an argument to
369
+ parameter `action` in :class:`ButtonInput`.
370
+
371
+ .. data:: HOLD
372
+ :type: str
373
+ :value: "H"
374
+
375
+ Represents a hold action to pass as an argument to
376
+ parameter `action` in :class:`ButtonInput`.
377
+
378
+ .. data:: LONG_PRESS
379
+ :type: str
380
+ :value: "L"
381
+
382
+ Represents a long press to pass as an argument to
383
+ parameter `action` in :class:`ButtonInput`.
384
+
342
385
.. caution:: Attributes with a *leading underscore (_)* are meant for
343
386
**internal use only**, and accessing them may cause **unexpected behaviour**.
344
387
Please consider accessing a *property* (if available) instead.
@@ -361,31 +404,23 @@ def action(self):
361
404
362
405
:type: InputAction
363
406
:param InputAction action: The action associated with the input.
364
- :raise ValueError: if action is not a valid action. Valid actions are ``LONG_PRESS``,
365
- ``HOLD``, ``SHORT_PRESS`` (exactly the same as ``1_MULTI_PRESS``),
366
- ``DOUBLE_PRESS`` (exactly the same as ``2_MULTI_PRESS``) and
367
- ``x_MULTI_PRESS`` where x is an :type:`int` bigger than 0.
407
+ :raise ValueError: if *action* is not a valid action. Valid actions are
408
+ :const:`SHORT_PRESS`, :const:`DOUBLE_PRESS`, :const:`HOLD`, :const:`LONG_PRESS`
409
+ and any :type:`int` bigger than 0.
368
410
369
411
"""
370
412
return self ._action
371
413
372
414
@action .setter
373
- def action (
374
- self , action : Union [Literal ["SHORT_PRESS" , "LONG_PRESS" , "HOLD" , "DOUBLE_PRESS" ], str ]
375
- ):
376
- if action in {"SHORT_PRESS" , "LONG_PRESS" , "HOLD" }:
415
+ def action (self , action : Union [int , str ]):
416
+ if action in {ButtonInput .LONG_PRESS , ButtonInput .HOLD }:
377
417
self ._action = action
378
418
return
379
419
try :
380
- if action == "DOUBLE_PRESS" :
381
- action = "2_MULTI_PRESS"
382
- if not action .endswith ("_MULTI_PRESS" ):
420
+ if not isinstance (action , int ):
383
421
raise ValueError
384
- num = int (action .split ("_" )[0 ])
385
- if num < 1 :
422
+ if action < 1 :
386
423
raise ValueError
387
- if num == 1 :
388
- action = "SHORT_PRESS"
389
424
self ._action = action
390
425
except ValueError :
391
426
raise ValueError (f"Invalid action: { action } ." )
@@ -446,15 +481,15 @@ def __init__(
446
481
:param keypad.EventQueue event_queue: Sets :attr:`_event_queue`
447
482
(the :class:`keypad.EventQueue` object the handler should read events from).
448
483
:param set[ButtonInput] callable_inputs: Sets :attr:`callable_inputs`
449
- (the :class:`ButtonInitConfig ` objects used to define the callback functions).
484
+ (the :class:`ButtonInput ` objects used to define the callback functions).
450
485
:param int button_amount: The amount of buttons scanned by the :mod:`keypad` scanner
451
- that created the event_queue parameter's argument :class:`keypad.EventQueue` object.
486
+ that created the * event_queue* parameter's argument :class:`keypad.EventQueue` object.
452
487
:param dict[int, ButtonInitConfig] config: A dictionary containing
453
488
:class:`ButtonInitConfig` objects used to initialise :class:`Button` objects.
454
489
The dictionary's keys should be the index numbers of the target buttons.
455
490
For each button that doesn't have a :class:`ButtonInitConfig` attached to it, an object
456
491
containing the default values is created.
457
- :raise ValueError: if *button_amount* is smaller than 1, or if it is not an :type:`int`..
492
+ :raise ValueError: if *button_amount* is smaller than 1, or if it is not an :type:`int`.
458
493
459
494
.. attribute:: callable_inputs
460
495
:type: set[ButtonInput]
@@ -507,7 +542,7 @@ def buttons(self) -> list[Button]:
507
542
508
543
def update (self ) -> set [ButtonInput ]:
509
544
"""
510
- Check if any button ended a multi press since the last time this method was called,
545
+ Check if any button ended a multi- press since the last time this method was called,
511
546
process the next :class:`keypad.Event` in :attr:`_event_queue`, call all the relevant
512
547
callback functions and return a set of the detected :class:`ButtonInput`\\ s.
513
548
@@ -553,7 +588,7 @@ def _handle_buttons(self) -> set[ButtonInput]:
553
588
and calling them may cause **unexpected behaviour**. Please refrain from using them.
554
589
555
590
Check if any button began being held down since the last time this mehod was called
556
- and if any multi press ended, and return every detected :class:`ButtonInput`.
591
+ and if any multi- press ended, and return every detected :class:`ButtonInput`.
557
592
558
593
:return: A set containing every detected :class:`ButtonInput`.
559
594
:rtype: set[ButtonInput]
@@ -562,15 +597,13 @@ def _handle_buttons(self) -> set[ButtonInput]:
562
597
current_time = ticks_ms ()
563
598
for button in self ._buttons :
564
599
if button ._is_held (current_time ):
565
- inputs .add (ButtonInput ("HOLD" , button .button_number , timestamp = current_time ))
600
+ inputs .add (
601
+ ButtonInput (ButtonInput .HOLD , button .button_number , timestamp = current_time )
602
+ )
566
603
else :
567
604
num = button ._check_multi_press_timeout (current_time )
568
605
if num :
569
- inputs .add (
570
- ButtonInput (
571
- f"{ num } _MULTI_PRESS" , button .button_number , timestamp = current_time
572
- )
573
- )
606
+ inputs .add (ButtonInput (num , button .button_number , timestamp = current_time ))
574
607
return inputs
575
608
576
609
def _handle_event (self , event : Event ) -> Union [ButtonInput , None ]:
@@ -598,17 +631,21 @@ def _handle_event(self, event: Event) -> Union[ButtonInput, None]:
598
631
< button .long_press_threshold
599
632
): # Short press
600
633
if not button .enable_multi_press :
601
- input_ = ButtonInput ("SHORT_PRESS" , event .key_number , timestamp = event .timestamp )
634
+ input_ = ButtonInput (
635
+ ButtonInput .SHORT_PRESS , event .key_number , timestamp = event .timestamp
636
+ )
602
637
elif button ._press_count == button .max_multi_press :
603
638
input_ = ButtonInput (
604
- f" { button .max_multi_press } _MULTI_PRESS" ,
639
+ button .max_multi_press ,
605
640
event .key_number ,
606
641
timestamp = event .timestamp ,
607
642
)
608
643
else : # More short presses could follow
609
644
return None
610
645
else :
611
- input_ = ButtonInput ("LONG_PRESS" , event .key_number , timestamp = event .timestamp )
646
+ input_ = ButtonInput (
647
+ ButtonInput .LONG_PRESS , event .key_number , timestamp = event .timestamp
648
+ )
612
649
button ._is_holding = False
613
650
button ._last_press_time = None
614
651
button ._press_count = 0
0 commit comments