1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-23 08:06:13 +01:00

Cleaning of Turnout/Output/Sensor

PLEASE BE AWARE, I have not tested Outputs or Sensors either before or after this change.
(and they were certainly wrong before!)
This commit is contained in:
Asbelos 2020-09-06 12:44:19 +01:00
parent fd466a5e62
commit 4e0c227012
7 changed files with 49 additions and 99 deletions

View File

@ -250,8 +250,11 @@ void DCCEXParser::parse(Print * stream, byte *com, bool blocking) {
return; return;
case 'Q': // SENSORS <Q> case 'Q': // SENSORS <Q>
Sensor::status(stream); Sensor::checkAll();
break; for(Sensor * tt=Sensor::firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream,F("<%c %d>"), tt->active?'Q':'q', tt->data.snum);
}
return;
case 's': // <s> case 's': // <s>
StringFormatter::send(stream,F("<p%d>"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON ); StringFormatter::send(stream,F("<p%d>"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON );
@ -325,8 +328,14 @@ bool DCCEXParser::parseZ( Print * stream,int params, int p[]){
return Output::remove(p[0]); return Output::remove(p[0]);
case 0: // <Z> case 0: // <Z>
return Output::showAll(stream); {
bool gotone=false;
for(Output * tt=Output::firstOutput;tt!=NULL;tt=tt->nextOutput){
gotone=true;
StringFormatter::send(stream,F("<Y %d %d %d %d>"), tt->data.id, tt->data.pin, tt->data.iFlag, tt->data.oStatus);
}
return gotone;
}
default: default:
return false; return false;
} }
@ -367,20 +376,31 @@ void DCCEXParser::funcmap(int cab, byte value, byte fstart, byte fstop) {
//=================================== //===================================
bool DCCEXParser::parseT(Print * stream, int params, int p[]) { bool DCCEXParser::parseT(Print * stream, int params, int p[]) {
switch(params){ switch(params){
case 0: // <T> case 0: // <T> show all turnouts
return (Turnout::showAll(stream)); break; {
bool gotOne=false;
for(Turnout *tt=Turnout::firstTurnout;tt!=NULL;tt=tt->nextTurnout){
gotOne=true;
StringFormatter::send(stream,F("<H %d %d>"), tt->data.id, tt->data.tStatus & STATUS_ACTIVE);
}
return gotOne; // will <X> if none found
}
case 1: // <T id> case 1: // <T id> delete turnout
if (!Turnout::remove(p[0])) return false; if (!Turnout::remove(p[0])) return false;
StringFormatter::send(stream,F("<O>")); StringFormatter::send(stream,F("<O>"));
return true; return true;
case 2: // <T id 0|1> case 2: // <T id 0|1> activate turnout
if (!Turnout::activate(p[0],p[1])) return false; {
Turnout::show(stream,p[0]); Turnout* tt=Turnout::get(p[0]);
if (!tt) return false;
tt->activate(p[1]);
StringFormatter::send(stream,F("<H %d %d>"), tt->data.id, tt->data.tStatus & STATUS_ACTIVE);
}
return true; return true;
case 3: // <T id addr subaddr> case 3: // <T id addr subaddr> define turnout
if (!Turnout::create(p[0],p[1],p[2])) return false; if (!Turnout::create(p[0],p[1],p[2])) return false;
StringFormatter::send(stream,F("<O>")); StringFormatter::send(stream,F("<O>"));
return true; return true;
@ -393,17 +413,18 @@ bool DCCEXParser::parseT(Print * stream, int params, int p[]) {
bool DCCEXParser::parseS( Print * stream,int params, int p[]) { bool DCCEXParser::parseS( Print * stream,int params, int p[]) {
switch(params){ switch(params){
case 3: // <S id pin pullup> create sensor. pullUp indicator (0=LOW/1=HIGH)
case 3: // argument is string with id number of sensor followed by a pin number and pullUp indicator (0=LOW/1=HIGH)
Sensor::create(p[0],p[1],p[2]); Sensor::create(p[0],p[1],p[2]);
return true; return true;
case 1: // argument is a string with id number only case 1: // S id> remove sensor
if (Sensor::remove(p[0])) return true; if (Sensor::remove(p[0])) return true;
break; break;
case -1: // no arguments case 0: // <S> lit sensor states
Sensor::show(stream); for(Sensor * tt=Sensor::firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream, F("<Q %d %d %d>"), tt->data.snum, tt->data.pin, tt->data.pullUp);
}
return true; return true;
default: // invalid number of arguments default: // invalid number of arguments

View File

@ -83,7 +83,7 @@ the state of any outputs being monitored or controlled by a separate interface o
#include "Outputs.h" #include "Outputs.h"
#include "EEStore.h" #include "EEStore.h"
#include "StringFormatter.h"
void Output::activate(int s){ void Output::activate(int s){
data.oStatus=(s>0); // if s>0, set status to active, else inactive data.oStatus=(s>0); // if s>0, set status to active, else inactive
@ -121,23 +121,6 @@ bool Output::remove(int n){
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool Output::showAll(Print * stream){
bool gotone=false;
for(Output * tt=firstOutput;tt!=NULL;tt=tt->nextOutput){
gotone=true;
StringFormatter::send(stream,F("<Y %d %d %d %d>"), tt->data.id, tt->data.pin, tt->data.iFlag, tt->data.oStatus);
}
return gotone;
}
void Output::show(Print * stream){
for(Output * tt=firstOutput;tt!=NULL;tt=tt->nextOutput){
StringFormatter::send(stream,F("<Y %d %d>"), tt->data.id, tt->data.oStatus);
}
}
///////////////////////////////////////////////////////////////////////////////
void Output::load(){ void Output::load(){
struct OutputData data; struct OutputData data;
Output *tt; Output *tt;

View File

@ -36,14 +36,11 @@ class Output{
static void load(); static void load();
static void store(); static void store();
static Output *create(int, int, int, int=0); static Output *create(int, int, int, int=0);
static void show(Print * stream);
static bool showAll(Print * stream);
private:
static Output *firstOutput; static Output *firstOutput;
int num;
struct OutputData data; struct OutputData data;
Output *nextOutput; Output *nextOutput;
private:
int num; // Chris has no idea what this is all about!
}; // Output }; // Output

View File

@ -68,27 +68,23 @@ decide to ignore the <q ID> return and only react to <Q ID> triggers.
#include "Sensors.h" #include "Sensors.h"
#include "EEStore.h" #include "EEStore.h"
#include "StringFormatter.h"
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void Sensor::check(Print * stream){ void Sensor::checkAll(){
Sensor *tt;
for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
tt->signal=tt->signal*(1.0-SENSOR_DECAY)+digitalRead(tt->data.pin)*SENSOR_DECAY; tt->signal=tt->signal*(1.0-SENSOR_DECAY)+digitalRead(tt->data.pin)*SENSOR_DECAY;
if(!tt->active && tt->signal<0.5){ if(!tt->active && tt->signal<0.5){
tt->active=true; tt->active=true;
StringFormatter::send(stream,F("<Q %d>"), tt->data.snum);
} else if(tt->active && tt->signal>0.9){ } else if(tt->active && tt->signal>0.9){
tt->active=false; tt->active=false;
StringFormatter::send(stream,F("<q %d>"), tt->data.snum);
} }
} // loop over all sensors } // loop over all sensors
} // Sensor::check } // Sensor::checkAll
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -148,22 +144,6 @@ bool Sensor::remove(int n){
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void Sensor::show(Print * stream){
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream, F("<Q %d %d %d>"), tt->data.snum, tt->data.pin, tt->data.pullUp);
}
}
///////////////////////////////////////////////////////////////////////////////
void Sensor::status(Print * stream){
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream,F("<%c %d>"), tt->active?'Q':'q', tt->data.snum);
}
}
///////////////////////////////////////////////////////////////////////////////
void Sensor::load(){ void Sensor::load(){
struct SensorData data; struct SensorData data;
Sensor *tt; Sensor *tt;

View File

@ -40,9 +40,7 @@ struct Sensor{
static Sensor *create(int, int, int); static Sensor *create(int, int, int);
static Sensor* get(int); static Sensor* get(int);
static bool remove(int); static bool remove(int);
static void show(Print * stream); static void checkAll();
static void status(Print * stream);
static void check(Print * stream);
}; // Sensor }; // Sensor
#endif #endif

View File

@ -18,10 +18,7 @@
*/ */
#include "Turnouts.h" #include "Turnouts.h"
#include "EEStore.h" #include "EEStore.h"
#include "StringFormatter.h"
#include "PWMServoDriver.h" #include "PWMServoDriver.h"
//#include "DIAG.h" // uncomment if you need DIAG below
bool Turnout::activate(int n,bool state){ bool Turnout::activate(int n,bool state){
//DIAG(F("\nTurnout::activate(%d,%d)\n"),n,state); //DIAG(F("\nTurnout::activate(%d,%d)\n"),n,state);
@ -41,7 +38,6 @@
// 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) {
//DIAG(F("\nTurnout::activate\n"));
if (state) data.tStatus|=STATUS_ACTIVE; if (state) data.tStatus|=STATUS_ACTIVE;
else 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))); if (data.tStatus & STATUS_PWM) PWMServoDriver::setServo(data.tStatus & STATUS_PWMPIN, (data.inactiveAngle+(state?data.moveAngle:0)));
@ -71,30 +67,8 @@ bool Turnout::remove(int n){
free(tt); free(tt);
turnoutlistHash++; turnoutlistHash++;
return true; return true;
} }
///////////////////////////////////////////////////////////////////////////////
void Turnout::show(Print * stream, int n){
for(Turnout *tt=firstTurnout;tt!=NULL;tt=tt->nextTurnout){
if (tt->data.id==n) {
StringFormatter::send(stream,F("<H %d %d>"), tt->data.id, tt->data.tStatus & STATUS_ACTIVE);
return;
}
}
}
bool Turnout::showAll(Print * stream){
bool gotOne=false;
for(Turnout * tt=firstTurnout;tt!=NULL;tt=tt->nextTurnout){
StringFormatter::send(stream,F("<H %d %d %d %d>"), tt->data.id, tt->data.address, tt->data.subAddress, (tt->data.tStatus & STATUS_ACTIVE)!=0);
gotOne=true;
}
return gotOne;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void Turnout::load(){ void Turnout::load(){
@ -159,4 +133,3 @@ Turnout *Turnout::create(int id){
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

View File

@ -48,8 +48,6 @@ class Turnout {
static Turnout *create(int id , int address , int subAddress); static Turnout *create(int id , int address , int subAddress);
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);
static void show(Print * stream, int n);
static bool showAll(Print * stream);
void activate(bool state); void activate(bool state);
}; // Turnout }; // Turnout