diff --git a/DCC.cpp b/DCC.cpp index dec6646..01e6aa5 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -17,6 +17,13 @@ * You should have received a copy of the GNU General Public License * along with CommandStation. If not, see . */ + +#if __has_include ( "config.h") + #include "config.h" +#else + #include "config.example.h" +#endif + #include "DIAG.h" #include "DCC.h" #include "DCCWaveform.h" @@ -246,8 +253,17 @@ void DCC::setAccessory(int address, byte number, bool activate) { return; 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 + // first byte is of the form 10AAAAAA, where AAAAAA represent 6 least signifcant bits of accessory address + b[0] = address % 64 + 128; + // second byte is of the form 1AAACDDD, where C should be 1, and the least significant D represent activate/deactivate + // if we follow RCN-213, activate has to be reversed because in DCC++/DCC-EX activate=1 is "thrown, diverging", + // but in RCN-213, 1 means "closed, straight" and 0 "thown, diverging" +#ifdef TURNOUTS_RCN_213 + activate = !activate; +#else + #error fooo +#endif + b[1] = ((((address / 64) % 8) << 4) + (number % 4 << 1) + activate % 2) ^ 0xF8; DCCWaveform::mainTrack.schedulePacket(b, 2, 4); // Repeat the packet four times } diff --git a/WiThrottle.cpp b/WiThrottle.cpp index e9cfdfd..f8ac5da 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -170,19 +170,18 @@ void WiThrottle::parse(RingStream * stream, byte * cmdx) { StringFormatter::send(stream, F("HmTurnout %d created\n"),id); } switch (cmd[3]) { - // T and C according to RCN-213 where 0 is Stop, Red, Thrown, Diverging. - case 'T': - Turnout::setClosed(id,false); - break; - case 'C': - Turnout::setClosed(id,true); - break; - case '2': - Turnout::setClosed(id,!Turnout::isClosed(id)); - break; - default : - Turnout::setClosed(id,true); - break; + case 'T': + Turnout::setClosed(id,false); + break; + case 'C': + Turnout::setClosed(id,true); + break; + case '2': + Turnout::setClosed(id,!Turnout::isClosed(id)); + break; + default : + Turnout::setClosed(id,true); + break; } StringFormatter::send(stream, F("PTA%c%d\n"),Turnout::isClosed(id)?'2':'4',id ); } diff --git a/config.example.h b/config.example.h index 0debbc2..19e2928 100644 --- a/config.example.h +++ b/config.example.h @@ -129,4 +129,17 @@ The configuration file for DCC-EX Command Station #define SCROLLMODE 1 ///////////////////////////////////////////////////////////////////////////////////// +// +// DEFINE TURNOUTS/ACCESSORIES FOLLOW NORM RCN-213 +// +// According to norm RCN-213 a DCC packet with an 1 is closed/straight +// and one with a 0 is thrown/diverging. This is reversed to the +// definition from DCC++ in the DCC++ protocol. To make the states +// match with the norm, we need to reverse the bit in the DCC packet +// on the rails, but we don't want to cause havoc on existent layouts, +// so we define this only for new installations. For any new install +// there is no reason to not define this. +#define TURNOUTS_RCN_213 + +/////////////////////////////////////////////////////////////////////////////////////