Skip to content

Commit

Permalink
Merge pull request #14 from caternuson/iss13_busio
Browse files Browse the repository at this point in the history
Convert to BusIO
  • Loading branch information
caternuson authored Sep 3, 2021
2 parents c979bc1 + a1b6ce7 commit f1b8257
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
52 changes: 25 additions & 27 deletions Adafruit_SI1145.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@
****************************************************/

#include "Adafruit_SI1145.h"

/**
* @brief Destructor
*
*/
Adafruit_SI1145::~Adafruit_SI1145() {
if (i2c_dev)
delete i2c_dev;
}
/**
* @brief Construct a new Adafruit_SI1145::Adafruit_SI1145 object
*
*/
Adafruit_SI1145::Adafruit_SI1145() : m_pBus(&Wire), _addr(SI1145_ADDR) {}
Adafruit_SI1145::Adafruit_SI1145() {}
/**
* @brief Initize the driver, specifying the `TwoWire` bus to use
*
Expand All @@ -38,10 +47,13 @@ boolean Adafruit_SI1145::begin(TwoWire *pBus) {
* @return boolean true: success false: failure to initize the sensor
*/
boolean Adafruit_SI1145::begin(uint8_t addr, TwoWire *pBus) {
if (i2c_dev)
delete i2c_dev;
i2c_dev = new Adafruit_I2CDevice(addr, pBus);
if (!i2c_dev->begin()) {
return false;
}

_addr = addr;
m_pBus = pBus;
m_pBus->begin();
uint8_t id = read8(SI1145_REG_PARTID);
if (id != 0x45)
return false; // look for SI1145
Expand Down Expand Up @@ -174,32 +186,18 @@ uint8_t Adafruit_SI1145::readParam(uint8_t p) {
/*********************************************************************/

uint8_t Adafruit_SI1145::read8(uint8_t reg) {
m_pBus->beginTransmission(_addr);
m_pBus->write((uint8_t)reg);
m_pBus->endTransmission();

m_pBus->requestFrom((uint8_t)_addr, (uint8_t)1);
return m_pBus->read();
uint8_t buffer[1] = {reg};
i2c_dev->write_then_read(buffer, 1, buffer, 1);
return buffer[0];
}

uint16_t Adafruit_SI1145::read16(uint8_t a) {
uint16_t ret;

m_pBus->beginTransmission(_addr); // start transmission to device
m_pBus->write(a); // sends register address to read from
m_pBus->endTransmission(); // end transmission

m_pBus->requestFrom(_addr, (uint8_t)2); // send data n-bytes read
ret = m_pBus->read(); // receive DATA
ret |= (uint16_t)m_pBus->read() << 8; // receive DATA

return ret;
uint8_t buffer[2] = {a, 0};
i2c_dev->write_then_read(buffer, 1, buffer, 2);
return ((uint16_t)buffer[0]) | ((uint16_t)buffer[1] << 8);
}

void Adafruit_SI1145::write8(uint8_t reg, uint8_t val) {

m_pBus->beginTransmission(_addr); // start transmission to device
m_pBus->write(reg); // sends register address to write
m_pBus->write(val); // sends value
m_pBus->endTransmission(); // end transmission
}
uint8_t buffer[2] = {reg, val};
i2c_dev->write(buffer, 2);
}
6 changes: 3 additions & 3 deletions Adafruit_SI1145.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <Adafruit_I2CDevice.h>

/* COMMANDS */
#define SI1145_PARAM_QUERY 0x80
Expand Down Expand Up @@ -150,6 +150,7 @@
class Adafruit_SI1145 {
public:
Adafruit_SI1145(void);
~Adafruit_SI1145();
boolean begin(uint8_t addr = SI1145_ADDR, TwoWire *pBus = &Wire);
boolean begin(TwoWire *pBus);
void reset();
Expand All @@ -165,7 +166,6 @@ class Adafruit_SI1145 {
void write8(uint8_t reg, uint8_t val);
uint8_t readParam(uint8_t p);
uint8_t writeParam(uint8_t p, uint8_t v);
TwoWire *m_pBus;
uint8_t _addr;
Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
};
#endif
3 changes: 2 additions & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name=Adafruit SI1145 Library
version=1.1.1
version=1.2.0
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino library for the SI1145 sensors in the Adafruit shop
paragraph=Arduino library for the SI1145 sensors in the Adafruit shop
category=Sensors
url=https://github.com/adafruit/Adafruit_SI1145_Library
architectures=*
depends=Adafruit BusIO

0 comments on commit f1b8257

Please sign in to comment.