|
| 1 | +/*! |
| 2 | + * @file drvSgp30.h |
| 3 | + * |
| 4 | + * Device driver for the SGP30 VOC/gas sensor. |
| 5 | + * |
| 6 | + * Adafruit invests time and resources providing this open source code, |
| 7 | + * please support Adafruit and open-source hardware by purchasing |
| 8 | + * products from Adafruit! |
| 9 | + * |
| 10 | + * Copyright (c) Tyeth Gundry 2025 for Adafruit Industries. |
| 11 | + * |
| 12 | + * MIT license, all text here must be included in any redistribution. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef DRV_SGP30_H |
| 17 | +#define DRV_SGP30_H |
| 18 | + |
| 19 | +#include "drvBase.h" |
| 20 | +#include <Adafruit_SGP30.h> |
| 21 | +#include <Wire.h> |
| 22 | + |
| 23 | +/**************************************************************************/ |
| 24 | +/*! |
| 25 | + @brief Class that provides a driver interface for the SGP30 sensor. |
| 26 | +*/ |
| 27 | +/**************************************************************************/ |
| 28 | +class drvSgp30 : public drvBase { |
| 29 | +public: |
| 30 | + /*******************************************************************************/ |
| 31 | + /*! |
| 32 | + @brief Constructor for a SGP30 sensor. |
| 33 | + @param i2c |
| 34 | + The I2C interface. |
| 35 | + @param sensorAddress |
| 36 | + 7-bit device address. |
| 37 | + @param mux_channel |
| 38 | + The I2C multiplexer channel. |
| 39 | + @param driver_name |
| 40 | + The name of the driver. |
| 41 | + */ |
| 42 | + /*******************************************************************************/ |
| 43 | + drvSgp30(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel, |
| 44 | + const char *driver_name) |
| 45 | + : drvBase(i2c, sensorAddress, mux_channel, driver_name) { |
| 46 | + // Initialization handled by drvBase constructor |
| 47 | + } |
| 48 | + |
| 49 | + /*******************************************************************************/ |
| 50 | + /*! |
| 51 | + @brief Initializes the SGP30 sensor and begins I2C. |
| 52 | + @returns True if initialized successfully, False otherwise. |
| 53 | + */ |
| 54 | + /*******************************************************************************/ |
| 55 | + bool begin() override { |
| 56 | + _sgp30 = new Adafruit_SGP30(); |
| 57 | + if (!_sgp30->begin(_i2c)) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + // TODO: update to use setCalibration() and pass in temp/humidity |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + /*******************************************************************************/ |
| 67 | + /*! |
| 68 | + @brief Gets the sensor's current equivalent/estimated CO2 value. |
| 69 | + @param co2Event |
| 70 | + Pointer to an Adafruit_Sensor event. |
| 71 | + @returns True if the temperature was obtained successfully, False |
| 72 | + otherwise. |
| 73 | + */ |
| 74 | + /*******************************************************************************/ |
| 75 | + bool getEventECO2(sensors_event_t *co2Event) { |
| 76 | + bool result = _sgp30->IAQmeasure(); |
| 77 | + if (result) { |
| 78 | + co2Event->eCO2 = (float)_sgp30->eCO2; |
| 79 | + } |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + /*******************************************************************************/ |
| 84 | + /*! |
| 85 | + @brief Gets the SGP30's current VOC reading. |
| 86 | + @param vocIndexEvent |
| 87 | + Adafruit Sensor event for VOC Index (1-500, 100 is normal) |
| 88 | + @returns True if the sensor value was obtained successfully, False |
| 89 | + otherwise. |
| 90 | + */ |
| 91 | + /*******************************************************************************/ |
| 92 | + bool getEventVOCIndex(sensors_event_t *vocIndexEvent) { |
| 93 | + bool result = _sgp30->IAQmeasure(); |
| 94 | + if (result) { |
| 95 | + vocIndexEvent->voc_index = (float)_sgp30->TVOC; |
| 96 | + } |
| 97 | + return result; |
| 98 | + } |
| 99 | + |
| 100 | +protected: |
| 101 | + Adafruit_SGP30 *_sgp30; ///< SGP30 driver object |
| 102 | +}; |
| 103 | + |
| 104 | +#endif // WipperSnapper_I2C_Driver_SGP30 |
0 commit comments