1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00

tIMER WRAP PROTECTION and broadcast speed fix

This commit is contained in:
Asbelos 2020-06-16 11:06:36 +01:00
parent 506f9e4353
commit ffea04a499
4 changed files with 23 additions and 16 deletions

12
DCC.cpp
View File

@ -272,8 +272,15 @@ byte DCC::cv2(int cv) {
void DCC::updateLocoReminder(int loco, byte speedCode) { void DCC::updateLocoReminder(int loco, byte speedCode) {
// determine speed reg for this loco
int reg; int reg;
if (loco==0) {
// broadcast message
for (reg = 0; reg < MAX_LOCOS; reg++) speedTable[reg].speedCode = speedCode;
return;
}
// determine speed reg for this loco
int firstEmpty = MAX_LOCOS; int firstEmpty = MAX_LOCOS;
for (reg = 0; reg < MAX_LOCOS; reg++) { for (reg = 0; reg < MAX_LOCOS; reg++) {
if (speedTable[reg].loco == loco) break; if (speedTable[reg].loco == loco) break;
@ -299,7 +306,7 @@ int DCC::ackManagerCv;
byte DCC::ackManagerBitNum; byte DCC::ackManagerBitNum;
bool DCC::ackReceived; bool DCC::ackReceived;
int DCC::ackTriggerMilliamps; int DCC::ackTriggerMilliamps;
long DCC::ackPulseStart; unsigned long DCC::ackPulseStart;
ACK_CALLBACK DCC::ackManagerCallback; ACK_CALLBACK DCC::ackManagerCallback;
@ -393,7 +400,6 @@ void DCC::ackManagerLoop() {
if (ackPulseStart==0) return; // keep waiting for leading edge if (ackPulseStart==0) return; // keep waiting for leading edge
{ // detected trailing edge of pulse { // detected trailing edge of pulse
long pulseDuration=micros()-ackPulseStart; long pulseDuration=micros()-ackPulseStart;
// TODO handle timer wrapover
if (pulseDuration>4500 && pulseDuration<8000) { if (pulseDuration>4500 && pulseDuration<8000) {
ackReceived=true; ackReceived=true;
DCCWaveform::progTrack.killRemainingRepeats(); // probably no need after 8.5ms!! DCCWaveform::progTrack.killRemainingRepeats(); // probably no need after 8.5ms!!

2
DCC.h
View File

@ -72,7 +72,7 @@ private:
static byte ackManagerStash; static byte ackManagerStash;
static bool ackReceived; static bool ackReceived;
static int ackTriggerMilliamps; static int ackTriggerMilliamps;
static long ackPulseStart; static unsigned long ackPulseStart;
static ACK_CALLBACK ackManagerCallback; static ACK_CALLBACK ackManagerCallback;
static void ackManagerSetup(int cv, byte bitNumOrbyteValue, ackOp const program[], ACK_CALLBACK callback); static void ackManagerSetup(int cv, byte bitNumOrbyteValue, ackOp const program[], ACK_CALLBACK callback);
static void ackManagerLoop(); static void ackManagerLoop();

View File

@ -55,11 +55,12 @@ DCCWaveform::DCCWaveform( byte preambleBits, bool isMain) {
requiredPreambles = preambleBits; requiredPreambles = preambleBits;
bytes_sent = 0; bytes_sent = 0;
bits_sent = 0; bits_sent = 0;
nextSampleDue = 0; sampleDelay = 0;
lastSampleTaken = millis();
} }
void DCCWaveform::beginTrack() { void DCCWaveform::beginTrack() {
setPowerMode(POWERMODE::ON); setPowerMode(POWERMODE::ON);
} }
POWERMODE DCCWaveform::getPowerMode() { POWERMODE DCCWaveform::getPowerMode() {
@ -74,32 +75,31 @@ void DCCWaveform::setPowerMode(POWERMODE mode) {
void DCCWaveform::checkPowerOverload() { void DCCWaveform::checkPowerOverload() {
if (millis() < nextSampleDue) return; if (millis() - lastSampleTaken < sampleDelay) return;
int delay; lastSampleTaken = millis();
switch (powerMode) { switch (powerMode) {
case POWERMODE::OFF: case POWERMODE::OFF:
delay = POWER_SAMPLE_OFF_WAIT; sampleDelay = POWER_SAMPLE_OFF_WAIT;
break; break;
case POWERMODE::ON: case POWERMODE::ON:
// Check current // Check current
lastCurrent = Hardware::getCurrentMilliamps(isMainTrack); lastCurrent = Hardware::getCurrentMilliamps(isMainTrack);
if (lastCurrent < POWER_SAMPLE_MAX) delay = POWER_SAMPLE_ON_WAIT; if (lastCurrent < POWER_SAMPLE_MAX) sampleDelay = POWER_SAMPLE_ON_WAIT;
else { else {
setPowerMode(POWERMODE::OVERLOAD); setPowerMode(POWERMODE::OVERLOAD);
DIAG(F("\n*** %S TRACK POWER OVERLOAD current=%d max=%d ***\n"), isMainTrack ? F("MAIN") : F("PROG"), lastCurrent, POWER_SAMPLE_MAX); DIAG(F("\n*** %S TRACK POWER OVERLOAD current=%d max=%d ***\n"), isMainTrack ? F("MAIN") : F("PROG"), lastCurrent, POWER_SAMPLE_MAX);
delay = POWER_SAMPLE_OVERLOAD_WAIT; sampleDelay = POWER_SAMPLE_OVERLOAD_WAIT;
} }
break; break;
case POWERMODE::OVERLOAD: case POWERMODE::OVERLOAD:
// Try setting it back on after the OVERLOAD_WAIT // Try setting it back on after the OVERLOAD_WAIT
setPowerMode(POWERMODE::ON); setPowerMode(POWERMODE::ON);
delay = POWER_SAMPLE_ON_WAIT; sampleDelay = POWER_SAMPLE_ON_WAIT;
break; break;
default: default:
delay = 999; // cant get here..meaningless statement to avoid compiler warning. sampleDelay = 999; // cant get here..meaningless statement to avoid compiler warning.
} }
nextSampleDue = millis() + delay;
} }

View File

@ -78,6 +78,7 @@ class DCCWaveform {
// current sampling // current sampling
POWERMODE powerMode; POWERMODE powerMode;
unsigned long nextSampleDue; unsigned long lastSampleTaken;
int sampleDelay;
}; };
#endif #endif