1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 12:51:24 +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;
case 'Q': // SENSORS <Q>
Sensor::status(stream);
break;
Sensor::checkAll();
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>
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]);
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:
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[]) {
switch(params){
case 0: // <T>
return (Turnout::showAll(stream)); break;
case 1: // <T id>
case 0: // <T> show all turnouts
{
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> delete turnout
if (!Turnout::remove(p[0])) return false;
StringFormatter::send(stream,F("<O>"));
return true;
case 2: // <T id 0|1>
if (!Turnout::activate(p[0],p[1])) return false;
Turnout::show(stream,p[0]);
case 2: // <T id 0|1> activate turnout
{
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;
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;
StringFormatter::send(stream,F("<O>"));
return true;
@ -392,18 +412,19 @@ bool DCCEXParser::parseT(Print * stream, int params, int p[]) {
bool DCCEXParser::parseS( Print * stream,int params, int p[]) {
switch(params){
case 3: // argument is string with id number of sensor followed by a pin number and pullUp indicator (0=LOW/1=HIGH)
switch(params){
case 3: // <S id pin pullup> create sensor. pullUp indicator (0=LOW/1=HIGH)
Sensor::create(p[0],p[1],p[2]);
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;
break;
case -1: // no arguments
Sensor::show(stream);
case 0: // <S> lit sensor states
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;
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 "EEStore.h"
#include "StringFormatter.h"
void Output::activate(int s){
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(){
struct OutputData data;
Output *tt;

View File

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

View File

@ -68,27 +68,23 @@ decide to ignore the <q ID> return and only react to <Q ID> triggers.
#include "Sensors.h"
#include "EEStore.h"
#include "StringFormatter.h"
///////////////////////////////////////////////////////////////////////////////
void Sensor::check(Print * stream){
Sensor *tt;
for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
void Sensor::checkAll(){
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
tt->signal=tt->signal*(1.0-SENSOR_DECAY)+digitalRead(tt->data.pin)*SENSOR_DECAY;
if(!tt->active && tt->signal<0.5){
tt->active=true;
StringFormatter::send(stream,F("<Q %d>"), tt->data.snum);
} else if(tt->active && tt->signal>0.9){
tt->active=false;
StringFormatter::send(stream,F("<q %d>"), tt->data.snum);
}
} // 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(){
struct SensorData data;
Sensor *tt;

View File

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

View File

@ -18,10 +18,7 @@
*/
#include "Turnouts.h"
#include "EEStore.h"
#include "StringFormatter.h"
#include "PWMServoDriver.h"
//#include "DIAG.h" // uncomment if you need DIAG below
bool Turnout::activate(int n,bool 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
void Turnout::activate(bool state) {
//DIAG(F("\nTurnout::activate\n"));
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)));
@ -70,31 +66,9 @@ bool Turnout::remove(int n){
free(tt);
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(){
@ -159,4 +133,3 @@ Turnout *Turnout::create(int id){
Turnout *Turnout::firstTurnout=NULL;
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 , byte pin , int activeAngle, int inactiveAngle);
static Turnout *create(int id);
static void show(Print * stream, int n);
static bool showAll(Print * stream);
void activate(bool state);
}; // Turnout