Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 448a306

Browse files
committedFeb 10, 2025
Return pixels object for daisy chaining calls
1 parent 7d498cb commit 448a306

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed
 

‎src/modulino/pixels.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def _map(self, x: float | int, in_min: float | int, in_max: float | int, out_min
7474
def _mapi(self, x: float | int, in_min: float | int, in_max: float | int, out_min: float | int, out_max: float | int) -> int:
7575
return int(self._map(x, in_min, in_max, out_min, out_max))
7676

77-
def set_range_rgb(self, index_from: int, index_to: int, r: int, g: int, b: int, brightness: int = 100) -> None:
77+
def set_range_rgb(self, index_from: int, index_to: int, r: int, g: int, b: int, brightness: int = 100) -> 'ModulinoPixels':
7878
"""
7979
Sets the color of the LEDs in the given range to the given RGB values.
8080
@@ -85,10 +85,14 @@ def set_range_rgb(self, index_from: int, index_to: int, r: int, g: int, b: int,
8585
g (int): The green value of the color.
8686
b (int): The blue value of the color.
8787
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
88+
89+
Returns:
90+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
8891
"""
8992
self.set_range_color(index_from, index_to, ModulinoColor(r, g, b), brightness)
93+
return self
9094

91-
def set_range_color(self, index_from: int, index_to: int, color: ModulinoColor, brightness: int = 100) -> None:
95+
def set_range_color(self, index_from: int, index_to: int, color: ModulinoColor, brightness: int = 100) -> 'ModulinoPixels':
9296
"""
9397
Sets the color of the LEDs in the given range to the given color.
9498
@@ -97,11 +101,15 @@ def set_range_color(self, index_from: int, index_to: int, color: ModulinoColor,
97101
index_to (int): The ending index (inclusive) of the range.
98102
color (ModulinoColor): The color of the LEDs.
99103
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
104+
105+
Returns:
106+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
100107
"""
101108
for i in range(index_from, index_to + 1):
102109
self.set_color(i, color, brightness)
110+
return self
103111

104-
def set_all_rgb(self, r: int, g: int, b: int, brightness: int = 100) -> None:
112+
def set_all_rgb(self, r: int, g: int, b: int, brightness: int = 100) -> 'ModulinoPixels':
105113
"""
106114
Sets the color of all the LEDs to the given RGB values.
107115
@@ -110,27 +118,38 @@ def set_all_rgb(self, r: int, g: int, b: int, brightness: int = 100) -> None:
110118
g (int): The green value of the color.
111119
b (int): The blue value of the color.
112120
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
121+
122+
Returns:
123+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
113124
"""
114125
self.set_all_color(ModulinoColor(r, g, b), brightness)
126+
return self
115127

116-
def set_all_color(self, color: ModulinoColor, brightness: int = 100) -> None:
128+
def set_all_color(self, color: ModulinoColor, brightness: int = 100) -> 'ModulinoPixels':
117129
"""
118130
Sets the color of all the LEDs to the given color.
119131
120132
Parameters:
121133
color (ModulinoColor): The color of the LEDs.
122134
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
135+
136+
Returns:
137+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
123138
"""
124139
self.set_range_color(0, NUM_LEDS - 1, color, brightness)
140+
return self
125141

126-
def set_color(self, idx: int, rgb: ModulinoColor, brightness: int = 100) -> None:
142+
def set_color(self, idx: int, rgb: ModulinoColor, brightness: int = 100) -> 'ModulinoPixels':
127143
"""
128144
Sets the color of the given LED index to the given color.
129145
130146
Parameters:
131147
idx (int): The index of the LED (0..7).
132148
rgb (ModulinoColor): The color of the LED.
133149
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
150+
151+
Returns:
152+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
134153
"""
135154
if idx < 0 or idx >= NUM_LEDS:
136155
raise ValueError(f"LED index out of range {idx} (Valid: 0..{NUM_LEDS - 1})")
@@ -139,8 +158,9 @@ def set_color(self, idx: int, rgb: ModulinoColor, brightness: int = 100) -> None
139158
mapped_brightness = self._mapi(brightness, 0, 100, 0, 0x1f)
140159
color_data_bytes = int(rgb) | mapped_brightness | 0xE0
141160
self.data[byte_index: byte_index+4] = color_data_bytes.to_bytes(4, 'little')
161+
return self
142162

143-
def set_rgb(self, idx: int, r: int, g: int, b: int, brightness: int = 100) -> None:
163+
def set_rgb(self, idx: int, r: int, g: int, b: int, brightness: int = 100) -> 'ModulinoPixels':
144164
"""
145165
Set the color of the given LED index to the given RGB values.
146166
@@ -150,34 +170,50 @@ def set_rgb(self, idx: int, r: int, g: int, b: int, brightness: int = 100) -> No
150170
g (int): The green value of the color.
151171
b (int): The blue value of the color.
152172
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
173+
174+
Returns:
175+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
153176
"""
154177
self.set_color(idx, ModulinoColor(r, g, b), brightness)
178+
return self
155179

156-
def clear(self, idx: int) -> None:
180+
def clear(self, idx: int) -> 'ModulinoPixels':
157181
"""
158182
Turns off the LED at the given index.
159183
160184
Parameters:
161185
idx (int): The index of the LED (0..7).
186+
187+
Returns:
188+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
162189
"""
163190
self.set_color(idx, ModulinoColor(0, 0, 0), 0)
191+
return self
164192

165-
def clear_range(self, start: int, end: int) -> None:
193+
def clear_range(self, start: int, end: int) -> 'ModulinoPixels':
166194
"""
167195
Turns off the LEDs in the given range.
168196
169197
Parameters:
170198
start (int): The starting index of the range.
171199
end (int): The ending index (inclusive) of the range.
200+
201+
Returns:
202+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
172203
"""
173204
for i in range(start, end):
174205
self.clear(i)
206+
return self
175207

176-
def clear_all(self) -> None:
208+
def clear_all(self) -> 'ModulinoPixels':
177209
"""
178210
Turns all the LEDs off.
211+
212+
Returns:
213+
ModulinoPixels: The object itself. Allows for daily chaining of methods.
179214
"""
180215
self.data = bytearray([0xE0] * NUM_LEDS * 4)
216+
return self
181217

182218
def show(self) -> None:
183219
"""

0 commit comments

Comments
 (0)
Please sign in to comment.