File tree Expand file tree Collapse file tree 2 files changed +10
-5
lines changed
.agents/skills/add-sensor-component-v1
src/components/i2c/drivers Expand file tree Collapse file tree 2 files changed +10
-5
lines changed Original file line number Diff line number Diff line change @@ -137,7 +137,10 @@ call the Celsius method and convert. Only implement the Celsius version. Never i
137137** Read-and-cache requirement:** The calling order of ` getEvent*() ` methods is not guaranteed (°F
138138may be called before °C). Every ` getEvent*() ` must go through a shared ` _readSensor() ` with a
139139millis-based time guard so only the first call per cycle does the I2C read; subsequent calls
140- return cached data. See the driver template in Step 1 and ` WipperSnapper_I2C_Driver_SCD30.h ` and SGP30
140+ return cached data. ** Cache ` millis() ` once** at the top of the function (e.g.
141+ ` unsigned long now = millis(); ` ) and use the cached value for both the time guard check and
142+ setting ` _lastRead ` — never call ` millis() ` twice.
143+ See the driver template in Step 1 and ` WipperSnapper_I2C_Driver_SCD30.h ` and SGP30
141144for the canonical patterns.
142145
143146This is especially important for multi-reading sensors but also applies to temperature sensors
@@ -534,11 +537,12 @@ protected:
534537 unsigned long _ lastRead = 0;
535538
536539 bool _ readSensor() {
537- if (_ lastRead != 0 && millis() - _ lastRead < 1000)
540+ unsigned long now = millis();
541+ if (_ lastRead != 0 && now - _ lastRead < 1000)
538542 return true; // recently read, use cached value
539543 if (!_ tmp119->getEvent(&_ cachedTemp))
540544 return false;
541- _ lastRead = millis() ;
545+ _ lastRead = now ;
542546 return true;
543547 }
544548
Original file line number Diff line number Diff line change @@ -84,8 +84,9 @@ class WipperSnapper_I2C_Driver_APDS9999 : public WipperSnapper_I2C_Driver {
8484 */
8585 /* ******************************************************************************/
8686 bool ReadSensorData () {
87+ unsigned long now = millis ();
8788 // Don't read sensor more than once per second
88- if (_lastRead != 0 && (millis () - _lastRead < 1000 ))
89+ if (_lastRead != 0 && (now - _lastRead < 1000 ))
8990 return true ;
9091
9192 uint32_t r, g, b, ir;
@@ -99,7 +100,7 @@ class WipperSnapper_I2C_Driver_APDS9999 : public WipperSnapper_I2C_Driver {
99100 return false ;
100101
101102 _cachedProximity.data [0 ] = (float )prox;
102- _lastRead = millis () ;
103+ _lastRead = now ;
103104 return true ;
104105 }
105106
You can’t perform that action at this time.
0 commit comments