diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 4a27514..388cf60 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -710,7 +710,7 @@ bool DCCEXParser::parseT(Print *stream, int16_t params, int16_t p[]) } else if (p[1] == HASH_KEYWORD_VPIN) { // if (params==3) { - if (VpinTurnout::create(p[0], p[2])) return false; + if (!VpinTurnout::create(p[0], p[2])) return false; } else return false; } else diff --git a/Turnouts.cpp b/Turnouts.cpp index bd5fd5b..0869a58 100644 --- a/Turnouts.cpp +++ b/Turnouts.cpp @@ -105,7 +105,7 @@ const int16_t HASH_KEYWORD_VPIN=-415; // Static activate function is invoked from close(), throw() etc. to perform the // common parts of the turnout operation. Code which is specific to a turnout - // type should be placed in the polymorphic virtual function activate(bool) which is + // type should be placed in the virtual function activate(bool) which is // called from here. bool Turnout::activate(uint16_t id, bool closeFlag) { #ifdef EESTOREDEBUG @@ -124,8 +124,7 @@ const int16_t HASH_KEYWORD_VPIN=-415; EEPROM.put(tt->_eepromAddress, tt->_turnoutData.closed); #if defined(RMFT_ACTIVE) - // TODO: Check that the inversion is correct here! - RMFT2::turnoutEvent(id, !closeFlag); + RMFT2::turnoutEvent(id, closeFlag); #endif return ok; @@ -149,10 +148,10 @@ const int16_t HASH_KEYWORD_VPIN=-415; // Load one turnout from EEPROM Turnout *Turnout::loadTurnout () { - Turnout *tt; + Turnout *tt = 0; // Read turnout type from EEPROM struct TurnoutData turnoutData; - int eepromAddress = EEStore::pointer(); // Address of byte containing the _closed flag. + int eepromAddress = EEStore::pointer(); // Address of byte containing the closed flag. EEPROM.get(EEStore::pointer(), turnoutData); EEStore::advance(sizeof(turnoutData)); @@ -169,11 +168,14 @@ const int16_t HASH_KEYWORD_VPIN=-415; // VPIN turnout tt = VpinTurnout::load(&turnoutData); break; + default: + // If we find anything else, then we don't know what it is or how long it is, + // so we can't go any further through the EEPROM! + return NULL; } if (!tt) { // Save EEPROM address in object. Note that LCN turnouts always have eepromAddress of zero. tt->_eepromAddress = eepromAddress; - add(tt); } #ifdef EESTOREDEBUG diff --git a/Turnouts.h b/Turnouts.h index 152583e..6f6c408 100644 --- a/Turnouts.h +++ b/Turnouts.h @@ -204,7 +204,7 @@ public: static Turnout *create(uint16_t id, VPIN vpin, uint16_t thrownPosition, uint16_t closedPosition, uint8_t profile, bool closed = true) { #ifndef IO_NO_HAL Turnout *tt = get(id); - if (!tt) { + if (tt) { // Object already exists, check if it is usable if (tt->isType(TURNOUT_SERVO)) { // Yes, so set parameters @@ -237,6 +237,7 @@ public: #endif } + // ServoTurnout-specific code for throwing or closing a servo turnout. bool activate(bool close) override { #ifndef IO_NO_HAL IODevice::writeAnalogue(_servoTurnoutData.vpin, @@ -301,7 +302,7 @@ public: // Create function static Turnout *create(uint16_t id, uint16_t add, uint8_t subAdd) { Turnout *tt = get(id); - if (!tt) { + if (tt) { // Object already exists, check if it is usable if (tt->isType(TURNOUT_DCC)) { // Yes, so set parameters @@ -378,7 +379,7 @@ public: // Create function static Turnout *create(uint16_t id, VPIN vpin, bool closed=true) { Turnout *tt = get(id); - if (!tt) { + if (tt) { // Object already exists, check if it is usable if (tt->isType(TURNOUT_VPIN)) { // Yes, so set parameters @@ -450,7 +451,7 @@ public: // Create function static Turnout *create(uint16_t id, bool closed=true) { Turnout *tt = get(id); - if (!tt) { + if (tt) { // Object already exists, check if it is usable if (tt->isType(TURNOUT_LCN)) { // Yes, so return this object