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