Skip to content

Commit c67ea20

Browse files
committed
Closes #724 - Add SGP30 to offline mode
1 parent 974eccf commit c67ea20

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

src/components/i2c/controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ static const std::map<std::string, FnCreateI2CDriver> I2cFactory = {
223223
const char *driver_name) -> drvBase * {
224224
return new drvScd30(i2c, addr, mux_channel, driver_name);
225225
}},
226+
{"sgp30",
227+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
228+
const char *driver_name) -> drvBase * {
229+
return new drvSgp30(i2c, addr, mux_channel, driver_name);
230+
}},
226231
{"sgp40",
227232
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
228233
const char *driver_name) -> drvBase * {

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "drivers/drvScd30.h"
5151
#include "drivers/drvScd4x.h"
5252
#include "drivers/drvSen5x.h"
53+
#include "drivers/drvSgp30.h"
5354
#include "drivers/drvSgp40.h"
5455
#include "drivers/drvSht3x.h"
5556
#include "drivers/drvSht4x.h"

src/components/i2c/drivers/drvSgp30.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

src/components/i2c/drivers/drvSgp40.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class drvSgp40 : public drvBase {
9292
}
9393

9494
protected:
95-
Adafruit_SGP40 *_sgp40; ///< SEN5X driver object
95+
Adafruit_SGP40 *_sgp40; ///< SGP40 driver object
9696
};
9797

98-
#endif // WipperSnapper_I2C_Driver_SEN5X
98+
#endif // WipperSnapper_I2C_Driver_SGP40

0 commit comments

Comments
 (0)