Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor I2C_Driver Initialization #232

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
uint16_t i2cAddress = (uint16_t)msgDeviceInitReq->i2c_device_address;
if (strcmp("aht20", msgDeviceInitReq->i2c_device_name) == 0) {
_ahtx0 = new WipperSnapper_I2C_Driver_AHTX0(this->_i2c, i2cAddress);
if (!_ahtx0->isInitialized()) {
if (!_ahtx0->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize AHTX0 chip!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
WS_DEBUG_PRINTLN("AHTX0 Initialized Successfully!");
_ahtx0->configureDriver(msgDeviceInitReq);
drivers.push_back(_ahtx0);
} else if (strcmp("bme280", msgDeviceInitReq->i2c_device_name) == 0) {
_bme280 = new WipperSnapper_I2C_Driver_BME280(this->_i2c, i2cAddress);
if (!_bme280->isInitialized()) {
if (!_bme280->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize BME280!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
Expand All @@ -224,7 +223,7 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
WS_DEBUG_PRINTLN("BME280 Initialized Successfully!");
} else if (strcmp("dps310", msgDeviceInitReq->i2c_device_name) == 0) {
_dps310 = new WipperSnapper_I2C_Driver_DPS310(this->_i2c, i2cAddress);
if (!_dps310->isInitialized()) {
if (!_dps310->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize DPS310!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
Expand All @@ -235,7 +234,7 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
WS_DEBUG_PRINTLN("DPS310 Initialized Successfully!");
} else if (strcmp("scd30", msgDeviceInitReq->i2c_device_name) == 0) {
_scd30 = new WipperSnapper_I2C_Driver_SCD30(this->_i2c, i2cAddress);
if (!_scd30->isInitialized()) {
if (!_scd30->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize SCD30!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
Expand All @@ -246,7 +245,7 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
WS_DEBUG_PRINTLN("SCD30 Initialized Successfully!");
} else if (strcmp("mcp9808", msgDeviceInitReq->i2c_device_name) == 0) {
_mcp9808 = new WipperSnapper_I2C_Driver_MCP9808(this->_i2c, i2cAddress);
if (!_mcp9808->isInitialized()) {
if (!_mcp9808->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize MCP9808!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
Expand Down
42 changes: 18 additions & 24 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define WipperSnapper_I2C_Driver_H

#include <Adafruit_Sensor.h>
#include <Arduino.h>

/**************************************************************************/
/*!
Expand All @@ -28,13 +29,16 @@ class WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for an I2C sensor.
@brief Instanciates an I2C sensor.
@param i2c
The I2C hardware interface, default is Wire.
@param sensorAddress
7-bit device address.
The I2C sensor's unique address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver(TwoWire *, uint16_t sensorAddress) {
_sensorAddress = sensorAddress;
WipperSnapper_I2C_Driver(TwoWire *i2c, uint16_t sensorAddress) {
_i2c = i2c;
_sensorAddress;
}

/*******************************************************************************/
Expand All @@ -44,6 +48,14 @@ class WipperSnapper_I2C_Driver {
/*******************************************************************************/
~WipperSnapper_I2C_Driver() { _sensorAddress = 0; }

/*******************************************************************************/
/*!
@brief Initializes the I2C sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() { return false; }

/*******************************************************************************/
/*!
@brief Uses an I2CDeviceInitRequest message to configure the sensors
Expand Down Expand Up @@ -95,23 +107,6 @@ class WipperSnapper_I2C_Driver {
}
}

/*******************************************************************************/
/*!
@brief Gets the initialization status of an I2C driver.
@returns True if I2C device is initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool isInitialized() { return _isInitialized; }

/*******************************************************************************/
/*!
@brief Sets the I2C device's address.
@param i2cAddress
The I2C device's unique address.
*/
/*******************************************************************************/
void setI2CAddress(uint16_t i2cAddress) { _sensorAddress = i2cAddress; }

/*******************************************************************************/
/*!
@brief Gets the I2C device's address.
Expand Down Expand Up @@ -673,9 +668,8 @@ class WipperSnapper_I2C_Driver {
}

protected:
bool _isInitialized = false; ///< True if the I2C device was initialized
///< successfully, False otherwise.
uint16_t _sensorAddress; ///< The I2C device's unique I2C address.
TwoWire *_i2c; ///< Pointer to the I2C driver's Wire object
uint16_t _sensorAddress; ///< The I2C driver's unique I2C address.
long _tempSensorPeriod =
0L; ///< The time period between reading the temperature sensor's value.
long _tempSensorPeriodPrv =
Expand Down
26 changes: 19 additions & 7 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_AHTX0.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ class WipperSnapper_I2C_Driver_AHTX0 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
/*!
@brief Constructor for an AHTX0 sensor.
@param _i2c
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the AHTX0 sensor.
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_AHTX0(TwoWire *_i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(_i2c, sensorAddress) {
setI2CAddress(sensorAddress);
_aht = new Adafruit_AHTX0();
_isInitialized = _aht->begin(_i2c);
WipperSnapper_I2C_Driver_AHTX0(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
Expand All @@ -51,6 +50,19 @@ class WipperSnapper_I2C_Driver_AHTX0 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
~WipperSnapper_I2C_Driver_AHTX0() { delete _aht; }

/*******************************************************************************/
/*!
@brief Initializes the AHTX0 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.

*/
/*******************************************************************************/
bool begin() {
_aht = new Adafruit_AHTX0();
bool isInit = _aht->begin(_i2c, (int32_t)_sensorAddress);
return isInit;
}

/*******************************************************************************/
/*!
@brief Enables the AHTX0's temperature sensor.
Expand Down
25 changes: 18 additions & 7 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_BME280.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ class WipperSnapper_I2C_Driver_BME280 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
/*!
@brief Constructor for an BME280 sensor.
@param _i2c
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the BME280 sensor.
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_BME280(TwoWire *_i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(_i2c, sensorAddress) {
setI2CAddress(sensorAddress);
_bme = new Adafruit_BME280();
_isInitialized = _bme->begin(sensorAddress, _i2c);
WipperSnapper_I2C_Driver_BME280(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
Expand All @@ -53,6 +52,18 @@ class WipperSnapper_I2C_Driver_BME280 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
~WipperSnapper_I2C_Driver_BME280() { delete _bme; }

/*******************************************************************************/
/*!
@brief Initializes the BME280 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_bme = new Adafruit_BME280();
bool isInit = _bme->begin(_sensorAddress, _i2c);
return isInit;
}

/*******************************************************************************/
/*!
@brief Enables the BME280's temperature sensor.
Expand Down
25 changes: 18 additions & 7 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_DPS310.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ class WipperSnapper_I2C_Driver_DPS310 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
/*!
@brief Constructor for a DPS310 sensor.
@param _i2c
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_DPS310(TwoWire *_i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(_i2c, sensorAddress) {
setI2CAddress(sensorAddress);
_dps310 = new Adafruit_DPS310();
_isInitialized = _dps310->begin_I2C((uint8_t)_sensorAddress, _i2c);
WipperSnapper_I2C_Driver_DPS310(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
Expand All @@ -51,6 +50,18 @@ class WipperSnapper_I2C_Driver_DPS310 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
~WipperSnapper_I2C_Driver_DPS310() { delete _dps310; }

/*******************************************************************************/
/*!
@brief Initializes the DPS310 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_dps310 = new Adafruit_DPS310();
bool isInit = _dps310->begin_I2C((uint8_t)_sensorAddress, _i2c);
return isInit;
}

/*******************************************************************************/
/*!
@brief Enables the DPS310's temperature sensor. Sets highest precision
Expand Down
26 changes: 18 additions & 8 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_MCP9808.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@ class WipperSnapper_I2C_Driver_MCP9808 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
/*!
@brief Constructor for a MCP9808 sensor.
@param _i2c
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_MCP9808(TwoWire *_i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(_i2c, sensorAddress) {
// Called when a MCP9808 component is created
setI2CAddress(sensorAddress); // sets the driver's I2C address
_mcp9808 = new Adafruit_MCP9808();
_isInitialized = _mcp9808->begin();
WipperSnapper_I2C_Driver_MCP9808(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
Expand All @@ -52,6 +50,18 @@ class WipperSnapper_I2C_Driver_MCP9808 : public WipperSnapper_I2C_Driver {
delete _mcp9808;
}

/*******************************************************************************/
/*!
@brief Initializes the MCP9808 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_mcp9808 = new Adafruit_MCP9808();
bool isInit = _mcp9808->begin((uint8_t)_sensorAddress, _i2c);
return isInit;
}

/*******************************************************************************/
/*!
@brief Gets the MCP9808's current temperature.
Expand Down
25 changes: 18 additions & 7 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
/*!
@brief Constructor for a SCD30 sensor.
@param _i2c
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_SCD30(TwoWire *_i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(_i2c, sensorAddress) {
setI2CAddress(sensorAddress);
_scd30 = new Adafruit_SCD30();
_isInitialized = _scd30->begin((uint8_t)_sensorAddress, _i2c);
WipperSnapper_I2C_Driver_SCD30(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
Expand All @@ -50,6 +49,18 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
~WipperSnapper_I2C_Driver_SCD30() { delete _scd30; }

/*******************************************************************************/
/*!
@brief Initializes the SCD30 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_scd30 = new Adafruit_SCD30();
bool isInit = _scd30->begin((uint8_t)_sensorAddress, _i2c);
return isInit;
}

/*******************************************************************************/
/*!
@brief Gets the SCD30's current temperature.
Expand Down