From 0bf23ba3517341f2c4fb9d667986ea4f2476daed Mon Sep 17 00:00:00 2001 From: David Glaude Date: Wed, 8 Jan 2020 23:12:59 +0100 Subject: [PATCH 1/2] Update mlx90640_pygamer.py Use simpleio.map_range, remove custom function, more comments, cleaning, rename to max_t and min_t, general improvement. --- examples/mlx90640_pygamer.py | 53 ++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/examples/mlx90640_pygamer.py b/examples/mlx90640_pygamer.py index 8ad2b9e..1a294d5 100644 --- a/examples/mlx90640_pygamer.py +++ b/examples/mlx90640_pygamer.py @@ -5,11 +5,14 @@ import displayio import terminalio from adafruit_display_text.label import Label +from simpleio import map_range -number_of_colors = 64 +number_of_colors = 64 # Number of color in the gradian +last_color = number_of_colors-1 # Last color in palette palette = displayio.Palette(number_of_colors) # Palette with all our colors -## Heatmap code inspired from: http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients +## Heatmap code inspired from: +## http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients color_A = [[0, 0, 0], [0, 0, 255], [0, 255, 255], [0, 255, 0], [255, 255, 0], \ [255, 0, 0], [255, 255, 255]] color_B = [[0, 0, 255], [0, 255, 255] , [0, 255, 0], [255, 255, 0], [255, 0, 0]] @@ -21,7 +24,7 @@ def MakeHeatMapColor(): for c in range(number_of_colors): - value = c * (NUM_COLORS-1) / (number_of_colors - 1) + value = c * (NUM_COLORS-1) / last_color idx1 = int(value) # Our desired color will be after this index. if idx1 == value : # This is the corner case red = color[idx1][0] @@ -58,8 +61,7 @@ def MakeHeatMapColor(): group = displayio.Group() min_label = Label(terminalio.FONT, max_glyphs=10, color=palette[0], x = 0, y = 110) -max_label = Label(terminalio.FONT, max_glyphs=10, color=palette[number_of_colors-1], \ - x = 80, y = 110) +max_label = Label(terminalio.FONT, max_glyphs=10, color=palette[last_color], x = 80, y = 110) # Add all the sub-group to the SuperGroup group.append(image_group) @@ -70,23 +72,8 @@ def MakeHeatMapColor(): # Add the SuperGroup to the Display board.DISPLAY.show(group) -mini = 0 -maxi = 0 - -my_a1 = 20 -my_a2 = 37 - -def temp2index(s, a1, a2): - b1 = 0 - b2 = number_of_colors - 1 - - if s < a1: - r = b1 - elif s > a2: - r = b2 - else: - r = int( round( b1 + ( (s - a1) * (b2 - b1) / (a2 - a1) ) ) ) - return r +min_t = 20 # Initial minimum temperature range, before auto scale +max_t = 37 # Initial maximum temperature range, before auto scale i2c = busio.I2C(board.SCL, board.SDA, frequency=800000) @@ -94,8 +81,11 @@ def temp2index(s, a1, a2): print("MLX addr detected on I2C") print([hex(i) for i in mlx.serial_number]) +# Try whatever refresh_rate give the best result for you. 4_HZ seems to be optimal + #mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_4_HZ +#mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_8_HZ frame = [0] * 768 @@ -106,10 +96,11 @@ def temp2index(s, a1, a2): except ValueError: # these happen, no biggie - retry continue - print("Read 2 frames in %0.2f s" % (time.monotonic()-stamp)) - mini = frame[0] # Define a default min and max value - maxi = frame[0] # Will be updated by temp2index function +# print("Time for data aquisition: %0.2f s" % (time.monotonic()-stamp)) + + mini = frame[0] # Define a min temperature of current image + maxi = frame[0] # Define a max temperature of current image for h in range(24): for w in range(32): @@ -118,13 +109,15 @@ def temp2index(s, a1, a2): maxi = t if t < mini: mini = t - image_bitmap[w, (23-h)] = temp2index(t, my_a1, my_a2) + image_bitmap[w, (23-h)] = int(map_range(t, min_t, max_t, 0, last_color )) - min_label.text="%0.2f" % (my_a1) + min_label.text="%0.2f" % (min_t) - max_string="%0.2f" % (my_a2) + max_string="%0.2f" % (max_t) max_label.x=120-(5*len(max_string)) # Tricky calculation to left align max_label.text=max_string - my_a1 = mini # Automatically change the color scale - my_a2 = maxi + min_t = mini # Automatically change the color scale + max_t = maxi +# print((mini, maxi)) # Use this line to display min and max graph in Mu +# print("Total time for aquisition and display %0.2f s" % (time.monotonic()-stamp)) From fb926e84a8f4851853503bb61374350c3cda48ba Mon Sep 17 00:00:00 2001 From: David Glaude Date: Wed, 8 Jan 2020 23:19:21 +0100 Subject: [PATCH 2/2] Update mlx90640_pygamer.py Making pylint happy... --- examples/mlx90640_pygamer.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/examples/mlx90640_pygamer.py b/examples/mlx90640_pygamer.py index 1a294d5..f8e0989 100644 --- a/examples/mlx90640_pygamer.py +++ b/examples/mlx90640_pygamer.py @@ -11,8 +11,7 @@ last_color = number_of_colors-1 # Last color in palette palette = displayio.Palette(number_of_colors) # Palette with all our colors -## Heatmap code inspired from: -## http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients +## Heatmap code inspired from: http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients color_A = [[0, 0, 0], [0, 0, 255], [0, 255, 255], [0, 255, 0], [255, 255, 0], \ [255, 0, 0], [255, 255, 255]] color_B = [[0, 0, 255], [0, 255, 255] , [0, 255, 0], [255, 255, 0], [255, 0, 0]] @@ -81,11 +80,8 @@ def MakeHeatMapColor(): print("MLX addr detected on I2C") print([hex(i) for i in mlx.serial_number]) -# Try whatever refresh_rate give the best result for you. 4_HZ seems to be optimal - #mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_4_HZ -#mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_8_HZ frame = [0] * 768