Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
AMG8833.cpp
00001 #include "AMG8833.h" 00002 00003 //#define I2C_DEBUG 00004 00005 /**************************************************************************/ 00006 /*! 00007 @brief Setups the I2C interface and hardware 00008 @param addr Optional I2C address the sensor can be found on. Default is 0x69 00009 @returns True if device is set up, false on any failure 00010 */ 00011 /**************************************************************************/ 00012 Adafruit_AMG88xx::Adafruit_AMG88xx(PinName mysda, PinName myscl) : _i2c(mysda, myscl) 00013 { 00014 00015 } 00016 00017 int Adafruit_AMG88xx::begin(char addr) 00018 { 00019 _i2caddr = addr; 00020 00021 //enter normal mode 00022 _pctl.PCTL = AMG88xx_NORMAL_MODE; 00023 write8(AMG88xx_PCTL, _pctl.get()); 00024 00025 //software reset 00026 _rst.RST = AMG88xx_INITIAL_RESET; 00027 write8(AMG88xx_RST, _rst.get()); 00028 00029 //disable interrupts by default 00030 disableInterrupt(); 00031 00032 //set to 10 FPS 00033 _fpsc.FPS = AMG88xx_FPS_10; 00034 write8(AMG88xx_FPSC, _fpsc.get()); 00035 00036 wait_ms(100); 00037 00038 return 1; 00039 } 00040 00041 /**************************************************************************/ 00042 /*! 00043 @brief Set the moving average mode. 00044 @param mode if True is passed, output will be twice the moving average 00045 */ 00046 /**************************************************************************/ 00047 void Adafruit_AMG88xx::setMovingAverageMode(int mode) 00048 { 00049 _ave.MAMOD = mode; 00050 write8(AMG88xx_AVE, _ave.get()); 00051 } 00052 00053 /**************************************************************************/ 00054 /*! 00055 @brief Set the interrupt levels. The hysteresis value defaults to .95 * high 00056 @param high the value above which an interrupt will be triggered 00057 @param low the value below which an interrupt will be triggered 00058 */ 00059 /**************************************************************************/ 00060 void Adafruit_AMG88xx::setInterruptLevels(float high, float low) 00061 { 00062 setInterruptLevels(high, low, high * .95); 00063 } 00064 00065 /**************************************************************************/ 00066 /*! 00067 @brief Set the interrupt levels 00068 @param high the value above which an interrupt will be triggered 00069 @param low the value below which an interrupt will be triggered 00070 @param hysteresis the hysteresis value for interrupt detection 00071 */ 00072 /**************************************************************************/ 00073 void Adafruit_AMG88xx::setInterruptLevels(float high, float low, float hysteresis) 00074 { 00075 int highConv = high / AMG88xx_PIXEL_TEMP_CONVERSION; 00076 highConv = constrain(highConv, -4095, 4095); 00077 _inthl.INT_LVL_H = highConv & 0xFF; 00078 _inthh.INT_LVL_H = (highConv & 0xF) >> 4; 00079 this->write8(AMG88xx_INTHL, _inthl.get()); 00080 this->write8(AMG88xx_INTHH, _inthh.get()); 00081 00082 int lowConv = low / AMG88xx_PIXEL_TEMP_CONVERSION; 00083 lowConv = constrain(lowConv, -4095, 4095); 00084 _intll.INT_LVL_L = lowConv & 0xFF; 00085 _intlh.INT_LVL_L = (lowConv & 0xF) >> 4; 00086 this->write8(AMG88xx_INTLL, _intll.get()); 00087 this->write8(AMG88xx_INTLH, _intlh.get()); 00088 00089 int hysConv = hysteresis / AMG88xx_PIXEL_TEMP_CONVERSION; 00090 hysConv = constrain(hysConv, -4095, 4095); 00091 _ihysl.INT_HYS = hysConv & 0xFF; 00092 _ihysh.INT_HYS = (hysConv & 0xF) >> 4; 00093 this->write8(AMG88xx_IHYSL, _ihysl.get()); 00094 this->write8(AMG88xx_IHYSH, _ihysh.get()); 00095 } 00096 00097 /**************************************************************************/ 00098 /*! 00099 @brief enable the interrupt pin on the device. 00100 */ 00101 /**************************************************************************/ 00102 void Adafruit_AMG88xx::enableInterrupt() 00103 { 00104 _intc.INTEN = 1; 00105 this->write8(AMG88xx_INTC, _intc.get()); 00106 } 00107 00108 /**************************************************************************/ 00109 /*! 00110 @brief disable the interrupt pin on the device 00111 */ 00112 /**************************************************************************/ 00113 void Adafruit_AMG88xx::disableInterrupt() 00114 { 00115 _intc.INTEN = 0; 00116 this->write8(AMG88xx_INTC, _intc.get()); 00117 } 00118 00119 /**************************************************************************/ 00120 /*! 00121 @brief Set the interrupt to either absolute value or difference mode 00122 @param mode passing AMG88xx_DIFFERENCE sets the device to difference mode, AMG88xx_ABSOLUTE_VALUE sets to absolute value mode. 00123 */ 00124 /**************************************************************************/ 00125 void Adafruit_AMG88xx::setInterruptMode(uint8_t mode) 00126 { 00127 _intc.INTMOD = mode; 00128 this->write8(AMG88xx_INTC, _intc.get()); 00129 } 00130 00131 /**************************************************************************/ 00132 /*! 00133 @brief Read the state of the triggered interrupts on the device. The full interrupt register is 8 chars in length. 00134 @param buf the pointer to where the returned data will be stored 00135 @param size Optional number of chars to read. Default is 8 chars. 00136 @returns up to 8 chars of data in buf 00137 */ 00138 /**************************************************************************/ 00139 void Adafruit_AMG88xx::getInterrupt(uint8_t *buf, uint8_t size) 00140 { 00141 uint8_t charsToRead = min(size, (uint8_t)8); 00142 00143 this->read(AMG88xx_INT_OFFSET, buf, charsToRead); 00144 } 00145 00146 /**************************************************************************/ 00147 /*! 00148 @brief Clear any triggered interrupts 00149 */ 00150 /**************************************************************************/ 00151 void Adafruit_AMG88xx::clearInterrupt() 00152 { 00153 _rst.RST = AMG88xx_FLAG_RESET; 00154 write8(AMG88xx_RST, _rst.get()); 00155 } 00156 00157 /**************************************************************************/ 00158 /*! 00159 @brief read the onboard thermistor 00160 @returns a the floating point temperature in degrees Celsius 00161 */ 00162 /**************************************************************************/ 00163 float Adafruit_AMG88xx::readThermistor() 00164 { 00165 uint8_t raw[2]; 00166 this->read(AMG88xx_TTHL, raw, 2); 00167 uint16_t recast = ((uint16_t) raw[1] << 8) | ((uint16_t) raw[0]); 00168 00169 return signedMag12ToFloat(recast) * AMG88xx_THERMISTOR_CONVERSION; 00170 } 00171 00172 /**************************************************************************/ 00173 /*! 00174 @brief Read Infrared sensor values 00175 @param buf the array to place the pixels in 00176 @param size Optionsl number of chars to read (up to 64). Default is 64 chars. 00177 @return up to 64 chars of pixel data in buf 00178 */ 00179 /**************************************************************************/ 00180 void Adafruit_AMG88xx::readPixels(float *buf, uint8_t size) 00181 { 00182 uint16_t recast; 00183 float converted; 00184 uint8_t charsToRead = min((uint8_t)(size << 1), (uint8_t)(AMG88xx_PIXEL_ARRAY_SIZE << 1)); 00185 uint8_t rawArray[charsToRead]; 00186 this->read(AMG88xx_PIXEL_OFFSET, rawArray, charsToRead); 00187 00188 for(int i=0; i<size; i++){ 00189 uint8_t pos = i << 1; 00190 recast = ((uint16_t)rawArray[pos + 1] << 8) | ((uint16_t)rawArray[pos]); 00191 00192 converted = int12ToFloat(recast) * AMG88xx_PIXEL_TEMP_CONVERSION; 00193 buf[i] = converted; 00194 } 00195 } 00196 00197 /**************************************************************************/ 00198 /*! 00199 @brief write one char of data to the specified register 00200 @param reg the register to write to 00201 @param value the value to write 00202 */ 00203 /**************************************************************************/ 00204 void Adafruit_AMG88xx::write8(uint8_t reg, uint8_t value) 00205 { 00206 this->write(reg, &value, 1); 00207 } 00208 00209 /**************************************************************************/ 00210 /*! 00211 @brief read one char of data from the specified register 00212 @param reg the register to read 00213 @returns one char of register data 00214 */ 00215 /**************************************************************************/ 00216 uint8_t Adafruit_AMG88xx::read8(uint8_t reg) 00217 { 00218 uint8_t ret; 00219 this->read(reg, &ret, 1); 00220 return ret; 00221 } 00222 00223 int Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) 00224 { 00225 // char value; 00226 // char pos = 0; 00227 00228 // //on arduino we need to read in AMG_I2C_CHUNKSIZE char chunks 00229 // while(pos < num){ 00230 // char read_now = min((char)AMG_I2C_CHUNKSIZE, (char)(num - pos)); 00231 // Wire.beginTransmission((char)_i2caddr); 00232 // Wire.write((char)reg + pos); 00233 // Wire.endTransmission(); 00234 // Wire.requestFrom((char)_i2caddr, read_now); 00235 00236 // #ifdef I2C_DEBUG 00237 // Serial.print("[$"); Serial.print(reg + pos, HEX); Serial.print("] -> "); 00238 // #endif 00239 // for(int i=0; i<read_now; i++){ 00240 // buf[pos] = Wire.read(); 00241 // #ifdef I2C_DEBUG 00242 // Serial.print("0x"); Serial.print(buf[pos], HEX); Serial.print(", "); 00243 // #endif 00244 // pos++; 00245 // } 00246 // #ifdef I2C_DEBUG 00247 // Serial.println(); 00248 // #endif 00249 // } 00250 char reg_dest = reg; 00251 char rx_buf[num]; 00252 int nack = _i2c.write(_i2caddr, ®_dest, 1, 1); // no stop 00253 nack = _i2c.read(_i2caddr, rx_buf, num); 00254 for (int i = 0; i < num; i++) { 00255 buf[i] = (uint8_t) rx_buf[i]; 00256 } 00257 return nack; 00258 } 00259 00260 int Adafruit_AMG88xx::write(uint8_t reg, uint8_t *buf, uint8_t num) 00261 { 00262 // #ifdef I2C_DEBUG 00263 // Serial.print("[$"); Serial.print(reg, HEX); Serial.print("] <- "); 00264 // #endif 00265 // Wire.beginTransmission((char)_i2caddr); 00266 // Wire.write((char)reg); 00267 // for (int i=0; i<num; i++) { 00268 // Wire.write(buf[i]); 00269 // #ifdef I2C_DEBUG 00270 // Serial.print("0x"); Serial.print(buf[i], HEX); Serial.print(", "); 00271 // #endif 00272 // } 00273 // Wire.endTransmission(); 00274 // #ifdef I2C_DEBUG 00275 // Serial.println(); 00276 // #endif 00277 char reg_dest = reg; 00278 char tx_buf[num]; 00279 for (int i = 0; i < num; i++) { 00280 tx_buf[i] = (char) buf[i]; 00281 } 00282 int nack = _i2c.write(_i2caddr, ®_dest, 1, 1); // no stop 00283 nack = _i2c.write(_i2caddr, tx_buf, num); 00284 return nack; 00285 } 00286 00287 /**************************************************************************/ 00288 /*! 00289 @brief convert a 12-bit signed magnitude value to a floating point number 00290 @param val the 12-bit signed magnitude value to be converted 00291 @returns the converted floating point value 00292 */ 00293 /**************************************************************************/ 00294 float Adafruit_AMG88xx::signedMag12ToFloat(uint16_t val) 00295 { 00296 //take first 11 bits as absolute val 00297 uint16_t absVal = (val & 0x7FF); 00298 00299 return (val & 0x800) ? 0 - (float)absVal : (float)absVal ; 00300 } 00301 00302 /**************************************************************************/ 00303 /*! 00304 @brief convert a 12-bit integer two's complement value to a floating point number 00305 @param val the 12-bit integer two's complement value to be converted 00306 @returns the converted floating point value 00307 */ 00308 /**************************************************************************/ 00309 float Adafruit_AMG88xx::int12ToFloat(uint16_t val) 00310 { 00311 uint16_t sVal = (val << 4); //shift to left so that sign bit of 12 bit integer number is placed on sign bit of 16 bit signed integer number 00312 return sVal >> 4; //shift back the signed number, return converts to float 00313 } 00314 00315 /** 00316 * @brief helper function, constraints the value 00317 * @param value variable to be constrained 00318 * @param min minimum value of the variable 00319 */ 00320 int Adafruit_AMG88xx::constrain(int value, int min, int max) 00321 { 00322 if (value > max) { 00323 return max; 00324 } 00325 else if (value < min) { 00326 return min; 00327 } 00328 else { 00329 return value; 00330 } 00331 } 00332 00333 int Adafruit_AMG88xx::min(int val1, int val2) { 00334 if (val1 < val2) return val1; 00335 else return val2; 00336 } 00337 00338 int Adafruit_AMG88xx::max(int val1, int val2) { 00339 if (val1 > val2) return val1; 00340 else return val2; 00341 } 00342 00343 uint8_t Adafruit_AMG88xx::min(uint8_t val1, uint8_t val2) { 00344 if (val1 < val2) return val1; 00345 else return val2; 00346 } 00347 00348 uint8_t Adafruit_AMG88xx::max(uint8_t val1, uint8_t val2) { 00349 if (val1 > val2) return val1; 00350 else return val2; 00351 }
Generated on Thu Aug 25 2022 00:33:23 by
1.7.2