1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00
CommandStation-EX/WiThrottle.cpp

276 lines
11 KiB
C++
Raw Normal View History

2020-07-03 18:35:32 +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-27 16:36:32 +02:00
/*
* Truncated JMRI WiThrottle server implementation for DCC-EX command station
* Credit is due to Valerie Valley RR https://sites.google.com/site/valerievalleyrr/
* for showing how it could be done, but this code is very different to the original
* implemenatatin as it is designed to run on the Arduino and not the ESP and is
* also calling directly into the DCCEX Api rather than simulating JMRI text commands.
2020-06-29 14:49:46 +02:00
* Refer JMRI WiFi Throttle Communications Protocol https://www.jmri.org/help/en/package/jmri/jmrit/withrottle/Protocol.shtml
2020-06-27 16:36:32 +02:00
*
*
* PROTOTYPE NOTES:
* There will be one WiThrottle instance created for each WiThrottle client detected by the WifiInterface.
* Some shortcuts have been taken and there are some things that are yet to be included:
* e.g. Full response to adding a loco.
* What to do about unknown turnouts.
* Broadcasting to other WiThrottles when things change.
* - Bear in mind that changes may have taken place due to
* other WiThrottles, OR JMRI commands received OR TPL automation.
* - I suggest that at the end of parse(), then anything that has changed and is of interest could
* be notified then. (e.g loco speeds, directions or functions, turnout states.
*
* WiThrottle.h sets the max locos per client at 10, this is ok to increase but requires just an extra 3 bytes per loco per client.
*/
#include <Arduino.h>
#include "WiThrottle.h"
#include "DCC.h"
#include "DCCWaveform.h"
#include "StringFormatter.h"
#include "Turnouts.h"
#include "DIAG.h"
#define LOOPLOCOS(THROTTLECHAR, CAB) for (int loco=0;loco<MAX_MY_LOCO;loco++) \
if (myLocos[loco].throttle==THROTTLECHAR && (CAB<0 || myLocos[loco].cab==CAB))
WiThrottle * WiThrottle::firstThrottle=NULL;
2020-07-05 22:00:27 +02:00
WiThrottle* WiThrottle::getThrottle( int wifiClient) {
2020-06-27 16:36:32 +02:00
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle)
if (wt->clientid==wifiClient) return wt;
2020-07-05 22:00:27 +02:00
return new WiThrottle( wifiClient);
2020-06-27 16:36:32 +02:00
}
// One instance of WiTHrottle per connected client, so we know what the locos are
2020-07-05 22:00:27 +02:00
WiThrottle::WiThrottle( int wificlientid) {
2020-06-27 16:36:32 +02:00
DIAG(F("\nCreating new WiThrottle for client %d\n"),wificlientid);
nextThrottle=firstThrottle;
firstThrottle= this;
clientid=wificlientid;
2020-07-05 22:00:27 +02:00
heartBeatEnable=false; // until client turns it on
firstCall=true;
for (int loco=0;loco<MAX_MY_LOCO; loco++) myLocos[loco].throttle='\0';
2020-06-27 16:36:32 +02:00
}
WiThrottle::~WiThrottle() {
if (firstThrottle== this) {
firstThrottle=this->nextThrottle;
return;
}
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle) {
if (wt->nextThrottle==this) {
wt->nextThrottle=this->nextThrottle;
return;
}
}
}
2020-07-05 22:00:27 +02:00
void WiThrottle::parse(Print & stream, byte * cmdx) {
// we have to take a copy of the cmd buffer as the reply will get built into the cmdx
byte local[50];
for (byte i=0;i<sizeof(local);i++) {
local[i]=cmdx[i];
if (!cmdx[i]) break;
}
local[49]='\0'; // prevent runaway parser
byte * cmd=local;
2020-06-27 16:36:32 +02:00
heartBeat=millis();
2020-07-03 18:35:32 +02:00
DIAG(F("\nWiThrottle(%d) [%e]"),clientid, cmd);
2020-07-05 22:00:27 +02:00
if (firstCall) {
firstCall=false;
StringFormatter::send(stream,F("VN2.0\nHTDCC++EX\nRL0\nPPA%x\nPTT]\\[Turnouts}|{Turnout]\\[Closed}|{2]\\[Thrown}|{4\\PTL"),
DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON);
for(Turnout *tt=Turnout::firstTurnout;tt!=NULL;tt=tt->nextTurnout){
StringFormatter::send(stream,F("]\\[LT&d}|{%d}|{%d"), tt->data.id, tt->data.id, (bool)(tt->data.tStatus & STATUS_ACTIVE));
}
StringFormatter::send(stream,F("\n*10\n"));
}
while (cmd[0]) {
2020-06-27 16:36:32 +02:00
switch (cmd[0]) {
case '*': // heartbeat control
if (cmd[1]=='+') heartBeatEnable=true;
else if (cmd[1]=='-') heartBeatEnable=false;
break;
case 'P':
if (cmd[1]=='P' && cmd[2]=='A' ) { //PPA power mode
DCCWaveform::mainTrack.setPowerMode(cmd[3]=='1'?POWERMODE::ON:POWERMODE::OFF);
StringFormatter::send(stream, F("PPA%c"),cmd[3]);
}
else if (cmd[1]=='T' && cmd[2]=='A') { // PTA accessory toggle
2020-06-27 16:36:32 +02:00
// TODO... if we are given an address that is not a known Turnout...
// should we create one or just send the DCC message.
Turnout::activate(getInt(cmd+4),cmd[3]=='T');
}
break;
case 'N': // Heartbeat (2)
2020-06-29 14:45:16 +02:00
StringFormatter::send(stream, F("*10\n")); // 10 second timeout
2020-06-27 16:36:32 +02:00
break;
case 'M': // multithrottle
multithrottle(stream, cmd);
break;
2020-06-29 14:45:16 +02:00
case 'H': // hardware introduction....
break;
2020-07-03 18:35:32 +02:00
case 'Q': //
2020-06-29 14:45:16 +02:00
DIAG(F("\nWiThrottle Quit"));
delete this;
break;
2020-07-05 22:00:27 +02:00
}
// skip over cmd until 0 or past \r or \n
while(*cmd !='\0' && *cmd != '\r' && *cmd !='\n') cmd++;
if (*cmd!='\0') cmd++; // skip \r or \n
2020-06-27 16:36:32 +02:00
}
}
int WiThrottle::getInt(byte * cmd) {
2020-06-27 16:36:32 +02:00
int i=0;
while (cmd[0]>='0' && cmd[0]<='9') {
i=i*10 + (cmd[0]-'0');
cmd++;
}
return i;
}
int WiThrottle::getLocoId(byte * cmd) {
2020-06-27 16:36:32 +02:00
if (cmd[0]=='*') return -1; // match all locos
if (cmd[0]!='L' && cmd[0]!='S') return 0; // should not match any locos
return getInt(cmd+1);
}
void WiThrottle::multithrottle(Print & stream, byte * cmd){
2020-06-27 16:36:32 +02:00
char throttleChar=cmd[1];
int locoid=getLocoId(cmd+3); // -1 for *
byte * aval=cmd;
2020-06-27 16:36:32 +02:00
while(*aval !=';' && *aval !='\0') aval++;
2020-06-29 14:03:08 +02:00
if (*aval) aval+=2; // skip ;>
DIAG(F("\nMultithrottle aval=%c cab=%d"), aval[0],locoid);
2020-06-27 16:36:32 +02:00
switch(cmd[2]) {
case '+': // add loco
for (int loco=0;loco<MAX_MY_LOCO;loco++) {
if (myLocos[loco].throttle=='\0') {
myLocos[loco].throttle=throttleChar;
myLocos[loco].cab=locoid;
StringFormatter::send(stream, F("M%c+%c%d<;>\n"), throttleChar, cmd[3] ,locoid);
// TODO... get known Fn states from DCC (need memoryStream improvements to handle data length)
// for(fKey=0; fKey<29; fKey++)StringFormatter::send(stream,F("M%cA%c<;>F0&s\n"),throttleChar,cmd[3],fkey);
StringFormatter::send(stream, F("M%c+%c%d<;>V0\n"), throttleChar, cmd[3], locoid);
StringFormatter::send(stream, F("M%c+%c%d<;>R1\n"), throttleChar, cmd[3], locoid);
StringFormatter::send(stream, F("M%c+%c%d<;>s1\n"), throttleChar, cmd[3], locoid);
break;
}
}
break;
case '-': // remove loco
LOOPLOCOS(throttleChar, locoid) {
myLocos[loco].throttle='\0';
DCC::setThrottle(myLocos[loco].cab,0,0);
StringFormatter::send(stream, F("M%c-<;>\n"), throttleChar);
}
break;
case 'A':
locoAction(stream,aval, throttleChar, locoid);
}
}
void WiThrottle::locoAction(Print & stream, byte* aval, char throttleChar, int cab){
2020-07-16 14:19:29 +02:00
unsigned long millisIn=millis();
2020-06-27 16:36:32 +02:00
// Note cab=-1 for all cabs in the consist called throttleChar.
2020-07-16 14:19:29 +02:00
DIAG(F("\nLoco Action aval=%c%c throttleChar=%c, cab=%d"), aval[0],aval[1],throttleChar, cab);
2020-06-27 16:36:32 +02:00
switch (aval[0]) {
case 'V': // Vspeed
{
byte locospeed=getInt(aval+1);
LOOPLOCOS(throttleChar, cab) {
DCC::setThrottle(myLocos[loco].cab,locospeed, DCC::getThrottleDirection(myLocos[loco].cab));
}
}
break;
case 'F': //F onOff function
{
bool onOff=aval[1]=='1';
int fKey = getInt(aval+2);
LOOPLOCOS(throttleChar, cab) {
DCC::setFn(myLocos[loco].cab, fKey,onOff);
}
}
break;
case 'q':
if (aval[1]=='V') { //qV
LOOPLOCOS(throttleChar, cab) {
2020-07-05 22:00:27 +02:00
StringFormatter::send(stream,F("M%cAL%d<;>V%d\n"), throttleChar, myLocos[loco].cab, DCC::getThrottleSpeed(myLocos[loco].cab));
2020-06-27 16:36:32 +02:00
}
}
else if (aval[1]=='R') { // qR
LOOPLOCOS(throttleChar, cab) {
2020-07-05 22:00:27 +02:00
StringFormatter::send(stream,F("M%cAL%d<;>R%d\n"), throttleChar, myLocos[loco].cab, DCC::getThrottleDirection(myLocos[loco].cab));
2020-06-27 16:36:32 +02:00
}
}
break;
case 'R':
{
bool forward=aval[1]!='0';
LOOPLOCOS(throttleChar, cab) {
DCC::setThrottle(myLocos[loco].cab, DCC::getThrottleSpeed(myLocos[loco].cab), forward);
}
}
break;
case 'X':
//Emergency Stop (TODO check we have the correct speed code here)
LOOPLOCOS(throttleChar, cab) {
DCC::setThrottle(myLocos[loco].cab,1, DCC::getThrottleDirection(myLocos[loco].cab));
}
break;
case 'I': // Idle
case 'Q': // Quit
LOOPLOCOS(throttleChar, cab) {
DCC::setThrottle(myLocos[loco].cab,0, DCC::getThrottleDirection(myLocos[loco].cab));
}
break;
}
2020-07-16 14:19:29 +02:00
DIAG(F("\ in %lms\n"),millis()-millisIn);
2020-06-27 16:36:32 +02:00
}
void WiThrottle::loop() {
// for each WiThrottle, check the heartbeat
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle)
wt->checkHeartbeat();
}
void WiThrottle::checkHeartbeat() {
2020-06-29 14:45:16 +02:00
if(heartBeatEnable && (millis()-heartBeat > HEARTBEAT_TIMEOUT*1000)) {
DIAG(F("WiThrottle hearbeat missed client=%d"),clientid);
2020-06-29 14:03:08 +02:00
// Haertbeat missed... STOP all locos for this client
2020-06-27 16:36:32 +02:00
for (int loco=0;loco<MAX_MY_LOCO;loco++) {
if (myLocos[loco].throttle!='\0') {
DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab));
2020-06-29 14:03:08 +02:00
}
}
2020-06-29 14:45:16 +02:00
delete this;
2020-06-29 14:03:08 +02:00
}
else {
// TODO Check if anything has changed on my locos since last notified!
2020-06-27 16:36:32 +02:00
}
}