|
| 1 | +"""This example is for Raspberry Pi (Linux) only! |
| 2 | + It will not work on microcontrollers running CircuitPython!""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import math |
| 7 | +import time |
| 8 | +import atexit |
| 9 | +import busio |
| 10 | +import board |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +from scipy.interpolate import griddata |
| 14 | + |
| 15 | +from colour import Color |
| 16 | +from os import system, name |
| 17 | +import adafruit_amg88xx |
| 18 | + |
| 19 | +i2c_bus = busio.I2C(board.SCL, board.SDA) |
| 20 | + |
| 21 | +# low range of the sensor (this will be blue on the screen) |
| 22 | +MINTEMP = 26.0 |
| 23 | + |
| 24 | +# high range of the sensor (this will be red on the screen) |
| 25 | +MAXTEMP = 32.0 |
| 26 | + |
| 27 | +# how many color values we can have |
| 28 | +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) |
| 35 | + |
| 36 | +# 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] |
| 39 | +# pylint: enable=invalid-slice-index |
| 40 | + |
| 41 | +# sensor is an 8x8 grid so lets do a square |
| 42 | +height = 240 |
| 43 | +width = 240 |
| 44 | + |
| 45 | +# the list of colors we can choose from |
| 46 | +blue = Color("indigo") |
| 47 | +colors = list(blue.range_to(Color("red"), COLORDEPTH)) |
| 48 | + |
| 49 | +# 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] |
| 52 | + |
| 53 | +displayPixelWidth = width / 30 |
| 54 | +displayPixelHeight = height / 30 |
| 55 | + |
| 56 | +# some utility functions |
| 57 | +def constrain(val, min_val, max_val): |
| 58 | + return min(max_val, max(min_val, val)) |
| 59 | + |
| 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 | +def exit_handler(): |
| 74 | + print("maxpixel:" + str(maxpixel)) |
| 75 | + print("minpixel:" + str(minpixel)) |
| 76 | + |
| 77 | +atexit.register(exit_handler) |
| 78 | +# let the sensor initialize |
| 79 | +time.sleep(0.1) |
| 80 | + |
| 81 | +while True: |
| 82 | + |
| 83 | + # 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] |
| 88 | + |
| 89 | + # perform interpolation |
| 90 | + bicubic = griddata(points, pixels, (grid_x, grid_y), method="cubic") |
| 91 | + |
| 92 | + # draw everything |
| 93 | + y=2 |
| 94 | + for ix, row in enumerate(bicubic): |
| 95 | + x=2 |
| 96 | + for jx, pixel in enumerate(row): |
| 97 | + color_index = 0 |
| 98 | + if (color_range != 0): |
| 99 | + color_index = int(round((pixel-minpixel)/color_range)) |
| 100 | + if color_index < 0: |
| 101 | + 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) |
0 commit comments