/* BEFORE — the SENSOR->FILTER->ALERT pipeline as a POLLED SUPER-LOOP (no RTOS).
* Wokwi, ST Nucleo-C031C6.
*
* The sensor produces a NEW sample every 250 ms (indexed by wall-clock time). The loop reads
* whatever sample is available NOW, runs filter+alert, then comes back for the next one. When ALERT
* does heavy work, TIME passes -- and the samples the sensor produced meanwhile are GONE, because
* the loop wasn't there to read them. WATCH THE seq NUMBERS SKIP right after a HIGH/CRITICAL.
*
* LEDs (optional): yellow on a dropped glitch, red on an alert.
*/
#include "sensor_contract.h"
#define LED_YELLOW PB1
#define LED_RED PB14
void setup() {
Serial.begin(115200);
pinMode(LED_YELLOW, OUTPUT); pinMode(LED_RED, OUTPUT);
Serial.println("BEFORE: POLLED SUPER-LOOP (watch seq SKIP right after an alert)");
}
void loop() {
static uint32_t last = 0;
uint32_t seq = millis() / 250; // the sample the sensor has available RIGHT NOW
if (seq <= last) { delay(5); return; } // no new sample yet -- wait for the next 250 ms tick
if (seq > last + 1) { // we came back late: the sensor moved on without us
digitalWrite(LED_YELLOW, HIGH);
Serial.print(" !! MISSED "); Serial.print(seq - last - 1);
Serial.println(" sample(s) -- loop was busy, the sensor moved on");
digitalWrite(LED_YELLOW, LOW);
}
last = seq;
SensorSample_t s = hh_make_sample(seq);
if (!hh_valid(&s)) { // FILTER
digitalWrite(LED_YELLOW, HIGH);
Serial.print("seq="); Serial.print(seq); Serial.println(" FILTER: drop glitch");
delay(15); digitalWrite(LED_YELLOW, LOW);
return;
}
const char *lvl = "OK"; // ALERT
if (s.spo2_pct < 90.0f) lvl = "CRITICAL"; else if (s.temp_c >= 38.0f) lvl = "HIGH";
bool a = (lvl[0] != 'O');
digitalWrite(LED_RED, a ? HIGH : LOW);
Serial.print("seq="); Serial.print(seq); Serial.print(" ALERT: "); Serial.println(lvl);
if (a) delay(500); // heavy handling -- the loop is STUCK here, missing samples
}