1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-23 08:06:13 +01:00

WiThrottle ACTUALLY WORKS

This commit is contained in:
Asbelos 2020-06-29 13:03:08 +01:00
parent 6a923c4302
commit 20e8715fc6
2 changed files with 17 additions and 30 deletions

11
DCC.cpp
View File

@ -38,6 +38,7 @@ void DCC::setThrottle2( uint16_t cab, byte speedCode) {
uint8_t b[4]; uint8_t b[4];
uint8_t nB = 0; uint8_t nB = 0;
// DIAG(F("\nsetSpeedInternal %d %x"),cab,speedCode);
if (cab > 127) if (cab > 127)
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
@ -74,8 +75,8 @@ bool DCC::getThrottleDirection(int cab) {
return (speedTable[reg].speedCode & 0x80) !=0; return (speedTable[reg].speedCode & 0x80) !=0;
} }
static void DCC::setFn( int cab, byte functionNumber, bool on) { void DCC::setFn( int cab, byte functionNumber, bool on) {
if (cab<=0 || functionNumber<0 || functionNumber>28) return; if (cab<=0 || functionNumber>28) return;
int reg = lookupSpeedTable(cab); int reg = lookupSpeedTable(cab);
if (reg<0) return; if (reg<0) return;
// set the function on/off in the functions and set the group flag to // set the function on/off in the functions and set the group flag to
@ -94,9 +95,9 @@ static void DCC::setFn( int cab, byte functionNumber, bool on) {
void DCC::setAccessory(int address, byte number, bool activate) { void DCC::setAccessory(int address, byte number, bool activate) {
// use masks to detect wrong values and do nothing // use masks to detect wrong values and do nothing
if(address != address & 511) if(address != (address & 511))
return; return;
if(number != number & 3) if(number != (number & 3))
return; return;
byte b[2]; byte b[2];
@ -310,7 +311,7 @@ bool DCC::issueReminder(int reg) {
break; break;
case 1: // remind function group 1 (F0-F4) case 1: // remind function group 1 (F0-F4)
if (flags & FN_GROUP_1) if (flags & FN_GROUP_1)
setFunctionInternal(loco,0, 128 + ((functions>>1)& 0x0F) | (functions & 0x01)<<4); setFunctionInternal(loco,0, 128 | ((functions>>1)& 0x0F) | ((functions & 0x01)<<4));
break; break;
case 2: // remind function group 2 F5-F8 case 2: // remind function group 2 F5-F8
if (flags & FN_GROUP_2) if (flags & FN_GROUP_2)

View File

@ -115,8 +115,9 @@ void WiThrottle::multithrottle(Print & stream, byte * cmd){
int locoid=getLocoId(cmd+3); // -1 for * int locoid=getLocoId(cmd+3); // -1 for *
byte * aval=cmd; byte * aval=cmd;
while(*aval !=';' && *aval !='\0') aval++; while(*aval !=';' && *aval !='\0') aval++;
if (*aval) aval++; if (*aval) aval+=2; // skip ;>
DIAG(F("\nMultithrottle aval=%c cab=%d"), aval[0],locoid);
switch(cmd[2]) { switch(cmd[2]) {
case '+': // add loco case '+': // add loco
for (int loco=0;loco<MAX_MY_LOCO;loco++) { for (int loco=0;loco<MAX_MY_LOCO;loco++) {
@ -146,27 +147,9 @@ void WiThrottle::multithrottle(Print & stream, byte * cmd){
} }
} }
/*** TODO provide add feedback ***
void locoAdd(String th, String actionKey, int i) {
LocoThrottle[Throttle] = actionKey;
client[i].println("M"+th+"+"+actionKey+"<;>");
for(fKey=0; fKey<29; fKey++){
LocoState[Throttle][fKey] =0;
client[i].println("M"+th+"A"+actionKey+"<;>F0"+String(fKey));
}
client[i].println("M"+th+"+"+actionKey+"<;>V0");
client[i].println("M"+th+"+"+actionKey+"<;>R1");
client[i].println("M"+th+"+"+actionKey+"<;>s1");
}
*********/
void WiThrottle::locoAction(Print & stream, byte* aval, char throttleChar, int cab){ void WiThrottle::locoAction(Print & stream, byte* aval, char throttleChar, int cab){
// Note cab=-1 for all cabs in the consist called throttleChar. // Note cab=-1 for all cabs in the consist called throttleChar.
DIAG(F("\nLoco Action aval=%c throttleChar=%c, cab=%d"), aval[0],throttleChar, cab);
switch (aval[0]) { switch (aval[0]) {
case 'V': // Vspeed case 'V': // Vspeed
{ {
@ -228,11 +211,14 @@ void WiThrottle::loop() {
void WiThrottle::checkHeartbeat() { void WiThrottle::checkHeartbeat() {
if(millis()-heartBeat > HEARTBEAT_TIMEOUT*1000) { if(millis()-heartBeat > HEARTBEAT_TIMEOUT*1000) {
// Haertbeat missed... STOP all locos for this client
for (int loco=0;loco<MAX_MY_LOCO;loco++) { for (int loco=0;loco<MAX_MY_LOCO;loco++) {
if (myLocos[loco].throttle!='\0') { if (myLocos[loco].throttle!='\0') {
DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab)); DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab));
// TODO Which stream??? Multiple clients ?? StringFormatter::send(stream,F("M%cAL%d<;>V0"),myLocos[loco].throttle,myLocos[loco].cab); }
}
} }
} }
else {
// TODO Check if anything has changed on my locos since last notified!
}
} }