From 9099af31883f6fed1341a9544fa833be78609594 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 25 Oct 2020 23:52:22 +0100 Subject: [PATCH] Sensors show their status at change (as in classic) --- DCCEXParser.cpp | 8 +++----- Sensors.cpp | 26 ++++++++++++++++++++++++-- Sensors.h | 3 ++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index ece2842..e958ce5 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -96,6 +96,7 @@ void DCCEXParser::loop(Stream &stream) buffer[bufferLength++] = ch; } } + Sensor::checkAll(&stream); // Update and print changes } int DCCEXParser::splitValues(int result[MAX_PARAMS], const byte *cmd) @@ -359,11 +360,8 @@ void DCCEXParser::parse(Print *stream, byte *com, bool blocking) return; case 'Q': // SENSORS - Sensor::checkAll(); - for (Sensor *tt = Sensor::firstSensor; tt != NULL; tt = tt->nextSensor) - { - StringFormatter::send(stream, F("<%c %d>"), tt->active ? 'Q' : 'q', tt->data.snum); - } + Sensor::checkAll(NULL); // Update, don't print changes + Sensor::printAll(stream); // Print all return; case 's': // diff --git a/Sensors.cpp b/Sensors.cpp index 3f55c5f..e0913a2 100644 --- a/Sensors.cpp +++ b/Sensors.cpp @@ -65,27 +65,49 @@ decide to ignore the return and only react to triggers. **********************************************************************/ - +#include "StringFormatter.h" #include "Sensors.h" #include "EEStore.h" +/////////////////////////////////////////////////////////////////////////////// +// +// checks all defined sensors and prints _changed_ sensor states +// to stream unless stream is NULL in which case only internal +// state is updated +// /////////////////////////////////////////////////////////////////////////////// -void Sensor::checkAll(){ +void Sensor::checkAll(Print *stream){ for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ tt->signal=tt->signal*(1.0-SENSOR_DECAY)+digitalRead(tt->data.pin)*SENSOR_DECAY; if(!tt->active && tt->signal<0.5){ tt->active=true; + if (stream != NULL) StringFormatter::send(stream, F(""), tt->data.snum); } else if(tt->active && tt->signal>0.9){ tt->active=false; + if (stream != NULL) StringFormatter::send(stream, F(""), tt->data.snum); } } // loop over all sensors } // Sensor::checkAll +/////////////////////////////////////////////////////////////////////////////// +// +// prints all sensor states to stream +// +/////////////////////////////////////////////////////////////////////////////// + +void Sensor::printAll(Print *stream){ + + for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ + if (stream != NULL) + StringFormatter::send(stream, F("<%c %d>"), tt->active ? 'Q' : 'q', tt->data.snum); + } // loop over all sensors +} // Sensor::printAll + /////////////////////////////////////////////////////////////////////////////// Sensor *Sensor::create(int snum, int pin, int pullUp){ diff --git a/Sensors.h b/Sensors.h index 71f60ba..717da69 100644 --- a/Sensors.h +++ b/Sensors.h @@ -40,7 +40,8 @@ struct Sensor{ static Sensor *create(int, int, int); static Sensor* get(int); static bool remove(int); - static void checkAll(); + static void checkAll(Print *); + static void printAll(Print *); }; // Sensor #endif