2020-07-03 18:35:02 +02:00
|
|
|
/*
|
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/>.
|
|
|
|
*/
|
2020-06-03 15:26:49 +02:00
|
|
|
#include "Turnouts.h"
|
|
|
|
#include "EEStore.h"
|
2020-06-23 21:01:43 +02:00
|
|
|
#include "PWMServoDriver.h"
|
2021-01-04 16:57:03 +01:00
|
|
|
#include "StringFormatter.h"
|
2020-10-03 14:07:25 +02:00
|
|
|
#ifdef EESTOREDEBUG
|
|
|
|
#include "DIAG.h"
|
|
|
|
#endif
|
|
|
|
|
2021-01-04 16:57:03 +01:00
|
|
|
// print all turnout states to stream
|
|
|
|
void Turnout::printAll(Print *stream){
|
|
|
|
for (Turnout *tt = Turnout::firstTurnout; tt != NULL; tt = tt->nextTurnout)
|
|
|
|
StringFormatter::send(stream, F("<H %d %d>"), tt->data.id, (tt->data.tStatus & STATUS_ACTIVE)!=0);
|
|
|
|
} // Turnout::printAll
|
|
|
|
|
2020-10-03 14:07:25 +02:00
|
|
|
bool Turnout::activate(int n,bool state){
|
|
|
|
#ifdef EESTOREDEBUG
|
|
|
|
DIAG(F("\nTurnout::activate(%d,%d)\n"),n,state);
|
|
|
|
#endif
|
2020-06-03 15:26:49 +02:00
|
|
|
Turnout * tt=get(n);
|
|
|
|
if (tt==NULL) return false;
|
2020-06-22 12:58:33 +02:00
|
|
|
tt->activate(state);
|
2020-10-03 14:07:25 +02:00
|
|
|
EEStore::store();
|
2020-08-20 13:38:09 +02:00
|
|
|
turnoutlistHash++;
|
2020-06-03 15:26:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-03 14:07:25 +02:00
|
|
|
bool Turnout::isActive(int n){
|
2020-07-23 18:34:35 +02:00
|
|
|
Turnout * tt=get(n);
|
|
|
|
if (tt==NULL) return false;
|
|
|
|
return tt->data.tStatus & STATUS_ACTIVE;
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:58:33 +02:00
|
|
|
// activate is virtual here so that it can be overridden by a non-DCC turnout mechanism
|
2020-10-03 14:07:25 +02:00
|
|
|
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();
|
2020-06-22 12:58:33 +02:00
|
|
|
}
|
2020-06-03 15:26:49 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Turnout* Turnout::get(int n){
|
|
|
|
Turnout *tt;
|
|
|
|
for(tt=firstTurnout;tt!=NULL && tt->data.id!=n;tt=tt->nextTurnout);
|
|
|
|
return(tt);
|
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool Turnout::remove(int n){
|
|
|
|
Turnout *tt,*pp=NULL;
|
|
|
|
|
|
|
|
for(tt=firstTurnout;tt!=NULL && tt->data.id!=n;pp=tt,tt=tt->nextTurnout);
|
|
|
|
|
|
|
|
if(tt==NULL) return false;
|
|
|
|
|
|
|
|
if(tt==firstTurnout)
|
|
|
|
firstTurnout=tt->nextTurnout;
|
|
|
|
else
|
|
|
|
pp->nextTurnout=tt->nextTurnout;
|
|
|
|
|
|
|
|
free(tt);
|
2020-08-20 13:38:09 +02:00
|
|
|
turnoutlistHash++;
|
2020-09-06 13:44:19 +02:00
|
|
|
return true;
|
2020-06-03 15:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void Turnout::load(){
|
|
|
|
struct TurnoutData data;
|
|
|
|
Turnout *tt;
|
|
|
|
|
|
|
|
for(int i=0;i<EEStore::eeStore->data.nTurnouts;i++){
|
|
|
|
EEPROM.get(EEStore::pointer(),data);
|
2020-06-23 18:43:50 +02:00
|
|
|
if (data.tStatus & STATUS_PWM) tt=create(data.id,data.tStatus & STATUS_PWMPIN, data.inactiveAngle,data.moveAngle);
|
|
|
|
else tt=create(data.id,data.address,data.subAddress);
|
2020-06-03 15:26:49 +02:00
|
|
|
tt->data.tStatus=data.tStatus;
|
|
|
|
EEStore::advance(sizeof(tt->data));
|
2020-10-03 14:07:25 +02:00
|
|
|
#ifdef EESTOREDEBUG
|
|
|
|
tt->print(tt);
|
|
|
|
#endif
|
2020-06-03 15:26:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void Turnout::store(){
|
|
|
|
Turnout *tt;
|
|
|
|
|
|
|
|
tt=firstTurnout;
|
|
|
|
EEStore::eeStore->data.nTurnouts=0;
|
|
|
|
|
|
|
|
while(tt!=NULL){
|
2020-10-03 14:07:25 +02:00
|
|
|
#ifdef EESTOREDEBUG
|
|
|
|
tt->print(tt);
|
|
|
|
#endif
|
2020-06-03 15:26:49 +02:00
|
|
|
EEPROM.put(EEStore::pointer(),tt->data);
|
|
|
|
EEStore::advance(sizeof(tt->data));
|
|
|
|
tt=tt->nextTurnout;
|
|
|
|
EEStore::eeStore->data.nTurnouts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Turnout *Turnout::create(int id, int add, int subAdd){
|
2020-06-23 18:43:50 +02:00
|
|
|
Turnout *tt=create(id);
|
2020-06-03 15:26:49 +02:00
|
|
|
tt->data.address=add;
|
|
|
|
tt->data.subAddress=subAdd;
|
|
|
|
tt->data.tStatus=0;
|
|
|
|
return(tt);
|
2020-06-23 18:43:50 +02:00
|
|
|
}
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-06-23 18:43:50 +02:00
|
|
|
Turnout *Turnout::create(int id, byte pin, int activeAngle, int inactiveAngle){
|
|
|
|
Turnout *tt=create(id);
|
|
|
|
tt->data.tStatus= STATUS_PWM | (pin & STATUS_PWMPIN);
|
|
|
|
tt->data.inactiveAngle=inactiveAngle;
|
|
|
|
tt->data.moveAngle=activeAngle-inactiveAngle;
|
|
|
|
return(tt);
|
2020-06-03 15:26:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:43:50 +02:00
|
|
|
Turnout *Turnout::create(int id){
|
|
|
|
Turnout *tt=get(id);
|
|
|
|
if (tt==NULL) {
|
|
|
|
tt=(Turnout *)calloc(1,sizeof(Turnout));
|
|
|
|
tt->nextTurnout=firstTurnout;
|
|
|
|
firstTurnout=tt;
|
|
|
|
tt->data.id=id;
|
|
|
|
}
|
2020-08-20 13:38:09 +02:00
|
|
|
turnoutlistHash++;
|
2020-06-23 18:43:50 +02:00
|
|
|
return tt;
|
|
|
|
}
|
2020-10-03 14:07:25 +02:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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
|
2020-06-03 15:26:49 +02:00
|
|
|
|
|
|
|
Turnout *Turnout::firstTurnout=NULL;
|
2020-08-20 13:38:09 +02:00
|
|
|
int Turnout::turnoutlistHash=0; //bump on every change so clients know when to refresh their lists
|