Source code for j5.components.button

"""Classes for Button."""

from abc import abstractmethod
from typing import Type

from j5.boards import Board
from j5.components import Component, Interface


[docs]class ButtonInterface(Interface): """An interface containing the methods required for a button."""
[docs] @abstractmethod def get_button_state(self, board: Board, identifier: int) -> bool: """Set the state of a button.""" raise NotImplementedError # pragma: no cover
[docs] @abstractmethod def wait_until_button_pressed(self, board: Board, identifier: int) -> None: """Halt the program until this button is pushed.""" raise NotImplementedError # pragma: no cover
[docs]class Button(Component): """A button.""" def __init__(self, identifier: int, board: Board, backend: ButtonInterface) -> None: self._board = board self._backend = backend self._identifier = identifier
[docs] @staticmethod def interface_class() -> Type[ButtonInterface]: """Get the interface class that is required to use this component.""" return ButtonInterface
@property def is_pressed(self) -> bool: """Get the current pushed state of the button.""" return self._backend.get_button_state(self._board, self._identifier)
[docs] def wait_until_pressed(self) -> None: """Halt the program until this button is pushed.""" self._backend.wait_until_button_pressed(self._board, self._identifier)