2021-08-03 23:12:25 +02:00
|
|
|
/*
|
2022-01-07 02:28:35 +01:00
|
|
|
* © 2021 Neil McKechnie
|
|
|
|
* © 2020-2022 Chris Harlow
|
2023-08-06 22:00:52 +02:00
|
|
|
* © 2022-2023 Colin Murdoch
|
2023-01-27 17:03:39 +01:00
|
|
|
* © 2023 Harald Barth
|
2022-01-07 02:28:35 +01:00
|
|
|
* All rights reserved.
|
2021-08-03 23:12:25 +02:00
|
|
|
*
|
|
|
|
* This file is part of CommandStation-EX
|
|
|
|
*
|
|
|
|
* 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-01-30 18:31:26 +01:00
|
|
|
#ifndef EXRAIL2_H
|
|
|
|
#define EXRAIL2_H
|
2021-08-03 23:12:25 +02:00
|
|
|
#include "FSH.h"
|
|
|
|
#include "IODevice.h"
|
2022-04-08 12:41:50 +02:00
|
|
|
#include "Turnouts.h"
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
// The following are the operation codes (or instructions) for a kind of virtual machine.
|
2022-01-03 13:43:06 +01:00
|
|
|
// Each instruction is normally 3 bytes long with an operation code followed by a parameter.
|
2021-08-03 23:12:25 +02:00
|
|
|
// In cases where more than one parameter is required, the first parameter is followed by one
|
|
|
|
// or more OPCODE_PAD instructions with the subsequent parameters. This wastes a byte but makes
|
|
|
|
// searching easier as a parameter can never be confused with an opcode.
|
|
|
|
//
|
|
|
|
enum OPCODE : byte {OPCODE_THROW,OPCODE_CLOSE,
|
|
|
|
OPCODE_FWD,OPCODE_REV,OPCODE_SPEED,OPCODE_INVERT_DIRECTION,
|
|
|
|
OPCODE_RESERVE,OPCODE_FREE,
|
2021-11-19 14:00:21 +01:00
|
|
|
OPCODE_AT,OPCODE_AFTER,OPCODE_AUTOSTART,
|
2022-03-18 14:46:07 +01:00
|
|
|
OPCODE_ATGTE,OPCODE_ATLT,
|
2022-04-17 11:10:22 +02:00
|
|
|
OPCODE_ATTIMEOUT1,OPCODE_ATTIMEOUT2,
|
2021-08-03 23:12:25 +02:00
|
|
|
OPCODE_LATCH,OPCODE_UNLATCH,OPCODE_SET,OPCODE_RESET,
|
2022-04-17 11:10:22 +02:00
|
|
|
OPCODE_ENDIF,OPCODE_ELSE,
|
2022-01-03 20:15:44 +01:00
|
|
|
OPCODE_DELAY,OPCODE_DELAYMINS,OPCODE_DELAYMS,OPCODE_RANDWAIT,
|
2021-09-06 13:27:21 +02:00
|
|
|
OPCODE_FON,OPCODE_FOFF,OPCODE_XFON,OPCODE_XFOFF,
|
2021-11-24 12:56:55 +01:00
|
|
|
OPCODE_RED,OPCODE_GREEN,OPCODE_AMBER,OPCODE_DRIVE,
|
2021-08-28 18:39:48 +02:00
|
|
|
OPCODE_SERVO,OPCODE_SIGNAL,OPCODE_TURNOUT,OPCODE_WAITFOR,
|
2021-08-03 23:12:25 +02:00
|
|
|
OPCODE_PAD,OPCODE_FOLLOW,OPCODE_CALL,OPCODE_RETURN,
|
2023-08-06 22:00:52 +02:00
|
|
|
#ifndef DISABLE_PROG
|
|
|
|
OPCODE_JOIN,OPCODE_UNJOIN,OPCODE_READ_LOCO1,OPCODE_READ_LOCO2,
|
|
|
|
#endif
|
|
|
|
OPCODE_POM,
|
2022-03-24 11:40:49 +01:00
|
|
|
OPCODE_START,OPCODE_SETLOCO,OPCODE_SENDLOCO,OPCODE_FORGET,
|
2022-02-28 11:38:26 +01:00
|
|
|
OPCODE_PAUSE, OPCODE_RESUME,OPCODE_POWEROFF,OPCODE_POWERON,
|
2021-08-10 17:32:23 +02:00
|
|
|
OPCODE_ONCLOSE, OPCODE_ONTHROW, OPCODE_SERVOTURNOUT, OPCODE_PINTURNOUT,
|
2021-11-22 12:10:26 +01:00
|
|
|
OPCODE_PRINT,OPCODE_DCCACTIVATE,
|
2022-04-17 11:10:22 +02:00
|
|
|
OPCODE_ONACTIVATE,OPCODE_ONDEACTIVATE,
|
2022-03-31 22:52:43 +02:00
|
|
|
OPCODE_ROSTER,OPCODE_KILLALL,
|
2022-04-17 11:10:22 +02:00
|
|
|
OPCODE_ROUTE,OPCODE_AUTOMATION,OPCODE_SEQUENCE,
|
|
|
|
OPCODE_ENDTASK,OPCODE_ENDEXRAIL,
|
2022-04-18 17:59:02 +02:00
|
|
|
OPCODE_SET_TRACK,
|
2022-06-17 12:48:37 +02:00
|
|
|
OPCODE_ONRED,OPCODE_ONAMBER,OPCODE_ONGREEN,
|
2022-12-30 00:46:42 +01:00
|
|
|
OPCODE_ONCHANGE,
|
2023-01-11 18:36:11 +01:00
|
|
|
OPCODE_ONCLOCKTIME,
|
|
|
|
OPCODE_ONTIME,
|
2023-08-06 22:00:52 +02:00
|
|
|
OPCODE_ONOVERLOAD,
|
2022-04-17 11:10:22 +02:00
|
|
|
|
|
|
|
// OPcodes below this point are skip-nesting IF operations
|
|
|
|
// placed here so that they may be skipped as a group
|
|
|
|
// see skipIfBlock()
|
|
|
|
IF_TYPE_OPCODES, // do not move this...
|
2022-04-17 10:58:32 +02:00
|
|
|
OPCODE_IFRED,OPCODE_IFAMBER,OPCODE_IFGREEN,
|
2022-04-17 11:10:22 +02:00
|
|
|
OPCODE_IFGTE,OPCODE_IFLT,
|
|
|
|
OPCODE_IFTIMEOUT,
|
|
|
|
OPCODE_IF,OPCODE_IFNOT,
|
|
|
|
OPCODE_IFRANDOM,OPCODE_IFRESERVE,
|
2022-12-30 00:46:42 +01:00
|
|
|
OPCODE_IFCLOSED,OPCODE_IFTHROWN,
|
|
|
|
OPCODE_IFRE,
|
2023-01-26 16:55:58 +01:00
|
|
|
OPCODE_IFLOCO
|
2021-08-03 23:12:25 +02:00
|
|
|
};
|
|
|
|
|
2023-02-11 16:47:50 +01:00
|
|
|
// Ensure thrunge_lcd is put last as there may be more than one display,
|
|
|
|
// sequentially numbered from thrunge_lcd.
|
2022-12-16 14:14:48 +01:00
|
|
|
enum thrunger: byte {
|
2023-08-06 22:00:52 +02:00
|
|
|
thrunge_print, thrunge_broadcast, thrunge_withrottle,
|
|
|
|
thrunge_serial,thrunge_parse,
|
2022-12-16 14:14:48 +01:00
|
|
|
thrunge_serial1, thrunge_serial2, thrunge_serial3,
|
|
|
|
thrunge_serial4, thrunge_serial5, thrunge_serial6,
|
2023-02-11 16:47:50 +01:00
|
|
|
thrunge_lcn,
|
|
|
|
thrunge_lcd, // Must be last!!
|
|
|
|
};
|
2022-12-16 14:14:48 +01:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Flag bits for status of hardware and TPL
|
2022-01-03 13:43:06 +01:00
|
|
|
static const byte SECTION_FLAG = 0x80;
|
2022-04-17 10:58:32 +02:00
|
|
|
static const byte LATCH_FLAG = 0x40;
|
|
|
|
static const byte TASK_FLAG = 0x20;
|
|
|
|
static const byte SPARE_FLAG = 0x10;
|
|
|
|
static const byte SIGNAL_MASK = 0x0C;
|
|
|
|
static const byte SIGNAL_RED = 0x08;
|
|
|
|
static const byte SIGNAL_AMBER = 0x0C;
|
|
|
|
static const byte SIGNAL_GREEN = 0x04;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
static const byte MAX_STACK_DEPTH=4;
|
|
|
|
|
|
|
|
static const short MAX_FLAGS=256;
|
|
|
|
#define FLAGOVERFLOW(x) x>=MAX_FLAGS
|
|
|
|
|
2021-11-18 11:42:54 +01:00
|
|
|
class LookList {
|
|
|
|
public:
|
2021-11-18 15:57:09 +01:00
|
|
|
LookList(int16_t size);
|
|
|
|
void add(int16_t lookup, int16_t result);
|
|
|
|
int16_t find(int16_t value);
|
|
|
|
private:
|
2021-11-18 11:42:54 +01:00
|
|
|
int16_t m_size;
|
|
|
|
int16_t m_loaded;
|
|
|
|
int16_t * m_lookupArray;
|
2021-11-18 15:57:09 +01:00
|
|
|
int16_t * m_resultArray;
|
2021-11-18 11:42:54 +01:00
|
|
|
};
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
class RMFT2 {
|
|
|
|
public:
|
|
|
|
static void begin();
|
|
|
|
static void loop();
|
|
|
|
RMFT2(int progCounter);
|
|
|
|
RMFT2(int route, uint16_t cab);
|
|
|
|
~RMFT2();
|
2021-08-21 14:17:14 +02:00
|
|
|
static void readLocoCallback(int16_t cv);
|
2021-08-03 23:12:25 +02:00
|
|
|
static void createNewTask(int route, uint16_t cab);
|
2021-09-03 23:39:13 +02:00
|
|
|
static void turnoutEvent(int16_t id, bool closed);
|
2021-11-27 12:29:26 +01:00
|
|
|
static void activateEvent(int16_t addr, bool active);
|
2022-12-30 00:46:42 +01:00
|
|
|
static void changeEvent(int16_t id, bool change);
|
2023-01-11 18:36:11 +01:00
|
|
|
static void clockEvent(int16_t clocktime, bool change);
|
2023-08-06 22:00:52 +02:00
|
|
|
static void powerEvent(char track, bool overload);
|
2022-02-06 14:56:51 +01:00
|
|
|
static const int16_t SERVO_SIGNAL_FLAG=0x4000;
|
2022-03-18 14:46:07 +01:00
|
|
|
static const int16_t ACTIVE_HIGH_SIGNAL_FLAG=0x2000;
|
2022-06-18 13:31:54 +02:00
|
|
|
static const int16_t DCC_SIGNAL_FLAG=0x1000;
|
2022-05-19 10:03:28 +02:00
|
|
|
static const int16_t SIGNAL_ID_MASK=0x0FFF;
|
2022-04-03 12:19:04 +02:00
|
|
|
// Throttle Info Access functions built by exrail macros
|
|
|
|
static const byte rosterNameCount;
|
2022-12-16 14:14:48 +01:00
|
|
|
static const int16_t HIGHFLASH routeIdList[];
|
|
|
|
static const int16_t HIGHFLASH automationIdList[];
|
|
|
|
static const int16_t HIGHFLASH rosterIdList[];
|
2022-04-03 12:19:04 +02:00
|
|
|
static const FSH * getRouteDescription(int16_t id);
|
2022-04-08 17:13:15 +02:00
|
|
|
static char getRouteType(int16_t id);
|
2022-04-03 12:19:04 +02:00
|
|
|
static const FSH * getTurnoutDescription(int16_t id);
|
|
|
|
static const FSH * getRosterName(int16_t id);
|
|
|
|
static const FSH * getRosterFunctions(int16_t id);
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
private:
|
2021-08-21 14:17:14 +02:00
|
|
|
static void ComandFilter(Print * stream, byte & opcode, byte & paramCount, int16_t p[]);
|
|
|
|
static bool parseSlash(Print * stream, byte & paramCount, int16_t p[]) ;
|
2021-08-03 23:12:25 +02:00
|
|
|
static void streamFlags(Print* stream);
|
2022-06-18 13:31:54 +02:00
|
|
|
static bool setFlag(VPIN id,byte onMask, byte OffMask=0);
|
2022-01-03 13:43:06 +01:00
|
|
|
static bool getFlag(VPIN id,byte mask);
|
2021-08-21 14:17:14 +02:00
|
|
|
static int16_t progtrackLocoId;
|
2022-06-18 13:31:54 +02:00
|
|
|
static void doSignal(int16_t id,char rag);
|
|
|
|
static bool isSignal(int16_t id,char rag);
|
|
|
|
static int16_t getSignalSlot(int16_t id);
|
2022-04-08 12:41:50 +02:00
|
|
|
static void setTurnoutHiddenState(Turnout * t);
|
2022-06-17 12:48:37 +02:00
|
|
|
static LookList* LookListLoader(OPCODE op1,
|
|
|
|
OPCODE op2=OPCODE_ENDEXRAIL,OPCODE op3=OPCODE_ENDEXRAIL);
|
|
|
|
static void handleEvent(const FSH* reason,LookList* handlers, int16_t id);
|
2022-12-16 14:14:48 +01:00
|
|
|
static uint16_t getOperand(int progCounter,byte n);
|
2021-08-03 23:12:25 +02:00
|
|
|
static RMFT2 * loopTask;
|
|
|
|
static RMFT2 * pausingTask;
|
|
|
|
void delayMe(long millisecs);
|
|
|
|
void driveLoco(byte speedo);
|
2021-09-04 11:38:38 +02:00
|
|
|
bool readSensor(uint16_t sensorId);
|
2021-08-03 23:12:25 +02:00
|
|
|
bool skipIfBlock();
|
|
|
|
bool readLoco();
|
|
|
|
void loop2();
|
|
|
|
void kill(const FSH * reason=NULL,int operand=0);
|
2021-08-10 17:32:23 +02:00
|
|
|
void printMessage(uint16_t id); // Built by RMFTMacros.h
|
|
|
|
void printMessage2(const FSH * msg);
|
2022-12-16 14:14:48 +01:00
|
|
|
void thrungeString(uint32_t strfar, thrunger mode, byte id=0);
|
|
|
|
uint16_t getOperand(byte n);
|
2021-11-27 12:29:26 +01:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
static bool diag;
|
2022-12-16 14:14:48 +01:00
|
|
|
static const HIGHFLASH byte RouteCode[];
|
|
|
|
static const HIGHFLASH int16_t SignalDefinitions[];
|
2021-08-03 23:12:25 +02:00
|
|
|
static byte flags[MAX_FLAGS];
|
2021-11-18 11:42:54 +01:00
|
|
|
static LookList * sequenceLookup;
|
|
|
|
static LookList * onThrowLookup;
|
|
|
|
static LookList * onCloseLookup;
|
2021-11-25 12:36:05 +01:00
|
|
|
static LookList * onActivateLookup;
|
|
|
|
static LookList * onDeactivateLookup;
|
2022-06-17 12:48:37 +02:00
|
|
|
static LookList * onRedLookup;
|
|
|
|
static LookList * onAmberLookup;
|
|
|
|
static LookList * onGreenLookup;
|
2022-12-30 00:46:42 +01:00
|
|
|
static LookList * onChangeLookup;
|
2023-01-11 18:36:11 +01:00
|
|
|
static LookList * onClockLookup;
|
2023-08-06 22:00:52 +02:00
|
|
|
//CHM
|
|
|
|
static LookList * onOverloadLookup;
|
|
|
|
|
2022-04-03 12:19:04 +02:00
|
|
|
|
2021-08-16 00:15:02 +02:00
|
|
|
// Local variables - exist for each instance/task
|
2021-08-03 23:12:25 +02:00
|
|
|
RMFT2 *next; // loop chain
|
|
|
|
int progCounter; // Byte offset of next route opcode in ROUTES table
|
|
|
|
unsigned long delayStart; // Used by opcodes that must be recalled before completing
|
|
|
|
unsigned long delayTime;
|
2022-01-03 11:15:10 +01:00
|
|
|
union {
|
|
|
|
unsigned long waitAfter; // Used by OPCODE_AFTER
|
|
|
|
unsigned long timeoutStart; // Used by OPCODE_ATTIMEOUT
|
|
|
|
};
|
|
|
|
bool timeoutFlag;
|
2021-08-16 00:15:02 +02:00
|
|
|
byte taskId;
|
|
|
|
|
2021-09-04 11:38:38 +02:00
|
|
|
uint16_t loco;
|
2021-08-03 23:12:25 +02:00
|
|
|
bool forward;
|
|
|
|
bool invert;
|
2021-09-04 11:38:38 +02:00
|
|
|
byte speedo;
|
2022-06-17 12:48:37 +02:00
|
|
|
int onEventStartPosition;
|
2021-08-03 23:12:25 +02:00
|
|
|
byte stackDepth;
|
|
|
|
int callStack[MAX_STACK_DEPTH];
|
|
|
|
};
|
|
|
|
#endif
|