Skip to content

Commit

Permalink
Merge pull request #22 from caternuson/iss21_busio
Browse files Browse the repository at this point in the history
Convert to BusIO
  • Loading branch information
ladyada authored Aug 8, 2021
2 parents 90e9eea + 2b91c3e commit 2665171
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 67 deletions.
88 changes: 27 additions & 61 deletions Adafruit_AMG88xx.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
#include "Adafruit_AMG88xx.h"

//#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
@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
*/
/**************************************************************************/
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;
Expand Down Expand Up @@ -219,60 +211,34 @@ 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;

// 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);

#ifdef I2C_DEBUG
Serial.print("[$");
Serial.print(reg + pos, HEX);
Serial.print("] -> ");
#endif
for (int i = 0; i < read_now; i++) {
buf[pos] = Wire.read();
#ifdef I2C_DEBUG
Serial.print("0x");
Serial.print(buf[pos], HEX);
Serial.print(", ");
#endif
pos++;
uint8_t buffer[1];
size_t chunkSize = i2c_dev->maxBufferSize();
if (chunkSize > num) {
// can just read
buffer[0] = reg;
i2c_dev->write(buffer, 1);
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(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];
pos++;
}
}
#ifdef I2C_DEBUG
Serial.println();
#endif
}
}

void Adafruit_AMG88xx::write(uint8_t reg, uint8_t *buf, uint8_t num) {
#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]);
#ifdef I2C_DEBUG
Serial.print("0x");
Serial.print(buf[i], HEX);
Serial.print(", ");
#endif
}
Wire.endTransmission();
#ifdef I2C_DEBUG
Serial.println();
#endif
uint8_t prefix[1] = {reg};
i2c_dev->write(buf, num, true, prefix, 1);
}

/**************************************************************************/
Expand Down
7 changes: 3 additions & 4 deletions Adafruit_AMG88xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "WProgram.h"
#endif

#include <Wire.h>
#include <Adafruit_I2CDevice.h>

/*=========================================================================
I2C ADDRESS/BITS
Expand Down Expand Up @@ -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();
Expand All @@ -92,15 +92,14 @@ 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);
uint8_t read8(byte reg);

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);
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=Adafruit AMG88xx Library
version=1.1.0
version=1.2.0
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino library for AMG88xx 8x8 Thermocouple Sensors.
paragraph=Arduino library for AMG88xx (especially AMG8833) 8x8 Grid-eye thermal camera sensors that use I2C
category=Sensors
url=https://github.com/adafruit/Adafruit_AMG88xx
architectures=*
depends=Adafruit ST7735 and ST7789 Library, Adafruit ILI9341
depends=Adafruit ST7735 and ST7789 Library, Adafruit ILI9341, Adafruit BusIO

0 comments on commit 2665171

Please sign in to comment.