Skip to content

Commit

Permalink
Tested and finalised version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
EGJ-Moorington committed Aug 19, 2024
1 parent 19aba0c commit fff6ffb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
46 changes: 23 additions & 23 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,44 +101,44 @@ This simple script showcases the usage of this library using a single button.

.. code-block:: python
import time
import time

import board
from keypad import Keys
import board
from keypad import Keys

from button_handler import ButtonHandler
from button_handler import ButtonHandler, ButtonInput


def double_press():
print("Double press detected!")
def double_press():
print("Double press detected!")


def short_press():
print("Short press detected!")
def short_press():
print("Short press detected!")


def long_press():
print("Long press detected!")
def long_press():
print("Long press detected!")


def hold():
print("The button began being held down!")
def hold():
print("The button began being held down!")


actions = {
"DOUBLE_PRESS": double_press,
"SHORT_PRESS": short_press,
"LONG_PRESS": long_press,
"HOLD": hold,
}
actions = {
ButtonInput("DOUBLE_PRESS", callback=double_press),
ButtonInput("SHORT_PRESS", callback=short_press),
ButtonInput("LONG_PRESS", callback=long_press),
ButtonInput("HOLD", callback=hold),
}

scanner = Keys([board.D9], value_when_pressed=False)
button_handler = ButtonHandler(scanner.events, actions)
scanner = Keys([board.D9], value_when_pressed=False)
button_handler = ButtonHandler(scanner.events, actions)


while True:
button_handler.update()
time.sleep(0.0025)
while True:
button_handler.update()
time.sleep(0.0025)

Documentation
=============
Expand Down
14 changes: 6 additions & 8 deletions button_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def ticks_ms() -> int:
except ImportError:
pass

__version__ = "2.0.0-beta.2"
__version__ = "2.0.0"
__repo__ = "https://github.com/EGJ-Moorington/CircuitPython_Button_Handler.git"

_TICKS_PERIOD = 1 << 29
Expand Down Expand Up @@ -301,13 +301,9 @@ def _is_held(self, current_time: int) -> bool:
class ButtonInput:
"""Defines a button's input's characteristics."""

InputAction: TypeAlias = Union[
Literal["SHORT_PRESS", "LONG_PRESS", "HOLD", "DOUBLE_PRESS"], str
]

def __init__(
self,
action: InputAction,
action: Union[Literal["SHORT_PRESS", "LONG_PRESS", "HOLD", "DOUBLE_PRESS"], str],
button_number: int = 0,
callback: Callable[[], None] = lambda: None,
timestamp: int = 0,
Expand Down Expand Up @@ -374,7 +370,9 @@ def action(self):
return self._action

@action.setter
def action(self, action: InputAction):
def action(
self, action: Union[Literal["SHORT_PRESS", "LONG_PRESS", "HOLD", "DOUBLE_PRESS"], str]
):
if action in {"SHORT_PRESS", "LONG_PRESS", "HOLD"}:
self._action = action
return
Expand Down Expand Up @@ -546,7 +544,7 @@ def _call_callbacks(self, inputs: set[ButtonInput]) -> None:
if not input_ in self.callable_inputs:
continue
for callable_input in self.callable_inputs:
if callable_input is input_:
if callable_input == input_:
callable_input.callback()

def _handle_buttons(self) -> set[ButtonInput]:
Expand Down
2 changes: 1 addition & 1 deletion examples/button_handler_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from button_handler import ButtonHandler

scanner = Keys([board.D9], value_when_pressed=False)
button_handler = ButtonHandler(scanner.events)
button_handler = ButtonHandler(scanner.events, set())

while True:
inputs = button_handler.update()
Expand Down
10 changes: 5 additions & 5 deletions examples/button_handler_singlebutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import board
from keypad import Keys

from button_handler import ButtonHandler
from button_handler import ButtonHandler, ButtonInput


def double_press():
Expand All @@ -28,10 +28,10 @@ def hold():


actions = {
"DOUBLE_PRESS": double_press,
"SHORT_PRESS": short_press,
"LONG_PRESS": long_press,
"HOLD": hold,
ButtonInput("DOUBLE_PRESS", callback=double_press),
ButtonInput("SHORT_PRESS", callback=short_press),
ButtonInput("LONG_PRESS", callback=long_press),
ButtonInput("HOLD", callback=hold),
}

scanner = Keys([board.D9], value_when_pressed=False)
Expand Down

0 comments on commit fff6ffb

Please sign in to comment.