1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-25 05:31:24 +01:00
CommandStation-EX/WiThrottle.cpp

403 lines
18 KiB
C++
Raw Normal View History

2020-07-03 18:35:32 +02:00
/*
* © 2020, Chris Harlow. All rights reserved.
2020-08-01 00:35:22 +02:00
* © 2020, Harald Barth
2020-07-03 18:35:32 +02:00
*
* 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
2020-08-14 16:43:35 +02:00
* implementation as it is designed to run on the Arduino and not the ESP and is
2020-06-27 16:36:32 +02:00
* 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"
#include "GITHUB_SHA.h"
#include "version.h"
2020-06-27 16:36:32 +02:00
#define LOOPLOCOS(THROTTLECHAR, CAB) for (int loco=0;loco<MAX_MY_LOCO;loco++) \
if ((myLocos[loco].throttle==THROTTLECHAR || '*'==THROTTLECHAR) && (CAB<0 || myLocos[loco].cab==CAB))
2020-06-27 16:36:32 +02:00
WiThrottle * WiThrottle::firstThrottle=NULL;
bool WiThrottle::annotateLeftRight=false;
2020-06-27 16:36:32 +02:00
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)
2020-08-14 13:26:14 +02:00
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
}
2020-08-14 13:26:14 +02:00
bool WiThrottle::isThrottleInUse(int cab) {
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle)
if (wt->areYouUsingThrottle(cab)) return true;
return false;
}
bool WiThrottle::areYouUsingThrottle(int cab) {
LOOPLOCOS('*', cab) { // see if I have this cab in use
return true;
}
return false;
}
// One instance of WiThrottle per connected client, so we know what the locos are
2020-06-27 16:36:32 +02:00
2020-07-05 22:00:27 +02:00
WiThrottle::WiThrottle( int wificlientid) {
if (Diag::WITHROTTLE) DIAG(F("%l Creating new WiThrottle for client %d"),millis(),wificlientid);
2020-06-27 16:36:32 +02:00
nextThrottle=firstThrottle;
firstThrottle= this;
clientid=wificlientid;
2020-08-20 18:16:47 +02:00
initSent=false; // prevent sending heartbeats before connection completed
heartBeatEnable=false; // until client turns it on
2020-08-20 18:16:47 +02:00
turnoutListHash = -1; // make sure turnout list is sent once
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;
}
}
}
void WiThrottle::parse(RingStream * stream, byte * cmdx) {
2020-07-05 22:00:27 +02:00
byte * cmd=cmdx;
2020-07-05 22:00:27 +02:00
2020-06-27 16:36:32 +02:00
heartBeat=millis();
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d)<-[%e]"),millis(),clientid,cmd);
2020-08-20 18:16:47 +02:00
if (initSent) {
// Send power state if different than last sent
bool currentPowerState = (DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON);
if (lastPowerState != currentPowerState) {
StringFormatter::send(stream,F("PPA%x\n"),currentPowerState);
lastPowerState = currentPowerState;
}
// Send turnout list if changed since last sent (will replace list on client)
if (turnoutListHash != Turnout::turnoutlistHash) {
StringFormatter::send(stream,F("PTL"));
for(Turnout *tt=Turnout::firstTurnout;tt!=NULL;tt=tt->nextTurnout){
StringFormatter::send(stream,F("]\\[%d}|{%d}|{%c"), tt->data.id, tt->data.id, Turnout::isActive(tt->data.id)?'4':'2');
}
StringFormatter::send(stream,F("\n"));
turnoutListHash = Turnout::turnoutlistHash; // keep a copy of hash for later comparison
}
}
2020-07-05 22:00:27 +02:00
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);
Nano every2 (#129) * Start adding back unowifi stuffz * Uno Wifi compiling * Fixes for compile arduino unowifi r2 * FlasString and Timers for Uno Wifi ALL these changes should be portable back to master * Remove extra timer that was already added * Changed to EveryTimerB * Add everytimerb.h * Cleanup * Linear address <a> cmd * Allow lower case keywords * Add the F define to be on safe side if it is not present in the library core code * Clean simple Timer interface Removes overkill files, puts all timer in a single small file. (DCCTimer) * Timer port * Timer working And slow wave command removed * Correcting non-portables merged from master * Wave-state machine ( part 11) * Microtuning waveform Significant reduction in code paths and call overheads * Current check cleanup * Fix no-loco id Has to handle -1 correctly * fix wrong format letter * redo flow through wifisetup again * version++ * bugfixes wifi setup * Retry harder for AP mode * Remove unued if * DIO2 replacement Currently for writing signal pins during waveform. * Drop analogReadFast (see DCCTimer) AnalogRead speed set in DCCTimer for ease of porting. Code tidy and diagnostics in MotorDriver * UNTESTED fast power,brake,fault pins * Distunguish between in/out of FASTPIN * minor performance tweaks * Config comments and example use * Config/example loading * IP/PORT on LCD * Ethernet simulated mac Plus fixed listening port * Github SHA * Committing a SHA * Fix for nano compile * Comments and a reliability fix. * UnoRev2 protection * PWM pin implementation * fix wifi setup issue * Reinstate IP_PORT * Wifi channel and code cleaninga * Reduce duplicated F() macros Compiler isn't as clever as one might expect * Committing a SHA * Update config.example.h Add comment to wifi channel section * Committing a SHA * Handle shields with common fault pins (Pololu) * Committing a SHA * remove warning * Committing a SHA * only do the sha generation on master * yaml syntax * Fast SSD1306 OLED driver Incorporate code from SSD1306Ascii library to speed up OLED screen updates, reduce memory requirements and eliminate some library dependences. * Fix auto-configure from cold boot. Add call to Wire.begin(). * Update comment for OLED_DRIVER define. * Update MotorDrivers.h Add a motor board definition for using the IBT_2 board for a high current to the main track and keep the Arduino Motor Shield for operating the programming track. * Committing a SHA * Fix missing F in motor drivers * JOIN relay pin * Swap Join Relay high/low * Hide WIFI_CONNECT_TIMEOUT This is not what the config suggests it is... The timeout is in the ES and defaults to 15 seconds. Abandoning it early leads to confused setup. * Enhance OLED/LCD speed Write one character or position command per loop entry so as not to hold up the loop. Add support for SH1106 OLED as 132x64 size option. * Enhance OLED/LCD speed * Delete comment about OLED on UNO. * Trim unwanted code * Handle display types correctly * Update comments * Speed up OLED writes Add new flushDisplay() to end any in-progress I2C transaction. Previously, an redundant command was sent that ended the in-progress transaction but also sent another complete, but unnecessary, transaction. * Comments and copyright update * Reduce RAM and flash requirement a few more bytes. * Move statics into LCDDisplay class, and reduce RAM. Some state variables were static in LCDDisplay.write(). Moved to class members. Also, types of data items like row, column & character position changed to int8_t to save a few bytes of RAM. * Type lcdCols and lcdRows to unsigned. Since lcdCols is normally 128, it needs to be uint8_t, not int8_t. * remove timeout from user config * faultpin is common only if it exists ; make code prettier * Rationalisation of SSD1306 driver Merge SSD1306AsciiWire.cpp into SSD1306Ascii.cpp and rename SSD1306AsciiWire.h as SSD1306Ascii.h. Merge allFonts.h into System5x7.h and rename as SSD1306font.h. Move all SSD1306 files into root folder to facilitate compilation in Arduino IDE. * Fix some font attributes as const. * Remove unused initialisation sequences for tiny oled screens * Add m_ to variables * Bump up I2C speed Speed was 100kHz (default). Max for OLEDis 400kHz. * Revert "Bump up I2C speed" This reverts commit 1c1168f43314d3a6855738fbc406d5654e801831. * Bump up I2C speed Speed was 100kHz (default). Max for OLEDis 400kHz. * Drop duplicate DIAG * ignore mySetup.h files * Restore uno to default_envs Restore uno (previously commented out) to default_envs. * Update objdump.bat Allows other editors as Notepad is very slow on large files * Prog Track overload during cv read * Faster LCD Driver Extract LCD driver from library; Trim unused functionality; Reduce I2C communications to minimum; Speed up I2C clock to 400kHz. * Update config.example.h Add IBT_2_WITH_ARDUINO to example config * Update config.example.h * Screen enhancements (#126) * Add I2CManager to coordinate I2C shared parameters. * Add use of I2CManager, and experimental scrolling strategies. New scrolling capability by defining SCROLLMODE in Config.h to 0 (original), 1 (by page) or 2 (by line). If not defined, defaults to 0. * Scrolling updates New scrolling capability by defining SCROLLMODE in Config.h to 0 (original), 1 (by page) or 2 (by line). If not defined, defaults to 0. Reformat. * Add I2CManager calls. Remove unnecessary delays. * Add I2CManager calls, remove unnecessary I2C delays. * SSD1306: Move methods from .h to .cpp and reformat. * Fix compiler warning in LiquidCrystal_I2C * Allow forcing of I2C clock speed. New method forceClock allows the I2C speed to be overridden. For example, if the I2C bus is long then the speed can be forced lower. It can also be forced higher to gain performance if devices are capable. * Make Config.h conditionally included. Allow for non-existence of Config.h. * Correct scrolling and allow longer messages Correct the handling of scrolling in scrollmode 1 to avoid a blank page being displayed. Also, allow MAX_MSG_SIZE to be optionally configured to override maximum message length on screens. * compiler warning on uno Co-authored-by: dexslab <dex35803@gmail.com> Co-authored-by: Asbelos <asbelos@btinternet.com> Co-authored-by: Harald Barth <haba@kth.se> Co-authored-by: Neil McKechnie <neilmck999@gmail.com> Co-authored-by: Neil McKechnie <75813993+Neil-McK@users.noreply.github.com>
2021-03-07 21:58:35 +01:00
if (MotorDriver::commonFaultPin) // commonFaultPin prevents individual track handling
DCCWaveform::progTrack.setPowerMode(cmd[3]=='1'?POWERMODE::ON:POWERMODE::OFF);
2020-08-14 16:43:35 +02:00
StringFormatter::send(stream,F("PPA%x\n"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON);
2020-08-20 18:16:47 +02:00
lastPowerState = (DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON); //remember power state sent for comparison later
2020-06-27 16:36:32 +02:00
}
else if (cmd[1]=='T' && cmd[2]=='A') { // PTA accessory toggle
2020-07-25 18:50:23 +02:00
int id=getInt(cmd+4);
2020-07-23 18:34:35 +02:00
bool newstate=false;
2020-07-25 18:33:39 +02:00
Turnout * tt=Turnout::get(id);
if (!tt) {
2020-08-14 16:43:35 +02:00
// If turnout does not exist, create it
int addr = ((id - 1) / 4) + 1;
int subaddr = (id - 1) % 4;
Turnout::create(id,addr,subaddr);
2020-08-20 18:16:47 +02:00
StringFormatter::send(stream, F("HmTurnout %d created\n"),id);
2020-07-25 18:33:39 +02:00
}
2020-07-23 18:34:35 +02:00
switch (cmd[3]) {
case 'T': newstate=true; break;
case 'C': newstate=false; break;
case '2': newstate=!Turnout::isActive(id);
}
Turnout::activate(id,newstate);
2020-07-25 18:33:39 +02:00
StringFormatter::send(stream, F("PTA%c%d\n"),newstate?'4':'2',id );
2020-06-27 16:36:32 +02:00
}
break;
2020-08-20 18:16:47 +02:00
case 'N': // Heartbeat (2), only send if connection completed by 'HU' message
if (initSent) {
StringFormatter::send(stream, F("*%d\n"),HEARTBEAT_SECONDS); // return timeout value
2020-08-20 18:16:47 +02:00
}
2020-06-27 16:36:32 +02:00
break;
case 'M': // multithrottle
multithrottle(stream, cmd);
break;
case 'H': // send initial connection info after receiving "HU" message
if (cmd[1] == 'U') {
StringFormatter::send(stream,F("VN2.0\nHTDCC-EX\nRL0\n"));
StringFormatter::send(stream,F("HtDCC-EX v%S, %S, %S, %S\n"), F(VERSION), F(ARDUINO_TYPE), DCC::getMotorShieldName(), F(GITHUB_SHA));
if (annotateLeftRight) StringFormatter::send(stream,F("PTT]\\[Turnouts}|{Turnout]\\[Left}|{2]\\[Right}|{4\n"));
else StringFormatter::send(stream,F("PTT]\\[Turnouts}|{Turnout]\\[Closed}|{2]\\[Thrown}|{4\n"));
2020-08-20 18:16:47 +02:00
StringFormatter::send(stream,F("PPA%x\n"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON);
lastPowerState = (DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON); //remember power state sent for comparison later
StringFormatter::send(stream,F("*%d\n"),HEARTBEAT_SECONDS);
2020-08-20 18:16:47 +02:00
initSent = true;
}
2020-06-29 14:45:16 +02:00
break;
2020-07-03 18:35:32 +02:00
case 'Q': //
2020-08-14 16:43:35 +02:00
LOOPLOCOS('*', -1) { // tell client to drop any locos still assigned to this WiThrottle
if (myLocos[loco].throttle!='\0') {
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), myLocos[loco].throttle, LorS(myLocos[loco].cab), myLocos[loco].cab);
}
}
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d) Quit"),millis(),clientid);
2020-06-29 14:45:16 +02:00
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);
}
2021-03-09 21:44:44 +01:00
void WiThrottle::multithrottle(RingStream * 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("Multithrottle aval=%c cab=%d"), aval[0],locoid);
2020-06-27 16:36:32 +02:00
switch(cmd[2]) {
case '+': // add loco request
2021-03-09 21:44:44 +01:00
if (cmd[3]=='*') {
2021-03-11 14:35:47 +01:00
// M+* means get loco from prog track, then join tracks ready to drive away
// Stash the things the callback will need later
2021-03-09 21:44:44 +01:00
stashStream= stream;
stashClient=stream->peekTargetMark();
stashThrottleChar=throttleChar;
stashInstance=this;
2021-03-11 14:35:47 +01:00
// ask DCC to call us back when the loco id has been read
DCC::getLocoId(getLocoCallback); // will remove any previous join
return; // return nothing in stream as response is sent later in the callback
2021-03-09 21:44:44 +01:00
}
//return error if address zero requested
if (locoid==0) {
StringFormatter::send(stream, F("HMAddress '0' not supported!\n"), cmd[3] ,locoid);
return;
}
//return error if L or S from request doesn't match DCC++ assumptions
if (cmd[3] != LorS(locoid)) {
StringFormatter::send(stream, F("HMLength '%c' not valid for %d!\n"), cmd[3] ,locoid);
return;
}
2020-08-20 18:16:47 +02:00
//use first empty "slot" on this client's list, will be added to DCC registration list
2020-08-14 16:43:35 +02:00
for (int loco=0;loco<MAX_MY_LOCO;loco++) {
if (myLocos[loco].throttle=='\0') {
2020-06-27 16:36:32 +02:00
myLocos[loco].throttle=throttleChar;
myLocos[loco].cab=locoid;
2020-08-14 16:43:35 +02:00
StringFormatter::send(stream, F("M%c+%c%d<;>\n"), throttleChar, cmd[3] ,locoid); //tell client to add loco
2020-10-12 23:24:37 +02:00
//Get known Fn states from DCC
2020-10-13 19:01:11 +02:00
for(int fKey=0; fKey<=28; fKey++) {
2020-10-12 23:24:37 +02:00
int fstate=DCC::getFn(locoid,fKey);
2021-03-12 11:38:30 +01:00
if (fstate>=0) StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"),throttleChar,cmd[3],locoid,fstate,fKey);
2020-10-12 23:24:37 +02:00
}
2020-08-20 18:16:47 +02:00
StringFormatter::send(stream, F("M%cA%c%d<;>V%d\n"), throttleChar, cmd[3], locoid, DCCToWiTSpeed(DCC::getThrottleSpeed(locoid)));
StringFormatter::send(stream, F("M%cA%c%d<;>R%d\n"), throttleChar, cmd[3], locoid, DCC::getThrottleDirection(locoid));
StringFormatter::send(stream, F("M%cA%c%d<;>s1\n"), throttleChar, cmd[3], locoid); //default speed step 128
2020-08-20 18:16:47 +02:00
return;
2020-06-27 16:36:32 +02:00
}
}
2020-08-20 18:16:47 +02:00
StringFormatter::send(stream, F("HMMax locos (%d) exceeded, %d not added!\n"), MAX_MY_LOCO ,locoid);
2020-06-27 16:36:32 +02:00
break;
2020-08-14 16:43:35 +02:00
case '-': // remove loco(s) from this client (leave in DCC registration)
2020-06-27 16:36:32 +02:00
LOOPLOCOS(throttleChar, locoid) {
myLocos[loco].throttle='\0';
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab);
2020-06-27 16:36:32 +02:00
}
break;
case 'A':
locoAction(stream,aval, throttleChar, locoid);
}
}
void WiThrottle::locoAction(RingStream * stream, byte* aval, char throttleChar, int cab){
2020-06-27 16:36:32 +02:00
// Note cab=-1 for all cabs in the consist called throttleChar.
// DIAG(F("Loco 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
{
int witSpeed=getInt(aval+1);
2020-06-27 16:36:32 +02:00
LOOPLOCOS(throttleChar, cab) {
DCC::setThrottle(myLocos[loco].cab, WiTToDCCSpeed(witSpeed), DCC::getThrottleDirection(myLocos[loco].cab));
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, witSpeed);
2020-06-27 16:36:32 +02:00
}
}
break;
case 'F': //F onOff function
{
bool funcstate;
bool pressed=aval[1]=='1';
int fKey = getInt(aval+2);
LOOPLOCOS(throttleChar, cab) {
funcstate = DCC::changeFn(myLocos[loco].cab, fKey, pressed);
if(funcstate==0 || funcstate==1)
StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"), throttleChar, LorS(myLocos[loco].cab),
myLocos[loco].cab, funcstate, fKey);
}
2020-06-27 16:36:32 +02:00
}
break;
case 'q':
if (aval[1]=='V') { //qV
LOOPLOCOS(throttleChar, cab) {
2020-08-20 18:16:47 +02:00
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, DCCToWiTSpeed(DCC::getThrottleSpeed(myLocos[loco].cab)));
2020-06-27 16:36:32 +02:00
}
}
else if (aval[1]=='R') { // qR
LOOPLOCOS(throttleChar, cab) {
StringFormatter::send(stream,F("M%cA%c%d<;>R%d\n"), throttleChar, LorS(myLocos[loco].cab), 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);
2020-08-20 18:16:47 +02:00
StringFormatter::send(stream,F("M%cA%c%d<;>R%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, forward);
}
2020-06-27 16:36:32 +02:00
}
break;
case 'X':
//Emergency Stop (speed code 1)
2020-06-27 16:36:32 +02:00
LOOPLOCOS(throttleChar, cab) {
2020-08-20 18:16:47 +02:00
DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab));
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, -1);
}
break;
case 'I': // Idle, set speed to 0
case 'Q': // Quit, set speed to 0
2020-06-27 16:36:32 +02:00
LOOPLOCOS(throttleChar, cab) {
2020-08-20 18:16:47 +02:00
DCC::setThrottle(myLocos[loco].cab, 0, DCC::getThrottleDirection(myLocos[loco].cab));
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, 0);
}
break;
2020-06-27 16:36:32 +02:00
}
}
2020-08-20 18:16:47 +02:00
// convert between DCC++ speed values and WiThrottle speed values
int WiThrottle::DCCToWiTSpeed(int DCCSpeed) {
if (DCCSpeed == 0) return 0; //stop is stop
if (DCCSpeed == 1) return -1; //eStop value
return DCCSpeed - 1; //offset others by 1
}
// convert between WiThrottle speed values and DCC++ speed values
int WiThrottle::WiTToDCCSpeed(int WiTSpeed) {
if (WiTSpeed == 0) return 0; //stop is stop
if (WiTSpeed == -1) return 1; //eStop value
return WiTSpeed + 1; //offset others by 1
}
void WiThrottle::loop(RingStream * stream) {
2020-06-27 16:36:32 +02:00
// for each WiThrottle, check the heartbeat
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle)
wt->checkHeartbeat();
// TODO... any broadcasts to be done
(void)stream;
/* MUST follow this model in this loop.
* stream->mark();
* send 1 digit client id, and any data
* stream->commit()
*/
2020-06-27 16:36:32 +02:00
}
void WiThrottle::checkHeartbeat() {
// if eStop time passed... eStop any locos still assigned to this client and then drop the connection
if(heartBeatEnable && (millis()-heartBeat > ESTOP_SECONDS*1000)) {
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d) eStop(%ds) timeout, drop connection"), millis(), clientid, ESTOP_SECONDS);
2020-08-20 18:16:47 +02:00
LOOPLOCOS('*', -1) {
if (myLocos[loco].throttle!='\0') {
if (Diag::WITHROTTLE) DIAG(F("%l eStopping cab %d"),millis(),myLocos[loco].cab);
2020-08-20 18:16:47 +02:00
DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab)); // speed 1 is eStop
}
2020-06-29 14:03:08 +02:00
}
2020-06-29 14:45:16 +02:00
delete this;
}
2020-06-27 16:36:32 +02:00
}
char WiThrottle::LorS(int cab) {
return (cab<127)?'S':'L';
2021-03-11 14:35:47 +01:00
}
// Drive Away feature. Callback handling
RingStream * WiThrottle::stashStream;
WiThrottle * WiThrottle::stashInstance;
byte WiThrottle::stashClient;
char WiThrottle::stashThrottleChar;
void WiThrottle::getLocoCallback(int locoid) {
stashStream->mark(stashClient);
if (locoid<0) StringFormatter::send(stashStream,F("HMNo loco found on prog track\n"));
else {
char addcmd[20]={'M',stashThrottleChar,'+',LorS(locoid) };
itoa(locoid,addcmd+4,10);
stashInstance->multithrottle(stashStream, (byte *)addcmd);
DCCWaveform::progTrack.setPowerMode(POWERMODE::ON);
DCC::setProgTrackSyncMain(true); // <1 JOIN> so we can drive loco away
}
stashStream->commit();
}