Skip to content

Commit

Permalink
Add led properties to Buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
sebromero committed Nov 6, 2024
1 parent fd2521c commit 76dde91
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
8 changes: 8 additions & 0 deletions examples/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

from modulino import ModulinoButtons
from time import sleep

buttons = ModulinoButtons()

Expand All @@ -24,6 +25,13 @@
buttons.on_button_c_long_press = lambda : print("Button C long press")
buttons.on_button_c_release = lambda : print("Button C released")

buttons.led_a.on()
sleep(0.5)
buttons.led_b.on()
sleep(0.5)
buttons.led_c.on()
sleep(0.5)
buttons.set_led_status(False, False, False) # Turn off all LEDs

while True:
buttons_state_changed = buttons.update()
Expand Down
59 changes: 54 additions & 5 deletions src/modulino/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@
from time import ticks_ms
from micropython import const

class ModulinoButtonsLED():
def __init__(self, buttons):
self._value = 0
self._buttons = buttons

def on(self):
self.value = 1

def off(self):
self.value = 0

@property
def value(self):
return self._value

@value.setter
def value(self, value):
self._value = value
self._buttons._update_leds()

class ModulinoButtons(Modulino):
"""
Class to interact with the buttons of the Modulino Buttons.
Expand Down Expand Up @@ -35,7 +55,37 @@ def __init__(self, i2c_bus = None, address = None):
self._on_button_a_long_press = None
self._on_button_b_long_press = None
self._on_button_c_long_press = None

# LEDs
self._led_a = ModulinoButtonsLED(self)
self._led_b = ModulinoButtonsLED(self)
self._led_c = ModulinoButtonsLED(self)

@property
def led_a(self) -> ModulinoButtonsLED:
""" Returns the LED A object of the module. """
return self._led_a

@property
def led_b(self) -> ModulinoButtonsLED:
""" Returns the LED B object of the module. """
return self._led_b

@property
def led_c(self) -> ModulinoButtonsLED:
""" Returns the LED C object of the module. """
return self._led_c

def _update_leds(self):
"""
Update the physical status of the button LEDs by writing the current values to the module.
"""
data = bytearray(3)
data[0] = self._led_a.value
data[1] = self._led_b.value
data[2] = self._led_c.value
self.write(data)

def set_led_status(self, a: bool, b: bool, c: bool) -> None:
"""
Turn on or off the button LEDs according to the given status.
Expand All @@ -45,11 +95,10 @@ def set_led_status(self, a: bool, b: bool, c: bool) -> None:
b (bool): The status of the LED B.
c (bool): The status of the LED C.
"""
data = bytearray(3)
data[0] = 1 if a else 0
data[1] = 1 if b else 0
data[2] = 1 if c else 0
self.write(data)
self._led_a._value = 1 if a else 0
self._led_b._value = 1 if b else 0
self._led_c._value = 1 if c else 0
self._update_leds()

@property
def long_press_duration(self) -> int:
Expand Down

0 comments on commit 76dde91

Please sign in to comment.