/ examples / amg88xx_rpi_thermal_cam.py
amg88xx_rpi_thermal_cam.py
  1  """This example is for Raspberry Pi (Linux) only!
  2     It will not work on microcontrollers running CircuitPython!"""
  3  
  4  import os
  5  import math
  6  import time
  7  
  8  import busio
  9  import board
 10  
 11  import numpy as np
 12  import pygame
 13  from scipy.interpolate import griddata
 14  
 15  from colour import Color
 16  
 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  
 52  displayPixelWidth = width / 30
 53  displayPixelHeight = height / 30
 54  
 55  lcd = pygame.display.set_mode((width, height))
 56  
 57  lcd.fill((255, 0, 0))
 58  
 59  pygame.display.update()
 60  pygame.mouse.set_visible(False)
 61  
 62  lcd.fill((0, 0, 0))
 63  pygame.display.update()
 64  
 65  # some utility functions
 66  def constrain(val, min_val, max_val):
 67      return min(max_val, max(min_val, val))
 68  
 69  
 70  def map_value(x, in_min, in_max, out_min, out_max):
 71      return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
 72  
 73  
 74  # let the sensor initialize
 75  time.sleep(0.1)
 76  
 77  while True:
 78  
 79      # read the pixels
 80      pixels = []
 81      for row in sensor.pixels:
 82          pixels = pixels + row
 83      pixels = [map_value(p, MINTEMP, MAXTEMP, 0, COLORDEPTH - 1) for p in pixels]
 84  
 85      # perform interpolation
 86      bicubic = griddata(points, pixels, (grid_x, grid_y), method="cubic")
 87  
 88      # draw everything
 89      for ix, row in enumerate(bicubic):
 90          for jx, pixel in enumerate(row):
 91              pygame.draw.rect(
 92                  lcd,
 93                  colors[constrain(int(pixel), 0, COLORDEPTH - 1)],
 94                  (
 95                      displayPixelHeight * ix,
 96                      displayPixelWidth * jx,
 97                      displayPixelHeight,
 98                      displayPixelWidth,
 99                  ),
100              )
101  
102      pygame.display.update()