2020-07-03 18:35:02 +02:00
|
|
|
/*
|
2021-08-19 22:22:59 +02:00
|
|
|
* © 2021 Restructured Neil McKechnie
|
2020-10-03 14:07:25 +02:00
|
|
|
* © 2013-2016 Gregg E. Berman
|
2020-07-03 18:35:02 +02:00
|
|
|
* © 2020, Chris Harlow. All rights reserved.
|
2020-10-03 14:07:25 +02:00
|
|
|
* © 2020, Harald Barth.
|
2020-07-03 18:35:02 +02:00
|
|
|
*
|
|
|
|
* This file is part of Asbelos DCC API
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-08-18 19:55:22 +02:00
|
|
|
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
#include "defines.h"
|
2020-06-03 15:26:49 +02:00
|
|
|
#include "EEStore.h"
|
2021-01-04 16:57:03 +01:00
|
|
|
#include "StringFormatter.h"
|
2021-08-03 23:12:25 +02:00
|
|
|
#include "RMFT2.h"
|
2021-08-19 22:22:59 +02:00
|
|
|
#include "Turnouts.h"
|
2020-10-03 14:07:25 +02:00
|
|
|
#ifdef EESTOREDEBUG
|
|
|
|
#include "DIAG.h"
|
|
|
|
#endif
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
// Keywords used for turnout configuration.
|
|
|
|
const int16_t HASH_KEYWORD_SERVO=27709;
|
|
|
|
const int16_t HASH_KEYWORD_DCC=6436;
|
|
|
|
const int16_t HASH_KEYWORD_VPIN=-415;
|
|
|
|
|
2021-08-18 19:55:22 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
/*
|
|
|
|
* Protected static data
|
|
|
|
*/
|
2021-08-18 19:55:22 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
Turnout *Turnout::_firstTurnout = 0;
|
2021-08-18 19:55:22 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
/*
|
|
|
|
* Public static data
|
|
|
|
*/
|
|
|
|
int Turnout::turnoutlistHash = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Protected static functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
Turnout *Turnout::get(uint16_t id) {
|
|
|
|
// Find turnout object from list.
|
|
|
|
for (Turnout *tt = _firstTurnout; tt != NULL; tt = tt->_nextTurnout)
|
|
|
|
if (tt->_turnoutData.id == id) return tt;
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
// Add new turnout to end of chain
|
|
|
|
void Turnout::add(Turnout *tt) {
|
|
|
|
if (!_firstTurnout)
|
|
|
|
_firstTurnout = tt;
|
|
|
|
else {
|
|
|
|
// Find last object on chain
|
|
|
|
Turnout *ptr = _firstTurnout;
|
|
|
|
for ( ; ptr->_nextTurnout!=0; ptr=ptr->_nextTurnout) {}
|
|
|
|
// Line new object to last object.
|
|
|
|
ptr->_nextTurnout = tt;
|
|
|
|
}
|
|
|
|
turnoutlistHash++;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
2021-08-19 22:22:59 +02:00
|
|
|
|
|
|
|
// Remove nominated turnout from turnout linked list and delete the object.
|
|
|
|
bool Turnout::remove(uint16_t id) {
|
|
|
|
Turnout *tt,*pp=NULL;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
for(tt=_firstTurnout; tt!=NULL && tt->_turnoutData.id!=id; pp=tt, tt=tt->_nextTurnout) {}
|
|
|
|
if (tt == NULL) return false;
|
|
|
|
|
|
|
|
if (tt == _firstTurnout)
|
|
|
|
_firstTurnout = tt->_nextTurnout;
|
|
|
|
else
|
|
|
|
pp->_nextTurnout = tt->_nextTurnout;
|
2021-08-18 19:55:22 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
delete (ServoTurnout *)tt;
|
2021-08-18 19:55:22 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
turnoutlistHash++;
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
/*
|
|
|
|
* Public static functions
|
|
|
|
*/
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
bool Turnout::isClosed(uint16_t id) {
|
|
|
|
Turnout *tt = get(id);
|
|
|
|
if (tt)
|
|
|
|
return tt->isClosed();
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
// 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
|
|
|
|
// called from here.
|
|
|
|
bool Turnout::activate(uint16_t id, bool closeFlag) {
|
|
|
|
#ifdef EESTOREDEBUG
|
|
|
|
if (closeFlag)
|
|
|
|
DIAG(F("Turnout::close(%d)"), id);
|
|
|
|
else
|
|
|
|
DIAG(F("Turnout::throw(%d)"), id);
|
|
|
|
#endif
|
|
|
|
Turnout *tt = Turnout::get(id);
|
|
|
|
if (!tt) return false;
|
|
|
|
bool ok = tt->activate(closeFlag);
|
|
|
|
|
|
|
|
// Write new closed/thrown state to EEPROM if required. Note that eepromAddress
|
|
|
|
// is always zero for LCN turnouts.
|
|
|
|
if (EEStore::eeStore->data.nTurnouts > 0 && tt->_eepromAddress)
|
|
|
|
EEPROM.put(tt->_eepromAddress, tt->_turnoutData.closed);
|
|
|
|
|
|
|
|
#if defined(RMFT_ACTIVE)
|
|
|
|
// TODO: Check that the inversion is correct here!
|
|
|
|
RMFT2::turnoutEvent(id, !closeFlag);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
// Load all turnout objects
|
|
|
|
void Turnout::load() {
|
|
|
|
for (uint16_t i=0; i<EEStore::eeStore->data.nTurnouts; i++) {
|
|
|
|
Turnout::loadTurnout();
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
// Save all turnout objects
|
|
|
|
void Turnout::store() {
|
|
|
|
EEStore::eeStore->data.nTurnouts=0;
|
|
|
|
for (Turnout *tt = _firstTurnout; tt != 0; tt = tt->_nextTurnout) {
|
|
|
|
tt->save();
|
|
|
|
EEStore::eeStore->data.nTurnouts++;
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 18:34:35 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
// Load one turnout from EEPROM
|
|
|
|
Turnout *Turnout::loadTurnout () {
|
|
|
|
Turnout *tt;
|
|
|
|
// Read turnout type from EEPROM
|
|
|
|
struct TurnoutData turnoutData;
|
|
|
|
int eepromAddress = EEStore::pointer(); // Address of byte containing the _closed flag.
|
|
|
|
EEPROM.get(EEStore::pointer(), turnoutData);
|
|
|
|
EEStore::advance(sizeof(turnoutData));
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2021-08-19 22:22:59 +02:00
|
|
|
switch (turnoutData.turnoutType) {
|
2021-08-03 23:12:25 +02:00
|
|
|
case TURNOUT_SERVO:
|
2021-08-19 22:22:59 +02:00
|
|
|
// Servo turnout
|
|
|
|
tt = ServoTurnout::load(&turnoutData);
|
2021-08-18 00:41:34 +02:00
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
case TURNOUT_DCC:
|
2021-08-19 22:22:59 +02:00
|
|
|
// DCC Accessory turnout
|
|
|
|
tt = DCCTurnout::load(&turnoutData);
|
2021-08-03 23:12:25 +02:00
|
|
|
break;
|
|
|
|
case TURNOUT_VPIN:
|
2021-08-19 22:22:59 +02:00
|
|
|
// VPIN turnout
|
|
|
|
tt = VpinTurnout::load(&turnoutData);
|
2021-08-03 23:12:25 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-08-19 22:22:59 +02:00
|
|
|
if (!tt) {
|
|
|
|
// Save EEPROM address in object. Note that LCN turnouts always have eepromAddress of zero.
|
|
|
|
tt->_eepromAddress = eepromAddress;
|
|
|
|
add(tt);
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 14:07:25 +02:00
|
|
|
#ifdef EESTOREDEBUG
|
2021-08-19 22:22:59 +02:00
|
|
|
printAll(&Serial);
|
2020-10-03 14:07:25 +02:00
|
|
|
#endif
|
2021-08-19 22:22:59 +02:00
|
|
|
return tt;
|
|
|
|
}
|
2020-06-03 15:26:49 +02:00
|
|
|
|