@@ -55,36 +55,55 @@ class WipperSnapper_I2C_Driver_SGP30 : public WipperSnapper_I2C_Driver {
5555 return false ;
5656 }
5757 _sgp30->IAQinit (); // start IAQ algorithm
58- _did_measure = false ;
58+
59+ // Initialize cached values and cadence
60+ _eco2 = 0 ;
61+ _tvoc = 0 ;
5962 _lastFastMs = millis () - SGP30_FASTTICK_INTERVAL_MS;
6063 return true ;
6164 }
6265
66+ /* ******************************************************************************/
67+ /* !
68+ @brief Gets the most recently cached eCO2 reading.
69+
70+ This value is updated in `fastTick()` at a ~1 Hz cadence
71+ and returned directly here without re-triggering an I2C
72+ transaction.
73+
74+ @param senseEvent
75+ Pointer to an Adafruit Sensor event that will be populated
76+ with the cached eCO2 value (in ppm).
77+
78+ @returns True if a cached value is available, False otherwise.
79+ */
80+ /* ******************************************************************************/
6381 bool getEventECO2 (sensors_event_t *senseEvent) override {
6482 if (!_sgp30)
6583 return false ;
66- if (!_did_measure) {
67- _did_measure = _sgp30->IAQmeasure ();
68- }
69- if (!_did_measure)
70- return false ;
71-
72- senseEvent->eCO2 = (uint16_t )_sgp30->eCO2 ;
73- _did_measure = false ; // consume cached reading
84+ senseEvent->eCO2 = _eco2;
7485 return true ;
7586 }
7687
88+ /* ******************************************************************************/
89+ /* !
90+ @brief Gets the most recently cached TVOC reading.
91+
92+ This value is updated in `fastTick()` at a ~1 Hz cadence
93+ and returned directly here without re-triggering an I2C
94+ transaction.
95+
96+ @param senseEvent
97+ Pointer to an Adafruit Sensor event that will be populated
98+ with the cached TVOC value (in ppb).
99+
100+ @returns True if a cached value is available, False otherwise.
101+ */
102+ /* ******************************************************************************/
77103 bool getEventTVOC (sensors_event_t *senseEvent) override {
78104 if (!_sgp30)
79105 return false ;
80- if (!_did_measure) {
81- _did_measure = _sgp30->IAQmeasure ();
82- }
83- if (!_did_measure)
84- return false ;
85-
86- senseEvent->tvoc = (uint16_t )_sgp30->TVOC ;
87- _did_measure = false ; // consume cached reading
106+ senseEvent->tvoc = _tvoc;
88107 return true ;
89108 }
90109
@@ -96,11 +115,10 @@ class WipperSnapper_I2C_Driver_SGP30 : public WipperSnapper_I2C_Driver {
96115 datasheet. On each call, it checks the elapsed time since the
97116 last poll using `millis()`. If at least
98117 SGP30_FASTTICK_INTERVAL_MS have passed, it performs a single
99- IAQ measurement and caches the result in `_did_measure `.
118+ IAQ measurement and caches the results in `_eco2` and `_tvoc `.
100119
101- Cached values (eCO2, TVOC) are later returned by
102- `getEventECO2()` and `getEventTVOC()` without re-triggering I2C
103- traffic.
120+ Cached values are then returned by `getEventECO2()` and
121+ `getEventTVOC()` without re-triggering I2C traffic.
104122
105123 @note Called automatically from
106124 `WipperSnapper_Component_I2C::update()` once per loop iteration.
@@ -116,16 +134,20 @@ class WipperSnapper_I2C_Driver_SGP30 : public WipperSnapper_I2C_Driver {
116134
117135 uint32_t now = millis ();
118136 if (now - _lastFastMs >= SGP30_FASTTICK_INTERVAL_MS) {
119- _did_measure = _sgp30->IAQmeasure ();
137+ if (_sgp30->IAQmeasure ()) {
138+ _eco2 = (uint16_t )_sgp30->eCO2 ;
139+ _tvoc = (uint16_t )_sgp30->TVOC ;
140+ }
120141 _lastFastMs = now;
121142 }
122143 }
123144
124145protected:
125146 Adafruit_SGP30 *_sgp30; // /< Pointer to SGP30 sensor object
126147
127- /* * Whether we have a fresh IAQ measurement ready. */
128- bool _did_measure = false ;
148+ /* * Cached latest measurements (no averaging). */
149+ uint16_t _eco2 = 0 ; // /< eCO2, in ppm
150+ uint16_t _tvoc = 0 ; // /< TVOC, in ppb
129151
130152 /* * Timestamp of last poll to enforce 1 Hz cadence. */
131153 uint32_t _lastFastMs = 0 ;
0 commit comments