5
5
import displayio
6
6
import terminalio
7
7
from adafruit_display_text .label import Label
8
+ from simpleio import map_range
8
9
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
10
12
palette = displayio .Palette (number_of_colors ) # Palette with all our colors
11
13
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
13
16
color_A = [[0 , 0 , 0 ], [0 , 0 , 255 ], [0 , 255 , 255 ], [0 , 255 , 0 ], [255 , 255 , 0 ], \
14
17
[255 , 0 , 0 ], [255 , 255 , 255 ]]
15
18
color_B = [[0 , 0 , 255 ], [0 , 255 , 255 ] , [0 , 255 , 0 ], [255 , 255 , 0 ], [255 , 0 , 0 ]]
21
24
22
25
def MakeHeatMapColor ():
23
26
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
25
28
idx1 = int (value ) # Our desired color will be after this index.
26
29
if idx1 == value : # This is the corner case
27
30
red = color [idx1 ][0 ]
@@ -58,8 +61,7 @@ def MakeHeatMapColor():
58
61
group = displayio .Group ()
59
62
60
63
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 )
63
65
64
66
# Add all the sub-group to the SuperGroup
65
67
group .append (image_group )
@@ -70,32 +72,20 @@ def MakeHeatMapColor():
70
72
# Add the SuperGroup to the Display
71
73
board .DISPLAY .show (group )
72
74
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
90
77
91
78
i2c = busio .I2C (board .SCL , board .SDA , frequency = 800000 )
92
79
93
80
mlx = adafruit_mlx90640 .MLX90640 (i2c )
94
81
print ("MLX addr detected on I2C" )
95
82
print ([hex (i ) for i in mlx .serial_number ])
96
83
84
+ # Try whatever refresh_rate give the best result for you. 4_HZ seems to be optimal
85
+
97
86
#mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ
98
87
mlx .refresh_rate = adafruit_mlx90640 .RefreshRate .REFRESH_4_HZ
88
+ #mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_8_HZ
99
89
100
90
frame = [0 ] * 768
101
91
@@ -106,10 +96,11 @@ def temp2index(s, a1, a2):
106
96
except ValueError :
107
97
# these happen, no biggie - retry
108
98
continue
109
- print ("Read 2 frames in %0.2f s" % (time .monotonic ()- stamp ))
110
99
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
113
104
114
105
for h in range (24 ):
115
106
for w in range (32 ):
@@ -118,13 +109,15 @@ def temp2index(s, a1, a2):
118
109
maxi = t
119
110
if t < mini :
120
111
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 ) )
122
113
123
- min_label .text = "%0.2f" % (my_a1 )
114
+ min_label .text = "%0.2f" % (min_t )
124
115
125
- max_string = "%0.2f" % (my_a2 )
116
+ max_string = "%0.2f" % (max_t )
126
117
max_label .x = 120 - (5 * len (max_string )) # Tricky calculation to left align
127
118
max_label .text = max_string
128
119
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