Skip to content

Commit cd4821c

Browse files
Copilottyeth
andauthored
Cache millis() in ReadSensorData() to avoid duplicate calls; update skill template
Agent-Logs-Url: https://github.com/adafruit/Adafruit_Wippersnapper_Arduino/sessions/985bd733-1a3f-4f6f-82b8-13e20eddfef9 Co-authored-by: tyeth <6692083+tyeth@users.noreply.github.com>
1 parent 0ac6a2b commit cd4821c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

.agents/skills/add-sensor-component-v1/SKILL.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff 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
138138
may be called before °C). Every `getEvent*()` must go through a shared `_readSensor()` with a
139139
millis-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
141144
for the canonical patterns.
142145

143146
This 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

src/components/i2c/drivers/WipperSnapper_I2C_Driver_APDS9999.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)