From a943e65bc81f8b0ca665ad0aac90868835f29d28 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 10 Nov 2021 14:23:33 -0500 Subject: [PATCH] start work on scd4x --- src/components/i2c/WipperSnapper_I2C.cpp | 15 ++++ src/components/i2c/WipperSnapper_I2C.h | 6 +- src/drivers/WipperSnapper_I2C_Driver.h | 10 ++- src/drivers/WipperSnapper_I2C_Driver_SCD4X.h | 93 ++++++++++++++++++++ 4 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 src/drivers/WipperSnapper_I2C_Driver_SCD4X.h diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 9cc49e983..23222e772 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -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") } diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index ca9496e79..6286171e9 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -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. @@ -69,11 +70,12 @@ class WipperSnapper_Component_I2C { int32_t _portNum; TwoWire *_i2c = nullptr; wippersnapper_i2c_v1_BusResponse _busStatusResponse; - std::vector drivers; - // Sensor drivers + std::vector 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; diff --git a/src/drivers/WipperSnapper_I2C_Driver.h b/src/drivers/WipperSnapper_I2C_Driver.h index f04e183e7..04a53413c 100644 --- a/src/drivers/WipperSnapper_I2C_Driver.h +++ b/src/drivers/WipperSnapper_I2C_Driver.h @@ -25,6 +25,7 @@ typedef enum { AHTX0, // AHTX0 device driver DPS310, // DPS310 device driver SCD30, // SCD30 device driver + SCD4X, // SCD4X device driver } DriverType_t; /**************************************************************************/ @@ -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; } @@ -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. */ /*********************************************************************************/ @@ -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 \ No newline at end of file diff --git a/src/drivers/WipperSnapper_I2C_Driver_SCD4X.h b/src/drivers/WipperSnapper_I2C_Driver_SCD4X.h new file mode 100644 index 000000000..66da9a2b9 --- /dev/null +++ b/src/drivers/WipperSnapper_I2C_Driver_SCD4X.h @@ -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 + +/**************************************************************************/ +/*! + @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 \ No newline at end of file