|
1 | 1 | """This example is for Raspberry Pi (Linux) only!
|
2 | 2 | It will not work on microcontrollers running CircuitPython!"""
|
3 | 3 |
|
4 |
| -import os |
5 | 4 | import sys
|
6 | 5 | import math
|
7 | 6 | import time
|
8 | 7 | import atexit
|
9 | 8 | import busio
|
10 | 9 | import board
|
11 |
| - |
12 | 10 | import numpy as np
|
13 | 11 | from scipy.interpolate import griddata
|
14 |
| - |
15 | 12 | from colour import Color
|
16 |
| -from os import system, name |
17 | 13 | import adafruit_amg88xx
|
18 | 14 |
|
19 |
| -i2c_bus = busio.I2C(board.SCL, board.SDA) |
| 15 | +I2C_BUS = busio.I2C(board.SCL, board.SDA) |
20 | 16 |
|
21 | 17 | # low range of the sensor (this will be blue on the screen)
|
22 | 18 | MINTEMP = 26.0
|
23 |
| - |
24 | 19 | # high range of the sensor (this will be red on the screen)
|
25 | 20 | MAXTEMP = 32.0
|
26 |
| - |
27 |
| -# how many color values we can have |
28 | 21 | COLORDEPTH = 1024
|
29 |
| - |
30 |
| -#os.putenv("SDL_FBDEV", "/dev/fb1") |
31 |
| -#pygame.init() |
32 |
| - |
33 |
| -# initialize the sensor |
34 |
| -sensor = adafruit_amg88xx.AMG88XX(i2c_bus) |
| 22 | +SENSOR = adafruit_amg88xx.AMG88XX(I2C_BUS) |
35 | 23 |
|
36 | 24 | # pylint: disable=invalid-slice-index
|
37 |
| -points = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)] |
38 |
| -grid_x, grid_y = np.mgrid[0:7:32j, 0:7:32j] |
| 25 | +POINTS = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)] |
| 26 | +GRID_X, GRID_Y = np.mgrid[0:7:32j, 0:7:32j] |
39 | 27 | # pylint: enable=invalid-slice-index
|
40 | 28 |
|
41 | 29 | # sensor is an 8x8 grid so lets do a square
|
42 |
| -height = 240 |
43 |
| -width = 240 |
| 30 | +HEIGHT = 240 |
| 31 | +WIDTH = 240 |
44 | 32 |
|
45 | 33 | # the list of colors we can choose from
|
46 |
| -blue = Color("indigo") |
47 |
| -colors = list(blue.range_to(Color("red"), COLORDEPTH)) |
| 34 | +BLUE = Color("indigo") |
| 35 | +COLORS = list(BLUE.range_to(Color("red"), COLORDEPTH)) |
48 | 36 |
|
49 | 37 | # create the array of colors
|
50 |
| -colors = [(int(c.red * 255), int(c.green * 255), int(c.blue * 255)) for c in colors] |
51 |
| -console_colors = [17,18,19,20,21,57,93,129,165,201,200,199,198,197,196,202,208,214,220] |
| 38 | +COLORS = [(int(c.red * 255), int(c.green * 255), int(c.blue * 255)) for c in COLORS] |
| 39 | +CONSOLE_COLORS = [ |
| 40 | + 17, 18, 19, 20, 21, 57, 93, 129, 165, 201, 200, |
| 41 | + 199, 198, 197, 196, 202, 208, 214, 220] |
52 | 42 |
|
53 |
| -displayPixelWidth = width / 30 |
54 |
| -displayPixelHeight = height / 30 |
| 43 | +def map_value(x_value, in_min, in_max, out_min, out_max): |
| 44 | + """Maps value of the temperature to color""" |
| 45 | + return (x_value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min |
55 | 46 |
|
56 |
| -# some utility functions |
57 |
| -def constrain(val, min_val, max_val): |
58 |
| - return min(max_val, max(min_val, val)) |
| 47 | +def print_there(console_x, console_y, text, color): |
| 48 | + """ Outputs a colored text to console at coordinates """ |
| 49 | + sys.stdout.write("\x1b7\x1b[48;5;%dm" % (color)) |
| 50 | + sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (console_x, console_y, text)) |
59 | 51 |
|
60 |
| - |
61 |
| -def map_value(x, in_min, in_max, out_min, out_max): |
62 |
| - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min |
63 |
| - |
64 |
| -def clear(): |
65 |
| - _ = system('clear') |
66 |
| - |
67 |
| -def print_there(x, y, text, color): |
68 |
| - sys.stdout.write("\x1b7\x1b[48;5;%dm" % (color)) |
69 |
| - sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x, y, text)) |
70 |
| -maxpixel = 0 |
71 |
| -minpixel = 0 |
72 |
| -color_range = 1 |
73 | 52 | def exit_handler():
|
74 |
| - print("maxpixel:" + str(maxpixel)) |
75 |
| - print("minpixel:" + str(minpixel)) |
| 53 | + """ print values on exit for debugging """ |
| 54 | + print("maxpixel:" + str(MAXPIXEL)) |
| 55 | + print("minpixel:" + str(MINPIXEL)) |
76 | 56 |
|
77 | 57 | atexit.register(exit_handler)
|
| 58 | + |
78 | 59 | # let the sensor initialize
|
79 | 60 | time.sleep(0.1)
|
80 | 61 |
|
| 62 | +COLOR_RANGE = 1 |
81 | 63 | while True:
|
82 | 64 |
|
83 | 65 | # read the pixels
|
84 |
| - pixels = [] |
85 |
| - for row in sensor.pixels: |
86 |
| - pixels = pixels + row |
87 |
| - pixels = [map_value(p, MINTEMP, MAXTEMP, 0, COLORDEPTH - 1) for p in pixels] |
| 66 | + PIXELS = [] |
| 67 | + for row in SENSOR.pixels: |
| 68 | + PIXELS = PIXELS + row |
| 69 | + PIXELS = [map_value(p, MINTEMP, MAXTEMP, 0, COLORDEPTH - 1) for p in PIXELS] |
88 | 70 |
|
89 | 71 | # perform interpolation
|
90 |
| - bicubic = griddata(points, pixels, (grid_x, grid_y), method="cubic") |
| 72 | + BICUBIC = griddata(POINTS, PIXELS, (GRID_X, GRID_Y), method="cubic") |
| 73 | + |
| 74 | + MAXPIXEL = 0 |
| 75 | + MINPIXEL = 0 |
91 | 76 |
|
92 | 77 | # draw everything
|
93 |
| - y=2 |
94 |
| - for ix, row in enumerate(bicubic): |
95 |
| - x=2 |
| 78 | + Y_CONSOLE = 2 |
| 79 | + for ix, row in enumerate(BICUBIC): |
| 80 | + x_console = 2 |
96 | 81 | for jx, pixel in enumerate(row):
|
97 | 82 | color_index = 0
|
98 |
| - if (color_range != 0): |
99 |
| - color_index = int(round((pixel-minpixel)/color_range)) |
| 83 | + if COLOR_RANGE != 0: |
| 84 | + color_index = int(round((pixel-MINPIXEL)/COLOR_RANGE)) |
100 | 85 | if color_index < 0:
|
101 | 86 | color_index = 0
|
102 |
| - if color_index > len(console_colors)-1: |
103 |
| - color_index = len(console_colors)-1 |
104 |
| - print_there(x, y*2-2, ' ', console_colors[color_index]) |
105 |
| - if pixel > maxpixel: |
106 |
| - maxpixel = pixel |
107 |
| - if pixel < minpixel: |
108 |
| - minpixel = pixel |
109 |
| - x+=1 |
110 |
| - y+=1 |
111 |
| - heat_range = maxpixel - minpixel |
112 |
| - color_range = heat_range / len(console_colors) |
| 87 | + if color_index > len(CONSOLE_COLORS)-1: |
| 88 | + color_index = len(CONSOLE_COLORS)-1 |
| 89 | + print_there(x_console, Y_CONSOLE*2-2, ' ', CONSOLE_COLORS[color_index]) |
| 90 | + if pixel > MAXPIXEL: |
| 91 | + MAXPIXEL = pixel |
| 92 | + if pixel < MINPIXEL: |
| 93 | + MINPIXEL = pixel |
| 94 | + x_console += 1 |
| 95 | + Y_CONSOLE += 1 |
| 96 | + sys.stdout.flush() |
| 97 | + HEAT_RANGE = MAXPIXEL - MINPIXEL |
| 98 | + COLOR_RANGE = HEAT_RANGE / len(CONSOLE_COLORS) |
0 commit comments