Skip to content

Commit

Permalink
Added unit tests, and fixed some found issues
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
EGJ-Moorington committed Aug 19, 2024
1 parent ba700c9 commit 19aba0c
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 20 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*.mpy

# Python-specific files
__pycache__
**/__pycache__
*.pyc

# Sphinx build-specific files
Expand All @@ -35,6 +35,9 @@ _build
# Pre-commit related caches
.ruff_cache

# Pytest related caches
.pytest_cache

# This file results from running `pip -e install .` in a local repository
*.egg-info

Expand Down
37 changes: 18 additions & 19 deletions button_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
"""

# imports
from keypad import Event
from keypad import Event, EventQueue

try:
from keypad import EventQueue
except ImportError:
from keypad import _EventQueue as EventQueue # noqa: F401
try:
from supervisor import ticks_ms # type: ignore
except ImportError:
Expand All @@ -37,15 +33,15 @@
start_time = time()

def ticks_ms() -> int:
((time() - start_time + 536.805, 912) * 1000) & _TICKS_MAX
return int((time() - start_time + 536805.912) * 1000) & _TICKS_MAX


try:
from typing import Callable, Literal, TypeAlias, Union # noqa: F401
except ImportError:
pass

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

_TICKS_PERIOD = 1 << 29
Expand All @@ -55,7 +51,7 @@ def ticks_ms() -> int:
def timestamp_diff(time1: int, time2: int) -> int:
"""
Compute the difference between two ticks values,
assuming that they are within 2\ :sup:`28` ticks.
assuming that they are within 2\\ :sup:`28` ticks.
:param int time1: The minuend of the time difference, in milliseconds.
:param int time2: The subtrahend of the time difference, in milliseconds.
Expand Down Expand Up @@ -209,7 +205,7 @@ def __init__(
.. attribute:: _press_start_time
:type: float
:value: 0
:value: ticks_ms()
The time (in milliseconds, tracked by :meth:`supervisor.ticks_ms`)
at which the last button press began.
Expand All @@ -224,7 +220,7 @@ def __init__(

self._last_press_time = None
self._press_count = 0
self._press_start_time = 0
self._press_start_time = ticks_ms()
self._is_holding = False
self._is_pressed = False

Expand Down Expand Up @@ -446,7 +442,7 @@ def __init__(
event_queue: EventQueue,
callable_inputs: set[ButtonInput],
button_amount: int = 1,
config: dict[int, ButtonInitConfig] = {},
config: dict[int, ButtonInitConfig] = None,
) -> None:
"""
:param keypad.EventQueue event_queue: Sets :attr:`_event_queue`
Expand All @@ -460,7 +456,7 @@ def __init__(
The dictionary's keys should be the index numbers of the target buttons.
For each button that doesn't have a :class:`ButtonInitConfig` attached to it, an object
containing the default values is created.
:raise ValueError: if *button_amount* is smaller than 1.
:raise ValueError: if *button_amount* is smaller than 1, or if it is not an :type:`int`..
.. attribute:: callable_inputs
:type: set[ButtonInput]
Expand All @@ -485,14 +481,18 @@ def __init__(
The :class:`keypad.EventQueue` object the handler should read events from.
"""
if button_amount < 1:
if not isinstance(button_amount, int) or button_amount < 1:
raise ValueError("button_amount must be bigger than 0.")

self.callable_inputs = callable_inputs

self._buttons: list[Button] = []
for i in range(button_amount): # Create a Button object for each button to handle
self._buttons.append(Button(i, config.get(i, ButtonInitConfig())))
if config:
conf = config.get(i, ButtonInitConfig())
else:
conf = ButtonInitConfig()
self._buttons.append(Button(i, conf))

self._event = Event()
self._event_queue = event_queue
Expand All @@ -511,9 +511,9 @@ def update(self) -> set[ButtonInput]:
"""
Check if any button ended a multi press since the last time this method was called,
process the next :class:`keypad.Event` in :attr:`_event_queue`, call all the relevant
callback functions and return a set of the detected :class:`ButtonInput`\ s.
callback functions and return a set of the detected :class:`ButtonInput`\\ s.
:return: Returns a set containing all of the detected :class:`ButtonInput`\ s
:return: Returns a set containing all of the detected :class:`ButtonInput`\\ s
:rtype: set[ButtonInput]
"""
inputs = set()
Expand All @@ -528,7 +528,7 @@ def update(self) -> set[ButtonInput]:
if input_:
inputs.add(input_)

self._call_callbacks()
self._call_callbacks(inputs)
return inputs

def _call_callbacks(self, inputs: set[ButtonInput]) -> None:
Expand Down Expand Up @@ -590,8 +590,7 @@ def _handle_event(self, event: Event) -> Union[ButtonInput, None]:
if event.pressed: # Button just pressed
button._is_pressed = True
button._press_start_time = event.timestamp
if button._press_count < button.max_multi_press:
button._last_press_time = event.timestamp
button._last_press_time = event.timestamp
button._press_count += 1

else: # Button just released
Expand Down
Loading

0 comments on commit 19aba0c

Please sign in to comment.