Skip to content

Commit

Permalink
Add Button Devices (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC authored Dec 12, 2024
1 parent 2b25608 commit 7104ff9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions leads_gpio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
if not _find_spec("serial"):
raise ImportError("Please install `pyserial` to run this module\n>>>pip install pyserial")

from leads_gpio.button import *
from leads_gpio.cpu_monitor import *
from leads_gpio.gps_receiver import *
from leads_gpio.led import *
Expand Down
45 changes: 45 additions & 0 deletions leads_gpio/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import override as _override

from gpiozero import Button as _Button

from leads import Device as _Device, CallbackChain as _CallbackChain


class ButtonCallback(_CallbackChain):
def on_pressed(self) -> None: ...

def on_released(self) -> None: ...


class Button(_Device):
"""
Listen to a button.
Supports:
- Any binary button
"""

def __init__(self, pin: int, callback: ButtonCallback = ButtonCallback()) -> None:
super().__init__(pin)
self._button: _Button = _Button(pin)
self._callback: ButtonCallback = callback

def _when_activated(self) -> None:
self._callback.on_pressed()

def _when_deactivated(self) -> None:
self._callback.on_released()

@_override
def initialize(self, *parent_tags: str) -> None:
super().initialize(*parent_tags)
self._button.when_activated = self._when_activated
self._button.when_deactivated = self._when_deactivated

@_override
def read(self) -> bool:
return self._button.is_active

@_override
def write(self, callback: ButtonCallback) -> None:
callback.bind_chain(self._callback)
self._callback = callback
31 changes: 29 additions & 2 deletions leads_vec/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
Controller, CENTER_REAR_WHEEL_SPEED_SENSOR, require_config, mark_device, ODOMETER, GPS_RECEIVER, \
ConcurrentOdometer, LEFT_INDICATOR, RIGHT_INDICATOR, VOLTAGE_SENSOR, DataContainer, has_device, \
FRONT_VIEW_CAMERA, LEFT_VIEW_CAMERA, RIGHT_VIEW_CAMERA, REAR_VIEW_CAMERA, VisualDataContainer, BRAKE_INDICATOR, \
SFT, read_device_marker, has_controller, POWER_CONTROLLER, WHEEL_SPEED_CONTROLLER, ACCELEROMETER
SFT, read_device_marker, has_controller, POWER_CONTROLLER, WHEEL_SPEED_CONTROLLER, ACCELEROMETER, require_context
from leads_arduino import ArduinoMicro, WheelSpeedSensor, VoltageSensor, Accelerometer, Acceleration
from leads_comm_serial import SOBD
from leads_gpio import NMEAGPSReceiver, LEDGroup, LED, LEDGroupCommand, LEDCommand, Entire, Transition, CPUMonitor
from leads_gpio import NMEAGPSReceiver, LEDGroup, LED, LEDGroupCommand, LEDCommand, Entire, Transition, Button, \
ButtonCallback, CPUMonitor
from leads_vec.config import Config
from leads_video import Base64Camera, get_camera

Expand Down Expand Up @@ -218,4 +219,30 @@ def write(self, payload: bool) -> None:
) if payload else LEDGroupCommand(LEDCommand.OFF, Entire()))


@device("lib", MAIN_CONTROLLER)
class LeftIndicatorButton(Button, ButtonCallback):
@override
def on_pressed(self) -> None:
ctx = require_context()
ctx.left_indicator(not ctx.left_indicator())

@override
def initialize(self, *parent_tags: str) -> None:
super().initialize(*parent_tags)
self.write(self)


@device("rib", MAIN_CONTROLLER)
class RightIndicatorButton(Button, ButtonCallback):
@override
def on_pressed(self) -> None:
ctx = require_context()
ctx.right_indicator(not ctx.right_indicator())

@override
def initialize(self, *parent_tags: str) -> None:
super().initialize(*parent_tags)
self.write(self)


_: None = None

0 comments on commit 7104ff9

Please sign in to comment.