2020-07-03 18:35:02 +02:00
|
|
|
/*
|
|
|
|
* © 2020, Chris Harlow. All rights reserved.
|
2020-09-08 22:43:25 +02:00
|
|
|
* © 2020, Harald Barth.
|
2020-07-03 18:35:02 +02:00
|
|
|
*
|
|
|
|
* This file is part of Asbelos DCC API
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-05-27 10:40:12 +02:00
|
|
|
#ifndef DCCWaveform_h
|
|
|
|
#define DCCWaveform_h
|
2020-08-15 12:32:32 +02:00
|
|
|
#include "MotorDriver.h"
|
2020-08-28 13:34:58 +02:00
|
|
|
#include "ArduinoTimers.h"
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
const int POWER_SAMPLE_ON_WAIT = 100;
|
|
|
|
const int POWER_SAMPLE_OFF_WAIT = 1000;
|
2020-07-12 01:36:56 +02:00
|
|
|
const int POWER_SAMPLE_OVERLOAD_WAIT = 20;
|
2020-05-24 00:02:54 +02:00
|
|
|
|
2020-08-17 16:30:25 +02:00
|
|
|
const int MIN_ACK_PULSE_DURATION = 2000;
|
2020-07-03 18:57:24 +02:00
|
|
|
const int MAX_ACK_PULSE_DURATION = 8500;
|
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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-08-15 12:32:32 +02:00
|
|
|
DCCWaveform( byte preambleBits, bool isMain);
|
2020-08-28 13:34:58 +02:00
|
|
|
static void begin(MotorDriver * mainDriver, MotorDriver * progDriver, byte timerNumber);
|
2020-05-24 11:27:33 +02:00
|
|
|
static void loop();
|
|
|
|
static DCCWaveform mainTrack;
|
|
|
|
static DCCWaveform progTrack;
|
|
|
|
|
|
|
|
void beginTrack();
|
|
|
|
void setPowerMode(POWERMODE);
|
|
|
|
POWERMODE getPowerMode();
|
|
|
|
void checkPowerOverload();
|
2020-06-02 14:20:36 +02:00
|
|
|
int getLastCurrent();
|
2020-05-24 11:27:33 +02:00
|
|
|
void schedulePacket(const byte buffer[], byte byteCount, byte repeats);
|
|
|
|
volatile bool packetPending;
|
2020-06-06 12:11:03 +02:00
|
|
|
volatile byte sentResetsSincePacket;
|
2020-09-08 09:47:40 +02:00
|
|
|
volatile bool autoPowerOff=false;
|
2020-07-02 13:49:35 +02:00
|
|
|
void setAckBaseline(bool debug); //prog track only
|
|
|
|
void setAckPending(bool debug); //prog track only
|
|
|
|
byte getAck(bool debug); //prog track only 0=NACK, 1=ACK 2=keep waiting
|
2020-07-12 01:11:30 +02:00
|
|
|
static bool progTrackSyncMain; // true when prog track is a siding switched to main
|
2020-09-08 22:14:05 +02:00
|
|
|
inline void doAutoPowerOff() {
|
|
|
|
if (autoPowerOff) {
|
|
|
|
setPowerMode(POWERMODE::OFF);
|
|
|
|
autoPowerOff=false;
|
|
|
|
}
|
|
|
|
};
|
2020-07-12 01:11:30 +02:00
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
private:
|
2020-08-28 13:34:58 +02:00
|
|
|
static VirtualTimer * interruptTimer;
|
2020-05-24 11:27:33 +02:00
|
|
|
static void interruptHandler();
|
|
|
|
bool interrupt1();
|
|
|
|
void interrupt2();
|
2020-07-02 13:49:35 +02:00
|
|
|
void checkAck();
|
2020-07-12 01:11:30 +02:00
|
|
|
void setSignal(bool high);
|
2020-07-02 13:49:35 +02:00
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
bool isMainTrack;
|
2020-08-15 12:32:32 +02:00
|
|
|
MotorDriver* motorDriver;
|
2020-05-24 11:27:33 +02:00
|
|
|
// 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
|
|
|
|
|
|
|
|
byte pendingPacket[MAX_PACKET_SIZE];
|
|
|
|
byte pendingLength;
|
|
|
|
byte pendingRepeats;
|
2020-06-02 14:20:36 +02:00
|
|
|
int lastCurrent;
|
2020-05-24 11:27:33 +02:00
|
|
|
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
// current sampling
|
|
|
|
POWERMODE powerMode;
|
2020-06-16 12:06:36 +02:00
|
|
|
unsigned long lastSampleTaken;
|
2020-06-30 10:09:39 +02:00
|
|
|
unsigned int sampleDelay;
|
2020-07-11 10:06:34 +02:00
|
|
|
static const int ACK_CURRENT_TRIP=1000; // During ACK processing limit can be higher
|
2020-07-12 01:36:56 +02:00
|
|
|
unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT;
|
|
|
|
unsigned int power_good_counter = 0;
|
2020-07-02 13:49:35 +02:00
|
|
|
|
|
|
|
// ACK management (Prog track only)
|
|
|
|
bool ackPending;
|
|
|
|
bool ackDetected;
|
|
|
|
int ackThreshold;
|
|
|
|
int ackMaxCurrent;
|
|
|
|
unsigned long ackCheckStart; // millis
|
|
|
|
unsigned int ackCheckDuration; // millis
|
|
|
|
|
|
|
|
unsigned int ackPulseDuration; // micros
|
|
|
|
unsigned long ackPulseStart; // micros
|
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
};
|
2020-05-27 10:40:12 +02:00
|
|
|
#endif
|