@@ -55,36 +55,55 @@ class WipperSnapper_I2C_Driver_SGP30 : public WipperSnapper_I2C_Driver {
55
55
return false ;
56
56
}
57
57
_sgp30->IAQinit (); // start IAQ algorithm
58
- _did_measure = false ;
58
+
59
+ // Initialize cached values and cadence
60
+ _eco2 = 0 ;
61
+ _tvoc = 0 ;
59
62
_lastFastMs = millis () - SGP30_FASTTICK_INTERVAL_MS;
60
63
return true ;
61
64
}
62
65
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
+ /* ******************************************************************************/
63
81
bool getEventECO2 (sensors_event_t *senseEvent) override {
64
82
if (!_sgp30)
65
83
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;
74
85
return true ;
75
86
}
76
87
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
+ /* ******************************************************************************/
77
103
bool getEventTVOC (sensors_event_t *senseEvent) override {
78
104
if (!_sgp30)
79
105
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;
88
107
return true ;
89
108
}
90
109
@@ -96,11 +115,10 @@ class WipperSnapper_I2C_Driver_SGP30 : public WipperSnapper_I2C_Driver {
96
115
datasheet. On each call, it checks the elapsed time since the
97
116
last poll using `millis()`. If at least
98
117
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 `.
100
119
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.
104
122
105
123
@note Called automatically from
106
124
`WipperSnapper_Component_I2C::update()` once per loop iteration.
@@ -116,16 +134,20 @@ class WipperSnapper_I2C_Driver_SGP30 : public WipperSnapper_I2C_Driver {
116
134
117
135
uint32_t now = millis ();
118
136
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
+ }
120
141
_lastFastMs = now;
121
142
}
122
143
}
123
144
124
145
protected:
125
146
Adafruit_SGP30 *_sgp30; // /< Pointer to SGP30 sensor object
126
147
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
129
151
130
152
/* * Timestamp of last poll to enforce 1 Hz cadence. */
131
153
uint32_t _lastFastMs = 0 ;
0 commit comments