1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 21:01:25 +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) {
// determine speed reg for this loco
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;
for (reg = 0; reg < MAX_LOCOS; reg++) {
if (speedTable[reg].loco == loco) break;
@ -299,7 +306,7 @@ int DCC::ackManagerCv;
byte DCC::ackManagerBitNum;
bool DCC::ackReceived;
int DCC::ackTriggerMilliamps;
long DCC::ackPulseStart;
unsigned long DCC::ackPulseStart;
ACK_CALLBACK DCC::ackManagerCallback;
@ -393,7 +400,6 @@ void DCC::ackManagerLoop() {
if (ackPulseStart==0) return; // keep waiting for leading edge
{ // detected trailing edge of pulse
long pulseDuration=micros()-ackPulseStart;
// TODO handle timer wrapover
if (pulseDuration>4500 && pulseDuration<8000) {
ackReceived=true;
DCCWaveform::progTrack.killRemainingRepeats(); // probably no need after 8.5ms!!

2
DCC.h
View File

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

View File

@ -55,11 +55,12 @@ DCCWaveform::DCCWaveform( byte preambleBits, bool isMain) {
requiredPreambles = preambleBits;
bytes_sent = 0;
bits_sent = 0;
nextSampleDue = 0;
sampleDelay = 0;
lastSampleTaken = millis();
}
void DCCWaveform::beginTrack() {
setPowerMode(POWERMODE::ON);
}
POWERMODE DCCWaveform::getPowerMode() {
@ -74,32 +75,31 @@ void DCCWaveform::setPowerMode(POWERMODE mode) {
void DCCWaveform::checkPowerOverload() {
if (millis() < nextSampleDue) return;
int delay;
if (millis() - lastSampleTaken < sampleDelay) return;
lastSampleTaken = millis();
switch (powerMode) {
case POWERMODE::OFF:
delay = POWER_SAMPLE_OFF_WAIT;
sampleDelay = POWER_SAMPLE_OFF_WAIT;
break;
case POWERMODE::ON:
// Check current
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 {
setPowerMode(POWERMODE::OVERLOAD);
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;
case POWERMODE::OVERLOAD:
// Try setting it back on after the OVERLOAD_WAIT
setPowerMode(POWERMODE::ON);
delay = POWER_SAMPLE_ON_WAIT;
sampleDelay = POWER_SAMPLE_ON_WAIT;
break;
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
POWERMODE powerMode;
unsigned long nextSampleDue;
unsigned long lastSampleTaken;
int sampleDelay;
};
#endif