22from time import ticks_ms
33from 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+
536class 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