2021-08-03 23:12:25 +02:00
|
|
|
/*
|
2024-05-07 12:20:37 +02:00
|
|
|
* © 2024 Paul M. Antoine
|
2022-01-07 02:28:35 +01:00
|
|
|
* © 2021 Neil McKechnie
|
2023-01-27 17:03:39 +01:00
|
|
|
* © 2021-2023 Harald Barth
|
2023-03-18 19:52:01 +01:00
|
|
|
* © 2020-2023 Chris Harlow
|
2023-08-12 19:40:48 +02:00
|
|
|
* © 2022-2023 Colin Murdoch
|
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/>.
|
|
|
|
*/
|
2021-11-18 11:42:54 +01:00
|
|
|
|
|
|
|
/* EXRAILPlus planned FEATURE additions
|
2021-11-25 12:36:05 +01:00
|
|
|
F1. [DONE] DCC accessory packet opcodes (short and long form)
|
|
|
|
F2. [DONE] ONAccessory catchers
|
2021-11-28 13:09:36 +01:00
|
|
|
F3. [DONE] Turnout descriptions for Withrottle
|
2023-03-18 19:52:01 +01:00
|
|
|
F4. [DONE] Oled announcements (depends on HAL)
|
|
|
|
F5. [DONE] Withrottle roster info
|
2021-11-18 11:42:54 +01:00
|
|
|
F6. Multi-occupancy semaphore
|
2021-11-22 12:10:26 +01:00
|
|
|
F7. [DONE see AUTOSTART] Self starting sequences
|
2021-11-18 11:42:54 +01:00
|
|
|
F8. Park/unpark
|
2021-11-28 13:09:36 +01:00
|
|
|
F9. [DONE] Analog drive
|
|
|
|
F10. [DONE] Alias anywhere
|
|
|
|
F11. [DONE]EXRAIL/ENDEXRAIL unnecessary
|
|
|
|
F12. [DONE] Allow guarded code (as effect of ALIAS anywhere)
|
2021-11-30 14:52:22 +01:00
|
|
|
F13. [DONE] IFGTE/IFLT function
|
2021-11-18 11:42:54 +01:00
|
|
|
*/
|
|
|
|
/* EXRAILPlus planned TRANSPARENT additions
|
2021-11-22 12:10:26 +01:00
|
|
|
T1. [DONE] RAM based fast lookup for sequences ON* event catchers and signals.
|
2021-11-18 11:42:54 +01:00
|
|
|
T2. Extend to >64k
|
|
|
|
*/
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
#include <Arduino.h>
|
2022-12-16 14:14:48 +01:00
|
|
|
#include "defines.h"
|
2022-01-30 18:31:26 +01:00
|
|
|
#include "EXRAIL2.h"
|
2021-08-03 23:12:25 +02:00
|
|
|
#include "DCC.h"
|
2021-09-16 17:47:47 +02:00
|
|
|
#include "DCCWaveform.h"
|
2021-08-03 23:12:25 +02:00
|
|
|
#include "DIAG.h"
|
|
|
|
#include "WiThrottle.h"
|
|
|
|
#include "DCCEXParser.h"
|
|
|
|
#include "Turnouts.h"
|
2021-12-07 01:57:08 +01:00
|
|
|
#include "CommandDistributor.h"
|
2022-02-23 16:44:34 +01:00
|
|
|
#include "TrackManager.h"
|
2023-09-02 00:29:49 +02:00
|
|
|
#include "Turntables.h"
|
2023-09-08 23:22:10 +02:00
|
|
|
#include "IODevice.h"
|
2024-04-13 09:12:35 +02:00
|
|
|
#include "EXRAILSensor.h"
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
// One instance of RMFT clas is used for each "thread" in the automation.
|
|
|
|
// Each thread manages a loco on a journey through the layout, and/or may manage a scenery automation.
|
2022-03-06 16:05:35 +01:00
|
|
|
// The threads exist in a ring, each time through loop() the next thread in the ring is serviced.
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
// Statics
|
2021-10-21 23:44:25 +02:00
|
|
|
const int16_t LOCO_ID_WAITING=-99; // waiting for loco id from prog track
|
2022-03-06 16:05:35 +01:00
|
|
|
int16_t RMFT2::progtrackLocoId; // used for callback when detecting a loco on prog track
|
2021-08-03 23:12:25 +02:00
|
|
|
bool RMFT2::diag=false; // <D EXRAIL ON>
|
|
|
|
RMFT2 * RMFT2::loopTask=NULL; // loopTask contains the address of ONE of the tasks in a ring.
|
2021-08-16 00:15:02 +02:00
|
|
|
RMFT2 * RMFT2::pausingTask=NULL; // Task causing a PAUSE.
|
2021-08-03 23:12:25 +02:00
|
|
|
// when pausingTask is set, that is the ONLY task that gets any service,
|
|
|
|
// and all others will have their locos stopped, then resumed after the pausing task resumes.
|
|
|
|
byte RMFT2::flags[MAX_FLAGS];
|
2023-10-13 14:59:06 +02:00
|
|
|
Print * RMFT2::LCCSerial=0;
|
2023-11-09 21:25:10 +01:00
|
|
|
LookList * RMFT2::routeLookup=NULL;
|
2024-09-22 13:36:19 +02:00
|
|
|
LookList * RMFT2::signalLookup=NULL;
|
2021-11-18 11:42:54 +01:00
|
|
|
LookList * RMFT2::onThrowLookup=NULL;
|
|
|
|
LookList * RMFT2::onCloseLookup=NULL;
|
2021-11-25 12:36:05 +01:00
|
|
|
LookList * RMFT2::onActivateLookup=NULL;
|
|
|
|
LookList * RMFT2::onDeactivateLookup=NULL;
|
2022-06-17 12:48:37 +02:00
|
|
|
LookList * RMFT2::onRedLookup=NULL;
|
|
|
|
LookList * RMFT2::onAmberLookup=NULL;
|
|
|
|
LookList * RMFT2::onGreenLookup=NULL;
|
2022-12-30 00:46:42 +01:00
|
|
|
LookList * RMFT2::onChangeLookup=NULL;
|
2023-01-11 18:36:11 +01:00
|
|
|
LookList * RMFT2::onClockLookup=NULL;
|
2023-09-06 23:58:19 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2023-09-02 00:29:49 +02:00
|
|
|
LookList * RMFT2::onRotateLookup=NULL;
|
2023-09-06 23:58:19 +02:00
|
|
|
#endif
|
2023-08-12 19:40:48 +02:00
|
|
|
LookList * RMFT2::onOverloadLookup=NULL;
|
2024-09-28 13:29:12 +02:00
|
|
|
LookList * RMFT2::onBlockEnterLookup=NULL;
|
|
|
|
LookList * RMFT2::onBlockExitLookup=NULL;
|
2023-11-09 20:27:52 +01:00
|
|
|
byte * RMFT2::routeStateArray=nullptr;
|
2023-11-09 21:25:10 +01:00
|
|
|
const FSH * * RMFT2::routeCaptionArray=nullptr;
|
2023-11-23 11:41:35 +01:00
|
|
|
int16_t * RMFT2::stashArray=nullptr;
|
|
|
|
int16_t RMFT2::maxStashId=0;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-12-16 14:14:48 +01:00
|
|
|
// getOperand instance version, uses progCounter from instance.
|
|
|
|
uint16_t RMFT2::getOperand(byte n) {
|
|
|
|
return getOperand(progCounter,n);
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOperand static version, must be provided prog counter from loop etc.
|
|
|
|
uint16_t RMFT2::getOperand(int progCounter,byte n) {
|
|
|
|
int offset=progCounter+1+(n*3);
|
2023-03-11 23:46:11 +01:00
|
|
|
byte lsb=GETHIGHFLASH(RouteCode,offset);
|
|
|
|
byte msb=GETHIGHFLASH(RouteCode,offset+1);
|
|
|
|
return msb<<8|lsb;
|
2022-12-16 14:14:48 +01:00
|
|
|
}
|
2021-11-18 11:42:54 +01:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
LookList::LookList(int16_t size) {
|
|
|
|
m_size=size;
|
|
|
|
m_loaded=0;
|
2023-11-09 21:25:10 +01:00
|
|
|
m_chain=nullptr;
|
2022-01-06 23:03:57 +01:00
|
|
|
if (size) {
|
|
|
|
m_lookupArray=new int16_t[size];
|
|
|
|
m_resultArray=new int16_t[size];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LookList::add(int16_t lookup, int16_t result) {
|
|
|
|
if (m_loaded==m_size) return; // and forget
|
|
|
|
m_lookupArray[m_loaded]=lookup;
|
|
|
|
m_resultArray[m_loaded]=result;
|
|
|
|
m_loaded++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t LookList::find(int16_t value) {
|
|
|
|
for (int16_t i=0;i<m_size;i++) {
|
|
|
|
if (m_lookupArray[i]==value) return m_resultArray[i];
|
|
|
|
}
|
2023-11-09 21:25:10 +01:00
|
|
|
return m_chain ? m_chain->find(value) :-1;
|
|
|
|
}
|
|
|
|
void LookList::chain(LookList * chain) {
|
|
|
|
m_chain=chain;
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2024-09-28 13:29:12 +02:00
|
|
|
void LookList::handleEvent(const FSH* reason,int16_t id, int16_t loco) {
|
2023-11-10 21:13:33 +01:00
|
|
|
// New feature... create multiple ONhandlers
|
|
|
|
for (int i=0;i<m_size;i++)
|
|
|
|
if (m_lookupArray[i]==id)
|
2024-09-28 13:29:12 +02:00
|
|
|
RMFT2::startNonRecursiveTask(reason,id,m_resultArray[i],loco);
|
2023-11-10 21:13:33 +01:00
|
|
|
}
|
|
|
|
|
2023-11-09 21:25:10 +01:00
|
|
|
|
2023-11-10 20:25:24 +01:00
|
|
|
void LookList::stream(Print * _stream) {
|
|
|
|
for (int16_t i=0;i<m_size;i++) {
|
|
|
|
_stream->print(" ");
|
|
|
|
_stream->print(m_lookupArray[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 20:27:52 +01:00
|
|
|
int16_t LookList::findPosition(int16_t value) {
|
|
|
|
for (int16_t i=0;i<m_size;i++) {
|
|
|
|
if (m_lookupArray[i]==value) return i;
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2023-11-09 20:27:52 +01:00
|
|
|
int16_t LookList::size() {
|
|
|
|
return m_size;
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2022-06-17 12:48:37 +02:00
|
|
|
LookList* RMFT2::LookListLoader(OPCODE op1, OPCODE op2, OPCODE op3) {
|
2021-08-03 23:12:25 +02:00
|
|
|
int progCounter;
|
2022-06-17 12:48:37 +02:00
|
|
|
int16_t count=0;
|
|
|
|
// find size for list
|
2021-11-18 11:42:54 +01:00
|
|
|
for (progCounter=0;; SKIPOP) {
|
2022-01-06 23:03:57 +01:00
|
|
|
byte opcode=GET_OPCODE;
|
|
|
|
if (opcode==OPCODE_ENDEXRAIL) break;
|
2022-06-17 12:48:37 +02:00
|
|
|
if (opcode==op1 || opcode==op2 || opcode==op3) count++;
|
|
|
|
}
|
|
|
|
// create list
|
|
|
|
LookList* list=new LookList(count);
|
|
|
|
if (count==0) return list;
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2022-06-17 12:48:37 +02:00
|
|
|
for (progCounter=0;; SKIPOP) {
|
|
|
|
byte opcode=GET_OPCODE;
|
|
|
|
if (opcode==OPCODE_ENDEXRAIL) break;
|
2022-12-16 14:14:48 +01:00
|
|
|
if (opcode==op1 || opcode==op2 || opcode==op3) list->add(getOperand(progCounter,0),progCounter);
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2022-06-17 12:48:37 +02:00
|
|
|
return list;
|
|
|
|
}
|
2021-11-25 12:36:05 +01:00
|
|
|
|
2022-06-17 12:48:37 +02:00
|
|
|
/* static */ void RMFT2::begin() {
|
2022-12-16 14:14:48 +01:00
|
|
|
|
2024-04-09 21:59:57 +02:00
|
|
|
//DIAG(F("EXRAIL RoutCode at =%P"),RouteCode);
|
2022-12-16 14:14:48 +01:00
|
|
|
|
|
|
|
bool saved_diag=diag;
|
|
|
|
diag=true;
|
2022-06-17 12:48:37 +02:00
|
|
|
DCCEXParser::setRMFTFilter(RMFT2::ComandFilter);
|
|
|
|
for (int f=0;f<MAX_FLAGS;f++) flags[f]=0;
|
|
|
|
|
2021-11-18 11:42:54 +01:00
|
|
|
// create lookups
|
2023-11-09 21:25:10 +01:00
|
|
|
routeLookup=LookListLoader(OPCODE_ROUTE, OPCODE_AUTOMATION);
|
|
|
|
routeLookup->chain(LookListLoader(OPCODE_SEQUENCE));
|
2023-11-10 20:25:24 +01:00
|
|
|
if (compileFeatures && FEATURE_ROUTESTATE) {
|
|
|
|
routeStateArray=(byte *)calloc(routeLookup->size(),sizeof(byte));
|
|
|
|
routeCaptionArray=(const FSH * *)calloc(routeLookup->size(),sizeof(const FSH *));
|
|
|
|
}
|
2022-06-17 12:48:37 +02:00
|
|
|
onThrowLookup=LookListLoader(OPCODE_ONTHROW);
|
|
|
|
onCloseLookup=LookListLoader(OPCODE_ONCLOSE);
|
|
|
|
onActivateLookup=LookListLoader(OPCODE_ONACTIVATE);
|
|
|
|
onDeactivateLookup=LookListLoader(OPCODE_ONDEACTIVATE);
|
2022-12-30 00:46:42 +01:00
|
|
|
onChangeLookup=LookListLoader(OPCODE_ONCHANGE);
|
2023-01-11 18:36:11 +01:00
|
|
|
onClockLookup=LookListLoader(OPCODE_ONTIME);
|
2023-09-06 23:58:19 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2023-09-02 00:29:49 +02:00
|
|
|
onRotateLookup=LookListLoader(OPCODE_ONROTATE);
|
2023-09-06 23:58:19 +02:00
|
|
|
#endif
|
2023-08-12 19:40:48 +02:00
|
|
|
onOverloadLookup=LookListLoader(OPCODE_ONOVERLOAD);
|
2024-09-28 13:29:12 +02:00
|
|
|
|
|
|
|
if (compileFeatures & FEATURE_BLOCK) {
|
|
|
|
onBlockEnterLookup=LookListLoader(OPCODE_ONBLOCKENTER);
|
|
|
|
onBlockExitLookup=LookListLoader(OPCODE_ONBLOCKEXIT);
|
|
|
|
}
|
|
|
|
|
2023-10-13 14:59:06 +02:00
|
|
|
// onLCCLookup is not the same so not loaded here.
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2021-11-18 11:42:54 +01:00
|
|
|
// Second pass startup, define any turnouts or servos, set signals red
|
2022-01-06 23:03:57 +01:00
|
|
|
// add sequences onRoutines to the lookups
|
2024-04-21 19:03:24 +02:00
|
|
|
if (compileFeatures & FEATURE_SIGNAL) {
|
2024-09-22 13:36:19 +02:00
|
|
|
|
2024-04-21 19:03:24 +02:00
|
|
|
onRedLookup=LookListLoader(OPCODE_ONRED);
|
|
|
|
onAmberLookup=LookListLoader(OPCODE_ONAMBER);
|
|
|
|
onGreenLookup=LookListLoader(OPCODE_ONGREEN);
|
2024-09-22 13:36:19 +02:00
|
|
|
// Load the signal lookup with slot numbers in the signal table
|
|
|
|
int signalCount=0;
|
|
|
|
for (int16_t slot=0;;slot++) {
|
|
|
|
SIGNAL_DEFINITION signal=getSignalSlot(slot);
|
|
|
|
DIAG(F("Signal s=%d id=%d t=%d"),slot,signal.id,signal.type);
|
|
|
|
if (signal.type==sigtypeNoMoreSignals) break;
|
|
|
|
if (signal.type==sigtypeContinuation) continue;
|
|
|
|
signalCount++;
|
|
|
|
}
|
|
|
|
signalLookup=new LookList(signalCount);
|
|
|
|
for (int16_t slot=0;;slot++) {
|
|
|
|
SIGNAL_DEFINITION signal=getSignalSlot(slot);
|
|
|
|
if (signal.type==sigtypeNoMoreSignals) break;
|
|
|
|
if (signal.type==sigtypeContinuation) continue;
|
|
|
|
signalLookup->add(signal.id,slot);
|
|
|
|
doSignal(signal.id, SIGNAL_RED);
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2022-06-17 12:48:37 +02:00
|
|
|
int progCounter;
|
2021-11-18 11:42:54 +01:00
|
|
|
for (progCounter=0;; SKIPOP){
|
2022-01-06 23:03:57 +01:00
|
|
|
byte opcode=GET_OPCODE;
|
|
|
|
if (opcode==OPCODE_ENDEXRAIL) break;
|
2022-12-16 14:14:48 +01:00
|
|
|
VPIN operand=getOperand(progCounter,0);
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
switch (opcode) {
|
|
|
|
case OPCODE_AT:
|
2022-06-18 13:31:54 +02:00
|
|
|
case OPCODE_ATTIMEOUT2:
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_AFTER:
|
|
|
|
case OPCODE_IF:
|
|
|
|
case OPCODE_IFNOT: {
|
|
|
|
int16_t pin = (int16_t)operand;
|
|
|
|
if (pin<0) pin = -pin;
|
2023-03-27 14:08:14 +02:00
|
|
|
DIAG(F("EXRAIL input VPIN %u"),pin);
|
2022-01-06 23:03:57 +01:00
|
|
|
IODevice::configureInput((VPIN)pin,true);
|
|
|
|
break;
|
|
|
|
}
|
2023-11-23 11:41:35 +01:00
|
|
|
case OPCODE_STASH:
|
|
|
|
case OPCODE_CLEAR_STASH:
|
|
|
|
case OPCODE_PICKUP_STASH: {
|
|
|
|
maxStashId=max(maxStashId,((int16_t)operand));
|
|
|
|
break;
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2022-10-05 23:14:07 +02:00
|
|
|
case OPCODE_ATGTE:
|
|
|
|
case OPCODE_ATLT:
|
|
|
|
case OPCODE_IFGTE:
|
|
|
|
case OPCODE_IFLT:
|
|
|
|
case OPCODE_DRIVE: {
|
2023-03-27 14:08:14 +02:00
|
|
|
DIAG(F("EXRAIL analog input VPIN %u"),(VPIN)operand);
|
2022-10-05 23:14:07 +02:00
|
|
|
IODevice::configureAnalogIn((VPIN)operand);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-04-13 09:12:35 +02:00
|
|
|
case OPCODE_ONSENSOR:
|
2024-04-13 11:16:26 +02:00
|
|
|
if (compileFeatures & FEATURE_SENSOR)
|
2024-04-13 09:12:35 +02:00
|
|
|
new EXRAILSensor(operand,progCounter+3,true );
|
2024-04-13 11:16:26 +02:00
|
|
|
break;
|
2024-04-13 09:12:35 +02:00
|
|
|
case OPCODE_ONBUTTON:
|
2024-04-13 11:16:26 +02:00
|
|
|
if (compileFeatures & FEATURE_SENSOR)
|
2024-04-13 09:12:35 +02:00
|
|
|
new EXRAILSensor(operand,progCounter+3,false );
|
2024-04-13 11:16:26 +02:00
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_TURNOUT: {
|
2021-11-18 11:42:54 +01:00
|
|
|
VPIN id=operand;
|
2022-12-16 14:14:48 +01:00
|
|
|
int addr=getOperand(progCounter,1);
|
|
|
|
byte subAddr=getOperand(progCounter,2);
|
2022-04-08 12:41:50 +02:00
|
|
|
setTurnoutHiddenState(DCCTurnout::create(id,addr,subAddr));
|
2021-11-18 11:42:54 +01:00
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_SERVOTURNOUT: {
|
2021-11-18 11:42:54 +01:00
|
|
|
VPIN id=operand;
|
2022-12-16 14:14:48 +01:00
|
|
|
VPIN pin=getOperand(progCounter,1);
|
|
|
|
int activeAngle=getOperand(progCounter,2);
|
|
|
|
int inactiveAngle=getOperand(progCounter,3);
|
|
|
|
int profile=getOperand(progCounter,4);
|
2022-04-08 12:41:50 +02:00
|
|
|
setTurnoutHiddenState(ServoTurnout::create(id,pin,activeAngle,inactiveAngle,profile));
|
2021-11-18 11:42:54 +01:00
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_PINTURNOUT: {
|
2021-11-18 11:42:54 +01:00
|
|
|
VPIN id=operand;
|
2022-12-16 14:14:48 +01:00
|
|
|
VPIN pin=getOperand(progCounter,1);
|
2022-04-08 12:41:50 +02:00
|
|
|
setTurnoutHiddenState(VpinTurnout::create(id,pin));
|
2021-11-18 11:42:54 +01:00
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2023-09-02 00:29:49 +02:00
|
|
|
|
2023-09-02 10:45:59 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2023-09-02 00:29:49 +02:00
|
|
|
case OPCODE_DCCTURNTABLE: {
|
|
|
|
VPIN id=operand;
|
2023-09-08 23:22:10 +02:00
|
|
|
int home=getOperand(progCounter,1);
|
2023-09-02 00:29:49 +02:00
|
|
|
setTurntableHiddenState(DCCTurntable::create(id));
|
|
|
|
Turntable *tto=Turntable::get(id);
|
2023-09-08 23:22:10 +02:00
|
|
|
tto->addPosition(0,0,home);
|
2023-09-02 00:29:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OPCODE_EXTTTURNTABLE: {
|
|
|
|
VPIN id=operand;
|
|
|
|
VPIN pin=getOperand(progCounter,1);
|
2024-11-03 23:24:04 +01:00
|
|
|
int home=getOperand(progCounter,2);
|
2023-09-02 00:29:49 +02:00
|
|
|
setTurntableHiddenState(EXTTTurntable::create(id,pin));
|
|
|
|
Turntable *tto=Turntable::get(id);
|
2023-09-08 23:22:10 +02:00
|
|
|
tto->addPosition(0,0,home);
|
2023-09-02 00:29:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OPCODE_TTADDPOSITION: {
|
|
|
|
VPIN id=operand;
|
2023-09-05 23:59:43 +02:00
|
|
|
int position=getOperand(progCounter,1);
|
|
|
|
int value=getOperand(progCounter,2);
|
2023-09-08 23:22:10 +02:00
|
|
|
int angle=getOperand(progCounter,3);
|
2023-09-02 00:29:49 +02:00
|
|
|
Turntable *tto=Turntable::get(id);
|
2023-09-08 23:22:10 +02:00
|
|
|
tto->addPosition(position,value,angle);
|
2023-09-02 00:29:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-09-02 10:45:59 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_AUTOSTART:
|
|
|
|
// automatically create a task from here at startup.
|
2023-03-18 19:52:01 +01:00
|
|
|
// Removed if (progCounter>0) check 4.2.31 because
|
|
|
|
// default start it top of file is now removed. .
|
|
|
|
new RMFT2(progCounter);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: // Ignore
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
SKIPOP; // include ENDROUTES opcode
|
2023-11-23 11:41:35 +01:00
|
|
|
|
|
|
|
if (compileFeatures & FEATURE_STASH) {
|
|
|
|
// create the stash array from the highest id found
|
|
|
|
if (maxStashId>0) stashArray=(int16_t*)calloc(maxStashId+1, sizeof(int16_t));
|
|
|
|
//TODO check EEPROM and fetch stashArray
|
|
|
|
}
|
|
|
|
|
2023-11-23 15:15:58 +01:00
|
|
|
DIAG(F("EXRAIL %db, fl=%d, stash=%d"),progCounter,MAX_FLAGS, maxStashId);
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2023-03-18 19:52:01 +01:00
|
|
|
// Removed for 4.2.31 new RMFT2(0); // add the startup route
|
2022-12-16 14:14:48 +01:00
|
|
|
diag=saved_diag;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 12:41:50 +02:00
|
|
|
void RMFT2::setTurnoutHiddenState(Turnout * t) {
|
2024-01-10 08:15:30 +01:00
|
|
|
// turnout descriptions are in low flash F strings
|
|
|
|
const FSH *desc = getTurnoutDescription(t->getId());
|
|
|
|
if (desc) t->setHidden(GETFLASH(desc)==0x01);
|
2022-04-08 12:41:50 +02:00
|
|
|
}
|
2022-04-08 17:13:15 +02:00
|
|
|
|
2023-09-02 10:45:59 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2023-09-02 00:29:49 +02:00
|
|
|
void RMFT2::setTurntableHiddenState(Turntable * tto) {
|
2024-01-10 08:15:30 +01:00
|
|
|
const FSH *desc = getTurntableDescription(tto->getId());
|
|
|
|
if (desc) tto->setHidden(GETFLASH(desc)==0x01);
|
2023-09-02 00:29:49 +02:00
|
|
|
}
|
2023-09-02 10:45:59 +02:00
|
|
|
#endif
|
2023-09-02 00:29:49 +02:00
|
|
|
|
2022-04-08 17:13:15 +02:00
|
|
|
char RMFT2::getRouteType(int16_t id) {
|
2023-11-10 20:25:24 +01:00
|
|
|
int16_t progCounter=routeLookup->find(id);
|
|
|
|
if (progCounter>=0) {
|
2023-11-11 00:28:41 +01:00
|
|
|
byte type=GET_OPCODE;
|
2023-11-10 20:25:24 +01:00
|
|
|
if (type==OPCODE_ROUTE) return 'R';
|
|
|
|
if (type==OPCODE_AUTOMATION) return 'A';
|
2022-04-08 17:13:15 +02:00
|
|
|
}
|
|
|
|
return 'X';
|
2023-04-19 12:18:47 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2024-09-28 13:29:12 +02:00
|
|
|
RMFT2::RMFT2(int progCtr, int16_t _loco) {
|
2021-08-03 23:12:25 +02:00
|
|
|
progCounter=progCtr;
|
2021-08-16 00:15:02 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
// get an unused task id from the flags table
|
2021-08-16 00:15:02 +02:00
|
|
|
taskId=255; // in case of overflow
|
|
|
|
for (int f=0;f<MAX_FLAGS;f++) {
|
|
|
|
if (!getFlag(f,TASK_FLAG)) {
|
|
|
|
taskId=f;
|
|
|
|
setFlag(f, TASK_FLAG);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
delayTime=0;
|
2024-09-28 13:29:12 +02:00
|
|
|
loco=_loco;
|
2021-08-03 23:12:25 +02:00
|
|
|
invert=false;
|
2024-04-09 21:45:28 +02:00
|
|
|
blinkState=not_blink_task;
|
2021-08-03 23:12:25 +02:00
|
|
|
stackDepth=0;
|
2022-06-17 12:48:37 +02:00
|
|
|
onEventStartPosition=-1; // Not handling an ONxxx
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
// chain into ring of RMFTs
|
|
|
|
if (loopTask==NULL) {
|
|
|
|
loopTask=this;
|
|
|
|
next=this;
|
2022-01-06 23:03:57 +01:00
|
|
|
} else {
|
|
|
|
next=loopTask->next;
|
|
|
|
loopTask->next=this;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RMFT2::~RMFT2() {
|
2024-09-28 13:29:12 +02:00
|
|
|
// estop my loco if this is not an ONevent
|
|
|
|
// (prevents DONE stopping loco at the end of an
|
|
|
|
// ONBLOCKENTER or ONBLOCKEXIT )
|
|
|
|
if (loco>0 && this->onEventStartPosition==-1) DCC::setThrottle(loco,1,DCC::getThrottleDirection(loco));
|
2021-08-16 00:15:02 +02:00
|
|
|
setFlag(taskId,0,TASK_FLAG); // we are no longer using this id
|
2022-01-06 23:03:57 +01:00
|
|
|
if (next==this)
|
|
|
|
loopTask=NULL;
|
|
|
|
else
|
|
|
|
for (RMFT2* ring=next;;ring=ring->next)
|
|
|
|
if (ring->next == this) {
|
|
|
|
ring->next=next;
|
|
|
|
loopTask=next;
|
|
|
|
break;
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RMFT2::createNewTask(int route, uint16_t cab) {
|
2023-11-09 21:25:10 +01:00
|
|
|
int pc=routeLookup->find(route);
|
2021-08-03 23:12:25 +02:00
|
|
|
if (pc<0) return;
|
2024-09-28 13:29:12 +02:00
|
|
|
new RMFT2(pc,cab);
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-04 11:38:38 +02:00
|
|
|
bool RMFT2::readSensor(uint16_t sensorId) {
|
2022-01-06 23:03:57 +01:00
|
|
|
// Exrail operands are unsigned but we need the signed version as inserted by the macros.
|
2021-09-04 11:38:38 +02:00
|
|
|
int16_t sId=(int16_t) sensorId;
|
|
|
|
|
|
|
|
VPIN vpin=abs(sId);
|
2021-08-03 23:12:25 +02:00
|
|
|
if (getFlag(vpin,LATCH_FLAG)) return true; // latched on
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2021-09-04 11:38:38 +02:00
|
|
|
// negative sensorIds invert the logic (e.g. for a break-beam sensor which goes OFF when detecting)
|
|
|
|
bool s= IODevice::read(vpin) ^ (sId<0);
|
|
|
|
if (s && diag) DIAG(F("EXRAIL Sensor %d hit"),sId);
|
2021-08-03 23:12:25 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-01-02 20:41:57 +01:00
|
|
|
// This skips to the end of an if block, or to the ELSE within it.
|
2021-08-03 23:12:25 +02:00
|
|
|
bool RMFT2::skipIfBlock() {
|
|
|
|
// returns false if killed
|
|
|
|
short nest = 1;
|
|
|
|
while (nest > 0) {
|
|
|
|
SKIPOP;
|
|
|
|
byte opcode = GET_OPCODE;
|
2022-04-17 11:10:22 +02:00
|
|
|
// all other IF type commands increase the nesting level
|
|
|
|
if (opcode>IF_TYPE_OPCODES) nest++;
|
|
|
|
else switch(opcode) {
|
|
|
|
case OPCODE_ENDEXRAIL:
|
|
|
|
kill(F("missing ENDIF"), nest);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case OPCODE_ENDIF:
|
|
|
|
nest--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_ELSE:
|
|
|
|
// if nest==1 then this is the ELSE for the IF we are skipping
|
|
|
|
if (nest==1) nest=0; // cause loop exit and return after ELSE
|
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
default:
|
2021-08-03 23:12:25 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
|
|
|
return true;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-08-21 14:17:14 +02:00
|
|
|
/* static */ void RMFT2::readLocoCallback(int16_t cv) {
|
2024-09-04 09:11:51 +02:00
|
|
|
if (cv <= 0) {
|
|
|
|
DIAG(F("CV read error"));
|
|
|
|
progtrackLocoId = -1;
|
|
|
|
return;
|
|
|
|
}
|
2022-09-13 22:42:38 +02:00
|
|
|
if (cv & LONG_ADDR_MARKER) { // maker bit indicates long addr
|
|
|
|
progtrackLocoId = cv ^ LONG_ADDR_MARKER; // remove marker bit to get real long addr
|
|
|
|
if (progtrackLocoId <= HIGHEST_SHORT_ADDR ) { // out of range for long addr
|
2024-09-28 13:29:12 +02:00
|
|
|
DIAG(F("Long addr %d <= %d unsupported\n"), progtrackLocoId, HIGHEST_SHORT_ADDR);
|
2022-09-13 22:42:38 +02:00
|
|
|
progtrackLocoId = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
progtrackLocoId=cv;
|
|
|
|
}
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
2024-09-28 13:29:12 +02:00
|
|
|
void RMFT2::pause() {
|
|
|
|
if (loco)
|
|
|
|
pauseSpeed=DCC::getThrottleSpeedByte(loco);
|
|
|
|
}
|
|
|
|
void RMFT2::resume() {
|
|
|
|
if (loco)
|
|
|
|
DCC::setThrottle(loco,pauseSpeed & 0x7f, pauseSpeed & 0x80);
|
|
|
|
}
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
void RMFT2::loop() {
|
2024-04-13 11:16:26 +02:00
|
|
|
if (compileFeatures & FEATURE_SENSOR)
|
|
|
|
EXRAILSensor::checkAll();
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
// Round Robin call to a RMFT task each time
|
|
|
|
if (loopTask==NULL) return;
|
|
|
|
loopTask=loopTask->next;
|
|
|
|
if (pausingTask==NULL || pausingTask==loopTask) loopTask->loop2();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
void RMFT2::loop2() {
|
2022-01-06 23:03:57 +01:00
|
|
|
if (delayTime!=0 && millis()-delayStart < delayTime) return;
|
|
|
|
|
2024-04-09 21:45:28 +02:00
|
|
|
// special stand alone blink task
|
|
|
|
if (compileFeatures & FEATURE_BLINK) {
|
|
|
|
if (blinkState==blink_low) {
|
|
|
|
IODevice::write(blinkPin,HIGH);
|
|
|
|
blinkState=blink_high;
|
|
|
|
delayMe(getOperand(1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (blinkState==blink_high) {
|
|
|
|
IODevice::write(blinkPin,LOW);
|
|
|
|
blinkState=blink_low;
|
|
|
|
delayMe(getOperand(2));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal progstep following tasks continue here.
|
2021-08-03 23:12:25 +02:00
|
|
|
byte opcode = GET_OPCODE;
|
2022-12-16 14:14:48 +01:00
|
|
|
int16_t operand = getOperand(0);
|
2022-04-17 10:58:32 +02:00
|
|
|
|
|
|
|
// skipIf will get set to indicate a failing IF condition
|
|
|
|
bool skipIf=false;
|
2022-04-17 11:10:22 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
// if (diag) DIAG(F("RMFT2 %d %d"),opcode,operand);
|
2021-08-03 23:12:25 +02:00
|
|
|
// Attention: Returning from this switch leaves the program counter unchanged.
|
|
|
|
// This is used for unfinished waits for timers or sensors.
|
2022-01-06 23:03:57 +01:00
|
|
|
// Breaking from this switch will step to the next step in the route.
|
2021-08-03 23:12:25 +02:00
|
|
|
switch ((OPCODE)opcode) {
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_THROW:
|
|
|
|
Turnout::setClosed(operand, false);
|
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_CLOSE:
|
|
|
|
Turnout::setClosed(operand, true);
|
|
|
|
break;
|
2023-09-02 10:45:59 +02:00
|
|
|
|
2024-04-09 21:45:28 +02:00
|
|
|
case OPCODE_TOGGLE_TURNOUT:
|
|
|
|
Turnout::setClosed(operand, Turnout::isThrown(operand));
|
|
|
|
break;
|
|
|
|
|
2023-09-02 10:45:59 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2023-09-02 00:29:49 +02:00
|
|
|
case OPCODE_ROTATE:
|
|
|
|
uint8_t activity;
|
|
|
|
activity=getOperand(2);
|
|
|
|
Turntable::setPosition(operand,getOperand(1),activity);
|
|
|
|
break;
|
2023-09-02 10:45:59 +02:00
|
|
|
#endif
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_REV:
|
2024-09-28 13:29:12 +02:00
|
|
|
if (loco) DCC::setThrottle(loco,operand,invert);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_FWD:
|
2024-09-28 13:29:12 +02:00
|
|
|
if (loco) DCC::setThrottle(loco,operand,!invert);
|
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_SPEED:
|
2024-09-28 13:29:12 +02:00
|
|
|
if (loco) DCC::setThrottle(loco,operand,DCC::getThrottleDirection(loco));
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2024-11-24 15:44:36 +01:00
|
|
|
|
|
|
|
case OPCODE_ESTOPALL:
|
|
|
|
DCC::setThrottle(0,1,1); // all locos speed=1
|
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-03-24 11:40:49 +01:00
|
|
|
case OPCODE_FORGET:
|
|
|
|
if (loco!=0) {
|
|
|
|
DCC::forgetLoco(loco);
|
|
|
|
loco=0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_INVERT_DIRECTION:
|
|
|
|
invert= !invert;
|
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_RESERVE:
|
|
|
|
if (getFlag(operand,SECTION_FLAG)) {
|
2024-09-28 13:29:12 +02:00
|
|
|
if (loco) DCC::setThrottle(loco,0,DCC::getThrottleDirection(loco));
|
2022-01-06 23:03:57 +01:00
|
|
|
delayMe(500);
|
2021-08-03 23:12:25 +02:00
|
|
|
return;
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
|
|
|
setFlag(operand,SECTION_FLAG);
|
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_FREE:
|
|
|
|
setFlag(operand,0,SECTION_FLAG);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_AT:
|
2024-04-09 21:45:28 +02:00
|
|
|
blinkState=not_blink_task;
|
2022-01-06 23:03:57 +01:00
|
|
|
if (readSensor(operand)) break;
|
|
|
|
delayMe(50);
|
|
|
|
return;
|
|
|
|
|
2022-03-18 14:46:07 +01:00
|
|
|
case OPCODE_ATGTE: // wait for analog sensor>= value
|
2024-04-09 21:45:28 +02:00
|
|
|
blinkState=not_blink_task;
|
2022-12-16 14:14:48 +01:00
|
|
|
if (IODevice::readAnalogue(operand) >= (int)(getOperand(1))) break;
|
2022-03-18 14:46:07 +01:00
|
|
|
delayMe(50);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case OPCODE_ATLT: // wait for analog sensor < value
|
2024-04-09 21:45:28 +02:00
|
|
|
blinkState=not_blink_task;
|
2022-12-16 14:14:48 +01:00
|
|
|
if (IODevice::readAnalogue(operand) < (int)(getOperand(1))) break;
|
2022-03-18 14:46:07 +01:00
|
|
|
delayMe(50);
|
|
|
|
return;
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_ATTIMEOUT1: // ATTIMEOUT(vpin,timeout) part 1
|
|
|
|
timeoutStart=millis();
|
2024-04-09 21:45:28 +02:00
|
|
|
blinkState=not_blink_task;
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_ATTIMEOUT2:
|
|
|
|
if (readSensor(operand)) break; // success without timeout
|
2022-12-16 14:14:48 +01:00
|
|
|
if (millis()-timeoutStart > 100*getOperand(1)) {
|
2024-04-09 21:45:28 +02:00
|
|
|
blinkState=at_timeout;
|
2022-01-06 23:03:57 +01:00
|
|
|
break; // and drop through
|
|
|
|
}
|
|
|
|
delayMe(50);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case OPCODE_IFTIMEOUT: // do next operand if timeout flag set
|
2024-04-09 21:45:28 +02:00
|
|
|
skipIf=blinkState!=at_timeout;
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
2024-07-10 11:58:22 +02:00
|
|
|
case OPCODE_AFTER: // waits for sensor to hit and then remain off for x mS.
|
|
|
|
// Note, this must come after an AT operation, which is
|
|
|
|
// automatically inserted by the AFTER macro.
|
2022-01-06 23:03:57 +01:00
|
|
|
if (readSensor(operand)) {
|
2024-07-10 11:58:22 +02:00
|
|
|
// reset timer and keep waiting
|
2022-01-06 23:03:57 +01:00
|
|
|
waitAfter=millis();
|
2022-01-03 11:15:10 +01:00
|
|
|
delayMe(50);
|
|
|
|
return;
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2024-07-10 11:58:22 +02:00
|
|
|
if (millis()-waitAfter < getOperand(1) ) return;
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2023-09-05 13:21:09 +02:00
|
|
|
|
|
|
|
case OPCODE_AFTEROVERLOAD: // waits for the power to be turned back on - either by power routine or button
|
|
|
|
if (!TrackManager::isPowerOn(operand)) {
|
|
|
|
// reset timer to half a second and keep waiting
|
|
|
|
waitAfter=millis();
|
|
|
|
delayMe(50);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (millis()-waitAfter < 500 ) return;
|
|
|
|
break;
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_LATCH:
|
|
|
|
setFlag(operand,LATCH_FLAG);
|
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_UNLATCH:
|
|
|
|
setFlag(operand,0,LATCH_FLAG);
|
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_SET:
|
|
|
|
case OPCODE_RESET:
|
2024-11-05 13:23:06 +01:00
|
|
|
{
|
|
|
|
auto count=getOperand(1);
|
2024-11-06 02:05:35 +01:00
|
|
|
for (uint16_t i=0;i<count;i++) {
|
2024-11-05 13:23:06 +01:00
|
|
|
killBlinkOnVpin(operand+i);
|
|
|
|
IODevice::write(operand+i,opcode==OPCODE_SET);
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2024-04-09 21:45:28 +02:00
|
|
|
|
|
|
|
case OPCODE_BLINK:
|
|
|
|
// Start a new task to blink this vpin
|
|
|
|
killBlinkOnVpin(operand);
|
|
|
|
{
|
|
|
|
auto newtask=new RMFT2(progCounter);
|
|
|
|
newtask->blinkPin=operand;
|
|
|
|
newtask->blinkState=blink_low; // will go high on first call
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_PAUSE:
|
2024-09-28 13:29:12 +02:00
|
|
|
// all tasks save their speed bytes
|
|
|
|
pause();
|
|
|
|
for (RMFT2 * t=next; t!=this;t=t->next) t->pause();
|
|
|
|
|
2024-08-08 10:45:44 +02:00
|
|
|
DCC::setThrottle(0,1,true); // pause all locos on the track
|
2022-01-06 23:03:57 +01:00
|
|
|
pausingTask=this;
|
|
|
|
break;
|
2023-04-19 23:08:11 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_POM:
|
2022-12-16 14:14:48 +01:00
|
|
|
if (loco) DCC::writeCVByteMain(loco, operand, getOperand(1));
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2023-04-19 23:08:11 +02:00
|
|
|
|
2024-11-25 13:33:44 +01:00
|
|
|
case OPCODE_XPOM:
|
|
|
|
DCC::writeCVByteMain(operand, getOperand(1), getOperand(2));
|
|
|
|
break;
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_POWEROFF:
|
2022-02-23 16:44:34 +01:00
|
|
|
TrackManager::setPower(POWERMODE::OFF);
|
2022-03-19 12:22:31 +01:00
|
|
|
TrackManager::setJoin(false);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2023-09-24 16:40:42 +02:00
|
|
|
|
|
|
|
case OPCODE_SET_POWER:
|
|
|
|
// operand is TRACK_POWER , trackid
|
2023-09-25 15:32:54 +02:00
|
|
|
//byte thistrack=getOperand(1);
|
2023-09-24 16:40:42 +02:00
|
|
|
switch (operand) {
|
|
|
|
case TRACK_POWER_0:
|
2023-11-14 00:05:18 +01:00
|
|
|
TrackManager::setTrackPower(POWERMODE::OFF, getOperand(1));
|
2023-09-24 16:40:42 +02:00
|
|
|
break;
|
|
|
|
case TRACK_POWER_1:
|
2023-11-14 00:05:18 +01:00
|
|
|
TrackManager::setTrackPower(POWERMODE::ON, getOperand(1));
|
2023-09-24 16:40:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2022-02-24 12:50:22 +01:00
|
|
|
|
|
|
|
case OPCODE_SET_TRACK:
|
2022-03-19 17:26:29 +01:00
|
|
|
// operand is trackmode<<8 | track id
|
|
|
|
// If DC/DCX use my loco for DC address
|
|
|
|
{
|
|
|
|
TRACK_MODE mode = (TRACK_MODE)(operand>>8);
|
2023-11-10 23:58:30 +01:00
|
|
|
int16_t cab=(mode & TRACK_MODE_DC) ? loco : 0;
|
2022-03-19 17:26:29 +01:00
|
|
|
TrackManager::setTrackMode(operand & 0x0F, mode, cab);
|
|
|
|
}
|
2022-02-24 12:50:22 +01:00
|
|
|
break;
|
|
|
|
|
2024-01-20 19:09:03 +01:00
|
|
|
case OPCODE_SETFREQ:
|
|
|
|
// Frequency is default 0, or 1, 2,3
|
2024-07-04 18:20:37 +02:00
|
|
|
DCC::setDCFreq(loco,operand);
|
2024-01-20 19:09:03 +01:00
|
|
|
break;
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_RESUME:
|
|
|
|
pausingTask=NULL;
|
2024-09-28 13:29:12 +02:00
|
|
|
resume();
|
|
|
|
for (RMFT2 * t=next; t!=this;t=t->next) t->resume();
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2022-01-03 20:15:44 +01:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_IF: // do next operand if sensor set
|
2022-04-17 10:58:32 +02:00
|
|
|
skipIf=!readSensor(operand);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_ELSE: // skip to matching ENDIF
|
2022-04-17 10:58:32 +02:00
|
|
|
skipIf=true;
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_IFGTE: // do next operand if sensor>= value
|
2022-12-16 14:14:48 +01:00
|
|
|
skipIf=IODevice::readAnalogue(operand)<(int)(getOperand(1));
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_IFLT: // do next operand if sensor< value
|
2022-12-16 14:14:48 +01:00
|
|
|
skipIf=IODevice::readAnalogue(operand)>=(int)(getOperand(1));
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2023-01-26 16:55:58 +01:00
|
|
|
case OPCODE_IFLOCO: // do if the loco is the active one
|
2023-01-27 19:42:55 +01:00
|
|
|
skipIf=loco!=(uint16_t)operand; // bad luck if someone enters negative loco numbers into EXRAIL
|
2023-01-26 16:55:58 +01:00
|
|
|
break;
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_IFNOT: // do next operand if sensor not set
|
2022-04-17 10:58:32 +02:00
|
|
|
skipIf=readSensor(operand);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2022-12-30 00:46:42 +01:00
|
|
|
|
|
|
|
case OPCODE_IFRE: // do next operand if rotary encoder != position
|
|
|
|
skipIf=IODevice::readAnalogue(operand)!=(int)(getOperand(1));
|
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_IFRANDOM: // do block on random percentage
|
2022-11-24 20:24:15 +01:00
|
|
|
skipIf=(uint8_t)micros() >= operand * 255/100;
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_IFRESERVE: // do block if we successfully RERSERVE
|
|
|
|
if (!getFlag(operand,SECTION_FLAG)) setFlag(operand,SECTION_FLAG);
|
2022-04-17 10:58:32 +02:00
|
|
|
else skipIf=true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_IFRED: // do block if signal as expected
|
|
|
|
skipIf=!isSignal(operand,SIGNAL_RED);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_IFAMBER: // do block if signal as expected
|
|
|
|
skipIf=!isSignal(operand,SIGNAL_AMBER);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_IFGREEN: // do block if signal as expected
|
|
|
|
skipIf=!isSignal(operand,SIGNAL_GREEN);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_IFTHROWN:
|
2022-04-17 10:58:32 +02:00
|
|
|
skipIf=Turnout::isClosed(operand);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_IFCLOSED:
|
2022-04-17 10:58:32 +02:00
|
|
|
skipIf=Turnout::isThrown(operand);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2023-09-02 00:29:49 +02:00
|
|
|
|
2023-09-02 10:45:59 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2023-09-02 00:29:49 +02:00
|
|
|
case OPCODE_IFTTPOSITION: // do block if turntable at this position
|
|
|
|
skipIf=Turntable::getPosition(operand)!=(int)getOperand(1);
|
|
|
|
break;
|
2023-09-02 10:45:59 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_ENDIF:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_DELAYMS:
|
|
|
|
delayMe(operand);
|
|
|
|
break;
|
2021-09-06 13:27:21 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_DELAY:
|
|
|
|
delayMe(operand*100L);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_DELAYMINS:
|
|
|
|
delayMe(operand*60L*1000L);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_RANDWAIT:
|
2022-09-05 09:02:35 +02:00
|
|
|
delayMe(operand==0 ? 0 : (micros()%operand) *100L);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_RED:
|
2022-04-17 10:58:32 +02:00
|
|
|
doSignal(operand,SIGNAL_RED);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_AMBER:
|
2022-04-17 10:58:32 +02:00
|
|
|
doSignal(operand,SIGNAL_AMBER);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_GREEN:
|
2022-04-17 10:58:32 +02:00
|
|
|
doSignal(operand,SIGNAL_GREEN);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_FON:
|
|
|
|
if (loco) DCC::setFn(loco,operand,true);
|
|
|
|
break;
|
2021-11-24 12:56:55 +01:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_FOFF:
|
|
|
|
if (loco) DCC::setFn(loco,operand,false);
|
|
|
|
break;
|
2024-04-09 21:45:28 +02:00
|
|
|
|
|
|
|
case OPCODE_FTOGGLE:
|
|
|
|
if (loco) DCC::changeFn(loco,operand);
|
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_DRIVE:
|
|
|
|
{
|
2024-09-28 13:29:12 +02:00
|
|
|
// Non functional but reserved
|
2021-11-22 12:10:26 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_XFON:
|
2022-12-16 14:14:48 +01:00
|
|
|
DCC::setFn(operand,getOperand(1),true);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_XFOFF:
|
2022-12-16 14:14:48 +01:00
|
|
|
DCC::setFn(operand,getOperand(1),false);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2024-04-09 21:45:28 +02:00
|
|
|
|
|
|
|
case OPCODE_XFTOGGLE:
|
|
|
|
DCC::changeFn(operand,getOperand(1));
|
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_DCCACTIVATE: {
|
|
|
|
// operand is address<<3 | subaddr<<1 | active
|
|
|
|
int16_t addr=operand>>3;
|
|
|
|
int16_t subaddr=(operand>>1) & 0x03;
|
|
|
|
bool active=operand & 0x01;
|
|
|
|
DCC::setAccessory(addr,subaddr,active);
|
|
|
|
break;
|
|
|
|
}
|
2024-02-15 21:05:27 +01:00
|
|
|
case OPCODE_ASPECT: {
|
|
|
|
// operand is address<<5 | value
|
2024-02-16 13:36:33 +01:00
|
|
|
int16_t address=operand>>5;
|
|
|
|
byte aspect=operand & 0x1f;
|
|
|
|
if (!signalAspectEvent(address,aspect))
|
|
|
|
DCC::setExtendedAccessory(address,aspect);
|
2024-02-15 21:05:27 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_FOLLOW:
|
2023-11-09 21:25:10 +01:00
|
|
|
progCounter=routeLookup->find(operand);
|
2022-01-06 23:03:57 +01:00
|
|
|
if (progCounter<0) kill(F("FOLLOW unknown"), operand);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case OPCODE_CALL:
|
|
|
|
if (stackDepth==MAX_STACK_DEPTH) {
|
|
|
|
kill(F("CALL stack"), stackDepth);
|
2021-08-03 23:12:25 +02:00
|
|
|
return;
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
|
|
|
callStack[stackDepth++]=progCounter+3;
|
2023-11-09 21:25:10 +01:00
|
|
|
progCounter=routeLookup->find(operand);
|
2022-01-06 23:03:57 +01:00
|
|
|
if (progCounter<0) kill(F("CALL unknown"),operand);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case OPCODE_RETURN:
|
|
|
|
if (stackDepth==0) {
|
|
|
|
kill(F("RETURN stack"));
|
2021-08-03 23:12:25 +02:00
|
|
|
return;
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
|
|
|
progCounter=callStack[--stackDepth];
|
|
|
|
return;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_ENDTASK:
|
|
|
|
case OPCODE_ENDEXRAIL:
|
|
|
|
kill();
|
|
|
|
return;
|
2022-03-31 22:52:43 +02:00
|
|
|
|
|
|
|
case OPCODE_KILLALL:
|
|
|
|
while(loopTask) loopTask->kill(F("KILLALL"));
|
|
|
|
return;
|
|
|
|
|
2023-04-23 22:45:47 +02:00
|
|
|
#ifndef DISABLE_PROG
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_JOIN:
|
2022-02-23 16:44:34 +01:00
|
|
|
TrackManager::setPower(POWERMODE::ON);
|
2022-03-19 12:22:31 +01:00
|
|
|
TrackManager::setJoin(true);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2023-04-23 22:45:47 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_UNJOIN:
|
2022-03-19 12:22:31 +01:00
|
|
|
TrackManager::setJoin(false);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
2023-04-23 22:45:47 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_READ_LOCO1: // READ_LOCO is implemented as 2 separate opcodes
|
|
|
|
progtrackLocoId=LOCO_ID_WAITING; // Nothing found yet
|
|
|
|
DCC::getLocoId(readLocoCallback);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_READ_LOCO2:
|
|
|
|
if (progtrackLocoId==LOCO_ID_WAITING) {
|
|
|
|
delayMe(100);
|
|
|
|
return; // still waiting for callback
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2024-09-06 09:28:40 +02:00
|
|
|
// At failed read will result in loco == -1
|
|
|
|
// which is intended so it can be checked
|
|
|
|
// from within EXRAIL
|
2022-01-06 23:03:57 +01:00
|
|
|
loco=progtrackLocoId;
|
|
|
|
invert=false;
|
|
|
|
break;
|
2023-04-23 22:45:47 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
case OPCODE_POWERON:
|
|
|
|
TrackManager::setMainPower(POWERMODE::ON);
|
|
|
|
TrackManager::setJoin(false);
|
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_START:
|
|
|
|
{
|
2023-11-09 21:25:10 +01:00
|
|
|
int newPc=routeLookup->find(operand);
|
2022-01-06 23:03:57 +01:00
|
|
|
if (newPc<0) break;
|
|
|
|
new RMFT2(newPc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_SENDLOCO: // cab, route
|
|
|
|
{
|
2023-11-09 21:25:10 +01:00
|
|
|
int newPc=routeLookup->find(getOperand(1));
|
2022-01-06 23:03:57 +01:00
|
|
|
if (newPc<0) break;
|
2024-09-28 13:29:12 +02:00
|
|
|
new RMFT2(newPc,operand); // create new task
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_SETLOCO:
|
|
|
|
{
|
|
|
|
loco=operand;
|
|
|
|
invert=false;
|
|
|
|
}
|
|
|
|
break;
|
2023-10-13 14:59:06 +02:00
|
|
|
|
|
|
|
case OPCODE_LCC: // short form LCC
|
|
|
|
if ((compileFeatures & FEATURE_LCC) && LCCSerial)
|
|
|
|
StringFormatter::send(LCCSerial,F("<L x%h>"),(uint16_t)operand);
|
|
|
|
break;
|
2024-06-13 14:01:33 +02:00
|
|
|
|
|
|
|
case OPCODE_ACON: // MERG adapter
|
|
|
|
case OPCODE_ACOF:
|
|
|
|
if ((compileFeatures & FEATURE_LCC) && LCCSerial)
|
|
|
|
StringFormatter::send(LCCSerial,F("<L x%c%h%h>"),
|
|
|
|
opcode==OPCODE_ACON?'0':'1',
|
|
|
|
(uint16_t)operand,getOperand(progCounter,1));
|
|
|
|
break;
|
2023-10-13 14:59:06 +02:00
|
|
|
|
|
|
|
case OPCODE_LCCX: // long form LCC
|
|
|
|
if ((compileFeatures & FEATURE_LCC) && LCCSerial)
|
|
|
|
StringFormatter::send(LCCSerial,F("<L x%h%h%h%h>\n"),
|
|
|
|
getOperand(progCounter,1),
|
|
|
|
getOperand(progCounter,2),
|
|
|
|
getOperand(progCounter,3),
|
|
|
|
getOperand(progCounter,0)
|
|
|
|
);
|
|
|
|
break;
|
2022-01-06 23:03:57 +01:00
|
|
|
|
|
|
|
case OPCODE_SERVO: // OPCODE_SERVO,V(vpin),OPCODE_PAD,V(position),OPCODE_PAD,V(profile),OPCODE_PAD,V(duration)
|
2022-12-16 14:14:48 +01:00
|
|
|
IODevice::writeAnalogue(operand,getOperand(1),getOperand(2),getOperand(3));
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_WAITFOR: // OPCODE_SERVO,V(pin)
|
|
|
|
if (IODevice::isBusy(operand)) {
|
|
|
|
delayMe(100);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2024-09-22 13:36:19 +02:00
|
|
|
|
2023-09-06 23:58:19 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2024-09-22 13:36:19 +02:00
|
|
|
case OPCODE_NEOPIXEL:
|
|
|
|
// OPCODE_NEOPIXEL,V([-]vpin),OPCODE_PAD,V(colour_RG),OPCODE_PAD,V(colour_B),OPCODE_PAD,V(count)
|
|
|
|
{
|
|
|
|
VPIN vpin=operand>0?operand:-operand;
|
|
|
|
auto count=getOperand(3);
|
|
|
|
killBlinkOnVpin(vpin,count);
|
|
|
|
IODevice::writeAnalogueRange(vpin,getOperand(1),operand>0,getOperand(2),count);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-09-06 23:58:19 +02:00
|
|
|
case OPCODE_WAITFORTT: // OPCODE_WAITFOR,V(turntable_id)
|
|
|
|
if (Turntable::ttMoving(operand)) {
|
|
|
|
delayMe(100);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_PRINT:
|
|
|
|
printMessage(operand);
|
|
|
|
break;
|
2023-11-07 17:27:26 +01:00
|
|
|
case OPCODE_ROUTE_HIDDEN:
|
2023-11-09 21:25:10 +01:00
|
|
|
manageRouteState(operand,2);
|
2023-11-07 17:27:26 +01:00
|
|
|
break;
|
2023-11-11 18:31:38 +01:00
|
|
|
case OPCODE_ROUTE_INACTIVE:
|
2023-11-09 21:25:10 +01:00
|
|
|
manageRouteState(operand,0);
|
2023-11-07 17:27:26 +01:00
|
|
|
break;
|
2023-11-11 18:31:38 +01:00
|
|
|
case OPCODE_ROUTE_ACTIVE:
|
2023-11-09 21:25:10 +01:00
|
|
|
manageRouteState(operand,1);
|
2023-11-07 17:27:26 +01:00
|
|
|
break;
|
2023-11-17 11:45:36 +01:00
|
|
|
case OPCODE_ROUTE_DISABLED:
|
|
|
|
manageRouteState(operand,4);
|
|
|
|
break;
|
2023-11-23 11:41:35 +01:00
|
|
|
|
|
|
|
case OPCODE_STASH:
|
|
|
|
if (compileFeatures & FEATURE_STASH)
|
|
|
|
stashArray[operand] = invert? -loco : loco;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_CLEAR_STASH:
|
|
|
|
if (compileFeatures & FEATURE_STASH)
|
|
|
|
stashArray[operand] = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_CLEAR_ALL_STASH:
|
|
|
|
if (compileFeatures & FEATURE_STASH)
|
|
|
|
for (int i=0;i<=maxStashId;i++) stashArray[operand]=0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_PICKUP_STASH:
|
|
|
|
if (compileFeatures & FEATURE_STASH) {
|
|
|
|
int16_t x=stashArray[operand];
|
|
|
|
if (x>=0) {
|
|
|
|
loco=x;
|
|
|
|
invert=false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
loco=-x;
|
|
|
|
invert=true;
|
|
|
|
}
|
|
|
|
break;
|
2023-11-07 17:27:26 +01:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_ROUTE:
|
|
|
|
case OPCODE_AUTOMATION:
|
|
|
|
case OPCODE_SEQUENCE:
|
2024-04-09 21:59:57 +02:00
|
|
|
//if (diag) DIAG(F("EXRAIL begin(%d)"),operand);
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPCODE_AUTOSTART: // Handled only during begin process
|
2022-03-06 16:05:35 +01:00
|
|
|
case OPCODE_PAD: // Just a padding for previous opcode needing >1 operand byte.
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_TURNOUT: // Turnout definition ignored at runtime
|
|
|
|
case OPCODE_SERVOTURNOUT: // Turnout definition ignored at runtime
|
|
|
|
case OPCODE_PINTURNOUT: // Turnout definition ignored at runtime
|
2022-03-06 16:05:35 +01:00
|
|
|
case OPCODE_ONCLOSE: // Turnout event catchers ignored here
|
2023-10-13 14:59:06 +02:00
|
|
|
case OPCODE_ONLCC: // LCC event catchers ignored here
|
2024-06-13 14:01:33 +02:00
|
|
|
case OPCODE_ONACON: // MERG event catchers ignored here
|
|
|
|
case OPCODE_ONACOF: // MERG event catchers ignored here
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_ONTHROW:
|
2022-03-06 16:05:35 +01:00
|
|
|
case OPCODE_ONACTIVATE: // Activate event catchers ignored here
|
2022-01-06 23:03:57 +01:00
|
|
|
case OPCODE_ONDEACTIVATE:
|
2022-06-18 13:31:54 +02:00
|
|
|
case OPCODE_ONRED:
|
|
|
|
case OPCODE_ONAMBER:
|
|
|
|
case OPCODE_ONGREEN:
|
2022-12-30 00:46:42 +01:00
|
|
|
case OPCODE_ONCHANGE:
|
2023-01-11 18:36:11 +01:00
|
|
|
case OPCODE_ONTIME:
|
2024-04-13 09:12:35 +02:00
|
|
|
case OPCODE_ONBUTTON:
|
|
|
|
case OPCODE_ONSENSOR:
|
2023-09-06 23:58:19 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2023-09-02 00:29:49 +02:00
|
|
|
case OPCODE_DCCTURNTABLE: // Turntable definition ignored at runtime
|
|
|
|
case OPCODE_EXTTTURNTABLE: // Turntable definition ignored at runtime
|
|
|
|
case OPCODE_TTADDPOSITION: // Turntable position definition ignored at runtime
|
|
|
|
case OPCODE_ONROTATE:
|
2023-09-06 23:58:19 +02:00
|
|
|
#endif
|
2023-08-12 19:40:48 +02:00
|
|
|
case OPCODE_ONOVERLOAD:
|
2024-09-28 13:29:12 +02:00
|
|
|
case OPCODE_ONBLOCKENTER:
|
|
|
|
case OPCODE_ONBLOCKEXIT:
|
2022-01-06 23:03:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
kill(F("INVOP"),operand);
|
|
|
|
}
|
|
|
|
// Falling out of the switch means move on to the next opcode
|
2022-04-17 10:58:32 +02:00
|
|
|
// but if we are skipping a false IF or else
|
|
|
|
if (skipIf) if (!skipIfBlock()) return;
|
2022-01-06 23:03:57 +01:00
|
|
|
SKIPOP;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RMFT2::delayMe(long delay) {
|
2022-01-06 23:03:57 +01:00
|
|
|
delayTime=delay;
|
|
|
|
delayStart=millis();
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-30 03:11:27 +01:00
|
|
|
bool RMFT2::setFlag(VPIN id,byte onMask, byte offMask) {
|
2022-06-18 13:31:54 +02:00
|
|
|
if (FLAGOVERFLOW(id)) return false; // Outside range limit
|
2021-08-03 23:12:25 +02:00
|
|
|
byte f=flags[id];
|
|
|
|
f &= ~offMask;
|
|
|
|
f |= onMask;
|
2021-08-16 00:15:02 +02:00
|
|
|
flags[id]=f;
|
2022-06-18 13:31:54 +02:00
|
|
|
return true;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 00:15:02 +02:00
|
|
|
bool RMFT2::getFlag(VPIN id,byte mask) {
|
2022-01-06 23:03:57 +01:00
|
|
|
if (FLAGOVERFLOW(id)) return 0; // Outside range limit
|
|
|
|
return flags[id]&mask;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RMFT2::kill(const FSH * reason, int operand) {
|
2022-01-06 23:03:57 +01:00
|
|
|
if (reason) DIAG(F("EXRAIL ERROR pc=%d, cab=%d, %S %d"), progCounter,loco, reason, operand);
|
|
|
|
else if (diag) DIAG(F("ENDTASK at pc=%d"), progCounter);
|
|
|
|
delete this;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
2024-05-07 12:20:37 +02:00
|
|
|
|
2024-09-22 13:36:19 +02:00
|
|
|
SIGNAL_DEFINITION RMFT2::getSignalSlot(int16_t slot) {
|
|
|
|
SIGNAL_DEFINITION signal;
|
|
|
|
COPYHIGHFLASH(&signal,SignalDefinitions,slot*sizeof(SIGNAL_DEFINITION),sizeof(SIGNAL_DEFINITION));
|
|
|
|
return signal;
|
2022-04-17 10:58:32 +02:00
|
|
|
}
|
2022-12-16 14:14:48 +01:00
|
|
|
|
2022-06-18 13:31:54 +02:00
|
|
|
/* static */ void RMFT2::doSignal(int16_t id,char rag) {
|
2023-10-13 14:59:06 +02:00
|
|
|
if (!(compileFeatures & FEATURE_SIGNAL)) return; // dont compile code below
|
2024-04-09 21:59:57 +02:00
|
|
|
//if (diag) DIAG(F(" doSignal %d %x"),id,rag);
|
2022-06-17 12:48:37 +02:00
|
|
|
|
|
|
|
// Schedule any event handler for this signal change.
|
2024-02-16 12:49:02 +01:00
|
|
|
// This will work even without a signal definition.
|
2023-11-10 21:13:33 +01:00
|
|
|
if (rag==SIGNAL_RED) onRedLookup->handleEvent(F("RED"),id);
|
|
|
|
else if (rag==SIGNAL_GREEN) onGreenLookup->handleEvent(F("GREEN"),id);
|
|
|
|
else onAmberLookup->handleEvent(F("AMBER"),id);
|
2022-06-17 12:48:37 +02:00
|
|
|
|
2024-09-22 13:36:19 +02:00
|
|
|
auto sigslot=signalLookup->find(id);
|
2022-04-17 10:58:32 +02:00
|
|
|
if (sigslot<0) return;
|
|
|
|
|
|
|
|
// keep track of signal state
|
|
|
|
setFlag(sigslot,rag,SIGNAL_MASK);
|
|
|
|
|
|
|
|
// Correct signal definition found, get the rag values
|
2024-09-22 13:36:19 +02:00
|
|
|
auto signal=getSignalSlot(sigslot);
|
|
|
|
|
|
|
|
switch (signal.type) {
|
|
|
|
case sigtypeSERVO:
|
|
|
|
{
|
|
|
|
auto servopos = rag==SIGNAL_RED? signal.redpin: (rag==SIGNAL_GREEN? signal.greenpin : signal.amberpin);
|
2024-04-09 21:59:57 +02:00
|
|
|
//if (diag) DIAG(F("sigA %d %d"),id,servopos);
|
2022-04-17 10:58:32 +02:00
|
|
|
if (servopos!=0) IODevice::writeAnalogue(id,servopos,PCA9685::Bounce);
|
|
|
|
return;
|
2024-09-22 13:36:19 +02:00
|
|
|
}
|
2022-02-06 14:56:51 +01:00
|
|
|
|
2024-09-22 13:36:19 +02:00
|
|
|
case sigtypeDCC:
|
|
|
|
{
|
2022-06-18 13:31:54 +02:00
|
|
|
// redpin,amberpin are the DCC addr,subaddr
|
2024-09-22 13:36:19 +02:00
|
|
|
DCC::setAccessory(signal.redpin,signal.amberpin, rag!=SIGNAL_RED);
|
2022-06-18 13:31:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-22 13:36:19 +02:00
|
|
|
case sigtypeDCCX:
|
|
|
|
{
|
2024-02-16 12:49:02 +01:00
|
|
|
// redpin,amberpin,greenpin are the 3 aspects
|
2024-09-22 13:36:19 +02:00
|
|
|
auto value=signal.redpin;
|
|
|
|
if (rag==SIGNAL_AMBER) value=signal.amberpin;
|
|
|
|
if (rag==SIGNAL_GREEN) value=signal.greenpin;
|
|
|
|
DCC::setExtendedAccessory(id, value);
|
2024-02-16 12:49:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-22 13:36:19 +02:00
|
|
|
case sigtypeNEOPIXEL:
|
|
|
|
{
|
|
|
|
// redpin,amberpin,greenpin are the 3 RG values but with no blue permitted. . (code limitation hack)
|
|
|
|
auto colour_RG=signal.redpin;
|
|
|
|
if (rag==SIGNAL_AMBER) colour_RG=signal.amberpin;
|
|
|
|
if (rag==SIGNAL_GREEN) colour_RG=signal.greenpin;
|
|
|
|
|
|
|
|
// blue channel is in followng signal slot (a continuation)
|
|
|
|
auto signal2=getSignalSlot(sigslot+1);
|
|
|
|
auto colour_B=signal2.redpin;
|
|
|
|
if (rag==SIGNAL_AMBER) colour_B=signal2.amberpin;
|
|
|
|
if (rag==SIGNAL_GREEN) colour_B=signal2.greenpin;
|
|
|
|
IODevice::writeAnalogue(id, colour_RG,true,colour_B);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case sigtypeSIGNAL:
|
|
|
|
case sigtypeSIGNALH:
|
|
|
|
{
|
2022-06-18 13:31:54 +02:00
|
|
|
// LED or similar 3 pin signal, (all pins zero would be a virtual signal)
|
2022-04-17 10:58:32 +02:00
|
|
|
// If amberpin is zero, synthesise amber from red+green
|
|
|
|
const byte SIMAMBER=0x00;
|
2024-09-22 13:36:19 +02:00
|
|
|
if (rag==SIGNAL_AMBER && (signal.amberpin==0)) rag=SIMAMBER; // special case this func only
|
2022-04-17 10:58:32 +02:00
|
|
|
|
|
|
|
// Manage invert (HIGH on) pins
|
2024-09-22 13:36:19 +02:00
|
|
|
bool aHigh=signal.type==sigtypeSIGNALH;
|
2023-01-22 10:25:00 +01:00
|
|
|
|
2022-04-17 10:58:32 +02:00
|
|
|
// set the three pins
|
2024-09-22 13:36:19 +02:00
|
|
|
if (signal.redpin) {
|
2023-01-22 10:25:00 +01:00
|
|
|
bool redval=(rag==SIGNAL_RED || rag==SIMAMBER);
|
|
|
|
if (!aHigh) redval=!redval;
|
2024-09-22 13:36:19 +02:00
|
|
|
killBlinkOnVpin(signal.redpin);
|
|
|
|
IODevice::write(signal.redpin,redval);
|
2023-01-22 10:25:00 +01:00
|
|
|
}
|
2024-09-22 13:36:19 +02:00
|
|
|
if (signal.amberpin) {
|
2023-01-22 10:25:00 +01:00
|
|
|
bool amberval=(rag==SIGNAL_AMBER);
|
|
|
|
if (!aHigh) amberval=!amberval;
|
2024-09-22 13:36:19 +02:00
|
|
|
killBlinkOnVpin(signal.amberpin);
|
|
|
|
IODevice::write(signal.amberpin,amberval);
|
2023-01-22 10:25:00 +01:00
|
|
|
}
|
2024-09-22 13:36:19 +02:00
|
|
|
if (signal.greenpin) {
|
2023-01-22 10:25:00 +01:00
|
|
|
bool greenval=(rag==SIGNAL_GREEN || rag==SIMAMBER);
|
|
|
|
if (!aHigh) greenval=!greenval;
|
2024-09-22 13:36:19 +02:00
|
|
|
killBlinkOnVpin(signal.greenpin);
|
|
|
|
IODevice::write(signal.greenpin,greenval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case sigtypeVIRTUAL: break;
|
|
|
|
case sigtypeContinuation: break;
|
|
|
|
case sigtypeNoMoreSignals: break;
|
2023-01-22 10:25:00 +01:00
|
|
|
}
|
2022-04-17 10:58:32 +02:00
|
|
|
}
|
2022-02-06 14:56:51 +01:00
|
|
|
|
2022-06-18 13:31:54 +02:00
|
|
|
/* static */ bool RMFT2::isSignal(int16_t id,char rag) {
|
2023-10-13 14:59:06 +02:00
|
|
|
if (!(compileFeatures & FEATURE_SIGNAL)) return false;
|
2024-09-22 13:36:19 +02:00
|
|
|
int16_t sigslot=signalLookup->find(id);
|
2022-04-17 10:58:32 +02:00
|
|
|
if (sigslot<0) return false;
|
|
|
|
return (flags[sigslot] & SIGNAL_MASK) == rag;
|
2022-01-03 13:43:06 +01:00
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2024-02-16 13:20:58 +01:00
|
|
|
|
|
|
|
// signalAspectEvent returns true if the aspect is destined
|
|
|
|
// for a defined DCCX_SIGNAL which will handle all the RAG flags
|
|
|
|
// and ON* handlers.
|
|
|
|
// Otherwise false so the parser should send the command directly
|
|
|
|
bool RMFT2::signalAspectEvent(int16_t address, byte aspect ) {
|
|
|
|
if (!(compileFeatures & FEATURE_SIGNAL)) return false;
|
2024-09-22 13:36:19 +02:00
|
|
|
auto sigslot=signalLookup->find(address);
|
2024-02-16 13:20:58 +01:00
|
|
|
if (sigslot<0) return false; // this is not a defined signal
|
2024-09-22 13:36:19 +02:00
|
|
|
auto signal=getSignalSlot(sigslot);
|
|
|
|
if (signal.type!=sigtypeDCCX) return false; // not a DCCX signal
|
2024-02-16 13:20:58 +01:00
|
|
|
// Turn an aspect change into a RED/AMBER/GREEN setting
|
2024-09-22 13:36:19 +02:00
|
|
|
if (aspect==signal.redpin) {
|
|
|
|
doSignal(address,SIGNAL_RED);
|
2024-02-16 13:20:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-09-22 13:36:19 +02:00
|
|
|
if (aspect==signal.amberpin) {
|
|
|
|
doSignal(address,SIGNAL_AMBER);
|
2024-02-16 13:20:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-09-22 13:36:19 +02:00
|
|
|
if (aspect==signal.greenpin) {
|
|
|
|
doSignal(address,SIGNAL_GREEN);
|
2024-02-16 13:20:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; // aspect is not a defined one
|
|
|
|
}
|
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
void RMFT2::turnoutEvent(int16_t turnoutId, bool closed) {
|
|
|
|
// Hunt for an ONTHROW/ONCLOSE for this turnout
|
2023-11-10 21:13:33 +01:00
|
|
|
if (closed) onCloseLookup->handleEvent(F("CLOSE"),turnoutId);
|
|
|
|
else onThrowLookup->handleEvent(F("THROW"),turnoutId);
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2022-01-04 21:09:56 +01:00
|
|
|
|
2022-06-17 12:48:37 +02:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
void RMFT2::activateEvent(int16_t addr, bool activate) {
|
|
|
|
// Hunt for an ONACTIVATE/ONDEACTIVATE for this accessory
|
2023-11-10 21:13:33 +01:00
|
|
|
if (activate) onActivateLookup->handleEvent(F("ACTIVATE"),addr);
|
|
|
|
else onDeactivateLookup->handleEvent(F("DEACTIVATE"),addr);
|
2022-06-17 12:48:37 +02:00
|
|
|
}
|
2022-12-30 00:46:42 +01:00
|
|
|
|
2024-09-28 13:29:12 +02:00
|
|
|
void RMFT2::blockEvent(int16_t block, int16_t loco, bool entering) {
|
|
|
|
if (compileFeatures & FEATURE_BLOCK) {
|
|
|
|
// Hunt for an ONBLOCKENTER/ONBLOCKEXIT for this accessory
|
|
|
|
if (entering) onBlockEnterLookup->handleEvent(F("BLOCKENTER"),block,loco);
|
|
|
|
else onBlockExitLookup->handleEvent(F("BLOCKEXIT"),block,loco);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 00:46:42 +01:00
|
|
|
void RMFT2::changeEvent(int16_t vpin, bool change) {
|
|
|
|
// Hunt for an ONCHANGE for this sensor
|
2023-11-10 21:13:33 +01:00
|
|
|
if (change) onChangeLookup->handleEvent(F("CHANGE"),vpin);
|
2022-12-30 00:46:42 +01:00
|
|
|
}
|
2023-01-11 18:36:11 +01:00
|
|
|
|
2023-09-06 23:58:19 +02:00
|
|
|
#ifndef IO_NO_HAL
|
2023-09-02 00:29:49 +02:00
|
|
|
void RMFT2::rotateEvent(int16_t turntableId, bool change) {
|
|
|
|
// Hunt or an ONROTATE for this turntable
|
2023-11-10 21:13:33 +01:00
|
|
|
if (change) onRotateLookup->handleEvent(F("ROTATE"),turntableId);
|
2023-09-02 00:29:49 +02:00
|
|
|
}
|
2023-09-06 23:58:19 +02:00
|
|
|
#endif
|
2023-09-02 00:29:49 +02:00
|
|
|
|
2023-01-11 18:36:11 +01:00
|
|
|
void RMFT2::clockEvent(int16_t clocktime, bool change) {
|
|
|
|
// Hunt for an ONTIME for this time
|
|
|
|
if (Diag::CMD)
|
2024-04-09 21:59:57 +02:00
|
|
|
DIAG(F("clockEvent at : %d"), clocktime);
|
2023-04-12 13:07:08 +02:00
|
|
|
if (change) {
|
2023-11-10 21:13:33 +01:00
|
|
|
onClockLookup->handleEvent(F("CLOCK"),clocktime);
|
|
|
|
onClockLookup->handleEvent(F("CLOCK"),25*60+clocktime%60);
|
2023-04-12 13:07:08 +02:00
|
|
|
}
|
2023-01-11 18:36:11 +01:00
|
|
|
}
|
|
|
|
|
2023-08-12 20:10:35 +02:00
|
|
|
void RMFT2::powerEvent(int16_t track, bool overload) {
|
2023-08-12 19:40:48 +02:00
|
|
|
// Hunt for an ONOVERLOAD for this item
|
|
|
|
if (Diag::CMD)
|
2024-12-19 13:06:03 +01:00
|
|
|
DIAG(F("powerEvent : %c"), track + 'A');
|
2023-08-12 19:40:48 +02:00
|
|
|
if (overload) {
|
2023-11-10 21:13:33 +01:00
|
|
|
onOverloadLookup->handleEvent(F("POWER"),track);
|
2023-08-12 19:40:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 21:45:28 +02:00
|
|
|
// This function is used when setting pins so that a SET or RESET
|
|
|
|
// will cause any blink task on that pin to terminate.
|
|
|
|
// It will be compiled out of existence if no BLINK feature is used.
|
2024-09-22 13:36:19 +02:00
|
|
|
void RMFT2::killBlinkOnVpin(VPIN pin, uint16_t count) {
|
2024-04-09 21:45:28 +02:00
|
|
|
if (!(compileFeatures & FEATURE_BLINK)) return;
|
|
|
|
|
2024-09-22 13:36:19 +02:00
|
|
|
RMFT2 * stoptask=loopTask; // stop when we get back to here
|
2024-04-09 21:45:28 +02:00
|
|
|
RMFT2 * task=loopTask;
|
2024-09-22 13:36:19 +02:00
|
|
|
VPIN lastPin=pin+count-1;
|
2024-04-09 21:45:28 +02:00
|
|
|
while(task) {
|
2024-09-22 13:36:19 +02:00
|
|
|
auto nextTask=task->next;
|
2024-04-09 21:45:28 +02:00
|
|
|
if (
|
|
|
|
(task->blinkState==blink_high || task->blinkState==blink_low)
|
2024-09-22 13:36:19 +02:00
|
|
|
&& task->blinkPin>=pin
|
|
|
|
&& task->blinkPin<=lastPin
|
|
|
|
) {
|
|
|
|
if (diag) DIAG(F("kill blink %d"),task->blinkPin,lastPin);
|
2024-04-09 21:45:28 +02:00
|
|
|
task->kill();
|
2024-09-22 13:36:19 +02:00
|
|
|
}
|
|
|
|
task=nextTask;
|
|
|
|
if (task==stoptask) return;
|
2024-04-09 21:45:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 12:58:26 +02:00
|
|
|
void RMFT2::startNonRecursiveTask(const FSH* reason, int16_t id,int pc, uint16_t loco) {
|
2022-06-17 12:48:37 +02:00
|
|
|
// Check we dont already have a task running this handler
|
2022-01-06 23:03:57 +01:00
|
|
|
RMFT2 * task=loopTask;
|
|
|
|
while(task) {
|
2024-10-23 12:58:26 +02:00
|
|
|
if (task->onEventStartPosition==pc && task->loco==loco) {
|
2022-06-17 12:48:37 +02:00
|
|
|
DIAG(F("Recursive ON%S(%d)"),reason, id);
|
2022-01-06 23:03:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
task=task->next;
|
|
|
|
if (task==loopTask) break;
|
|
|
|
}
|
|
|
|
|
2024-09-28 13:29:12 +02:00
|
|
|
task=new RMFT2(pc,loco); // new task starts at this instruction
|
2022-06-17 12:48:37 +02:00
|
|
|
task->onEventStartPosition=pc; // flag for recursion detector
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2021-11-25 12:36:05 +01:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
void RMFT2::printMessage2(const FSH * msg) {
|
|
|
|
DIAG(F("EXRAIL(%d) %S"),loco,msg);
|
|
|
|
}
|
2022-12-16 14:14:48 +01:00
|
|
|
static StringBuffer * buffer=NULL;
|
|
|
|
/* thrungeString is used to stream a HIGHFLASH string to a suitable Serial
|
|
|
|
and handle the oddities like LCD, BROADCAST and PARSE */
|
|
|
|
void RMFT2::thrungeString(uint32_t strfar, thrunger mode, byte id) {
|
|
|
|
//DIAG(F("thrunge addr=%l mode=%d id=%d"), strfar,mode,id);
|
|
|
|
Print * stream=NULL;
|
|
|
|
// Find out where the string is going
|
|
|
|
switch (mode) {
|
|
|
|
case thrunge_print:
|
2023-02-15 01:51:21 +01:00
|
|
|
StringFormatter::send(&USB_SERIAL,F("<* EXRAIL(%d) "),loco);
|
|
|
|
stream=&USB_SERIAL;
|
2022-12-16 14:14:48 +01:00
|
|
|
break;
|
|
|
|
|
2023-02-15 01:51:21 +01:00
|
|
|
case thrunge_serial: stream=&USB_SERIAL; break;
|
2022-12-16 14:14:48 +01:00
|
|
|
case thrunge_serial1:
|
|
|
|
#ifdef SERIAL1_COMMANDS
|
|
|
|
stream=&Serial1;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case thrunge_serial2:
|
|
|
|
#ifdef SERIAL2_COMMANDS
|
|
|
|
stream=&Serial2;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case thrunge_serial3:
|
|
|
|
#ifdef SERIAL3_COMMANDS
|
|
|
|
stream=&Serial3;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case thrunge_serial4:
|
|
|
|
#ifdef SERIAL4_COMMANDS
|
|
|
|
stream=&Serial4;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case thrunge_serial5:
|
|
|
|
#ifdef SERIAL5_COMMANDS
|
|
|
|
stream=&Serial5;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case thrunge_serial6:
|
|
|
|
#ifdef SERIAL6_COMMANDS
|
|
|
|
stream=&Serial6;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case thrunge_lcn:
|
|
|
|
#if defined(LCN_SERIAL)
|
|
|
|
stream=&LCN_SERIAL;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case thrunge_parse:
|
|
|
|
case thrunge_broadcast:
|
2024-03-08 21:33:11 +01:00
|
|
|
case thrunge_message:
|
2022-12-16 14:14:48 +01:00
|
|
|
case thrunge_lcd:
|
2023-02-11 16:47:50 +01:00
|
|
|
default: // thrunge_lcd+1, ...
|
2022-12-16 14:14:48 +01:00
|
|
|
if (!buffer) buffer=new StringBuffer();
|
|
|
|
buffer->flush();
|
|
|
|
stream=buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!stream) return;
|
|
|
|
|
|
|
|
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
|
|
|
|
// if mega stream it out
|
|
|
|
for (;;strfar++) {
|
|
|
|
char c=pgm_read_byte_far(strfar);
|
|
|
|
if (c=='\0') break;
|
|
|
|
stream->write(c);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// UNO/NANO CPUs dont have high memory
|
|
|
|
// 32 bit cpus dont care anyway
|
|
|
|
stream->print((FSH *)strfar);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// and decide what to do next
|
|
|
|
switch (mode) {
|
|
|
|
case thrunge_print:
|
2023-02-15 01:51:21 +01:00
|
|
|
StringFormatter::send(&USB_SERIAL,F(" *>\n"));
|
2022-12-16 14:14:48 +01:00
|
|
|
break;
|
|
|
|
// TODO more serials for SAMx case thrunge_serial4: stream=&Serial4; break;
|
|
|
|
case thrunge_parse:
|
2023-02-15 01:51:21 +01:00
|
|
|
DCCEXParser::parseOne(&USB_SERIAL,(byte*)buffer->getString(),NULL);
|
2022-12-16 14:14:48 +01:00
|
|
|
break;
|
|
|
|
case thrunge_broadcast:
|
2023-05-01 15:25:45 +02:00
|
|
|
CommandDistributor::broadcastRaw(CommandDistributor::COMMAND_TYPE,buffer->getString());
|
|
|
|
break;
|
|
|
|
case thrunge_withrottle:
|
|
|
|
CommandDistributor::broadcastRaw(CommandDistributor::WITHROTTLE_TYPE,buffer->getString());
|
2022-12-16 14:14:48 +01:00
|
|
|
break;
|
2024-03-08 21:33:11 +01:00
|
|
|
case thrunge_message:
|
|
|
|
CommandDistributor::broadcastMessage(buffer->getString());
|
|
|
|
break;
|
2022-12-16 14:14:48 +01:00
|
|
|
case thrunge_lcd:
|
|
|
|
LCD(id,F("%s"),buffer->getString());
|
|
|
|
break;
|
2023-02-11 16:47:50 +01:00
|
|
|
default: // thrunge_lcd+1, ...
|
|
|
|
if (mode > thrunge_lcd)
|
2023-02-12 00:37:09 +01:00
|
|
|
SCREEN(mode-thrunge_lcd, id, F("%s"),buffer->getString()); // print to other display
|
2023-02-11 16:47:50 +01:00
|
|
|
break;
|
2022-12-16 14:14:48 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-09 20:27:52 +01:00
|
|
|
|
2023-11-09 21:25:10 +01:00
|
|
|
void RMFT2::manageRouteState(uint16_t id, byte state) {
|
2023-11-10 20:25:24 +01:00
|
|
|
if (compileFeatures && FEATURE_ROUTESTATE) {
|
|
|
|
// Route state must be maintained for when new throttles connect.
|
|
|
|
// locate route id in the Routes lookup
|
|
|
|
int16_t position=routeLookup->findPosition(id);
|
|
|
|
if (position<0) return;
|
|
|
|
// set state beside it
|
|
|
|
if (routeStateArray[position]==state) return;
|
|
|
|
routeStateArray[position]=state;
|
|
|
|
CommandDistributor::broadcastRouteState(id,state);
|
|
|
|
}
|
2023-11-09 21:25:10 +01:00
|
|
|
}
|
|
|
|
void RMFT2::manageRouteCaption(uint16_t id,const FSH* caption) {
|
2023-11-10 20:25:24 +01:00
|
|
|
if (compileFeatures && FEATURE_ROUTESTATE) {
|
|
|
|
// Route state must be maintained for when new throttles connect.
|
|
|
|
// locate route id in the Routes lookup
|
|
|
|
int16_t position=routeLookup->findPosition(id);
|
|
|
|
if (position<0) return;
|
|
|
|
// set state beside it
|
|
|
|
if (routeCaptionArray[position]==caption) return;
|
|
|
|
routeCaptionArray[position]=caption;
|
|
|
|
CommandDistributor::broadcastRouteCaption(id,caption);
|
|
|
|
}
|
2023-11-09 20:27:52 +01:00
|
|
|
}
|
2023-11-09 21:25:10 +01:00
|
|
|
|