Skip to content

Commit

Permalink
start work on scd4x
Browse files Browse the repository at this point in the history
  • Loading branch information
brentru committed Nov 10, 2021
1 parent 4516a58 commit a943e65
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
WS_DEBUG_PRINTLN("seconds]");
}
drivers.push_back(_scd30);
} else if (msgDeviceInitReq->has_scd30) {
// Initialize new SCD30 sensor
_scd4x = new WipperSnapper_I2C_Driver_SCD4X(this->_i2c, i2cAddress);

// Did we initialize successfully?
if (!_scd4x->getInitialized()) {
WS_DEBUG_PRINTLN("ERROR: SCD4x not initialized successfully!");
return false;
}
WS_DEBUG_PRINTLN("Successfully Initialized SCD4x!");

// Configure SCD4x
// TODO!

drivers.push_back(_scd4x);
} else {
WS_DEBUG_PRINTLN("ERROR: Sensor not found")
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "drivers/WipperSnapper_I2C_Driver_AHTX0.h"
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
#include "drivers/WipperSnapper_I2C_Driver_SCD4X.h"

#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.

Expand Down Expand Up @@ -69,11 +70,12 @@ class WipperSnapper_Component_I2C {
int32_t _portNum;
TwoWire *_i2c = nullptr;
wippersnapper_i2c_v1_BusResponse _busStatusResponse;
std::vector<WipperSnapper_I2C_Driver *> drivers;
// Sensor drivers
std::vector<WipperSnapper_I2C_Driver *> drivers; ///< List of sensor drivers
// Sensor driver objects
WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr;
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
WipperSnapper_I2C_Driver_SCD4X *_scd4x = nullptr;
};
extern Wippersnapper WS;

Expand Down
10 changes: 6 additions & 4 deletions src/drivers/WipperSnapper_I2C_Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef enum {
AHTX0, // AHTX0 device driver
DPS310, // DPS310 device driver
SCD30, // SCD30 device driver
SCD4X, // SCD4X device driver
} DriverType_t;

/**************************************************************************/
Expand Down Expand Up @@ -83,7 +84,8 @@ class WipperSnapper_I2C_Driver {
/*!
@brief Sets the I2C device driver's type.
@param type
The type of I2C driver (corresponds to header Driver_.h class name)
The type of I2C driver (corresponds to header Driver_.h class
name)
*/
/*******************************************************************************/
void setDriverType(DriverType_t type) { driverType = type; }
Expand Down Expand Up @@ -309,8 +311,8 @@ class WipperSnapper_I2C_Driver {

/*********************************************************************************/
/*!
@brief Base implementation - Returns the previous time interval at which
the CO2 sensor was queried last.
@brief Base implementation - Returns the previous time interval at
which the CO2 sensor was queried last.
@returns Time when the CO2 sensor was last queried, in seconds.
*/
/*********************************************************************************/
Expand Down Expand Up @@ -347,7 +349,7 @@ class WipperSnapper_I2C_Driver {
long _CO2SensorPeriod =
-1L; ///< The time period between reading the CO2 sensor's value.
long _CO2SensorPeriodPrv; ///< The time when the CO2 sensor
///< was last read.
///< was last read.
};

#endif // WipperSnapper_I2C_Driver_H
93 changes: 93 additions & 0 deletions src/drivers/WipperSnapper_I2C_Driver_SCD4X.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*!
* @file WipperSnapper_I2C_Driver_SCD4X.h
*
* Device driver for Sensirion SCD4x sensors.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2021 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/

#ifndef WipperSnapper_I2C_Driver_SCD4X_H
#define WipperSnapper_I2C_Driver_SCD4X_H

#include "WipperSnapper_I2C_Driver.h"
#include <SensirionI2CScd4x.h>

/**************************************************************************/
/*!
@brief Class that provides a driver interface for the SCD30 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {

public:
/*******************************************************************************/
/*!
@brief Constructor for a SCD4x sensor.
@param _i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_SCD4X(TwoWire *_i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(_i2c, sensorAddress) {
uint16_t error, serial0, serial1, serial2;
setDriverType(SCD4X);
_sensorAddress = sensorAddress;

// TODO: The sensioron scd4x constructor is different from the
// std adafruit libraries, the following may not work
// https://github.com/Sensirion/arduino-i2c-scd4x/blob/master/src/SensirionI2CScd4x.cpp#L49
Wire.begin();
_scd4x.begin(Wire);

// stop potentially previously started measurement
error = _scd4x.stopPeriodicMeasurement();
// Error trying to execute stopPeriodicMeasurement()
if (error) {
_isInitialized = false;
return;
}

// attempt to get serial number
error = _scd4x.getSerialNumber(serial0, serial1, serial2);
// Error trying to execute getSerialNumber()
if (error) {
_isInitialized = false;
return;
}

// attempt to start measurement
error = _scd4x.startPeriodicMeasurement();
if (error) {
_isInitialized = false;
return;
}

_isInitialized = true;
}

/*******************************************************************************/
/*!
@brief Destructor for an SCD4x sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_SCD4X() {
_tempSensorPeriod = -1L;
_humidSensorPeriod = -1L;
_CO2SensorPeriod = -1L;
setDriverType(UNSPECIFIED);
}

protected:
SensirionI2CScd4x _scd4x; ///< SCD4X object
};

#endif // WipperSnapper_I2C_Driver_SCD4X

0 comments on commit a943e65

Please sign in to comment.