From cace45eda7ea655c50685cf8c0159eeaf80b4208 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 2 Aug 2021 13:51:33 -0700 Subject: [PATCH 1/8] convert to busio --- Adafruit_AMG88xx.cpp | 37 ++++++++++++++++--------------------- Adafruit_AMG88xx.h | 7 +++---- library.properties | 4 ++-- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/Adafruit_AMG88xx.cpp b/Adafruit_AMG88xx.cpp index 0bdeac7..e04bbfd 100644 --- a/Adafruit_AMG88xx.cpp +++ b/Adafruit_AMG88xx.cpp @@ -17,10 +17,10 @@ @returns True if device is set up, false on any failure */ /**************************************************************************/ -bool Adafruit_AMG88xx::begin(uint8_t addr) { - _i2caddr = addr; - - _i2c_init(); +bool Adafruit_AMG88xx::begin(uint8_t addr, TwoWire *theWire) { + i2c_dev = new Adafruit_I2CDevice(addr, theWire); + if (!i2c_dev->begin()) + return false; // enter normal mode _pctl.PCTL = AMG88xx_NORMAL_MODE; @@ -219,27 +219,25 @@ uint8_t Adafruit_AMG88xx::read8(byte reg) { return ret; } -void Adafruit_AMG88xx::_i2c_init() { Wire.begin(); } - void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { - uint8_t value; uint8_t pos = 0; + uint8_t buffer[1]; + uint8_t read_buffer[AMG_I2C_CHUNKSIZE]; + i2c_dev->write(buffer, 1); // on arduino we need to read in AMG_I2C_CHUNKSIZE byte chunks while (pos < num) { - uint8_t read_now = min((uint8_t)AMG_I2C_CHUNKSIZE, (uint8_t)(num - pos)); - Wire.beginTransmission((uint8_t)_i2caddr); - Wire.write((uint8_t)reg + pos); - Wire.endTransmission(); - Wire.requestFrom((uint8_t)_i2caddr, read_now); - + buffer[0] = reg + pos; + i2c_dev->write(buffer, 1); #ifdef I2C_DEBUG Serial.print("[$"); Serial.print(reg + pos, HEX); Serial.print("] -> "); #endif + uint8_t read_now = min((uint8_t)AMG_I2C_CHUNKSIZE, (uint8_t)(num - pos)); + i2c_dev->read(read_buffer, read_now); for (int i = 0; i < read_now; i++) { - buf[pos] = Wire.read(); + buf[pos] = read_buffer[i]; #ifdef I2C_DEBUG Serial.print("0x"); Serial.print(buf[pos], HEX); @@ -254,25 +252,22 @@ void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { } void Adafruit_AMG88xx::write(uint8_t reg, uint8_t *buf, uint8_t num) { + uint8_t buffer[num + 1]; + buffer[0] = reg; #ifdef I2C_DEBUG Serial.print("[$"); Serial.print(reg, HEX); Serial.print("] <- "); #endif - Wire.beginTransmission((uint8_t)_i2caddr); - Wire.write((uint8_t)reg); for (int i = 0; i < num; i++) { - Wire.write(buf[i]); + buffer[i + 1] = buf[i]; #ifdef I2C_DEBUG Serial.print("0x"); Serial.print(buf[i], HEX); Serial.print(", "); #endif } - Wire.endTransmission(); -#ifdef I2C_DEBUG - Serial.println(); -#endif + i2c_dev->write(buffer, num + 1); } /**************************************************************************/ diff --git a/Adafruit_AMG88xx.h b/Adafruit_AMG88xx.h index 6fd1adb..a0a0250 100644 --- a/Adafruit_AMG88xx.h +++ b/Adafruit_AMG88xx.h @@ -7,7 +7,7 @@ #include "WProgram.h" #endif -#include +#include /*========================================================================= I2C ADDRESS/BITS @@ -72,7 +72,7 @@ class Adafruit_AMG88xx { Adafruit_AMG88xx(void){}; ~Adafruit_AMG88xx(void){}; - bool begin(uint8_t addr = AMG88xx_ADDRESS); + bool begin(uint8_t addr = AMG88xx_ADDRESS, TwoWire *theWire = &Wire); void readPixels(float *buf, uint8_t size = AMG88xx_PIXEL_ARRAY_SIZE); float readThermistor(); @@ -92,7 +92,7 @@ class Adafruit_AMG88xx { void setInterruptLevels(float high, float low, float hysteresis); private: - uint8_t _i2caddr; + Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface void write8(byte reg, byte value); void write16(byte reg, uint16_t value); @@ -100,7 +100,6 @@ class Adafruit_AMG88xx { void read(uint8_t reg, uint8_t *buf, uint8_t num); void write(uint8_t reg, uint8_t *buf, uint8_t num); - void _i2c_init(); float signedMag12ToFloat(uint16_t val); float int12ToFloat(uint16_t val); diff --git a/library.properties b/library.properties index edd09c3..5d0a711 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit AMG88xx Library -version=1.1.0 +version=1.2.0 author=Adafruit maintainer=Adafruit sentence=Arduino library for AMG88xx 8x8 Thermocouple Sensors. @@ -7,4 +7,4 @@ paragraph=Arduino library for AMG88xx (especially AMG8833) 8x8 Grid-eye thermal category=Sensors url=https://github.com/adafruit/Adafruit_AMG88xx architectures=* -depends=Adafruit ST7735 and ST7789 Library, Adafruit ILI9341 \ No newline at end of file +depends=Adafruit ST7735 and ST7789 Library, Adafruit ILI9341, Adafruit BusIO \ No newline at end of file From 56f46629a79041923efc2af37fc071c371949c68 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 2 Aug 2021 14:22:45 -0700 Subject: [PATCH 2/8] updates --- Adafruit_AMG88xx.cpp | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/Adafruit_AMG88xx.cpp b/Adafruit_AMG88xx.cpp index e04bbfd..50766f2 100644 --- a/Adafruit_AMG88xx.cpp +++ b/Adafruit_AMG88xx.cpp @@ -2,13 +2,6 @@ //#define I2C_DEBUG -#if defined(ESP32) -// https://github.com/espressif/arduino-esp32/issues/839 -#define AMG_I2C_CHUNKSIZE 16 -#else -#define AMG_I2C_CHUNKSIZE 32 -#endif - /**************************************************************************/ /*! @brief Setups the I2C interface and hardware @@ -222,10 +215,10 @@ uint8_t Adafruit_AMG88xx::read8(byte reg) { void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { uint8_t pos = 0; uint8_t buffer[1]; - uint8_t read_buffer[AMG_I2C_CHUNKSIZE]; + uint8_t chuckSize = uint8_t(i2c_dev->maxBufferSize()); + uint8_t read_buffer[chuckSize]; - i2c_dev->write(buffer, 1); - // on arduino we need to read in AMG_I2C_CHUNKSIZE byte chunks + // on arduino we need to read in chunks while (pos < num) { buffer[0] = reg + pos; i2c_dev->write(buffer, 1); @@ -234,7 +227,7 @@ void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { Serial.print(reg + pos, HEX); Serial.print("] -> "); #endif - uint8_t read_now = min((uint8_t)AMG_I2C_CHUNKSIZE, (uint8_t)(num - pos)); + uint8_t read_now = min(chuckSize, (uint8_t)(num - pos)); i2c_dev->read(read_buffer, read_now); for (int i = 0; i < read_now; i++) { buf[pos] = read_buffer[i]; @@ -252,22 +245,18 @@ void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { } void Adafruit_AMG88xx::write(uint8_t reg, uint8_t *buf, uint8_t num) { - uint8_t buffer[num + 1]; - buffer[0] = reg; + uint8_t prefix[1] = {reg}; #ifdef I2C_DEBUG Serial.print("[$"); Serial.print(reg, HEX); Serial.print("] <- "); -#endif for (int i = 0; i < num; i++) { - buffer[i + 1] = buf[i]; -#ifdef I2C_DEBUG Serial.print("0x"); Serial.print(buf[i], HEX); Serial.print(", "); -#endif } - i2c_dev->write(buffer, num + 1); +#endif + i2c_dev->write(buf, num, true, prefix, 1); } /**************************************************************************/ From 39f624b2ef6dff97dc26a4f808c072808bab13e4 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 2 Aug 2021 14:45:18 -0700 Subject: [PATCH 3/8] skip trinket m0 --- examples/thermal_cam_interpolate/.trinket_m0.test.skip | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 examples/thermal_cam_interpolate/.trinket_m0.test.skip diff --git a/examples/thermal_cam_interpolate/.trinket_m0.test.skip b/examples/thermal_cam_interpolate/.trinket_m0.test.skip new file mode 100644 index 0000000..e69de29 From 19b53f81dff58364caf66a58bdeb3342e41bd68d Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 2 Aug 2021 14:55:07 -0700 Subject: [PATCH 4/8] doxy --- Adafruit_AMG88xx.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Adafruit_AMG88xx.cpp b/Adafruit_AMG88xx.cpp index 50766f2..905d8c1 100644 --- a/Adafruit_AMG88xx.cpp +++ b/Adafruit_AMG88xx.cpp @@ -7,6 +7,7 @@ @brief Setups the I2C interface and hardware @param addr Optional I2C address the sensor can be found on. Default is 0x69 + @param theWire the I2C object to use, defaults to &Wire @returns True if device is set up, false on any failure */ /**************************************************************************/ From 069482f01acb75026ae3a0e5b4934df8d7d52026 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 2 Aug 2021 18:29:26 -0700 Subject: [PATCH 5/8] remove debug --- Adafruit_AMG88xx.cpp | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/Adafruit_AMG88xx.cpp b/Adafruit_AMG88xx.cpp index 905d8c1..2e53148 100644 --- a/Adafruit_AMG88xx.cpp +++ b/Adafruit_AMG88xx.cpp @@ -1,7 +1,5 @@ #include "Adafruit_AMG88xx.h" -//#define I2C_DEBUG - /**************************************************************************/ /*! @brief Setups the I2C interface and hardware @@ -223,40 +221,17 @@ void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { while (pos < num) { buffer[0] = reg + pos; i2c_dev->write(buffer, 1); -#ifdef I2C_DEBUG - Serial.print("[$"); - Serial.print(reg + pos, HEX); - Serial.print("] -> "); -#endif uint8_t read_now = min(chuckSize, (uint8_t)(num - pos)); i2c_dev->read(read_buffer, read_now); for (int i = 0; i < read_now; i++) { buf[pos] = read_buffer[i]; -#ifdef I2C_DEBUG - Serial.print("0x"); - Serial.print(buf[pos], HEX); - Serial.print(", "); -#endif pos++; } -#ifdef I2C_DEBUG - Serial.println(); -#endif } } void Adafruit_AMG88xx::write(uint8_t reg, uint8_t *buf, uint8_t num) { uint8_t prefix[1] = {reg}; -#ifdef I2C_DEBUG - Serial.print("[$"); - Serial.print(reg, HEX); - Serial.print("] <- "); - for (int i = 0; i < num; i++) { - Serial.print("0x"); - Serial.print(buf[i], HEX); - Serial.print(", "); - } -#endif i2c_dev->write(buf, num, true, prefix, 1); } From 20d1fc44dfb69efcec32d7f01243bbbaa229d079 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 2 Aug 2021 18:53:42 -0700 Subject: [PATCH 6/8] change type --- Adafruit_AMG88xx.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Adafruit_AMG88xx.cpp b/Adafruit_AMG88xx.cpp index 2e53148..30fa1a3 100644 --- a/Adafruit_AMG88xx.cpp +++ b/Adafruit_AMG88xx.cpp @@ -214,16 +214,16 @@ uint8_t Adafruit_AMG88xx::read8(byte reg) { void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { uint8_t pos = 0; uint8_t buffer[1]; - uint8_t chuckSize = uint8_t(i2c_dev->maxBufferSize()); + size_t chuckSize = i2c_dev->maxBufferSize(); uint8_t read_buffer[chuckSize]; // on arduino we need to read in chunks while (pos < num) { buffer[0] = reg + pos; i2c_dev->write(buffer, 1); - uint8_t read_now = min(chuckSize, (uint8_t)(num - pos)); + size_t read_now = min(chuckSize, num - pos); i2c_dev->read(read_buffer, read_now); - for (int i = 0; i < read_now; i++) { + for (size_t i = 0; i < read_now; i++) { buf[pos] = read_buffer[i]; pos++; } From f91af683c5f6b5b01442ffd19b83f02f47da4068 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 2 Aug 2021 19:31:57 -0700 Subject: [PATCH 7/8] only chunk if needed --- Adafruit_AMG88xx.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Adafruit_AMG88xx.cpp b/Adafruit_AMG88xx.cpp index 30fa1a3..cb560e5 100644 --- a/Adafruit_AMG88xx.cpp +++ b/Adafruit_AMG88xx.cpp @@ -212,20 +212,26 @@ uint8_t Adafruit_AMG88xx::read8(byte reg) { } void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { - uint8_t pos = 0; uint8_t buffer[1]; - size_t chuckSize = i2c_dev->maxBufferSize(); - uint8_t read_buffer[chuckSize]; - - // on arduino we need to read in chunks - while (pos < num) { - buffer[0] = reg + pos; + size_t chunckSize = i2c_dev->maxBufferSize(); + if (chunkSize > num) { + // can just read + buffer[0] = reg; i2c_dev->write(buffer, 1); - size_t read_now = min(chuckSize, num - pos); - i2c_dev->read(read_buffer, read_now); - for (size_t i = 0; i < read_now; i++) { - buf[pos] = read_buffer[i]; - pos++; + i2c_dev->read(buf, num); + } else { + // must read in chunks + uint8_t pos = 0; + uint8_t read_buffer[chunkSize]; + while (pos < num) { + buffer[0] = reg + pos; + i2c_dev->write(buffer, 1); + uint8_t read_now = min(uint8_t(chunckSize), (uint8_t)(num - pos)); + i2c_dev->read(read_buffer, read_now); + for (uint8_t i = 0; i < read_now; i++) { + buf[pos] = read_buffer[i]; + pos++; + } } } } From 2b91c3e1cff7bacf118fefe9664e55a0a3872d47 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 2 Aug 2021 19:35:18 -0700 Subject: [PATCH 8/8] fix typo --- Adafruit_AMG88xx.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_AMG88xx.cpp b/Adafruit_AMG88xx.cpp index cb560e5..7bfa579 100644 --- a/Adafruit_AMG88xx.cpp +++ b/Adafruit_AMG88xx.cpp @@ -213,7 +213,7 @@ uint8_t Adafruit_AMG88xx::read8(byte reg) { void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { uint8_t buffer[1]; - size_t chunckSize = i2c_dev->maxBufferSize(); + size_t chunkSize = i2c_dev->maxBufferSize(); if (chunkSize > num) { // can just read buffer[0] = reg; @@ -226,7 +226,7 @@ void Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) { while (pos < num) { buffer[0] = reg + pos; i2c_dev->write(buffer, 1); - uint8_t read_now = min(uint8_t(chunckSize), (uint8_t)(num - pos)); + uint8_t read_now = min(uint8_t(chunkSize), (uint8_t)(num - pos)); i2c_dev->read(read_buffer, read_now); for (uint8_t i = 0; i < read_now; i++) { buf[pos] = read_buffer[i];