diff --git a/DCC.cpp b/DCC.cpp index c3cd66f..d857cb8 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -153,12 +153,14 @@ uint8_t DCC::getThrottleSpeedByte(int cab) { return speedTable[reg].speedCode; } -// returns -1 for fault, 0 to 3 for frequency -int8_t DCC::getThrottleFrequency(int cab) { +// returns 0 to 3 for frequency +uint8_t DCC::getThrottleFrequency(int cab) { int reg=lookupSpeedTable(cab); if (reg<0) - return -1; - return (int8_t)(speedTable[reg].functions >>29); // shift out first 29 bits so we have the "frequency bits" left + return 0; // use default frequency + uint8_t res = (uint8_t)(speedTable[reg].functions >>30); + DIAG(F("Speed table %d functions %l shifted %d"), reg, speedTable[reg].functions, res); + return res; // shift out first 29 bits so we have the "frequency bits" left } // returns direction on loco @@ -191,9 +193,12 @@ bool DCC::setFn( int cab, int16_t functionNumber, bool on) { b[nB++] = functionNumber >>7 ; // high order bits } DCCWaveform::mainTrack.schedulePacket(b, nB, 4); - return true; } - + // We use the reminder table up to 28 for normal functions. + // We use 29 to 31 for DC frequency as well. + if (functionNumber > 31) + return true; + int reg = lookupSpeedTable(cab); if (reg<0) return false; @@ -206,7 +211,7 @@ bool DCC::setFn( int cab, int16_t functionNumber, bool on) { } else { speedTable[reg].functions &= ~funcmask; } - if (speedTable[reg].functions != previous) { + if (speedTable[reg].functions != previous && functionNumber > 28) { updateGroupflags(speedTable[reg].groupFlags, functionNumber); CommandDistributor::broadcastLoco(reg); } diff --git a/DCC.h b/DCC.h index bccdd24..b5cb0e9 100644 --- a/DCC.h +++ b/DCC.h @@ -61,7 +61,7 @@ public: static void setThrottle(uint16_t cab, uint8_t tSpeed, bool tDirection); static int8_t getThrottleSpeed(int cab); static uint8_t getThrottleSpeedByte(int cab); - static int8_t getThrottleFrequency(int cab); + static uint8_t getThrottleFrequency(int cab); static bool getThrottleDirection(int cab); static void writeCVByteMain(int cab, int cv, byte bValue); static void writeCVBitMain(int cab, int cv, byte bNum, bool bValue); diff --git a/DCCTimerAVR.cpp b/DCCTimerAVR.cpp index b27a906..90d43a7 100644 --- a/DCCTimerAVR.cpp +++ b/DCCTimerAVR.cpp @@ -142,9 +142,9 @@ void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t fbits) { if (pin == 9 || pin == 10) { // timer 2 is different if (fbits >= 3) - abits = B11; + abits = B00000011; else - abits = B01; + abits = B00000001; if (fbits >= 3) bbits = B0001; diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index 42646fe..1ccc6e5 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "devel-202312251647Z" +#define GITHUB_SHA "devel-202312310824Z" diff --git a/MotorDriver.cpp b/MotorDriver.cpp index c1c9492..265c8ed 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -349,6 +349,7 @@ void MotorDriver::setDCSignal(byte speedcode, uint8_t frequency /*default =0*/) } } #endif + DIAG(F("Brake %d freqencybits %x"), brakePin, f); DCCTimer::DCCEXanalogWriteFrequency(brakePin, f); // set DC PWM frequency to 100Hz XXX May move to setup DCCTimer::DCCEXanalogWrite(brakePin,brake); #else // all AVR here diff --git a/TrackManager.cpp b/TrackManager.cpp index f7be01a..e2d4d27 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -202,7 +202,7 @@ void TrackManager::setDCSignal(int16_t cab, byte speedbyte) { FOR_EACH_TRACK(t) { if (trackDCAddr[t]!=cab && cab != 0) continue; if (track[t]->getMode() & TRACK_MODE_DC) - track[t]->setDCSignal(speedbyte); + track[t]->setDCSignal(speedbyte, DCC::getThrottleFrequency(trackDCAddr[t])); } } @@ -348,11 +348,8 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr } void TrackManager::applyDCSpeed(byte t) { - int8_t frequency = DCC::getThrottleFrequency(trackDCAddr[t]); - if (frequency <0) // loco was not found - frequency = 0; // default - uint8_t speedByte = DCC::getThrottleSpeedByte(trackDCAddr[t]); - track[t]->setDCSignal(speedByte, (uint8_t)frequency); + track[t]->setDCSignal(DCC::getThrottleSpeedByte(trackDCAddr[t]), + DCC::getThrottleFrequency(trackDCAddr[t])); } bool TrackManager::parseEqualSign(Print *stream, int16_t params, int16_t p[])