-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpixels_thermo.py
55 lines (42 loc) · 1.72 KB
/
pixels_thermo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
This example shows how to use the ModulinoPixels and ModulinoThermo classes to display the temperature on a pixel strip.
A high temperature is represented by red color and a lower temperature with a yellow color.
The pixels will map to the temperature range so that the first pixel represents
the lowest temperature and the last pixel the highest temperature of the predefined range.
You can change the temperature range to accommodate the temperature range of your environment.
Initial author: Sebastian Romero ([email protected])
"""
from modulino import ModulinoPixels, ModulinoThermo
from time import sleep
pixels = ModulinoPixels()
thermo_module = ModulinoThermo()
# Yellow to red scale with 8 steps
colors = [
(255, 255, 0),
(255, 204, 0),
(255, 153, 0),
(255, 102, 0),
(255, 51, 0),
(255, 0, 0),
(204, 0, 0),
(153, 0, 0)
]
# Define the range of temperatures (°C) to map to the pixel strip
temperature_range = (20, 30)
while True:
temperature = thermo_module.temperature
print(f"🌡️ Temperature: {temperature:.1f} °C")
# Constrain temperature to the given range
if temperature < temperature_range[0]:
temperature = temperature_range[0]
elif temperature > temperature_range[1]:
temperature = temperature_range[1]
# Map temperature to the pixel strip
# temperature_range[0]°C : 0 index -> first pixel
# temperature_range[1]°C : 7 index -> last pixel
temperature_index = int((temperature - temperature_range[0]) * 7 / (temperature_range[1] - temperature_range[0]))
pixels.clear_all()
for index in range(0, temperature_index + 1):
pixels.set_rgb(index, *colors[index], 100)
pixels.show()
sleep(1)