mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2025-07-28 09:53:45 +02:00
DCC API file tidying
Cruft removal and replace (most) DCC message codes with named constants.
This commit is contained in:
77
DCC.cpp
77
DCC.cpp
@@ -27,21 +27,20 @@ void DCC::setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection) {
|
||||
|
||||
void DCC::setThrottle2( uint16_t cab, uint8_t tSpeed, bool tDirection) {
|
||||
|
||||
uint8_t b[5];
|
||||
uint8_t b[4];
|
||||
uint8_t nB = 0;
|
||||
|
||||
if (cab > 127)
|
||||
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
|
||||
|
||||
b[nB++] = lowByte(cab);
|
||||
b[nB++] = 0x3F; // 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)
|
||||
|
||||
DCCWaveform::mainTrack.schedulePacket(b, nB, 0);
|
||||
}
|
||||
|
||||
void DCC::setFunction(int cab, byte byte1) {
|
||||
uint8_t b[4];
|
||||
uint8_t b[3];
|
||||
uint8_t nB = 0;
|
||||
|
||||
if (cab > 127)
|
||||
@@ -53,7 +52,7 @@ void DCC::setFunction(int cab, byte byte1) {
|
||||
}
|
||||
|
||||
void DCC::setFunction(int cab, byte byte1, byte byte2) {
|
||||
byte b[5];
|
||||
byte b[4];
|
||||
byte nB = 0;
|
||||
|
||||
if (cab > 127)
|
||||
@@ -66,7 +65,7 @@ void DCC::setFunction(int cab, byte byte1, byte byte2) {
|
||||
}
|
||||
|
||||
void DCC::setAccessory(int address, byte number, bool activate) {
|
||||
byte b[3]; // save space for checksum byte
|
||||
byte b[2];
|
||||
|
||||
b[0] = address % 64 + 128; // first byte is of the form 10AAAAAA, where AAAAAA represent 6 least signifcant bits of accessory address
|
||||
b[1] = ((((address / 64) % 8) << 4) + (number % 4 << 1) + activate % 2) ^ 0xF8; // second byte is of the form 1AAACDDD, where C should be 1, and the least significant D represent activate/deactivate
|
||||
@@ -75,13 +74,13 @@ void DCC::setAccessory(int address, byte number, bool activate) {
|
||||
}
|
||||
|
||||
void DCC::writeCVByteMain(int cab, int cv, byte bValue) {
|
||||
byte b[6]; // save space for checksum byte
|
||||
byte b[5];
|
||||
byte nB = 0;
|
||||
if (cab > 127)
|
||||
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
|
||||
|
||||
b[nB++] = lowByte(cab);
|
||||
b[nB++] = cv1(0xEC, cv); // any CV>1023 will become modulus(1024) due to bit-mask of 0x03
|
||||
b[nB++] = cv1(WRITE_BYTE_MAIN, cv); // any CV>1023 will become modulus(1024) due to bit-mask of 0x03
|
||||
b[nB++] = cv2(cv);
|
||||
b[nB++] = bValue;
|
||||
|
||||
@@ -89,7 +88,7 @@ void DCC::writeCVByteMain(int cab, int cv, byte bValue) {
|
||||
}
|
||||
|
||||
void DCC::writeCVBitMain(int cab, int cv, byte bNum, bool bValue) {
|
||||
byte b[6]; // save space for checksum byte
|
||||
byte b[5];
|
||||
byte nB = 0;
|
||||
bValue = bValue % 2;
|
||||
bNum = bNum % 8;
|
||||
@@ -98,52 +97,51 @@ void DCC::writeCVBitMain(int cab, int cv, byte bNum, bool bValue) {
|
||||
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
|
||||
|
||||
b[nB++] = lowByte(cab);
|
||||
b[nB++] = cv1(0xE8, cv); // any CV>1023 will become modulus(1024) due to bit-mask of 0x03
|
||||
b[nB++] = cv1(WRITE_BIT_MAIN, cv); // any CV>1023 will become modulus(1024) due to bit-mask of 0x03
|
||||
b[nB++] = cv2(cv);
|
||||
b[nB++] = 0xF0 + bValue * 8 + bNum;
|
||||
b[nB++] = WRITE_BIT | (bValue?BIT_ON:BIT_OFF) | bNum;
|
||||
|
||||
DCCWaveform::mainTrack.schedulePacket(b, nB, 4);
|
||||
}
|
||||
|
||||
bool DCC::writeCVByte(int cv, byte bValue) {
|
||||
uint8_t message[] = {cv1(0x7C, cv), cv2(cv), bValue};
|
||||
uint8_t message[] = {cv1(WRITE_BYTE, cv), cv2(cv), bValue};
|
||||
DCCWaveform::progTrack.schedulePacket(message, sizeof(message), 6); // NMRA recommends 6 write or reset packets for decoder recovery time
|
||||
return verifyCV(cv, bValue);
|
||||
return verifyCVByte(cv, bValue);
|
||||
}
|
||||
|
||||
bool DCC::verifyCVByte(int cv, byte value) {
|
||||
byte message[] = { cv1(VERIFY_BYTE, cv), cv2(cv), value};
|
||||
return DCCWaveform::progTrack.schedulePacketWithAck(message, sizeof(message), 5);
|
||||
}
|
||||
|
||||
bool DCC::writeCVBit(int cv, byte bNum, bool bValue) {
|
||||
bValue = bValue % 2;
|
||||
bNum = bNum % 8;
|
||||
uint8_t message[] = {cv1(0x78, cv), cv2(cv), 0xF0 + bValue * 8 + bNum};
|
||||
DCCWaveform::progTrack.schedulePacket(message, sizeof(message), 6); // NMRA recommends 6 write or reset packets for decoder recovery time
|
||||
|
||||
/* TODO... what is the verify opcode here?
|
||||
bitWrite(message[2],4,1); // change instruction code from Write Bit to Verify Bit
|
||||
DCCWaveform::progTrack.schedulePacket(message,sizeof(message),6); // NMRA recommends 6 write or reset packets for decoder recovery time
|
||||
*/
|
||||
return true; // <<<< NOT ACCURATE... see comment above
|
||||
if (bNum>=8) return false;
|
||||
byte instruction=WRITE_BIT | bValue?BIT_ON:BIT_OFF | bNum;
|
||||
byte message[] = {cv1(BIT_MANIPULATE, cv), cv2(cv), instruction };
|
||||
DCCWaveform::progTrack.schedulePacket(message, sizeof(message), 6); // NMRA recommends 6 write or reset packets for decoder recovery time
|
||||
return verifyCVBit(cv, bNum, bValue);
|
||||
}
|
||||
|
||||
bool DCC::verifyCVBit(int cv, byte bNum, bool bValue) {
|
||||
if (bNum>=8) return false;
|
||||
byte instruction=VERIFY_BIT | bValue?BIT_ON:BIT_OFF | bNum;
|
||||
byte message[] = {cv1(BIT_MANIPULATE, cv), cv2(cv), instruction };
|
||||
return DCCWaveform::progTrack.schedulePacketWithAck(message, sizeof(message), 5); // NMRA recommends 6 write or reset packets for decoder recovery time
|
||||
}
|
||||
|
||||
|
||||
|
||||
int DCC::readCV(int cv) {
|
||||
byte message[] = { cv1(0x78, cv) , // any CV>1023 will become modulus(1024) due to bit-mask of 0x03
|
||||
cv2(cv),
|
||||
0
|
||||
}; // trailing zero will be updated in loop below
|
||||
|
||||
byte value = 0;
|
||||
|
||||
// get each bit individually
|
||||
for (int i = 0; i < 8; i++) {
|
||||
message[2] = 0xE8 + i;
|
||||
bool one=DCCWaveform::progTrack.schedulePacketWithAck(message, sizeof(message), 4); // NMRA recommends 5 read packets
|
||||
value += one << i;
|
||||
// get each bit individually by validating against a one.
|
||||
for (int bNum = 0; bNum < 8; bNum++) {
|
||||
value += verifyCVBit(cv,bNum,true) << bNum;
|
||||
}
|
||||
|
||||
return verifyCV(cv, value) ? value : -1;
|
||||
return verifyCVByte(cv, value) ? value : -1;
|
||||
}
|
||||
|
||||
|
||||
void DCC::loop() {
|
||||
DCCWaveform::loop(); // powwer overload checks
|
||||
// if the main track transmitter still has a pending packet, skip this loop.
|
||||
@@ -167,6 +165,7 @@ void DCC::loop() {
|
||||
}
|
||||
|
||||
///// Private helper functions below here /////////////////////
|
||||
|
||||
byte DCC::cv1(byte opcode, int cv) {
|
||||
cv--;
|
||||
return (highByte(cv) & (byte)0x03) | opcode;
|
||||
@@ -176,11 +175,7 @@ byte DCC::cv2(int cv) {
|
||||
return lowByte(cv);
|
||||
}
|
||||
|
||||
bool DCC::verifyCV(int cv, byte value) {
|
||||
byte message[] = { cv1(0x74, cv), cv2(cv), value};
|
||||
DIAG(F("\n\nVerifying cv %d = %d"), cv, value);
|
||||
return DCCWaveform::progTrack.schedulePacketWithAck(message, sizeof(message), 5);
|
||||
}
|
||||
|
||||
|
||||
void DCC::updateLocoReminder(int loco, byte tSpeed, bool forward) {
|
||||
// determine speed reg for this loco
|
||||
|
Reference in New Issue
Block a user