2022-02-22 02:27:27 +01:00
|
|
|
/*
|
|
|
|
* © 2022 Chris Harlow
|
2022-06-11 20:38:44 +02:00
|
|
|
* © 2022 Harald Barth
|
2023-09-05 13:21:09 +02:00
|
|
|
* © 2023 Colin Murdoch
|
|
|
|
*
|
2022-02-22 02:27:27 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2022-06-11 20:38:44 +02:00
|
|
|
* This file is part of CommandStation-EX
|
2022-02-22 02:27:27 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2022-08-01 22:56:56 +02:00
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
#include <vector>
|
|
|
|
#endif
|
2022-02-22 02:27:27 +01:00
|
|
|
#ifndef TrackManager_h
|
|
|
|
#define TrackManager_h
|
|
|
|
#include "FSH.h"
|
|
|
|
#include "MotorDriver.h"
|
|
|
|
// Virtualised Motor shield multi-track hardware Interface
|
|
|
|
|
2022-03-19 17:26:29 +01:00
|
|
|
// These constants help EXRAIL macros say SET_TRACK(2,mode) OR SET_TRACK(C,mode) etc.
|
|
|
|
const byte TRACK_NUMBER_0=0, TRACK_NUMBER_A=0;
|
|
|
|
const byte TRACK_NUMBER_1=1, TRACK_NUMBER_B=1;
|
|
|
|
const byte TRACK_NUMBER_2=2, TRACK_NUMBER_C=2;
|
|
|
|
const byte TRACK_NUMBER_3=3, TRACK_NUMBER_D=3;
|
|
|
|
const byte TRACK_NUMBER_4=4, TRACK_NUMBER_E=4;
|
|
|
|
const byte TRACK_NUMBER_5=5, TRACK_NUMBER_F=5;
|
|
|
|
const byte TRACK_NUMBER_6=6, TRACK_NUMBER_G=6;
|
|
|
|
const byte TRACK_NUMBER_7=7, TRACK_NUMBER_H=7;
|
2022-02-22 02:27:27 +01:00
|
|
|
|
2023-09-24 16:40:42 +02:00
|
|
|
// These constants help EXRAIL macros convert Track Power e.g. SET_POWER(A ON|OFF).
|
|
|
|
const byte TRACK_POWER_0=0, TRACK_POWER_OFF=0;
|
|
|
|
const byte TRACK_POWER_1=1, TRACK_POWER_ON=1;
|
|
|
|
|
2022-02-22 02:27:27 +01:00
|
|
|
class TrackManager {
|
|
|
|
public:
|
|
|
|
static void Setup(const FSH * shieldName,
|
|
|
|
MotorDriver * track0,
|
|
|
|
MotorDriver * track1=NULL,
|
|
|
|
MotorDriver * track2=NULL,
|
|
|
|
MotorDriver * track3=NULL,
|
|
|
|
MotorDriver * track4=NULL,
|
|
|
|
MotorDriver * track5=NULL,
|
|
|
|
MotorDriver * track6=NULL,
|
|
|
|
MotorDriver * track7=NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
static void setDCCSignal( bool on);
|
|
|
|
static void setCutout( bool on);
|
|
|
|
static void setPROGSignal( bool on);
|
|
|
|
static void setDCSignal(int16_t cab, byte speedbyte);
|
2022-02-23 16:44:34 +01:00
|
|
|
static MotorDriver * getProgDriver();
|
2022-08-01 22:56:56 +02:00
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
2023-11-14 00:05:18 +01:00
|
|
|
static std::vector<MotorDriver *>getMainDrivers();
|
2022-08-01 22:56:56 +02:00
|
|
|
#endif
|
2023-09-26 19:02:39 +02:00
|
|
|
|
2022-02-23 16:44:34 +01:00
|
|
|
static void setPower(POWERMODE mode) {setMainPower(mode); setProgPower(mode);}
|
2023-11-14 00:05:18 +01:00
|
|
|
static void setTrackPower(POWERMODE mode, byte t);
|
|
|
|
static void setTrackPower(TRACK_MODE trackmode, POWERMODE powermode);
|
|
|
|
static void setMainPower(POWERMODE mode) {setTrackPower(TRACK_MODE_MAIN, mode);}
|
|
|
|
static void setProgPower(POWERMODE mode) {setTrackPower(TRACK_MODE_PROG, mode);}
|
2022-02-23 16:44:34 +01:00
|
|
|
|
2022-02-22 02:27:27 +01:00
|
|
|
static const int16_t MAX_TRACKS=8;
|
2022-03-19 17:26:29 +01:00
|
|
|
static bool setTrackMode(byte track, TRACK_MODE mode, int16_t DCaddr=0);
|
2023-11-21 11:51:26 +01:00
|
|
|
static bool parseEqualSign(Print * stream, int16_t params, int16_t p[]);
|
2022-03-19 12:22:31 +01:00
|
|
|
static void loop();
|
2023-11-14 00:05:18 +01:00
|
|
|
static POWERMODE getMainPower();
|
2022-02-23 16:44:34 +01:00
|
|
|
static POWERMODE getProgPower();
|
2023-11-23 10:47:43 +01:00
|
|
|
static inline POWERMODE getPower(byte t) { return track[t]->getPower(); }
|
2023-11-14 00:05:18 +01:00
|
|
|
static bool getPower(byte t, char s[]);
|
2022-03-19 12:22:31 +01:00
|
|
|
static void setJoin(bool join);
|
|
|
|
static bool isJoined() { return progTrackSyncMain;}
|
2023-11-23 10:47:43 +01:00
|
|
|
static inline bool isActive (byte tr) {
|
|
|
|
if (tr > lastTrack) return false;
|
|
|
|
return track[tr]->getMode() & (TRACK_MODE_MAIN|TRACK_MODE_PROG|TRACK_MODE_DC|TRACK_MODE_BOOST|TRACK_MODE_EXT);}
|
2022-03-19 12:22:31 +01:00
|
|
|
static void setJoinRelayPin(byte joinRelayPin);
|
2022-10-02 00:43:46 +02:00
|
|
|
static void sampleCurrent();
|
2023-03-02 13:56:30 +01:00
|
|
|
static void reportGauges(Print* stream);
|
|
|
|
static void reportCurrent(Print* stream);
|
2023-09-26 19:02:39 +02:00
|
|
|
static void reportPowerChange(Print* stream, byte thistrack);
|
2023-03-02 13:56:30 +01:00
|
|
|
static void reportObsoleteCurrent(Print* stream);
|
2023-03-06 12:57:14 +01:00
|
|
|
static void streamTrackState(Print* stream, byte t);
|
2023-09-05 13:21:09 +02:00
|
|
|
static bool isPowerOn(byte t);
|
2023-09-22 18:03:40 +02:00
|
|
|
static bool isProg(byte t);
|
2023-10-06 20:11:11 +02:00
|
|
|
static byte returnMode(byte t);
|
2023-10-10 12:52:46 +02:00
|
|
|
static int16_t returnDCAddr(byte t);
|
2023-11-21 15:37:08 +01:00
|
|
|
static const FSH* getModeName(TRACK_MODE Mode);
|
2023-03-06 12:57:14 +01:00
|
|
|
|
2022-03-19 12:22:31 +01:00
|
|
|
static int16_t joinRelay;
|
|
|
|
static bool progTrackSyncMain; // true when prog track is a siding switched to main
|
2023-06-18 19:26:38 +02:00
|
|
|
static bool progTrackBoosted; // true when prog track is not current limited
|
|
|
|
|
|
|
|
#ifdef DEBUG_ADC
|
|
|
|
public:
|
|
|
|
#else
|
|
|
|
private:
|
|
|
|
#endif
|
|
|
|
static MotorDriver* track[MAX_TRACKS];
|
|
|
|
|
2022-02-22 02:27:27 +01:00
|
|
|
private:
|
2022-02-28 10:32:26 +01:00
|
|
|
static void addTrack(byte t, MotorDriver* driver);
|
|
|
|
static byte lastTrack;
|
2022-02-23 16:44:34 +01:00
|
|
|
static byte nextCycleTrack;
|
2022-03-31 23:19:13 +02:00
|
|
|
static void applyDCSpeed(byte t);
|
2022-02-23 16:44:34 +01:00
|
|
|
|
2023-11-10 23:58:30 +01:00
|
|
|
static int16_t trackDCAddr[MAX_TRACKS]; // dc address if TRACK_MODE_DC
|
2022-08-17 02:11:51 +02:00
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
static byte tempProgTrack; // holds the prog track number during join
|
|
|
|
#endif
|
2022-03-19 17:26:29 +01:00
|
|
|
};
|
2022-02-22 02:27:27 +01:00
|
|
|
|
|
|
|
#endif
|