HeatSensor.cpp
1 // SPDX-FileCopyrightText: 2019 teejaydub for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #if 0 // Change to 1 to enable this code (referenced by user_watch.cpp) 6 // CORRESPONDING LINE IN user_watch.cpp MUST ALSO BE ENABLED! 7 8 /* Read the IR sensor and try to figure out where the heat is located. 9 */ 10 #include "HeatSensor.h" 11 12 #include <Wire.h> 13 #include <Adafruit_AMG88xx.h> 14 15 Adafruit_AMG88xx amg; 16 17 float pixels[AMG88xx_PIXEL_ARRAY_SIZE]; 18 19 void HeatSensor::setup() 20 { 21 x = 0; 22 y = 0; 23 magnitude = 0; 24 25 bool status; 26 27 // default settings 28 status = amg.begin(); 29 if (!status) { 30 Serial.println("Could not find a valid AMG88xx sensor, check wiring!"); 31 while (1); 32 } 33 34 yield(); 35 delay(100); // let sensor boot up 36 } 37 38 // Find the approximate X and Y values of the peak temperature in the pixel array, 39 // along with the magnitude of the brightest spot. 40 void HeatSensor::find_focus() 41 { 42 amg.readPixels(pixels); 43 yield(); 44 45 x = 0, y = 0, magnitude = 0; 46 float minVal = 100, maxVal = 0; 47 int i = 0; 48 for (float yPos = 3.5; yPos > -4; yPos -= 1.0) { 49 for (float xPos = 3.5; xPos > -4; xPos -= 1.0) { 50 float p = pixels[i]; 51 x += xPos * p; 52 y += yPos * p; 53 minVal = min(minVal, p); 54 maxVal = max(maxVal, p); 55 i++; 56 } 57 } 58 x = - x / AMG88xx_PIXEL_ARRAY_SIZE / 5.0; 59 y = y / AMG88xx_PIXEL_ARRAY_SIZE / 5.0; 60 x = max(-1.0, min(1.0, x)); 61 y = max(-1.0, min(1.0, y)); 62 magnitude = max(0, min(50, maxVal - 20)); 63 64 // Report. 65 #define SERIAL_OUT 3 66 #if SERIAL_OUT == 1 67 // Print raw values 68 Serial.print("["); 69 for(int i=1; i<=AMG88xx_PIXEL_ARRAY_SIZE; i++){ 70 Serial.print(pixels[i-1]); 71 Serial.print(", "); 72 if( i%8 == 0 ) Serial.println(); 73 } 74 Serial.println("]"); 75 Serial.println(); 76 #endif 77 #if SERIAL_OUT == 2 78 // Print character-graphic array 79 const char charPixels[] = " .-*o0#"; 80 Serial.println("========"); 81 for (int i = 1; i <= AMG88xx_PIXEL_ARRAY_SIZE; i++) { 82 int val = min(5, round(max(0, pixels[i-1] - 20) / 2)); 83 Serial.print(charPixels[val]); 84 if (i % 8 == 0) 85 Serial.println(); 86 } 87 Serial.println(); 88 #endif 89 #if SERIAL_OUT == 3 || SERIAL_OUT == 2 90 // Print coordinates and brightness 91 Serial.print(x); 92 Serial.print(' '); 93 Serial.print(y); 94 Serial.print(' '); 95 Serial.println(magnitude); 96 #endif 97 } 98 99 /* 100 void loop() { 101 // Read all the pixels 102 103 // Find the focal point. 104 float x, y, magnitude; 105 find_focus(x, y, magnitude); 106 107 // Set diagnostic LEDs. 108 analogWrite(CENTER_LED, round(max(0, magnitude / 30) * 255)); 109 analogWrite(RIGHT_LED, round(min(1.0, max(0.0, -x / 3)) * 255)); 110 analogWrite(LEFT_LED, round(min(1.0, max(0.0, x / 3)) * 255)); 111 analogWrite(UP_LED, round(min(1.0, max(0.0, y / 3)) * 255)); 112 analogWrite(DOWN_LED, round(min(1.0, max(0.0, -y / 3)) * 255)); 113 114 delay(200); 115 } 116 */ 117 #endif