mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 08:06:13 +01:00
DCC API file tidying
Cruft removal and replace (most) DCC message codes with named constants.
This commit is contained in:
parent
257a81f969
commit
23f15131a8
14
CVReader.ino
14
CVReader.ino
|
@ -24,13 +24,13 @@ void setup() {
|
|||
Serial.begin(115200);
|
||||
DCC::begin();
|
||||
|
||||
DIAG(F("\n===== CVReader begin ==============================\n"));
|
||||
|
||||
for (byte x = 0; x < sizeof(cvnums) / sizeof(cvnums[0]); x++) {
|
||||
int value = DCC::readCV(cvnums[x]);
|
||||
DIAG(F("\nCV %d = %d 0x%x %s\n"), cvnums[x], value, value, value >= 0 ? " VERIFIED OK" : "FAILED VERIFICATION");
|
||||
}
|
||||
DIAG(F("\n===== CVReader done ==============================\n"));
|
||||
// DIAG(F("\n===== CVReader begin ==============================\n"));
|
||||
//
|
||||
// for (byte x = 0; x < sizeof(cvnums) / sizeof(cvnums[0]); x++) {
|
||||
// int value = DCC::readCV(cvnums[x]);
|
||||
// DIAG(F("\nCV %d = %d 0x%x %s\n"), cvnums[x], value, value, value >= 0 ? " VERIFIED OK" : "FAILED VERIFICATION");
|
||||
// }
|
||||
// DIAG(F("\n===== CVReader done ==============================\n"));
|
||||
DIAG(F("\nReady for JMRI commands\n"));
|
||||
}
|
||||
|
||||
|
|
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
|
||||
|
|
16
DCC.h
16
DCC.h
|
@ -14,7 +14,9 @@ class DCC {
|
|||
static void setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection);
|
||||
static int readCV(int cv);
|
||||
static bool writeCVByte(int cv, byte bValue) ;
|
||||
static bool verifyCVByte(int cv,byte bValue);
|
||||
static bool writeCVBit(int cv, byte bNum, bool bValue);
|
||||
static bool verifyCVBit(int cv, byte bNum, bool bValue);
|
||||
static void writeCVByteMain(int cab, int cv, byte bValue);
|
||||
static void writeCVBitMain(int cab, int cv, byte bNum, bool bValue);
|
||||
static void setFunction( int cab, byte fByte, byte eByte);
|
||||
|
@ -28,13 +30,23 @@ private:
|
|||
byte speed;
|
||||
bool forward;
|
||||
};
|
||||
static bool verifyCV(int cv,byte bValue);
|
||||
static void setThrottle2( uint16_t cab, uint8_t tSpeed, bool tDirection);
|
||||
static void updateLocoReminder(int loco, byte tSpeed, bool forward);
|
||||
static int nextLoco;
|
||||
static LOCO speedTable[MAX_LOCOS];
|
||||
static byte cv1(byte opcode, int cv);
|
||||
static byte cv2(int cv);
|
||||
|
||||
|
||||
// NMRA codes #
|
||||
static const byte SET_SPEED=0x3f;
|
||||
static const byte WRITE_BYTE_MAIN = 0xEC;
|
||||
static const byte WRITE_BIT_MAIN = 0xE8;
|
||||
static const byte WRITE_BYTE = 0x7C;
|
||||
static const byte VERIFY_BYTE= 0x74;
|
||||
static const byte BIT_MANIPULATE=0x78;
|
||||
static const byte WRITE_BIT=0xF0;
|
||||
static const byte VERIFY_BIT=0xE0;
|
||||
static const byte BIT_ON=0x08;
|
||||
static const byte BIT_OFF=0x00;
|
||||
};
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue
Block a user