From 940d7d70e28e93debf7fc9e61e8486b8a3df64a2 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Thu, 24 Jul 2025 15:49:20 +0100 Subject: [PATCH] Low ram sensor groups --- EXRAILMacros.h | 20 +++++++++++++++++- SensorGroup.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ SensorGroup.h | 24 ++++++++++++++++++++++ Sensors.cpp | 16 +++++++++------ 4 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 SensorGroup.cpp create mode 100644 SensorGroup.h diff --git a/EXRAILMacros.h b/EXRAILMacros.h index 0d6a7e2..e2eb3f0 100644 --- a/EXRAILMacros.h +++ b/EXRAILMacros.h @@ -113,11 +113,29 @@ bool exrailHalSetup1() { // the first pass from the opcode table. #include "EXRAIL2MacroReset.h" #undef JMRI_SENSOR -#define JMRI_SENSOR(vpin,count...) Sensor::createMultiple(vpin,##count); +#define JMRI_SENSOR(vpin,count...) \ + { \ + const int npins=#count[0]? count+0:1; \ + static byte state_map[(npins+7)/8]; \ + SensorGroup::doSensorGroup(vpin,npins,state_map,action,&USB_SERIAL); \ + } + +void SensorGroup::doExrailSensorGroup(GroupProcess action, Print * stream) { + (void) action; // suppress unused warning + (void) stream; // suppress unused warning + #include "myAutomation.h" +} + +// Pass 1s Implements servos by creating exrailHalSetup2 +// TODO Turnout and turntable creation should be moved to here instead of +// the first pass from the opcode table. +#include "EXRAIL2MacroReset.h" #undef CONFIGURE_SERVO #define CONFIGURE_SERVO(vpin,pos1,pos2,profile) IODevice::configureServo(vpin,pos1,pos2,PCA9685::profile); void exrailHalSetup2() { #include "myAutomation.h" + // pullup any group sensors + SensorGroup::pullupAll(); } // Pass 1c detect compile time featurtes diff --git a/SensorGroup.cpp b/SensorGroup.cpp new file mode 100644 index 0000000..92fc64b --- /dev/null +++ b/SensorGroup.cpp @@ -0,0 +1,54 @@ +#include "SensorGroup.h" + +// called in loop to check sensors +void SensorGroup::checkAll() { + #ifdef EXRAIL_ACTIVE + doExrailSensorGroup(GroupProcess::CHECK, & USB_SERIAL); + #endif +} + +// called by command to get sensor list +void SensorGroup::printAll(Print * serial) { + #ifdef EXRAIL_ACTIVE + doExrailSensorGroup(GroupProcess::PRINT,serial); + #endif +} + +void SensorGroup::pullupAll() { + #ifdef EXRAIL_ACTIVE + doExrailSensorGroup(GroupProcess::PULLUP, & USB_SERIAL); + #endif +} + +// called by EXRAIL constructed doExrailSensorGroup for each group +void SensorGroup::doSensorGroup(VPIN firstVpin, int nPins, byte* statebits, + GroupProcess action, Print * serial) { + + // Loop through the pins in the group + for (auto i=0;i\n"), + (statebits[stateByte]&stateMask)?'Q':'q', firstVpin+i); + break; + } + } +} + \ No newline at end of file diff --git a/SensorGroup.h b/SensorGroup.h new file mode 100644 index 0000000..a93defc --- /dev/null +++ b/SensorGroup.h @@ -0,0 +1,24 @@ +#ifndef SensorGroup_h +#define SensorGroup_h +#include +#include "defines.h" +#include "IODevice.h" +#include "StringFormatter.h" + + +// reference to the optional exrail built function which contains the +// calls to SensorGroup::doSensorGroup + +enum GroupProcess:byte {PULLUP,PRINT,CHECK}; + +class SensorGroup { + public: + static void checkAll(); + static void printAll(Print * serial); + static void pullupAll(); + static void doSensorGroup(VPIN vpin, int nPins, byte* statebits, + GroupProcess action, Print * serial); + private: + static void doExrailSensorGroup(GroupProcess action, Print * stream); +}; +#endif // SensorGroup_h diff --git a/Sensors.cpp b/Sensors.cpp index efd969d..760e138 100644 --- a/Sensors.cpp +++ b/Sensors.cpp @@ -91,6 +91,9 @@ decide to ignore the return and only react to triggers. /////////////////////////////////////////////////////////////////////////////// void Sensor::checkAll(){ + + SensorGroup::checkAll(); + uint16_t sensorCount = 0; #ifdef USE_NOTIFY @@ -181,12 +184,13 @@ void Sensor::inputChangeCallback(VPIN vpin, int state) { /////////////////////////////////////////////////////////////////////////////// void Sensor::printAll(Print *stream){ - - if (stream != NULL) { - for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ - StringFormatter::send(stream, F("<%c %d>\n"), tt->active ? 'Q' : 'q', tt->data.snum); - } - } // loop over all sensors + + if (stream == NULL) return; // Nothing to do + + SensorGroup::printAll(stream); + for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ + StringFormatter::send(stream, F("<%c %d>\n"), tt->active ? 'Q' : 'q', tt->data.snum); + } } // Sensor::printAll ///////////////////////////////////////////////////////////////////////////////