|
| 1 | +/** |
| 2 | + * Auto_Update — Mode 3: serialEvent() (Zero-Interrupt Fallback) |
| 3 | + * |
| 4 | + * Arduino calls serialEvent() automatically between loop() iterations |
| 5 | + * whenever bytes are waiting in the hardware Serial RX FIFO. This gives |
| 6 | + * near-immediate reaction to incoming data without needing a hardware timer |
| 7 | + * or UART RX interrupt. |
| 8 | + * |
| 9 | + * autoUpdateISR() is called from serialEvent() — the function name "ISR" is |
| 10 | + * kept for API consistency; serialEvent() is not a true hardware interrupt |
| 11 | + * on Arduino, but it provides equivalent zero-extra-step auto-update |
| 12 | + * behaviour on every Arduino board regardless of timer availability. |
| 13 | + * |
| 14 | + * When to choose this mode: |
| 15 | + * - No hardware timer is available or all timers are already occupied. |
| 16 | + * - You want zero-setup auto-update without any third-party library. |
| 17 | + * - Targeting boards where timer interrupt APIs differ across cores |
| 18 | + * (e.g., megaTinyCore, ATtinyCore). |
| 19 | + * |
| 20 | + * Limitation: serialEvent() is not called while the MCU is inside a blocking |
| 21 | + * function (delay(), analogRead() >10 ms, etc.). Keep loop() responsive. |
| 22 | + * |
| 23 | + * Platform: any Arduino-compatible board |
| 24 | + * Wiring : Serial TX → peer RX, Serial RX ← peer TX |
| 25 | + */ |
| 26 | + |
| 27 | +#include <TinyLink.h> |
| 28 | +#include <adapters/TinyArduinoAdapter.h> |
| 29 | +#include "SharedData.h" |
| 30 | + |
| 31 | +using namespace tinylink; |
| 32 | + |
| 33 | +TinyArduinoAdapter adapter(Serial); |
| 34 | +TinyLink<SensorData, TinyArduinoAdapter> link(adapter); |
| 35 | + |
| 36 | +// ----- Callback ----------------------------------------------------------- |
| 37 | + |
| 38 | +static volatile bool g_dataReady = false; |
| 39 | +static SensorData g_latest; |
| 40 | + |
| 41 | +void onReceive(const SensorData& d) { |
| 42 | + g_latest = d; |
| 43 | + g_dataReady = true; |
| 44 | +} |
| 45 | + |
| 46 | +// ----- Arduino serialEvent() — called between loop() iterations ----------- |
| 47 | +// |
| 48 | +// No extra setup required; the constructor already registered the instance. |
| 49 | +// Simply forward to autoUpdateISR() for API consistency and portability. |
| 50 | +// |
| 51 | +void serialEvent() { |
| 52 | + TinyLink<SensorData, TinyArduinoAdapter>::autoUpdateISR(); |
| 53 | +} |
| 54 | + |
| 55 | +// ------------------------------------------------------------------------- |
| 56 | + |
| 57 | +void setup() { |
| 58 | + Serial.begin(9600); |
| 59 | + link.onDataReceived(onReceive); |
| 60 | + // No enableAutoUpdate(), no timer setup — serialEvent() handles it all. |
| 61 | +} |
| 62 | + |
| 63 | +void loop() { |
| 64 | + // Process any data that arrived via serialEvent(). |
| 65 | + if (g_dataReady) { |
| 66 | + g_dataReady = false; |
| 67 | + Serial.print(F("[RX] uptime=")); |
| 68 | + Serial.print(g_latest.uptime); |
| 69 | + Serial.print(F(" temp=")); |
| 70 | + Serial.println(g_latest.temperature); |
| 71 | + } |
| 72 | + |
| 73 | + static uint32_t lastSend = 0; |
| 74 | + static uint8_t appSeq = 0; |
| 75 | + if (millis() - lastSend > 2000) { |
| 76 | + SensorData msg = { millis(), 24.5f, appSeq++ }; |
| 77 | + link.sendData(TYPE_DATA, msg); |
| 78 | + lastSend = millis(); |
| 79 | + } |
| 80 | +} |
0 commit comments