mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-26 17:46:14 +01:00
Turnouts stored to EEPROM without trashing other stuff
This commit is contained in:
parent
1b802cc600
commit
75d7547f11
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "DCCEX.h"
|
#include "DCCEX.h"
|
||||||
|
#include "EEStore.h"
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
|
@ -32,7 +32,6 @@ DCCEXParser serialParser;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
|
||||||
////////////////////////////////////////////
|
////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// More display stuff. Need to put this in a .h file and make
|
// 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
|
// This is normally Serial but uses SerialUSB on a SAMD processor
|
||||||
Serial.begin(115200);
|
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
|
// Start the WiFi interface on a MEGA, Uno cannot currently handle WiFi
|
||||||
// NOTE: References to Serial1 are for the serial port used to connect
|
// NOTE: References to Serial1 are for the serial port used to connect
|
||||||
// your wifi chip/shield.
|
// your wifi chip/shield.
|
||||||
|
|
|
@ -168,9 +168,9 @@ void DCCEXParser::setAtCommandCallback(AT_COMMAND_CALLBACK callback)
|
||||||
// See documentation on DCC class for info on this section
|
// See documentation on DCC class for info on this section
|
||||||
void DCCEXParser::parse(Print *stream, byte *com, bool blocking)
|
void DCCEXParser::parse(Print *stream, byte *com, bool blocking)
|
||||||
{
|
{
|
||||||
|
(void)EEPROM; // tell compiler not to warn this is unused
|
||||||
if (Diag::CMD)
|
if (Diag::CMD)
|
||||||
DIAG(F("\nPARSING:%s\n"), com);
|
DIAG(F("\nPARSING:%s\n"), com);
|
||||||
(void)EEPROM; // tell compiler not to warn thi is unused
|
|
||||||
int p[MAX_PARAMS];
|
int p[MAX_PARAMS];
|
||||||
while (com[0] == '<' || com[0] == ' ')
|
while (com[0] == '<' || com[0] == ' ')
|
||||||
com++; // strip off any number of < or spaces
|
com++; // strip off any number of < or spaces
|
||||||
|
|
15
EEStore.cpp
15
EEStore.cpp
|
@ -2,7 +2,9 @@
|
||||||
#include "Turnouts.h"
|
#include "Turnouts.h"
|
||||||
#include "Sensors.h"
|
#include "Sensors.h"
|
||||||
#include "Outputs.h"
|
#include "Outputs.h"
|
||||||
|
#ifdef EESTOREDEBUG
|
||||||
|
#include "DIAG.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_SAMD)
|
#if defined(ARDUINO_ARCH_SAMD)
|
||||||
ExternalEEPROM EEPROM;
|
ExternalEEPROM EEPROM;
|
||||||
|
@ -72,5 +74,16 @@ int EEStore::pointer(){
|
||||||
}
|
}
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifdef EESTOREDEBUG
|
||||||
|
void EEStore::dump(int num) {
|
||||||
|
byte b;
|
||||||
|
for (int n=0 ; n<num; n++) {
|
||||||
|
EEPROM.get(n, b);
|
||||||
|
DIAG(F("%d %x %c\n"),n,b,isascii(b) ? b : ' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
EEStore *EEStore::eeStore=NULL;
|
EEStore *EEStore::eeStore=NULL;
|
||||||
int EEStore::eeAddress=0;
|
int EEStore::eeAddress=0;
|
||||||
|
|
|
@ -29,6 +29,9 @@ struct EEStore{
|
||||||
static void advance(int);
|
static void advance(int);
|
||||||
static void store();
|
static void store();
|
||||||
static void clear();
|
static void clear();
|
||||||
|
#ifdef EESTOREDEBUG
|
||||||
|
static void dump(int);
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
53
Turnouts.cpp
53
Turnouts.cpp
|
@ -1,5 +1,7 @@
|
||||||
/*
|
/*
|
||||||
|
* © 2013-2016 Gregg E. Berman
|
||||||
* © 2020, Chris Harlow. All rights reserved.
|
* © 2020, Chris Harlow. All rights reserved.
|
||||||
|
* © 2020, Harald Barth.
|
||||||
*
|
*
|
||||||
* This file is part of Asbelos DCC API
|
* This file is part of Asbelos DCC API
|
||||||
*
|
*
|
||||||
|
@ -19,29 +21,42 @@
|
||||||
#include "Turnouts.h"
|
#include "Turnouts.h"
|
||||||
#include "EEStore.h"
|
#include "EEStore.h"
|
||||||
#include "PWMServoDriver.h"
|
#include "PWMServoDriver.h"
|
||||||
|
#ifdef EESTOREDEBUG
|
||||||
|
#include "DIAG.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
bool Turnout::activate(int n,bool state){
|
bool Turnout::activate(int n,bool state){
|
||||||
//DIAG(F("\nTurnout::activate(%d,%d)\n"),n,state);
|
#ifdef EESTOREDEBUG
|
||||||
|
DIAG(F("\nTurnout::activate(%d,%d)\n"),n,state);
|
||||||
|
#endif
|
||||||
Turnout * tt=get(n);
|
Turnout * tt=get(n);
|
||||||
if (tt==NULL) return false;
|
if (tt==NULL) return false;
|
||||||
tt->activate(state);
|
tt->activate(state);
|
||||||
if(n>0) EEPROM.put(n,tt->data.tStatus);
|
EEStore::store();
|
||||||
turnoutlistHash++;
|
turnoutlistHash++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Turnout::isActive(int n){
|
bool Turnout::isActive(int n){
|
||||||
Turnout * tt=get(n);
|
Turnout * tt=get(n);
|
||||||
if (tt==NULL) return false;
|
if (tt==NULL) return false;
|
||||||
return tt->data.tStatus & STATUS_ACTIVE;
|
return tt->data.tStatus & STATUS_ACTIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// activate is virtual here so that it can be overridden by a non-DCC turnout mechanism
|
// activate is virtual here so that it can be overridden by a non-DCC turnout mechanism
|
||||||
void Turnout::activate(bool state) {
|
void Turnout::activate(bool state) {
|
||||||
if (state) data.tStatus|=STATUS_ACTIVE;
|
#ifdef EESTOREDEBUG
|
||||||
else data.tStatus &= ~STATUS_ACTIVE;
|
DIAG(F("\nTurnout::activate(%d)\n"),state);
|
||||||
if (data.tStatus & STATUS_PWM) PWMServoDriver::setServo(data.tStatus & STATUS_PWMPIN, (data.inactiveAngle+(state?data.moveAngle:0)));
|
#endif
|
||||||
else DCC::setAccessory(data.address,data.subAddress, 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);
|
||||||
|
EEStore::store();
|
||||||
}
|
}
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
@ -81,6 +96,9 @@ void Turnout::load(){
|
||||||
else tt=create(data.id,data.address,data.subAddress);
|
else tt=create(data.id,data.address,data.subAddress);
|
||||||
tt->data.tStatus=data.tStatus;
|
tt->data.tStatus=data.tStatus;
|
||||||
EEStore::advance(sizeof(tt->data));
|
EEStore::advance(sizeof(tt->data));
|
||||||
|
#ifdef EESTOREDEBUG
|
||||||
|
tt->print(tt);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,6 +111,9 @@ void Turnout::store(){
|
||||||
EEStore::eeStore->data.nTurnouts=0;
|
EEStore::eeStore->data.nTurnouts=0;
|
||||||
|
|
||||||
while(tt!=NULL){
|
while(tt!=NULL){
|
||||||
|
#ifdef EESTOREDEBUG
|
||||||
|
tt->print(tt);
|
||||||
|
#endif
|
||||||
EEPROM.put(EEStore::pointer(),tt->data);
|
EEPROM.put(EEStore::pointer(),tt->data);
|
||||||
EEStore::advance(sizeof(tt->data));
|
EEStore::advance(sizeof(tt->data));
|
||||||
tt=tt->nextTurnout;
|
tt=tt->nextTurnout;
|
||||||
|
@ -129,7 +150,19 @@ Turnout *Turnout::create(int id){
|
||||||
turnoutlistHash++;
|
turnoutlistHash++;
|
||||||
return tt;
|
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;
|
Turnout *Turnout::firstTurnout=NULL;
|
||||||
int Turnout::turnoutlistHash=0; //bump on every change so clients know when to refresh their lists
|
int Turnout::turnoutlistHash=0; //bump on every change so clients know when to refresh their lists
|
||||||
|
|
|
@ -49,6 +49,9 @@ class Turnout {
|
||||||
static Turnout *create(int id , byte pin , int activeAngle, int inactiveAngle);
|
static Turnout *create(int id , byte pin , int activeAngle, int inactiveAngle);
|
||||||
static Turnout *create(int id);
|
static Turnout *create(int id);
|
||||||
void activate(bool state);
|
void activate(bool state);
|
||||||
|
#ifdef EESTOREDEBUG
|
||||||
|
void print(Turnout *tt);
|
||||||
|
#endif
|
||||||
}; // Turnout
|
}; // Turnout
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user