2020-05-24 00:02:54 +02:00
|
|
|
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
const int POWER_SAMPLE_MAX = 300;
|
|
|
|
const int POWER_SAMPLE_ON_WAIT = 100;
|
|
|
|
const int POWER_SAMPLE_OFF_WAIT = 1000;
|
|
|
|
const int POWER_SAMPLE_OVERLOAD_WAIT = 4000;
|
2020-05-24 00:02:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
// ACK current analogRead values (vary depending on motor shield and cpu voltage)
|
2020-05-24 10:07:54 +02:00
|
|
|
const int ACK_TIMEOUT = 25 ; // millis getAck is prepared to wait for a signal
|
2020-05-26 10:44:20 +02:00
|
|
|
const int ACK_MAX_NOT_PULSE = 40 ; // current below which this is NOT a pulse any more
|
2020-05-24 00:02:54 +02:00
|
|
|
const int ACK_MIN_PULSE = 50 ; // current above which a pulse is recognised
|
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
const int PREAMBLE_BITS_MAIN = 20;
|
|
|
|
const int PREAMBLE_BITS_PROG = 22;
|
2020-05-24 00:02:54 +02:00
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
// Railcom settings
|
|
|
|
const bool RAILCOM_CUTOUT = true;
|
|
|
|
const byte RAILCOM_PREAMBLES_BEFORE_CUTOUT = 1; // how far into the preamble do we cutout
|
|
|
|
const byte RAILCOM_PREAMBLES_SKIPPED_IN_CUTOUT = 5;
|
|
|
|
const byte RAILCOM_BRAKE_PIN = 9;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const byte MAX_PACKET_SIZE = 12;
|
|
|
|
// NOTE: static functions are used for the overall controller, then
|
2020-05-24 00:02:54 +02:00
|
|
|
// one instance is created for each track.
|
|
|
|
|
|
|
|
|
|
|
|
enum class POWERMODE { OFF, ON, OVERLOAD };
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
const byte idlePacket[] = {0xFF, 0x00, 0xFF};
|
|
|
|
const byte resetPacket[] = {0x00, 0x00, 0x00};
|
2020-05-24 00:02:54 +02:00
|
|
|
|
|
|
|
class DCCWaveform {
|
|
|
|
public:
|
2020-05-26 13:44:02 +02:00
|
|
|
DCCWaveform( byte preambleBits, bool isMain);
|
2020-05-24 11:27:33 +02:00
|
|
|
static void begin();
|
|
|
|
static void loop();
|
|
|
|
static DCCWaveform mainTrack;
|
|
|
|
static DCCWaveform progTrack;
|
|
|
|
|
|
|
|
void beginTrack();
|
|
|
|
void setPowerMode(POWERMODE);
|
|
|
|
POWERMODE getPowerMode();
|
|
|
|
void checkPowerOverload();
|
|
|
|
void schedulePacket(const byte buffer[], byte byteCount, byte repeats);
|
|
|
|
volatile bool packetPending;
|
|
|
|
bool startAckProcess();
|
|
|
|
bool getAck();
|
|
|
|
|
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
private:
|
2020-05-24 11:27:33 +02:00
|
|
|
|
|
|
|
static void interruptHandler();
|
|
|
|
bool interrupt1();
|
|
|
|
void interrupt2();
|
|
|
|
|
|
|
|
bool isMainTrack;
|
|
|
|
|
|
|
|
// Transmission controller
|
|
|
|
byte transmitPacket[MAX_PACKET_SIZE]; // packet being transmitted
|
|
|
|
byte transmitLength;
|
|
|
|
byte transmitRepeats; // remaining repeats of transmission
|
|
|
|
byte remainingPreambles;
|
|
|
|
byte requiredPreambles;
|
|
|
|
bool currentBit; // bit to be transmitted
|
|
|
|
byte bits_sent; // 0-8 (yes 9 bits) sent for current byte
|
|
|
|
byte bytes_sent; // number of bytes sent from transmitPacket
|
|
|
|
byte state; // wave generator state machine
|
|
|
|
void checkRailcom();
|
|
|
|
|
|
|
|
byte pendingPacket[MAX_PACKET_SIZE];
|
|
|
|
byte pendingLength;
|
|
|
|
byte pendingRepeats;
|
|
|
|
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
// current sampling
|
|
|
|
POWERMODE powerMode;
|
|
|
|
unsigned long nextSampleDue;
|
2020-05-24 00:02:54 +02:00
|
|
|
};
|