Skip to content

Commit fbaeef9

Browse files
committed
Add mapping functions to helpers
1 parent 52bbe40 commit fbaeef9

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"urls": [
33
["modulino/__init__.py", "github:arduino/modulino-mpy/src/modulino/__init__.py"],
4+
["modulino/helpers.py", "github:arduino/modulino-mpy/src/modulino/helpers.py"],
45
["modulino/buttons.py", "github:arduino/modulino-mpy/src/modulino/buttons.py"],
56
["modulino/buzzer.py", "github:arduino/modulino-mpy/src/modulino/buzzer.py"],
67
["modulino/distance.py", "github:arduino/modulino-mpy/src/modulino/distance.py"],

src/modulino/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
__maintainer__ = "Arduino"
55

66
# Import core classes and/or functions to expose them at the package level
7+
from .helpers import map_value, map_value_int
78
from .modulino import Modulino
89
from .pixels import ModulinoPixels, ModulinoColor
910
from .thermo import ModulinoThermo

src/modulino/helpers.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def map_value(x: float | int, in_min: float | int, in_max: float | int, out_min: float | int, out_max: float | int) -> float | int:
2+
"""
3+
Maps a value from one range to another.
4+
5+
Args:
6+
x: The value to map.
7+
in_min: The minimum value of the input range.
8+
in_max: The maximum value of the input range.
9+
out_min: The minimum value of the output range.
10+
out_max: The maximum value of the output range.
11+
12+
Returns:
13+
The mapped value as a float or int depending on the input.
14+
"""
15+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
16+
17+
def map_value_int(x: float | int, in_min: float | int, in_max: float | int, out_min: float | int, out_max: float | int) -> int:
18+
"""
19+
Maps a value from one range to another and returns an integer.
20+
21+
Args:
22+
x: The value to map.
23+
in_min: The minimum value of the input range.
24+
in_max: The maximum value of the input range.
25+
out_min: The minimum value of the output range.
26+
out_max: The maximum value of the output range.
27+
28+
Returns:
29+
The mapped value as an integer.
30+
"""
31+
return int(map_value(x, in_min, in_max, out_min, out_max))

src/modulino/pixels.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from .modulino import Modulino
2+
from .helpers import map_value_int
3+
24
from micropython import const
35

46
class ModulinoColor:
@@ -70,12 +72,6 @@ def __init__(self, i2c_bus = None, address=None):
7072
"""
7173
super().__init__(i2c_bus, address, "Pixels")
7274
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))
7975

8076
def set_range_rgb(self, index_from: int, index_to: int, r: int, g: int, b: int, brightness: int = 100) -> 'ModulinoPixels':
8177
"""
@@ -158,7 +154,7 @@ def set_color(self, idx: int, rgb: ModulinoColor, brightness: int = 100) -> 'Mod
158154
raise ValueError(f"LED index out of range {idx} (Valid: 0..{NUM_LEDS - 1})")
159155

160156
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)
162158
color_data_bytes = int(rgb) | mapped_brightness | 0xE0
163159
self.data[byte_index: byte_index+4] = color_data_bytes.to_bytes(4, 'little')
164160
return self
@@ -198,7 +194,7 @@ def set_brightness(self, idx: int, brightness: int) -> 'ModulinoPixels':
198194
raise ValueError(f"Brightness value {brightness} should be between 0 and 100")
199195

200196
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
202198
self.data[byte_index] = mapped_brightness | 0xE0 # Fill bits 5..7 with 1
203199
return self
204200

0 commit comments

Comments
 (0)