Skip to content

Commit e2627ff

Browse files
committed
Add led properties to Buttons
1 parent ba255ac commit e2627ff

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-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

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

5+
class ModulinoButtonsLED():
6+
"""
7+
Class to interact with the LEDs of the Modulino Buttons.
8+
"""
9+
10+
def __init__(self, buttons):
11+
self._value = 0
12+
self._buttons = buttons
13+
14+
def on(self):
15+
""" Turns the LED on. """
16+
self.value = 1
17+
18+
def off(self):
19+
""" Turns the LED off. """
20+
self.value = 0
21+
22+
@property
23+
def value(self):
24+
""" Returns the value of the LED (1 for on, 0 for off). """
25+
return self._value
26+
27+
@value.setter
28+
def value(self, value):
29+
"""
30+
Sets the value of the LED (1 for on, 0 for off).
31+
Calling this method will update the physical status of the LED immediately.
32+
"""
33+
self._value = value
34+
self._buttons._update_leds()
35+
536
class ModulinoButtons(Modulino):
637
"""
738
Class to interact with the buttons of the Modulino Buttons.
@@ -35,7 +66,37 @@ def __init__(self, i2c_bus = None, address = None):
3566
self._on_button_a_long_press = None
3667
self._on_button_b_long_press = None
3768
self._on_button_c_long_press = None
69+
70+
# LEDs
71+
self._led_a = ModulinoButtonsLED(self)
72+
self._led_b = ModulinoButtonsLED(self)
73+
self._led_c = ModulinoButtonsLED(self)
3874

75+
@property
76+
def led_a(self) -> ModulinoButtonsLED:
77+
""" Returns the LED A object of the module. """
78+
return self._led_a
79+
80+
@property
81+
def led_b(self) -> ModulinoButtonsLED:
82+
""" Returns the LED B object of the module. """
83+
return self._led_b
84+
85+
@property
86+
def led_c(self) -> ModulinoButtonsLED:
87+
""" Returns the LED C object of the module. """
88+
return self._led_c
89+
90+
def _update_leds(self):
91+
"""
92+
Update the physical status of the button LEDs by writing the current values to the module.
93+
"""
94+
data = bytearray(3)
95+
data[0] = self._led_a.value
96+
data[1] = self._led_b.value
97+
data[2] = self._led_c.value
98+
self.write(data)
99+
39100
def set_led_status(self, a: bool, b: bool, c: bool) -> None:
40101
"""
41102
Turn on or off the button LEDs according to the given status.
@@ -45,11 +106,10 @@ def set_led_status(self, a: bool, b: bool, c: bool) -> None:
45106
b (bool): The status of the LED B.
46107
c (bool): The status of the LED C.
47108
"""
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)
109+
self._led_a._value = 1 if a else 0
110+
self._led_b._value = 1 if b else 0
111+
self._led_c._value = 1 if c else 0
112+
self._update_leds()
53113

54114
@property
55115
def long_press_duration(self) -> int:

0 commit comments

Comments
 (0)