Skip to content

Commit 76dde91

Browse files
committed
Add led properties to Buttons
1 parent fd2521c commit 76dde91

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

examples/buttons.py

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
from modulino import ModulinoButtons
12+
from time import sleep
1213

1314
buttons = ModulinoButtons()
1415

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

28+
buttons.led_a.on()
29+
sleep(0.5)
30+
buttons.led_b.on()
31+
sleep(0.5)
32+
buttons.led_c.on()
33+
sleep(0.5)
34+
buttons.set_led_status(False, False, False) # Turn off all LEDs
2735

2836
while True:
2937
buttons_state_changed = buttons.update()

src/modulino/buttons.py

+54-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
from time import ticks_ms
33
from micropython import const
44

5+
class ModulinoButtonsLED():
6+
def __init__(self, buttons):
7+
self._value = 0
8+
self._buttons = buttons
9+
10+
def on(self):
11+
self.value = 1
12+
13+
def off(self):
14+
self.value = 0
15+
16+
@property
17+
def value(self):
18+
return self._value
19+
20+
@value.setter
21+
def value(self, value):
22+
self._value = value
23+
self._buttons._update_leds()
24+
525
class ModulinoButtons(Modulino):
626
"""
727
Class to interact with the buttons of the Modulino Buttons.
@@ -35,7 +55,37 @@ def __init__(self, i2c_bus = None, address = None):
3555
self._on_button_a_long_press = None
3656
self._on_button_b_long_press = None
3757
self._on_button_c_long_press = None
58+
59+
# LEDs
60+
self._led_a = ModulinoButtonsLED(self)
61+
self._led_b = ModulinoButtonsLED(self)
62+
self._led_c = ModulinoButtonsLED(self)
63+
64+
@property
65+
def led_a(self) -> ModulinoButtonsLED:
66+
""" Returns the LED A object of the module. """
67+
return self._led_a
68+
69+
@property
70+
def led_b(self) -> ModulinoButtonsLED:
71+
""" Returns the LED B object of the module. """
72+
return self._led_b
3873

74+
@property
75+
def led_c(self) -> ModulinoButtonsLED:
76+
""" Returns the LED C object of the module. """
77+
return self._led_c
78+
79+
def _update_leds(self):
80+
"""
81+
Update the physical status of the button LEDs by writing the current values to the module.
82+
"""
83+
data = bytearray(3)
84+
data[0] = self._led_a.value
85+
data[1] = self._led_b.value
86+
data[2] = self._led_c.value
87+
self.write(data)
88+
3989
def set_led_status(self, a: bool, b: bool, c: bool) -> None:
4090
"""
4191
Turn on or off the button LEDs according to the given status.
@@ -45,11 +95,10 @@ def set_led_status(self, a: bool, b: bool, c: bool) -> None:
4595
b (bool): The status of the LED B.
4696
c (bool): The status of the LED C.
4797
"""
48-
data = bytearray(3)
49-
data[0] = 1 if a else 0
50-
data[1] = 1 if b else 0
51-
data[2] = 1 if c else 0
52-
self.write(data)
98+
self._led_a._value = 1 if a else 0
99+
self._led_b._value = 1 if b else 0
100+
self._led_c._value = 1 if c else 0
101+
self._update_leds()
53102

54103
@property
55104
def long_press_duration(self) -> int:

0 commit comments

Comments
 (0)