From 75d7547f1163497ab5ee65aa1de8ffa50cd014d3 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 3 Oct 2020 14:07:25 +0200 Subject: [PATCH 1/2] Turnouts stored to EEPROM without trashing other stuff --- CommandStation-EX.ino | 10 ++++++-- DCCEXParser.cpp | 2 +- EEStore.cpp | 15 +++++++++++- EEStore.h | 3 +++ Turnouts.cpp | 53 +++++++++++++++++++++++++++++++++++-------- Turnouts.h | 3 +++ 6 files changed, 72 insertions(+), 14 deletions(-) diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index 5f1f87e..8dd29ba 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -11,7 +11,7 @@ #include "config.h" #include "DCCEX.h" - +#include "EEStore.h" //////////////////////////////////////////////////////////////// // @@ -32,7 +32,6 @@ DCCEXParser serialParser; void setup() { - //////////////////////////////////////////// // // More display stuff. Need to put this in a .h file and make @@ -69,6 +68,13 @@ void setup() // This is normally Serial but uses SerialUSB on a SAMD processor Serial.begin(115200); + // Responsibility 1b ;-) Load stuff from EEprom + (void)EEPROM; // tell compiler not to warn this is unused + EEStore::init(); +#ifdef EESTOREDEBUG + EEStore::dump(128); +#endif + // Start the WiFi interface on a MEGA, Uno cannot currently handle WiFi // NOTE: References to Serial1 are for the serial port used to connect // your wifi chip/shield. diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 8a96e23..5761845 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -168,9 +168,9 @@ void DCCEXParser::setAtCommandCallback(AT_COMMAND_CALLBACK callback) // See documentation on DCC class for info on this section void DCCEXParser::parse(Print *stream, byte *com, bool blocking) { + (void)EEPROM; // tell compiler not to warn this is unused if (Diag::CMD) DIAG(F("\nPARSING:%s\n"), com); - (void)EEPROM; // tell compiler not to warn thi is unused int p[MAX_PARAMS]; while (com[0] == '<' || com[0] == ' ') com++; // strip off any number of < or spaces diff --git a/EEStore.cpp b/EEStore.cpp index f52d1a1..f7bf061 100644 --- a/EEStore.cpp +++ b/EEStore.cpp @@ -2,7 +2,9 @@ #include "Turnouts.h" #include "Sensors.h" #include "Outputs.h" - +#ifdef EESTOREDEBUG +#include "DIAG.h" +#endif #if defined(ARDUINO_ARCH_SAMD) ExternalEEPROM EEPROM; @@ -72,5 +74,16 @@ int EEStore::pointer(){ } /////////////////////////////////////////////////////////////////////////////// +#ifdef EESTOREDEBUG +void EEStore::dump(int num) { + byte b; + for (int n=0 ; nactivate(state); - if(n>0) EEPROM.put(n,tt->data.tStatus); + EEStore::store(); turnoutlistHash++; return true; } - bool Turnout::isActive(int n){ +bool Turnout::isActive(int n){ Turnout * tt=get(n); if (tt==NULL) return false; return tt->data.tStatus & STATUS_ACTIVE; } // activate is virtual here so that it can be overridden by a non-DCC turnout mechanism - void Turnout::activate(bool state) { - if (state) data.tStatus|=STATUS_ACTIVE; - else data.tStatus &= ~STATUS_ACTIVE; - if (data.tStatus & STATUS_PWM) PWMServoDriver::setServo(data.tStatus & STATUS_PWMPIN, (data.inactiveAngle+(state?data.moveAngle:0))); - else DCC::setAccessory(data.address,data.subAddress, state); +void Turnout::activate(bool state) { +#ifdef EESTOREDEBUG + DIAG(F("\nTurnout::activate(%d)\n"),state); +#endif + if (state) + data.tStatus|=STATUS_ACTIVE; + else + data.tStatus &= ~STATUS_ACTIVE; + if (data.tStatus & STATUS_PWM) + PWMServoDriver::setServo(data.tStatus & STATUS_PWMPIN, (data.inactiveAngle+(state?data.moveAngle:0))); + else + DCC::setAccessory(data.address,data.subAddress, state); + EEStore::store(); } /////////////////////////////////////////////////////////////////////////////// @@ -81,6 +96,9 @@ void Turnout::load(){ else tt=create(data.id,data.address,data.subAddress); tt->data.tStatus=data.tStatus; EEStore::advance(sizeof(tt->data)); +#ifdef EESTOREDEBUG + tt->print(tt); +#endif } } @@ -93,6 +111,9 @@ void Turnout::store(){ EEStore::eeStore->data.nTurnouts=0; while(tt!=NULL){ +#ifdef EESTOREDEBUG + tt->print(tt); +#endif EEPROM.put(EEStore::pointer(),tt->data); EEStore::advance(sizeof(tt->data)); tt=tt->nextTurnout; @@ -129,7 +150,19 @@ Turnout *Turnout::create(int id){ turnoutlistHash++; return tt; } - /////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// +// print debug info about the state of a turnout +// +#ifdef EESTOREDEBUG +void Turnout::print(Turnout *tt) { + if (tt->data.tStatus & STATUS_PWM ) + DIAG(F("Turnout %d ZeroAngle %d MoveAngle %d Status %d\n"),tt->data.id, tt->data.inactiveAngle, tt->data.moveAngle,tt->data.tStatus & STATUS_ACTIVE); + else + DIAG(F("Turnout %d Addr %d Subaddr %d Status %d\n"),tt->data.id, tt->data.address, tt->data.subAddress,tt->data.tStatus & STATUS_ACTIVE); +} +#endif Turnout *Turnout::firstTurnout=NULL; int Turnout::turnoutlistHash=0; //bump on every change so clients know when to refresh their lists diff --git a/Turnouts.h b/Turnouts.h index 5c9b590..2aff97d 100644 --- a/Turnouts.h +++ b/Turnouts.h @@ -49,6 +49,9 @@ class Turnout { static Turnout *create(int id , byte pin , int activeAngle, int inactiveAngle); static Turnout *create(int id); void activate(bool state); +#ifdef EESTOREDEBUG + void print(Turnout *tt); +#endif }; // Turnout #endif From 6dc4bcdb71d9808005cf42fd1abdf18919f7f793 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 4 Oct 2020 21:20:13 +0200 Subject: [PATCH 2/2] D EEPROM command --- CommandStation-EX.ino | 8 -------- DCC.cpp | 7 ++++++- DCCEXParser.cpp | 6 ++++++ EEStore.cpp | 7 ++----- EEStore.h | 2 -- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index 8dd29ba..563bdb2 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -11,7 +11,6 @@ #include "config.h" #include "DCCEX.h" -#include "EEStore.h" //////////////////////////////////////////////////////////////// // @@ -68,13 +67,6 @@ void setup() // This is normally Serial but uses SerialUSB on a SAMD processor Serial.begin(115200); - // Responsibility 1b ;-) Load stuff from EEprom - (void)EEPROM; // tell compiler not to warn this is unused - EEStore::init(); -#ifdef EESTOREDEBUG - EEStore::dump(128); -#endif - // Start the WiFi interface on a MEGA, Uno cannot currently handle WiFi // NOTE: References to Serial1 are for the serial port used to connect // your wifi chip/shield. diff --git a/DCC.cpp b/DCC.cpp index 29fa9d7..956fea6 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -20,7 +20,7 @@ #include "DCC.h" #include "DCCWaveform.h" #include "DIAG.h" - +#include "EEStore.h" // This module is responsible for converting API calls into // messages to be sent to the waveform generator. @@ -45,6 +45,11 @@ __FlashStringHelper* DCC::shieldName=NULL; void DCC::begin(const __FlashStringHelper* motorShieldName, MotorDriver * mainDriver, MotorDriver* progDriver, byte timerNumber) { shieldName=(__FlashStringHelper*)motorShieldName; + + // Load stuff from EEprom + (void)EEPROM; // tell compiler not to warn this is unused + EEStore::init(); + DCCWaveform::begin(mainDriver,progDriver, timerNumber); } diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 5761845..2b62dc2 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -46,6 +46,7 @@ const int HASH_KEYWORD_ON = 2657; const int HASH_KEYWORD_DCC = 6436; const int HASH_KEYWORD_SLOW = -17209; const int HASH_KEYWORD_PROGBOOST = -6353; +const int HASH_KEYWORD_EEPROM = -7168; int DCCEXParser::stashP[MAX_PARAMS]; bool DCCEXParser::stashBusy; @@ -608,6 +609,11 @@ bool DCCEXParser::parseD(Print *stream, int params, int p[]) DCC::setProgTrackBoost(true); return true; + case HASH_KEYWORD_EEPROM: + if (params >= 1) + EEStore::dump(p[1]); + return true; + default: // invalid/unknown break; } diff --git a/EEStore.cpp b/EEStore.cpp index f7bf061..a3d2d55 100644 --- a/EEStore.cpp +++ b/EEStore.cpp @@ -2,9 +2,7 @@ #include "Turnouts.h" #include "Sensors.h" #include "Outputs.h" -#ifdef EESTOREDEBUG #include "DIAG.h" -#endif #if defined(ARDUINO_ARCH_SAMD) ExternalEEPROM EEPROM; @@ -74,15 +72,14 @@ int EEStore::pointer(){ } /////////////////////////////////////////////////////////////////////////////// -#ifdef EESTOREDEBUG void EEStore::dump(int num) { byte b; + DIAG(F("\nAddr 0x char\n")); for (int n=0 ; n