Skip to content

Commit

Permalink
Changes for compatibility with Installer
Browse files Browse the repository at this point in the history
  • Loading branch information
dexslab committed Oct 4, 2020
2 parents a8000aa + 6feda4e commit f0314e9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
3 changes: 3 additions & 0 deletions DCC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "DCC.h"
#include "DCCWaveform.h"
#include "DIAG.h"
#include "GITHUB_SHA.h"
#include "version.h"


// This module is responsible for converting API calls into
Expand All @@ -45,6 +47,7 @@ __FlashStringHelper* DCC::shieldName=NULL;

void DCC::begin(const __FlashStringHelper* motorShieldName, MotorDriver * mainDriver, MotorDriver* progDriver, byte timerNumber) {
shieldName=(__FlashStringHelper*)motorShieldName;
DIAG(F("<iDCC-EX V-%S / %S / %S G-%S>\n"), F(VERSION), F(ARDUINO_TYPE), shieldName, F(GITHUB_SHA));
DCCWaveform::begin(mainDriver,progDriver, timerNumber);
}

Expand Down
2 changes: 2 additions & 0 deletions DCC.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class DCC
#define ARDUINO_TYPE "MEGA"
#elif defined(ARDUINO_ARCH_MEGAAVR)
#define ARDUINO_TYPE "UNOWIFIR2"
#elif defined(ARDUINO_SAMD_ZERO)
#define ARDUINO_TYPE "FireBoxMK1"
#else
#error CANNOT COMPILE - DCC++ EX ONLY WORKS WITH AN ARDUINO UNO, NANO 328, OR ARDUINO MEGA 1280/2560
#endif
Expand Down
27 changes: 14 additions & 13 deletions WiThrottle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#include "StringFormatter.h"
#include "Turnouts.h"
#include "DIAG.h"
#include "GITHUB_SHA.h"
#include "version.h"

#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))
Expand Down Expand Up @@ -74,7 +76,7 @@ bool WiThrottle::areYouUsingThrottle(int cab) {
// One instance of WiThrottle per connected client, so we know what the locos are

WiThrottle::WiThrottle( int wificlientid) {
if (Diag::WITHROTTLE) DIAG(F("\nCreating new WiThrottle for client %d\n"),wificlientid);
if (Diag::WITHROTTLE) DIAG(F("\n%l Creating new WiThrottle for client %d\n"),millis(),wificlientid);
nextThrottle=firstThrottle;
firstThrottle= this;
clientid=wificlientid;
Expand Down Expand Up @@ -110,7 +112,7 @@ void WiThrottle::parse(Print & stream, byte * cmdx) {
byte * cmd=local;

heartBeat=millis();
if (Diag::WITHROTTLE) DIAG(F("\nWiThrottle(%d)<-[%e]\n"),clientid, cmd);
if (Diag::WITHROTTLE) DIAG(F("\n%l WiThrottle(%d)<-[%e]\n"),millis(),clientid,cmd);

if (initSent) {
// Send power state if different than last sent
Expand Down Expand Up @@ -164,20 +166,21 @@ void WiThrottle::parse(Print & stream, byte * cmdx) {
break;
case 'N': // Heartbeat (2), only send if connection completed by 'HU' message
if (initSent) {
StringFormatter::send(stream, F("*%d\n"),HEARTBEAT_TIMEOUT); // return timeout value
StringFormatter::send(stream, F("*%d\n"),HEARTBEAT_SECONDS); // return timeout value
}
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("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"));
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_TIMEOUT);
StringFormatter::send(stream,F("*%d\n"),HEARTBEAT_SECONDS);
initSent = true;
}
break;
Expand All @@ -187,7 +190,7 @@ void WiThrottle::parse(Print & stream, byte * cmdx) {
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), myLocos[loco].throttle, LorS(myLocos[loco].cab), myLocos[loco].cab);
}
}
if (Diag::WITHROTTLE) DIAG(F("WiThrottle(%d) Quit\n"), clientid);
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d) Quit\n"),millis(),clientid);
delete this;
break;
}
Expand Down Expand Up @@ -343,19 +346,17 @@ void WiThrottle::loop() {
}

void WiThrottle::checkHeartbeat() {
// if 2 heartbeats missed... drop connection and eStop any locos still assigned to this client
if(heartBeatEnable && (millis()-heartBeat > HEARTBEAT_TIMEOUT*2000)) {
if (Diag::WITHROTTLE) DIAG(F("\n\nWiThrottle(%d) hearbeat missed, dropping connection\n\n"),clientid);
// 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("\n\n%l WiThrottle(%d) eStop(%ds) timeout, drop connection\n"), millis(), clientid, ESTOP_SECONDS);
LOOPLOCOS('*', -1) {
if (myLocos[loco].throttle!='\0') {
if (Diag::WITHROTTLE) DIAG(F(" eStopping cab %d\n"), myLocos[loco].cab);
if (Diag::WITHROTTLE) DIAG(F("%l eStopping cab %d\n"),millis(),myLocos[loco].cab);
DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab)); // speed 1 is eStop
}
}
delete this;
} else {
// TODO Check if anything has changed on my locos since last notified!
}
}
}

char WiThrottle::LorS(int cab) {
Expand Down
5 changes: 3 additions & 2 deletions WiThrottle.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class WiThrottle {
WiThrottle( int wifiClientId);
~WiThrottle();

static const int MAX_MY_LOCO=10; //maximum number of locos assigned to a single client
static const int HEARTBEAT_TIMEOUT=2;// heartbeat at 2secs to provide messaging transport
static const int MAX_MY_LOCO=10; // maximum number of locos assigned to a single client
static const int HEARTBEAT_SECONDS=4; // heartbeat at 4secs to provide messaging transport
static const int ESTOP_SECONDS=8; // eStop if no incoming messages for more than 8secs
static WiThrottle* firstThrottle;
static int getInt(byte * cmd);
static int getLocoId(byte * cmd);
Expand Down
4 changes: 3 additions & 1 deletion defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
//
// WIFI_ON: All prereqs for running with WIFI are met
//
#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560))

#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO))
#define WIFI_ON true
#else
#define WIFI_ON false

#endif

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit f0314e9

Please sign in to comment.