Skip to content

Commit 63ab4ff

Browse files
committed
Add setter for brightness
1 parent de0c4b5 commit 63ab4ff

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/modulino/pixels.py

+36
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,42 @@ def set_rgb(self, idx: int, r: int, g: int, b: int, brightness: int = 100) -> 'M
180180
self.set_color(idx, ModulinoColor(r, g, b), brightness)
181181
return self
182182

183+
def set_brightness(self, idx: int, brightness: int) -> 'ModulinoPixels':
184+
"""
185+
Sets the brightness of the given LED index.
186+
187+
Parameters:
188+
idx (int): The index of the LED (0..7).
189+
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
190+
191+
Returns:
192+
ModulinoPixels: The object itself. Allows for daisy chaining of methods.
193+
"""
194+
if idx < 0 or idx >= NUM_LEDS:
195+
raise ValueError(f"LED index out of range {idx} (Valid: 0..{NUM_LEDS - 1})")
196+
197+
if brightness < 0 or brightness > 100:
198+
raise ValueError(f"Brightness value {brightness} should be between 0 and 100")
199+
200+
byte_index = (idx * 4) # The brightness is stored in the first byte of the 4-byte data (little-endian)
201+
mapped_brightness = self._mapi(brightness, 0, 100, 0, 0x1f) # Map to 0..31
202+
self.data[byte_index] = mapped_brightness | 0xE0 # Fill bits 5..7 with 1
203+
return self
204+
205+
def set_all_brightness(self, brightness: int) -> 'ModulinoPixels':
206+
"""
207+
Sets the brightness of all the LEDs.
208+
209+
Parameters:
210+
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
211+
212+
Returns:
213+
ModulinoPixels: The object itself. Allows for daisy chaining of methods.
214+
"""
215+
for i in range(NUM_LEDS):
216+
self.set_brightness(i, brightness)
217+
return self
218+
183219
def clear(self, idx: int) -> 'ModulinoPixels':
184220
"""
185221
Turns off the LED at the given index.

0 commit comments

Comments
 (0)