|
1 | 1 | from .modulino import Modulino
|
| 2 | +from .helpers import map_value_int |
| 3 | + |
2 | 4 | from micropython import const
|
3 | 5 |
|
4 | 6 | class ModulinoColor:
|
@@ -70,12 +72,6 @@ def __init__(self, i2c_bus = None, address=None):
|
70 | 72 | """
|
71 | 73 | super().__init__(i2c_bus, address, "Pixels")
|
72 | 74 | self.clear_all()
|
73 |
| - |
74 |
| - def _map(self, x: float | int, in_min: float | int, in_max: float | int, out_min: float | int, out_max: float | int) -> float | int: |
75 |
| - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min |
76 |
| - |
77 |
| - def _mapi(self, x: float | int, in_min: float | int, in_max: float | int, out_min: float | int, out_max: float | int) -> int: |
78 |
| - return int(self._map(x, in_min, in_max, out_min, out_max)) |
79 | 75 |
|
80 | 76 | def set_range_rgb(self, index_from: int, index_to: int, r: int, g: int, b: int, brightness: int = 100) -> 'ModulinoPixels':
|
81 | 77 | """
|
@@ -158,7 +154,7 @@ def set_color(self, idx: int, rgb: ModulinoColor, brightness: int = 100) -> 'Mod
|
158 | 154 | raise ValueError(f"LED index out of range {idx} (Valid: 0..{NUM_LEDS - 1})")
|
159 | 155 |
|
160 | 156 | byte_index = idx * 4
|
161 |
| - mapped_brightness = self._mapi(brightness, 0, 100, 0, 0x1f) |
| 157 | + mapped_brightness = map_value_int(brightness, 0, 100, 0, 0x1f) |
162 | 158 | color_data_bytes = int(rgb) | mapped_brightness | 0xE0
|
163 | 159 | self.data[byte_index: byte_index+4] = color_data_bytes.to_bytes(4, 'little')
|
164 | 160 | return self
|
@@ -198,7 +194,7 @@ def set_brightness(self, idx: int, brightness: int) -> 'ModulinoPixels':
|
198 | 194 | raise ValueError(f"Brightness value {brightness} should be between 0 and 100")
|
199 | 195 |
|
200 | 196 | 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 |
| 197 | + mapped_brightness = map_value_int(brightness, 0, 100, 0, 0x1f) # Map to 0..31 |
202 | 198 | self.data[byte_index] = mapped_brightness | 0xE0 # Fill bits 5..7 with 1
|
203 | 199 | return self
|
204 | 200 |
|
|
0 commit comments