Skip to content

Commit 355cade

Browse files
committed
DM: add blinka thermal cam example
1 parent dbe03ea commit 355cade

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

examples/thermalcam.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import math
3+
import time
4+
5+
import busio
6+
import board
7+
8+
import pygame
9+
import numpy as np
10+
from scipy.interpolate import griddata
11+
12+
from colour import Color
13+
14+
import adafruit_amg88xx
15+
16+
i2c_bus = busio.I2C(board.SCL, board.SDA)
17+
18+
#low range of the sensor (this will be blue on the screen)
19+
MINTEMP = 26.
20+
21+
#high range of the sensor (this will be red on the screen)
22+
MAXTEMP = 32.
23+
24+
#how many color values we can have
25+
COLORDEPTH = 1024
26+
27+
os.putenv('SDL_FBDEV', '/dev/fb1')
28+
pygame.init()
29+
30+
#initialize the sensor
31+
sensor = adafruit_amg88xx.AMG88XX(i2c_bus)
32+
33+
points = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)]
34+
grid_x, grid_y = np.mgrid[0:7:32j, 0:7:32j]
35+
36+
#sensor is an 8x8 grid so lets do a square
37+
height = 240
38+
width = 240
39+
40+
#the list of colors we can choose from
41+
blue = Color("indigo")
42+
colors = list(blue.range_to(Color("red"), COLORDEPTH))
43+
44+
#create the array of colors
45+
colors = [(int(c.red * 255), int(c.green * 255), int(c.blue * 255)) for c in colors]
46+
47+
displayPixelWidth = width / 30
48+
displayPixelHeight = height / 30
49+
50+
lcd = pygame.display.set_mode((width, height))
51+
52+
lcd.fill((255, 0, 0))
53+
54+
pygame.display.update()
55+
pygame.mouse.set_visible(False)
56+
57+
lcd.fill((0, 0, 0))
58+
pygame.display.update()
59+
60+
#some utility functions
61+
def constrain(val, min_val, max_val):
62+
return min(max_val, max(min_val, val))
63+
64+
def map_value(x, in_min, in_max, out_min, out_max):
65+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
66+
67+
#let the sensor initialize
68+
time.sleep(.1)
69+
70+
while True:
71+
72+
#read the pixels
73+
pixels = []
74+
for row in sensor.pixels:
75+
pixels = pixels + row
76+
pixels = [map_value(p, MINTEMP, MAXTEMP, 0, COLORDEPTH - 1) for p in pixels]
77+
78+
#perform interpolation
79+
bicubic = griddata(points, pixels, (grid_x, grid_y), method='cubic')
80+
81+
#draw everything
82+
for ix, row in enumerate(bicubic):
83+
for jx, pixel in enumerate(row):
84+
pygame.draw.rect(lcd, colors[constrain(int(pixel), 0, COLORDEPTH- 1)],
85+
(displayPixelHeight * ix, displayPixelWidth * jx, displayPixelHeight, displayPixelWidth))
86+
87+
pygame.display.update()
88+

0 commit comments

Comments
 (0)