diff --git a/DCCPacket.h b/DCCPacket.h index 261cff3..9ac480a 100644 --- a/DCCPacket.h +++ b/DCCPacket.h @@ -2,6 +2,10 @@ const byte MAX_PACKET_SIZE = 5; // NMRA standard extended packets, payload size WITHOUT checksum. +// Number of preamble bits (moved here so MotorDriver and Waveform know) +const int PREAMBLE_BITS_MAIN = 16; +const int PREAMBLE_BITS_PROG = 22; + class dccPacket { public: byte data[MAX_PACKET_SIZE+1]; // space for checksum if needed diff --git a/DCCRMT.cpp b/DCCRMT.cpp index 6a81c25..e26dd18 100644 --- a/DCCRMT.cpp +++ b/DCCRMT.cpp @@ -62,7 +62,7 @@ void IRAM_ATTR interrupt(rmt_channel_t channel, void *t) { tt->RMTinterrupt(); } -RMTChannel::RMTChannel(byte pin, byte ch, byte plen) { +RMTChannel::RMTChannel(byte pin, byte ch, byte plen, bool isMain) { // preamble preambleLen = plen+2; // plen 1 bits, one 0 bit and one EOF marker @@ -79,12 +79,17 @@ RMTChannel::RMTChannel(byte pin, byte ch, byte plen) { // idle idleLen = 28; idle = (rmt_item32_t*)malloc(idleLen*sizeof(rmt_item32_t)); - for (byte n=0; n<8; n++) // 0 to 7 - setDCCBit1(idle + n); - for (byte n=8; n<18; n++) // 8, 9 to 16, 17 - setDCCBit0(idle + n); - for (byte n=18; n<26; n++) // 18 to 25 - setDCCBit1(idle + n); + if (isMain) { + for (byte n=0; n<8; n++) // 0 to 7 + setDCCBit1(idle + n); + for (byte n=8; n<18; n++) // 8, 9 to 16, 17 + setDCCBit0(idle + n); + for (byte n=18; n<26; n++) // 18 to 25 + setDCCBit1(idle + n); + } else { + for (byte n=0; n<26; n++) // all zero + setDCCBit0(idle + n); + } setDCCBit1(idle + 26); // end bit setEOT(idle + 27); // EOT marker diff --git a/DCCRMT.h b/DCCRMT.h index 8d8a93c..94234e7 100644 --- a/DCCRMT.h +++ b/DCCRMT.h @@ -32,7 +32,13 @@ class RMTChannel { public: - RMTChannel(byte pin, byte ch, byte plen); + inline RMTChannel(byte pin, bool isMain) { + if (isMain) + RMTChannel(pin, 0, PREAMBLE_BITS_MAIN, 1); + else + RMTChannel(pin, 2, PREAMBLE_BITS_PROG, 0); + }; + RMTChannel(byte pin, byte ch, byte plen, bool isProg); void IRAM_ATTR RMTinterrupt(); void RMTprefill(); bool RMTfillData(dccPacket packet); diff --git a/MotorDriver.cpp b/MotorDriver.cpp index ee878ec..5f3d68a 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -41,7 +41,7 @@ MotorDriver::MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8 if (dtype == RMT_MAIN) { signalPin=signal_pin; #if defined(ARDUINO_ARCH_ESP32) - rmtChannel = new RMTChannel(signalPin, 0, PREAMBLE_BITS_MAIN); + rmtChannel = new RMTChannel(signalPin, true); // true: isMain #endif dualSignal=false; } else if (dtype & (TIMER_MAIN | TIMER_PROG)) { diff --git a/MotorDriver.h b/MotorDriver.h index da9e0d9..65f97f2 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -29,10 +29,6 @@ #include "DCCRMT.h" #endif -// Number of preamble bits (moved here so MotorDriver and Waveform know) -const int PREAMBLE_BITS_MAIN = 16; -const int PREAMBLE_BITS_PROG = 22; - #ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h #define UNUSED_PIN 127 // inside int8_t #endif