diff --git a/DCC.cpp b/DCC.cpp index 6a33585..ecc11af 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -47,7 +47,7 @@ void DCC::begin() { } void DCC::setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection) { - byte speedCode = tSpeed + (tSpeed > 0) + tDirection * 128; // max speed is 126, but speed codes range from 2-127 (0=stop, 1=emergency stop) + byte speedCode = (tSpeed & 0x7F) + tDirection * 128; //speed codes range from 2-127 (0=stop, 1=emergency stop) setThrottle2(cab, speedCode); // retain speed for loco reminders updateLocoReminder(cab, speedCode ); diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 3df40e7..2f57bd9 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -141,10 +141,18 @@ void DCCEXParser::parse(Print & stream, const byte *com, bool blocking) { switch(opcode) { case '\0': return; // filterCallback asked us to ignore case 't': // THROTTLE - DCC::setThrottle(p[1],p[2],p[3]); - StringFormatter::send(stream,F(""), p[0], p[2],p[3]); + { + // Convert JMRI bizarre -1=emergency stop, 0-126 as speeds + // to DCC 0=stop, 1= emergency stop, 2-127 speeds + int tspeed=p[2]; + if (tspeed>126 | tspeed<-1) break; // invalid JMRI speed code + if (tspeed<0) tspeed=1; // emergency stop DCC speed + else if (tspeed>0) tspeed++; // map 1-126 -> 2-127 + DCC::setThrottle(p[1],tspeed,p[3]); + // report speed 0 after emergency stop + StringFormatter::send(stream,F(""), p[0], p[2]<0?0:p[2],p[3]); return; - + } case 'f': // FUNCTION if (parsef(stream,params,p)) return; break; diff --git a/WiThrottle.cpp b/WiThrottle.cpp index 09419ce..df252f9 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -117,7 +117,7 @@ void WiThrottle::parse(Print & stream, byte * cmdx) { 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]); + StringFormatter::send(stream, F("PPA%c\n"),cmd[3]); } else if (cmd[1]=='T' && cmd[2]=='A') { // PTA accessory toggle // TODO... if we are given an address that is not a known Turnout... @@ -236,7 +236,7 @@ void WiThrottle::locoAction(Print & stream, byte* aval, char throttleChar, int c } break; case 'X': - //Emergency Stop (TODO check we have the correct speed code here) + //Emergency Stop (speed code 1) LOOPLOCOS(throttleChar, cab) { DCC::setThrottle(myLocos[loco].cab,1, DCC::getThrottleDirection(myLocos[loco].cab)); }