2 Commits d73a395744 ... 8649fd6e6d

Autor SHA1 Mensaje Fecha
  Daniel Sheffield 8649fd6e6d write out sentinal value if invalid value hace 1 mes
  Daniel Sheffield 2903ad6c28 refactor some AI slop hace 1 mes

+ 6 - 9
hello-world/sonic-HC-SR04/sonic-HC-SR04.ino

@@ -3,6 +3,8 @@
 
 const char * sensorName = "SR04";
 const int dp = 2;
+#define SENTINEL -1.0f
+
 /*
  * Arduino UNO:
  *
@@ -13,6 +15,7 @@ const int dp = 2;
  *
  */
 LiquidCrystal_I2C lcd(0x27, 16, 2);
+SensorEMA_t sensorCtx;
 
 
 // HC-SR04 Pins
@@ -39,7 +42,7 @@ void my_init() {
 float readDistance() {
   // Enforce minimum 60ms delay between readings to prevent ghost echoes
   if (millis() - lastRead < 60) {
-    return -1.0; // Return invalid flag if called too soon
+    return SENTINEL;
   }
 
   // Clear trigPin
@@ -59,7 +62,7 @@ float readDistance() {
   
   // Handle timeout (no echo received)
   if (duration == 0) {
-    return -1.0; // Return invalid flag
+    return SENTINEL;
   }
   
   // Calculate distance: (duration * speed of sound) / 2
@@ -74,20 +77,14 @@ void my_write(float val) {
     lcd.print("   ");
 }
 
-// --- Global Context ---
-SensorEMA_t sensorCtx;
 
 void setup() {
-    // Assign function pointers
     sensorCtx.fn_init = my_init;
     sensorCtx.fn_read = readDistance;
     sensorCtx.fn_write = my_write;
-
-    // Initialize Library (Alpha=0.1, Interval=2000ms)
-    SensorEMA_setup(&sensorCtx, 0.1, 2000);
+    SensorEMA_setup(&sensorCtx, 0.1, 2000, SENTINEL);
 }
 
 void loop() {
-    // Call library loop as fast as possible
     SensorEMA_loop(&sensorCtx, millis());
 }

+ 16 - 26
lib/sensor_ema/sensor_ema.cpp

@@ -1,25 +1,16 @@
+#include <math.h>
 #include "sensor_ema.h"
-
-void SensorEMA_setup(SensorEMA_t* ctx, float alpha, unsigned long interval_ms) {
+void SensorEMA_setup(SensorEMA_t* ctx, float alpha, unsigned long interval_ms, float sentinel)
+{
     ctx->alpha = alpha;
-    ctx->update_interval_ms = interval_ms;
-    ctx->current_ema = 0.0;
-    ctx->last_update_time = 0;
-    ctx->has_valid_data = false;
+    ctx->interval_ms = interval_ms;
+    ctx->ema = 0.0;
+    ctx->last_write = 0;
+    ctx->sentinel = sentinel;
 
-    // Run user init if provided
     if (ctx->fn_init) {
         ctx->fn_init();
     }
-
-    // Seed EMA with first reading if possible
-    if (ctx->fn_read) {
-        float val = ctx->fn_read();
-        if (val >= 0) {
-            ctx->current_ema = val;
-            ctx->has_valid_data = true;
-        }
-    }
 }
 
 void SensorEMA_loop(SensorEMA_t* ctx, unsigned long  now) {
@@ -27,17 +18,16 @@ void SensorEMA_loop(SensorEMA_t* ctx, unsigned long  now) {
 
     float raw = ctx->fn_read();
 
-    if (raw >= 0.0) {
-        if (!ctx->has_valid_data) {
-            ctx->current_ema = raw;
-            ctx->has_valid_data = true;
-        } else {
-            ctx->current_ema = (raw * ctx->alpha) + (ctx->current_ema * (1.0 - ctx->alpha));
-        }
+    bool invalid = isnan(ctx->sentinel) ? isnan(raw) : raw == ctx->sentinel;
+
+    if (!invalid)
+    {
+        ctx->ema = (raw * ctx->alpha) + (ctx->ema * (1.0 - ctx->alpha));
     }
 
-    if (now - ctx->last_update_time >= ctx->update_interval_ms) {
-        ctx->last_update_time = now;
-        ctx->fn_write(ctx->current_ema);
+    if (now - ctx->last_write >= ctx->interval_ms) {
+        ctx->last_write = now;
+        ctx->fn_write(invalid ? ctx->sentinel : ctx->ema);
     }
 }
+

+ 6 - 5
lib/sensor_ema/sensor_ema.h

@@ -10,11 +10,12 @@ typedef void (*ema_write_func_t)(float val);
 
 typedef struct {
     float alpha;
-    unsigned long update_interval_ms;
+    unsigned long interval_ms;
 
-    float current_ema;
-    unsigned long last_update_time;
-    bool has_valid_data;
+    float ema;
+    float sentinel;
+
+    unsigned long last_write;
 
     ema_init_func_t fn_init;
     ema_read_func_t fn_read;
@@ -23,7 +24,7 @@ typedef struct {
 } SensorEMA_t;
 
 // XXX: Should we pass function pointers in?
-void SensorEMA_setup(SensorEMA_t* ctx, float alpha, unsigned long interval_ms);
+void SensorEMA_setup(SensorEMA_t* ctx, float alpha, unsigned long interval_ms, float sentinel);
 
 void SensorEMA_loop(SensorEMA_t* ctx, unsigned long now);