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

Loco reminders speedcode

Saves one byte per loco and avoids recalculating the speed message bits each time
This commit is contained in:
Asbelos 2020-06-03 10:36:01 +01:00
parent 26dffa3be3
commit d42589aff5
2 changed files with 15 additions and 15 deletions

21
DCC.cpp
View File

@ -20,12 +20,13 @@ void DCC::begin() {
} }
void DCC::setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection) { void DCC::setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection) {
setThrottle2(cab, tSpeed, tDirection); byte speedCode= tSpeed + (tSpeed > 0) + tDirection * 128; // max speed is 126, but speed codes range from 2-127 (0=stop, 1=emergency stop)
setThrottle2(cab, speedCode);
// retain speed for loco reminders // retain speed for loco reminders
updateLocoReminder(cab, tSpeed, tDirection ); updateLocoReminder(cab, speedCode );
} }
void DCC::setThrottle2( uint16_t cab, uint8_t tSpeed, bool tDirection) { void DCC::setThrottle2( uint16_t cab, byte speedCode) {
uint8_t b[4]; uint8_t b[4];
uint8_t nB = 0; uint8_t nB = 0;
@ -34,7 +35,7 @@ void DCC::setThrottle2( uint16_t cab, uint8_t tSpeed, bool tDirection) {
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
b[nB++] = lowByte(cab); b[nB++] = lowByte(cab);
b[nB++] = SET_SPEED; // 128-step speed control byte b[nB++] = SET_SPEED; // 128-step speed control byte
b[nB++] = tSpeed + (tSpeed > 0) + tDirection * 128; // max speed is 126, but speed codes range from 2-127 (0=stop, 1=emergency stop) b[nB++] = speedCode; // for encoding see setThrottle
DCCWaveform::mainTrack.schedulePacket(b, nB, 0); DCCWaveform::mainTrack.schedulePacket(b, nB, 0);
} }
@ -157,14 +158,14 @@ void DCC::loop() {
// each time around the Arduino loop, we resend a loco speed packet reminder // each time around the Arduino loop, we resend a loco speed packet reminder
for (; nextLoco < MAX_LOCOS; nextLoco++) { for (; nextLoco < MAX_LOCOS; nextLoco++) {
if (speedTable[nextLoco].loco > 0) { if (speedTable[nextLoco].loco > 0) {
setThrottle2(speedTable[nextLoco].loco, speedTable[nextLoco].speed, speedTable[nextLoco].forward); setThrottle2(speedTable[nextLoco].loco, speedTable[nextLoco].speedCode);
nextLoco++; nextLoco++;
return; return;
} }
} }
for (nextLoco = 0; nextLoco < MAX_LOCOS; nextLoco++) { for (nextLoco = 0; nextLoco < MAX_LOCOS; nextLoco++) {
if (speedTable[nextLoco].loco > 0) { if (speedTable[nextLoco].loco > 0) {
setThrottle2(speedTable[nextLoco].loco, speedTable[nextLoco].speed, speedTable[nextLoco].forward); setThrottle2(speedTable[nextLoco].loco, speedTable[nextLoco].speedCode);
nextLoco++; nextLoco++;
return; return;
} }
@ -204,7 +205,7 @@ byte DCC::cv2(int cv) {
void DCC::updateLocoReminder(int loco, byte tSpeed, bool forward) { void DCC::updateLocoReminder(int loco, byte speedCode) {
// determine speed reg for this loco // determine speed reg for this loco
int reg; int reg;
int firstEmpty = MAX_LOCOS; int firstEmpty = MAX_LOCOS;
@ -218,8 +219,8 @@ void DCC::updateLocoReminder(int loco, byte tSpeed, bool forward) {
return; return;
} }
speedTable[reg].loco = loco; speedTable[reg].loco = loco;
speedTable[reg].speed = tSpeed; speedTable[reg].speedCode = speedCode;
speedTable[reg].forward = forward; }
}
DCC::LOCO DCC::speedTable[MAX_LOCOS]; DCC::LOCO DCC::speedTable[MAX_LOCOS];
int DCC::nextLoco = 0; int DCC::nextLoco = 0;

7
DCC.h
View File

@ -29,11 +29,10 @@ class DCC {
private: private:
struct LOCO { struct LOCO {
int loco; int loco;
byte speed; byte speedCode;
bool forward;
}; };
static void setThrottle2( uint16_t cab, uint8_t tSpeed, bool tDirection); static void setThrottle2( uint16_t cab, uint8_t speedCode);
static void updateLocoReminder(int loco, byte tSpeed, bool forward); static void updateLocoReminder(int loco, byte speedCode);
static int nextLoco; static int nextLoco;
static LOCO speedTable[MAX_LOCOS]; static LOCO speedTable[MAX_LOCOS];
static byte cv1(byte opcode, int cv); static byte cv1(byte opcode, int cv);