You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -85,8 +85,8 @@ Before writing any code, gather this information:
85
85
86
86
| What | Where to look |
87
87
|------|---------------|
88
-
| Product page | Search the web for "adafruit <SENSOR>" to find the product page. The product page (https://www.adafruit.com/product/<PRODUCT_ID>) html data has links to the learn guide (do not use the API or guess urls). This is the fastest route to all other info. |
89
-
| Learn guide |**Always read the learn guide before writing any code.** Fetch the text view `.md?view=all` version (e.g.`https://learn.adafruit.com/<guide-slug>/overview` goes to `https://learn.adafruit.com/<guide-slug>.md?view=all`). The Arduino section (`## Arduino`) names the exact library to use. Do NOT skip this step even if you think you recognise a sub-component (e.g. a board with an SHT41 onboard may have its own dedicated library like `Adafruit_STCC4` — you won't know without reading the guide). There is also a subsequent section for `## Example Code`beneath the arduino section showing a link to the basic example sketch/code. |
88
+
| Product page | Search the web for "adafruit <SENSOR>" to find the product page. The product page (`https://www.adafruit.com/product/<PRODUCT_ID>`) html data has links to the learn guide (do not use the API or guess URLs). Use a tool like wget or curl: `curl -sL https://www.adafruit.com/product/5817 \| grep "learn.adafruit.com"` (Note: replace `5817` with the actual product ID found via search). |
89
+
| Learn guide |**Always read the learn guide before writing any code.** Fetch the text view `.md?view=all` version of the guide found on the product page (e.g., `curl -sL "https://learn.adafruit.com/<guide-slug>.md?view=all"`). The Arduino section (`## Arduino`) names the exact library to use. Do NOT skip this step even if you think you recognise a sub-component. There is also a `## Example Code` section showing the basic example sketch. |
90
90
| Adafruit Arduino library | Named in the learn guide. Or: `gh search repos "<SENSOR>" --owner adafruit`. If no dedicated library from adafruit, check related chips (e.g. TMP119 lives inside `Adafruit_TMP117`). Try partial matches like `TMP11` if exact fails. Fall back to 3rd party or ask user. |
91
91
| Library API | Read the library header on GitHub — find `begin()` signature and sensor read methods (`getEvent`, `readTempC`, etc.) |
92
92
| I2C addresses | Sensor datasheet or Adafruit product page or learn guide or driver. Check https://learn.adafruit.com/i2c-addresses/the-list|
@@ -204,104 +204,21 @@ bool begin() {
204
204
}
205
205
```
206
206
207
-
### Then: Write the driver using this template
207
+
### Then: Write the driver
208
208
209
-
This is a header-only class. Use the closest existing driver as a template, but **do not blindly
210
-
copy** — older drivers may lack the caching and explicit-defaults patterns described above.
211
-
Always apply the patterns from this skill even if the closest driver doesn't use them.
209
+
This is a header-only class. Use the closest existing driver as a template. Always apply the patterns from this skill!
212
210
213
-
> **Note:** Some existing drivers (e.g. TMP117, MCP9808) use simpler patterns that predate
214
-
> current best practices. Follow this template, not those older drivers.
215
-
216
-
```cpp
217
-
/*!
218
-
* @file WipperSnapper_I2C_Driver_<SENSOR>.h
219
-
*
220
-
* Device driver for the <SENSOR> <description> sensor.
221
-
*
222
-
* Adafruit invests time and resources providing this open source code,
223
-
* please support Adafruit and open-source hardware by purchasing
224
-
* products from Adafruit!
225
-
*
226
-
* Copyright (c) <AUTHOR> <YEAR> for Adafruit Industries.
227
-
*
228
-
* MIT license, all text here must be included in any redistribution.
if (_lastRead != 0 && millis() - _lastRead < 1000)
291
-
return true; // use cached values
292
-
// Do actual I2C read — adapt to library API:
293
-
if (!_<sensor_ptr>->getEvent(&_cachedTemp))
294
-
return false;
295
-
_lastRead = millis();
296
-
return true;
297
-
}
298
-
};
299
-
300
-
#endif// WipperSnapper_I2C_Driver_<SENSOR>_H
301
-
```
211
+
> **Good References:**
212
+
> - Look at `WipperSnapper_I2C_Driver_SCD30.h` for a clean example of the new style, including read caching and setting explicit defaults in `begin()`.
213
+
> - Check `WipperSnapper_I2C_Driver_SGP30.h` for a complete example of `fastTick()` usage, which is needed when the datasheet explicitly requires a minimum polling cadence for correct operation.
302
214
303
215
### Key decisions when writing the driver:
304
216
217
+
-**Handling `dataReady()` or Data Availability:** Some sensors (like the SCD30) require you to check a flag before reading to avoid stale data or blocked I2C buses.
218
+
- If the library requires polling a `dataReady()` method, implement a short, limited test in your read loop.
219
+
- Never use infinite `while (!sensor.dataReady()) delay(10);` loops. Since WipperSnapper runs many components concurrently, infinite loops crash the whole system if a sensor hangs.
220
+
- Instead, use a brief check (optionally with a small finite `delay` and subsequent retry, like `if(!dataReady) { delay(100); if(!dataReady) return false; }`). See `WipperSnapper_I2C_Driver_SCD30.h` for a safe implementation.
221
+
305
222
-**Library API style:** Some Adafruit libraries use Unified Sensor (`getEvent(sensors_event_t*)`)
306
223
which fills the event struct directly. Others expose raw read methods like `readTempC()`. If the
307
224
library uses raw reads, assign to the appropriate field (e.g. `tempEvent->temperature = readTempC()`)
@@ -327,28 +244,7 @@ protected:
327
244
4. Have `getEvent*()` return the cached values instead of doing a fresh I2C read.
328
245
329
246
See `WipperSnapper_I2C_Driver_SGP30.h` for a complete example — the SGP30 datasheet requires
330
-
~1 Hz polling to maintain its IAQ algorithm, so the driver defines:
331
-
332
-
```cpp
333
-
#defineSGP30_FASTTICK_INTERVAL_MS 1000
334
-
335
-
voidfastTick() override {
336
-
if (!_sgp30) return;
337
-
uint32_t now = millis();
338
-
if (now - _lastFastMs >= SGP30_FASTTICK_INTERVAL_MS) {
0 commit comments