Skip to content

Commit 0bf23ba

Browse files
authored
Update mlx90640_pygamer.py
Use simpleio.map_range, remove custom function, more comments, cleaning, rename to max_t and min_t, general improvement.
1 parent 874651c commit 0bf23ba

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

examples/mlx90640_pygamer.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import displayio
66
import terminalio
77
from adafruit_display_text.label import Label
8+
from simpleio import map_range
89

9-
number_of_colors = 64
10+
number_of_colors = 64 # Number of color in the gradian
11+
last_color = number_of_colors-1 # Last color in palette
1012
palette = displayio.Palette(number_of_colors) # Palette with all our colors
1113

12-
## Heatmap code inspired from: http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients
14+
## Heatmap code inspired from:
15+
## http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients
1316
color_A = [[0, 0, 0], [0, 0, 255], [0, 255, 255], [0, 255, 0], [255, 255, 0], \
1417
[255, 0, 0], [255, 255, 255]]
1518
color_B = [[0, 0, 255], [0, 255, 255] , [0, 255, 0], [255, 255, 0], [255, 0, 0]]
@@ -21,7 +24,7 @@
2124

2225
def MakeHeatMapColor():
2326
for c in range(number_of_colors):
24-
value = c * (NUM_COLORS-1) / (number_of_colors - 1)
27+
value = c * (NUM_COLORS-1) / last_color
2528
idx1 = int(value) # Our desired color will be after this index.
2629
if idx1 == value : # This is the corner case
2730
red = color[idx1][0]
@@ -58,8 +61,7 @@ def MakeHeatMapColor():
5861
group = displayio.Group()
5962

6063
min_label = Label(terminalio.FONT, max_glyphs=10, color=palette[0], x = 0, y = 110)
61-
max_label = Label(terminalio.FONT, max_glyphs=10, color=palette[number_of_colors-1], \
62-
x = 80, y = 110)
64+
max_label = Label(terminalio.FONT, max_glyphs=10, color=palette[last_color], x = 80, y = 110)
6365

6466
# Add all the sub-group to the SuperGroup
6567
group.append(image_group)
@@ -70,32 +72,20 @@ def MakeHeatMapColor():
7072
# Add the SuperGroup to the Display
7173
board.DISPLAY.show(group)
7274

73-
mini = 0
74-
maxi = 0
75-
76-
my_a1 = 20
77-
my_a2 = 37
78-
79-
def temp2index(s, a1, a2):
80-
b1 = 0
81-
b2 = number_of_colors - 1
82-
83-
if s < a1:
84-
r = b1
85-
elif s > a2:
86-
r = b2
87-
else:
88-
r = int( round( b1 + ( (s - a1) * (b2 - b1) / (a2 - a1) ) ) )
89-
return r
75+
min_t = 20 # Initial minimum temperature range, before auto scale
76+
max_t = 37 # Initial maximum temperature range, before auto scale
9077

9178
i2c = busio.I2C(board.SCL, board.SDA, frequency=800000)
9279

9380
mlx = adafruit_mlx90640.MLX90640(i2c)
9481
print("MLX addr detected on I2C")
9582
print([hex(i) for i in mlx.serial_number])
9683

84+
# Try whatever refresh_rate give the best result for you. 4_HZ seems to be optimal
85+
9786
#mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ
9887
mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_4_HZ
88+
#mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_8_HZ
9989

10090
frame = [0] * 768
10191

@@ -106,10 +96,11 @@ def temp2index(s, a1, a2):
10696
except ValueError:
10797
# these happen, no biggie - retry
10898
continue
109-
print("Read 2 frames in %0.2f s" % (time.monotonic()-stamp))
11099

111-
mini = frame[0] # Define a default min and max value
112-
maxi = frame[0] # Will be updated by temp2index function
100+
# print("Time for data aquisition: %0.2f s" % (time.monotonic()-stamp))
101+
102+
mini = frame[0] # Define a min temperature of current image
103+
maxi = frame[0] # Define a max temperature of current image
113104

114105
for h in range(24):
115106
for w in range(32):
@@ -118,13 +109,15 @@ def temp2index(s, a1, a2):
118109
maxi = t
119110
if t < mini:
120111
mini = t
121-
image_bitmap[w, (23-h)] = temp2index(t, my_a1, my_a2)
112+
image_bitmap[w, (23-h)] = int(map_range(t, min_t, max_t, 0, last_color ))
122113

123-
min_label.text="%0.2f" % (my_a1)
114+
min_label.text="%0.2f" % (min_t)
124115

125-
max_string="%0.2f" % (my_a2)
116+
max_string="%0.2f" % (max_t)
126117
max_label.x=120-(5*len(max_string)) # Tricky calculation to left align
127118
max_label.text=max_string
128119

129-
my_a1 = mini # Automatically change the color scale
130-
my_a2 = maxi
120+
min_t = mini # Automatically change the color scale
121+
max_t = maxi
122+
# print((mini, maxi)) # Use this line to display min and max graph in Mu
123+
# print("Total time for aquisition and display %0.2f s" % (time.monotonic()-stamp))

0 commit comments

Comments
 (0)