2020-07-03 18:35:02 +02:00
|
|
|
/*
|
|
|
|
* © 2020, Chris Harlow. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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-10 18:31:26 +02:00
|
|
|
#include "StringFormatter.h"
|
2020-06-04 21:55:18 +02:00
|
|
|
#include "DCCEXParser.h"
|
2020-05-25 14:38:18 +02:00
|
|
|
#include "DCC.h"
|
|
|
|
#include "DCCWaveform.h"
|
2020-08-01 17:32:16 +02:00
|
|
|
#include "WifiInterface.h"
|
2020-06-03 15:26:49 +02:00
|
|
|
#include "Turnouts.h"
|
|
|
|
#include "Outputs.h"
|
|
|
|
#include "Sensors.h"
|
2020-09-10 14:09:32 +02:00
|
|
|
#include "freeMemory.h"
|
2020-06-03 15:26:49 +02:00
|
|
|
|
|
|
|
#include "EEStore.h"
|
2020-06-11 14:35:16 +02:00
|
|
|
#include "DIAG.h"
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-08-28 15:44:06 +02:00
|
|
|
const char VERSION[] PROGMEM ="0.1.9";
|
2020-05-25 20:59:47 +02:00
|
|
|
|
2020-07-30 16:34:56 +02:00
|
|
|
// These keywords are used in the <1> command. The number is what you get if you use the keyword as a parameter.
|
|
|
|
// To discover new keyword numbers , use the <$ YOURKEYWORD> command
|
2020-06-19 10:19:41 +02:00
|
|
|
const int HASH_KEYWORD_PROG=-29718;
|
|
|
|
const int HASH_KEYWORD_MAIN=11339;
|
2020-07-30 16:34:56 +02:00
|
|
|
const int HASH_KEYWORD_JOIN=-30750;
|
2020-09-10 14:09:32 +02:00
|
|
|
const int HASH_KEYWORD_CABS=-11981;
|
|
|
|
const int HASH_KEYWORD_RAM=25982;
|
|
|
|
const int HASH_KEYWORD_CMD=9962;
|
|
|
|
const int HASH_KEYWORD_WIT=31594;
|
|
|
|
const int HASH_KEYWORD_WIFI=-5583;
|
|
|
|
const int HASH_KEYWORD_ACK=3113;
|
|
|
|
const int HASH_KEYWORD_ON=2657;
|
2020-09-10 18:11:50 +02:00
|
|
|
const int HASH_KEYWORD_DCC=6436;
|
|
|
|
const int HASH_KEYWORD_SLOW=-17209;
|
2020-07-30 16:34:56 +02:00
|
|
|
|
2020-06-19 10:19:41 +02:00
|
|
|
|
2020-06-10 18:31:26 +02:00
|
|
|
int DCCEXParser::stashP[MAX_PARAMS];
|
2020-06-11 14:35:16 +02:00
|
|
|
bool DCCEXParser::stashBusy;
|
|
|
|
|
2020-08-01 17:32:16 +02:00
|
|
|
Print * DCCEXParser::stashStream=NULL;
|
2020-06-10 18:31:26 +02:00
|
|
|
|
|
|
|
// This is a JMRI command parser, one instance per incoming stream
|
2020-05-25 14:38:18 +02:00
|
|
|
// It doesnt know how the string got here, nor how it gets back.
|
|
|
|
// It knows nothing about hardware or tracks... it just parses strings and
|
|
|
|
// calls the corresponding DCC api.
|
2020-06-03 15:26:49 +02:00
|
|
|
// Non-DCC things like turnouts, pins and sensors are handled in additional JMRI interface classes.
|
|
|
|
|
2020-06-11 14:35:16 +02:00
|
|
|
DCCEXParser::DCCEXParser() {}
|
2020-06-12 15:28:35 +02:00
|
|
|
void DCCEXParser::flush() {
|
2020-09-10 14:09:32 +02:00
|
|
|
if (Diag::CMD) DIAG(F("\nBuffer flush"));
|
2020-06-12 15:28:35 +02:00
|
|
|
bufferLength=0;
|
|
|
|
inCommandPayload=false;
|
|
|
|
}
|
2020-06-11 14:35:16 +02:00
|
|
|
|
|
|
|
void DCCEXParser::loop(Stream & stream) {
|
2020-06-12 15:28:35 +02:00
|
|
|
while(stream.available()) {
|
2020-06-10 18:31:26 +02:00
|
|
|
if (bufferLength==MAX_BUFFER) {
|
2020-06-12 15:28:35 +02:00
|
|
|
flush();
|
2020-06-10 18:31:26 +02:00
|
|
|
}
|
|
|
|
char ch = stream.read();
|
|
|
|
if (ch == '<') {
|
|
|
|
inCommandPayload = true;
|
|
|
|
bufferLength=0;
|
|
|
|
buffer[0]='\0';
|
|
|
|
}
|
|
|
|
else if (ch == '>') {
|
|
|
|
buffer[bufferLength]='\0';
|
2020-08-01 17:32:16 +02:00
|
|
|
parse( & stream, buffer, false); // Parse this allowing async responses
|
2020-06-10 18:31:26 +02:00
|
|
|
inCommandPayload = false;
|
|
|
|
break;
|
|
|
|
} else if(inCommandPayload) {
|
|
|
|
buffer[bufferLength++]= ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:37:05 +02:00
|
|
|
int DCCEXParser::splitValues( int result[MAX_PARAMS], const byte * cmd) {
|
2020-06-10 18:31:26 +02:00
|
|
|
byte state=1;
|
|
|
|
byte parameterCount=0;
|
|
|
|
int runningValue=0;
|
2020-06-29 12:37:05 +02:00
|
|
|
const byte * remainingCmd=cmd+1; // skips the opcode
|
2020-06-10 18:31:26 +02:00
|
|
|
bool signNegative=false;
|
|
|
|
|
|
|
|
// clear all parameters in case not enough found
|
|
|
|
for (int i=0;i<MAX_PARAMS;i++) result[i]=0;
|
|
|
|
|
|
|
|
while(parameterCount<MAX_PARAMS) {
|
2020-06-29 12:37:05 +02:00
|
|
|
byte hot=*remainingCmd;
|
2020-06-10 18:31:26 +02:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
|
|
|
case 1: // skipping spaces before a param
|
|
|
|
if (hot==' ') break;
|
|
|
|
if (hot == '\0' || hot=='>') return parameterCount;
|
|
|
|
state=2;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 2: // checking sign
|
|
|
|
signNegative=false;
|
|
|
|
runningValue=0;
|
|
|
|
state=3;
|
|
|
|
if (hot!='-') continue;
|
|
|
|
signNegative=true;
|
|
|
|
break;
|
|
|
|
case 3: // building a parameter
|
|
|
|
if (hot>='0' && hot<='9') {
|
|
|
|
runningValue=10*runningValue+(hot-'0');
|
|
|
|
break;
|
|
|
|
}
|
2020-06-19 10:19:41 +02:00
|
|
|
if (hot>='A' && hot<='Z') {
|
|
|
|
// Since JMRI got modified to send keywords in some rare cases, we need this
|
|
|
|
// Super Kluge to turn keywords into a hash value that can be recognised later
|
|
|
|
runningValue = ((runningValue << 5) + runningValue) ^ hot;
|
|
|
|
break;
|
|
|
|
}
|
2020-06-10 18:31:26 +02:00
|
|
|
result[parameterCount] = runningValue * (signNegative ?-1:1);
|
|
|
|
parameterCount++;
|
|
|
|
state=1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
remainingCmd++;
|
|
|
|
}
|
|
|
|
return parameterCount;
|
|
|
|
}
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-18 20:36:37 +02:00
|
|
|
FILTER_CALLBACK DCCEXParser::filterCallback=0;
|
|
|
|
void DCCEXParser::setFilter(FILTER_CALLBACK filter) {
|
|
|
|
filterCallback=filter;
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:38:18 +02:00
|
|
|
// See documentation on DCC class for info on this section
|
2020-08-01 17:32:16 +02:00
|
|
|
void DCCEXParser::parse(Print * stream, byte *com, bool blocking) {
|
2020-09-10 14:09:32 +02:00
|
|
|
if (Diag::CMD) DIAG(F("\nPARSING:%s\n"),com);
|
2020-06-04 21:55:18 +02:00
|
|
|
(void) EEPROM; // tell compiler not to warn thi is unused
|
2020-07-12 01:08:50 +02:00
|
|
|
int p[MAX_PARAMS];
|
|
|
|
while (com[0]=='<' || com[0]==' ') com++; // strip off any number of < or spaces
|
2020-06-18 20:36:37 +02:00
|
|
|
byte params=splitValues(p, com);
|
|
|
|
byte opcode=com[0];
|
|
|
|
|
|
|
|
if (filterCallback) filterCallback(stream,opcode,params,p);
|
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
// Functions return from this switch if complete, break from switch implies error <X> to send
|
2020-06-18 20:36:37 +02:00
|
|
|
switch(opcode) {
|
|
|
|
case '\0': return; // filterCallback asked us to ignore
|
2020-09-10 14:09:32 +02:00
|
|
|
case 't': // THROTTLE <t [REGISTER] CAB SPEED DIRECTION>
|
2020-07-23 16:41:43 +02:00
|
|
|
{
|
2020-09-10 14:09:32 +02:00
|
|
|
int cab;
|
|
|
|
int tspeed;
|
|
|
|
int direction;
|
|
|
|
|
|
|
|
if (params==4) { // <t REGISTER CAB SPEED DIRECTION>
|
|
|
|
cab=p[1];
|
|
|
|
tspeed=p[2];
|
|
|
|
direction=p[3];
|
|
|
|
}
|
|
|
|
else if (params==3) { // <t CAB SPEED DIRECTION>
|
|
|
|
cab=p[0];
|
|
|
|
tspeed=p[1];
|
|
|
|
direction=p[2];
|
|
|
|
}
|
|
|
|
else break;
|
2020-09-03 12:28:52 +02:00
|
|
|
|
2020-07-23 16:41:43 +02:00
|
|
|
// Convert JMRI bizarre -1=emergency stop, 0-126 as speeds
|
|
|
|
// to DCC 0=stop, 1= emergency stop, 2-127 speeds
|
2020-07-23 18:33:54 +02:00
|
|
|
if (tspeed>126 || tspeed<-1) break; // invalid JMRI speed code
|
2020-07-23 16:41:43 +02:00
|
|
|
if (tspeed<0) tspeed=1; // emergency stop DCC speed
|
|
|
|
else if (tspeed>0) tspeed++; // map 1-126 -> 2-127
|
2020-09-10 14:09:32 +02:00
|
|
|
if (cab == 0 && tspeed>1) break; // ignore broadcasts of speed>1
|
2020-09-03 12:28:52 +02:00
|
|
|
|
2020-09-10 14:09:32 +02:00
|
|
|
if (direction<0 || direction>1) break; // invalid direction code
|
2020-09-03 12:28:52 +02:00
|
|
|
|
2020-09-10 14:09:32 +02:00
|
|
|
DCC::setThrottle(cab,tspeed,direction);
|
|
|
|
if (params==4) StringFormatter::send(stream,F("<T %d %d %d>"), p[0], p[2],p[3]);
|
2020-09-11 13:56:36 +02:00
|
|
|
else StringFormatter::send(stream,F("<O>"));
|
2020-08-12 17:17:05 +02:00
|
|
|
return;
|
2020-07-23 16:41:43 +02:00
|
|
|
}
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'f': // FUNCTION <f CAB BYTE1 [BYTE2]>
|
2020-06-22 11:54:57 +02:00
|
|
|
if (parsef(stream,params,p)) return;
|
|
|
|
break;
|
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'a': // ACCESSORY <a ADDRESS SUBADDRESS ACTIVATE>
|
2020-06-29 12:37:05 +02:00
|
|
|
if(p[2] != (p[2] & 1)) return;
|
2020-06-23 21:00:34 +02:00
|
|
|
DCC::setAccessory(p[0],p[1],p[2]==1);
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-06-07 21:11:44 +02:00
|
|
|
|
|
|
|
case 'T': // TURNOUT <T ...>
|
2020-06-11 14:35:16 +02:00
|
|
|
if (parseT(stream,params,p)) return;
|
2020-05-27 10:24:56 +02:00
|
|
|
break;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'Z': // OUTPUT <Z ...>
|
2020-06-11 14:35:16 +02:00
|
|
|
if (parseZ(stream,params,p)) return;
|
2020-06-03 15:26:49 +02:00
|
|
|
break;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'S': // SENSOR <S ...>
|
2020-06-11 14:35:16 +02:00
|
|
|
if (parseS(stream,params,p)) return;
|
2020-06-03 15:26:49 +02:00
|
|
|
break;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'w': // WRITE CV on MAIN <w CAB CV VALUE>
|
2020-05-25 14:38:18 +02:00
|
|
|
DCC::writeCVByteMain(p[0],p[1],p[2]);
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'b': // WRITE CV BIT ON MAIN <b CAB CV BIT VALUE>
|
2020-05-25 14:38:18 +02:00
|
|
|
DCC::writeCVBitMain(p[0],p[1],p[2],p[3]);
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'W': // WRITE CV ON PROG <W CV VALUE CALLBACKNUM CALLBACKSUB>
|
2020-06-07 14:48:42 +02:00
|
|
|
if (!stashCallback(stream,p)) break;
|
2020-07-19 19:39:08 +02:00
|
|
|
DCC::writeCVByte(p[0],p[1],callback_W,blocking);
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-06-07 21:11:44 +02:00
|
|
|
|
|
|
|
case 'B': // WRITE CV BIT ON PROG <B CV BIT VALUE CALLBACKNUM CALLBACKSUB>
|
2020-06-07 14:48:42 +02:00
|
|
|
if (!stashCallback(stream,p)) break;
|
2020-07-19 19:39:08 +02:00
|
|
|
DCC::writeCVBit(p[0],p[1],p[2],callback_B,blocking);
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-06-07 14:48:42 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'R': // READ CV ON PROG <R CV CALLBACKNUM CALLBACKSUB>
|
|
|
|
if (!stashCallback(stream,p)) break;
|
2020-07-19 19:39:08 +02:00
|
|
|
DCC::readCV(p[0],callback_R,blocking);
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-19 10:19:41 +02:00
|
|
|
case '1': // POWERON <1 [MAIN|PROG]>
|
|
|
|
case '0': // POWEROFF <0 [MAIN | PROG] >
|
|
|
|
if (params>1) break;
|
|
|
|
{
|
|
|
|
POWERMODE mode= opcode=='1'?POWERMODE::ON:POWERMODE::OFF;
|
2020-07-30 16:34:56 +02:00
|
|
|
DCC::setProgTrackSyncMain(false); // Only <1 JOIN> will set this on, all others set it off
|
2020-06-19 10:19:41 +02:00
|
|
|
if (params==0) {
|
|
|
|
DCCWaveform::mainTrack.setPowerMode(mode);
|
|
|
|
DCCWaveform::progTrack.setPowerMode(mode);
|
|
|
|
StringFormatter::send(stream,F("<p%c>"),opcode);
|
|
|
|
return;
|
|
|
|
}
|
2020-07-30 16:34:56 +02:00
|
|
|
switch (p[0]) {
|
|
|
|
case HASH_KEYWORD_MAIN:
|
2020-06-19 10:19:41 +02:00
|
|
|
DCCWaveform::mainTrack.setPowerMode(mode);
|
|
|
|
StringFormatter::send(stream,F("<p%c MAIN>"),opcode);
|
|
|
|
return;
|
2020-07-30 16:34:56 +02:00
|
|
|
|
|
|
|
case HASH_KEYWORD_PROG:
|
2020-06-19 10:19:41 +02:00
|
|
|
DCCWaveform::progTrack.setPowerMode(mode);
|
|
|
|
StringFormatter::send(stream,F("<p%c PROG>"),opcode);
|
|
|
|
return;
|
2020-07-30 16:34:56 +02:00
|
|
|
case HASH_KEYWORD_JOIN:
|
|
|
|
DCCWaveform::mainTrack.setPowerMode(mode);
|
|
|
|
DCCWaveform::progTrack.setPowerMode(mode);
|
|
|
|
if (mode==POWERMODE::ON) {
|
|
|
|
DCC::setProgTrackSyncMain(true);
|
|
|
|
StringFormatter::send(stream,F("<p1 JOIN>"),opcode);
|
|
|
|
}
|
|
|
|
else StringFormatter::send(stream,F("<p0>"));
|
|
|
|
return;
|
|
|
|
|
2020-06-19 10:19:41 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'c': // READ CURRENT <c>
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stream,F("<a %d>"), DCCWaveform::mainTrack.getLastCurrent());
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'Q': // SENSORS <Q>
|
2020-09-06 13:44:19 +02:00
|
|
|
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;
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-05-25 14:38:18 +02:00
|
|
|
case 's': // <s>
|
2020-06-12 15:28:35 +02:00
|
|
|
StringFormatter::send(stream,F("<p%d>"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON );
|
2020-08-28 15:44:06 +02:00
|
|
|
StringFormatter::send(stream,F("<iDCC-EX-API / V-%S %s/%s>"), VERSION, __DATE__, __TIME__ );
|
2020-06-03 15:26:49 +02:00
|
|
|
// TODO Send stats of speed reminders table
|
|
|
|
// TODO send status of turnouts etc etc
|
|
|
|
return;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'E': // STORE EPROM <E>
|
2020-05-25 14:38:18 +02:00
|
|
|
EEStore::store();
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stream,F("<e %d %d %d>"), EEStore::eeStore->data.nTurnouts, EEStore::eeStore->data.nSensors, EEStore::eeStore->data.nOutputs);
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 'e': // CLEAR EPROM <e>
|
2020-05-25 14:38:18 +02:00
|
|
|
EEStore::clear();
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stream, F("<O>"));
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case ' ': // < >
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stream,F("\n"));
|
2020-06-03 15:26:49 +02:00
|
|
|
return;
|
2020-06-22 11:54:57 +02:00
|
|
|
|
2020-07-01 11:27:53 +02:00
|
|
|
case 'D': // < >
|
2020-09-10 14:09:32 +02:00
|
|
|
if (parseD(stream,params,p)) return;
|
2020-07-01 11:27:53 +02:00
|
|
|
return;
|
2020-07-04 21:53:44 +02:00
|
|
|
|
|
|
|
case '#': // NUMBER OF LOCOSLOTS <#>
|
2020-07-30 16:34:56 +02:00
|
|
|
StringFormatter::send(stream,F("<# %d>"), MAX_LOCOS);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'F': // New command to call the new Loco Function API <F cab func 1|0>
|
2020-09-10 14:09:32 +02:00
|
|
|
if (Diag::CMD) DIAG(F("Setting loco %d F%d %S"),p[0],p[1],p[2]?F("ON"):F("OFF"));
|
2020-07-30 16:34:56 +02:00
|
|
|
DCC::setFn(p[0],p[1],p[2]==1);
|
2020-08-01 17:32:16 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
case '+' : // Complex Wifi interface command (not usual parse)
|
|
|
|
WifiInterface::ATCommand(com);
|
|
|
|
break;
|
2020-07-30 16:34:56 +02:00
|
|
|
|
|
|
|
default: //anything else will diagnose and drop out to <X>
|
|
|
|
DIAG(F("\nOpcode=%c params=%d\n"),opcode,params);
|
|
|
|
for (int i=0;i<params;i++) DIAG(F("p[%d]=%d (0x%x)\n"),i,p[i],p[i]);
|
2020-06-07 21:11:44 +02:00
|
|
|
break;
|
2020-06-03 15:26:49 +02:00
|
|
|
|
|
|
|
} // end of opcode switch
|
|
|
|
|
|
|
|
// Any fallout here sends an <X>
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stream, F("<X>"));
|
2020-05-25 14:38:18 +02:00
|
|
|
}
|
|
|
|
|
2020-08-01 17:32:16 +02:00
|
|
|
bool DCCEXParser::parseZ( Print * stream,int params, int p[]){
|
2020-06-07 21:11:44 +02:00
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
switch (params) {
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 2: // <Z ID ACTIVATE>
|
2020-06-03 15:26:49 +02:00
|
|
|
{
|
|
|
|
Output * o=Output::get(p[0]);
|
|
|
|
if(o==NULL) return false;
|
|
|
|
o->activate(p[1]);
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stream,F("<Y %d %d>"), p[0],p[1]);
|
2020-06-03 15:26:49 +02:00
|
|
|
}
|
2020-06-07 21:11:44 +02:00
|
|
|
return true;
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 3: // <Z ID PIN INVERT>
|
2020-06-03 15:26:49 +02:00
|
|
|
Output::create(p[0],p[1],p[2],1);
|
2020-06-07 21:11:44 +02:00
|
|
|
return true;
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-06-07 21:11:44 +02:00
|
|
|
case 1: // <Z ID>
|
|
|
|
return Output::remove(p[0]);
|
|
|
|
|
|
|
|
case 0: // <Z>
|
2020-09-06 13:44:19 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2020-06-03 15:26:49 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 11:54:57 +02:00
|
|
|
//===================================
|
2020-08-01 17:32:16 +02:00
|
|
|
bool DCCEXParser::parsef(Print * stream, int params, int p[]) {
|
2020-06-22 11:54:57 +02:00
|
|
|
// JMRI sends this info in DCC message format but it's not exactly
|
|
|
|
// convenient for other processing
|
|
|
|
if (params==2) {
|
|
|
|
byte groupcode=p[1] & 0xE0;
|
|
|
|
if (groupcode == 0x80) {
|
|
|
|
byte normalized= (p[1]<<1 & 0x1e ) | (p[1]>>4 & 0x01);
|
|
|
|
funcmap(p[0],normalized,0,4);
|
|
|
|
}
|
|
|
|
else if (groupcode == 0xC0) {
|
|
|
|
funcmap(p[0],p[1],5,8);
|
|
|
|
}
|
|
|
|
else if (groupcode == 0xA0) {
|
|
|
|
funcmap(p[0],p[1],9,12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (params==3) {
|
|
|
|
if (p[1]==222) funcmap(p[0],p[2],13,20);
|
|
|
|
else if (p[1]==223) funcmap(p[0],p[2],21,28);
|
|
|
|
}
|
2020-06-29 12:37:05 +02:00
|
|
|
(void)stream;// NO RESPONSE
|
2020-06-22 11:54:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DCCEXParser::funcmap(int cab, byte value, byte fstart, byte fstop) {
|
|
|
|
for (int i=fstart;i<=fstop;i++) {
|
|
|
|
DCC::setFn(cab, i, value & 1);
|
2020-06-29 12:37:05 +02:00
|
|
|
value>>=1;
|
2020-06-22 11:54:57 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-03 15:26:49 +02:00
|
|
|
|
|
|
|
//===================================
|
2020-08-01 17:32:16 +02:00
|
|
|
bool DCCEXParser::parseT(Print * stream, int params, int p[]) {
|
2020-06-03 15:26:49 +02:00
|
|
|
switch(params){
|
2020-09-06 13:44:19 +02:00
|
|
|
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
|
2020-06-07 21:11:44 +02:00
|
|
|
if (!Turnout::remove(p[0])) return false;
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stream,F("<O>"));
|
2020-06-07 21:11:44 +02:00
|
|
|
return true;
|
|
|
|
|
2020-09-06 13:44:19 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-06-07 21:11:44 +02:00
|
|
|
return true;
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-09-06 13:44:19 +02:00
|
|
|
case 3: // <T id addr subaddr> define turnout
|
2020-06-03 15:26:49 +02:00
|
|
|
if (!Turnout::create(p[0],p[1],p[2])) return false;
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stream,F("<O>"));
|
2020-06-07 21:11:44 +02:00
|
|
|
return true;
|
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
default:
|
|
|
|
return false; // will <x>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 17:32:16 +02:00
|
|
|
bool DCCEXParser::parseS( Print * stream,int params, int p[]) {
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-09-06 13:44:19 +02:00
|
|
|
switch(params){
|
|
|
|
case 3: // <S id pin pullup> create sensor. pullUp indicator (0=LOW/1=HIGH)
|
2020-06-03 15:26:49 +02:00
|
|
|
Sensor::create(p[0],p[1],p[2]);
|
|
|
|
return true;
|
|
|
|
|
2020-09-06 13:44:19 +02:00
|
|
|
case 1: // S id> remove sensor
|
2020-06-03 15:26:49 +02:00
|
|
|
if (Sensor::remove(p[0])) return true;
|
|
|
|
break;
|
|
|
|
|
2020-09-06 13:44:19 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-06-03 15:26:49 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default: // invalid number of arguments
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-07 14:48:42 +02:00
|
|
|
|
2020-09-10 14:09:32 +02:00
|
|
|
bool DCCEXParser::parseD( Print * stream,int params, int p[]) {
|
|
|
|
if (params==0) return false;
|
2020-09-10 18:11:50 +02:00
|
|
|
bool onOff=(params>0) && (p[1]==1 || p[1]==HASH_KEYWORD_ON); // dont care if other stuff or missing... just means off
|
2020-09-10 14:09:32 +02:00
|
|
|
switch(p[0]){
|
|
|
|
case HASH_KEYWORD_CABS: // <D CABS>
|
|
|
|
DCC::displayCabList(stream);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case HASH_KEYWORD_RAM: // <D RAM>
|
|
|
|
StringFormatter::send(stream,F("\nFree memory=%d\n"),freeMemory());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HASH_KEYWORD_ACK: // <D ACK ON/OFF>
|
|
|
|
Diag::ACK=onOff;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case HASH_KEYWORD_CMD: // <D CMD ON/OFF>
|
|
|
|
Diag::CMD=onOff;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case HASH_KEYWORD_WIFI: // <D WIFI ON/OFF>
|
|
|
|
Diag::WIFI=onOff;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case HASH_KEYWORD_WIT: // <D WIT ON/OFF>
|
|
|
|
Diag::WITHROTTLE=onOff;
|
|
|
|
return true;
|
|
|
|
|
2020-09-10 18:11:50 +02:00
|
|
|
case HASH_KEYWORD_DCC:
|
|
|
|
DCCWaveform::setDiagnosticSlowWave(params>=1 && p[1]==HASH_KEYWORD_SLOW);
|
|
|
|
return true;
|
2020-09-10 14:09:32 +02:00
|
|
|
default: // invalid/unknown
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:31:26 +02:00
|
|
|
|
|
|
|
// CALLBACKS must be static
|
2020-08-01 17:32:16 +02:00
|
|
|
bool DCCEXParser::stashCallback(Print * stream,int p[MAX_PARAMS]) {
|
2020-07-01 17:58:48 +02:00
|
|
|
if (stashBusy || asyncBanned) return false;
|
2020-06-07 14:48:42 +02:00
|
|
|
stashBusy=true;
|
|
|
|
stashStream=stream;
|
|
|
|
memcpy(stashP,p,MAX_PARAMS*sizeof(p[0]));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DCCEXParser::callback_W(int result) {
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stashStream,F("<r%d|%d|%d %d>"), stashP[2], stashP[3],stashP[0],result==1?stashP[1]:-1);
|
2020-06-07 14:48:42 +02:00
|
|
|
stashBusy=false;
|
|
|
|
}
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-06-07 14:48:42 +02:00
|
|
|
void DCCEXParser::callback_B(int result) {
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stashStream,F("<r%d|%d|%d %d %d>"), stashP[3],stashP[4], stashP[0],stashP[1],result==1?stashP[2]:-1);
|
2020-06-07 14:48:42 +02:00
|
|
|
stashBusy=false;
|
|
|
|
}
|
|
|
|
void DCCEXParser::callback_R(int result) {
|
2020-06-10 18:31:26 +02:00
|
|
|
StringFormatter::send(stashStream,F("<r%d|%d|%d %d>"),stashP[1],stashP[2],stashP[0],result);
|
2020-06-07 14:48:42 +02:00
|
|
|
stashBusy=false;
|
|
|
|
}
|
|
|
|
|