From 343a5262418f97c3f3d4a0d7c7effdaa6f9f1a3e Mon Sep 17 00:00:00 2001 From: Asbelos Date: Tue, 6 Aug 2024 14:14:11 +0100 Subject: [PATCH] onblock... --- EXRAIL2.cpp | 81 ++++++++++++++++++++++----------------------- EXRAIL2.h | 9 +++-- EXRAIL2MacroReset.h | 4 +++ EXRAIL2Parser.cpp | 35 ++++++++++++++------ EXRAILMacros.h | 6 ++++ 5 files changed, 77 insertions(+), 58 deletions(-) diff --git a/EXRAIL2.cpp b/EXRAIL2.cpp index 5395286..7c885db 100644 --- a/EXRAIL2.cpp +++ b/EXRAIL2.cpp @@ -204,8 +204,12 @@ LookList* RMFT2::LookListLoader(OPCODE op1, OPCODE op2, OPCODE op3) { onRotateLookup=LookListLoader(OPCODE_ONROTATE); #endif onOverloadLookup=LookListLoader(OPCODE_ONOVERLOAD); - onBlockEnterLookup=LookListLoader(OPCODE_ONBLOCKENTER); - onBlockExitLookup=LookListLoader(OPCODE_ONBLOCKEXIT); + + if (compileFeatures & FEATURE_BLOCK) { + onBlockEnterLookup=LookListLoader(OPCODE_ONBLOCKENTER); + onBlockExitLookup=LookListLoader(OPCODE_ONBLOCKEXIT); + } + // onLCCLookup is not the same so not loaded here. // Second pass startup, define any turnouts or servos, set signals red @@ -370,7 +374,7 @@ char RMFT2::getRouteType(int16_t id) { } -RMFT2::RMFT2(int progCtr) { +RMFT2::RMFT2(int progCtr, int16_t _loco) { progCounter=progCtr; // get an unused task id from the flags table @@ -383,9 +387,7 @@ RMFT2::RMFT2(int progCtr) { } } delayTime=0; - loco=0; - speedo=0; - forward=true; + loco=_loco; invert=false; blinkState=not_blink_task; stackDepth=0; @@ -403,7 +405,10 @@ RMFT2::RMFT2(int progCtr) { RMFT2::~RMFT2() { - driveLoco(1); // ESTOP my loco if any + // 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)); setFlag(taskId,0,TASK_FLAG); // we are no longer using this id if (next==this) loopTask=NULL; @@ -419,23 +424,9 @@ RMFT2::~RMFT2() { void RMFT2::createNewTask(int route, uint16_t cab) { int pc=routeLookup->find(route); if (pc<0) return; - RMFT2* task=new RMFT2(pc); - task->loco=cab; + new RMFT2(pc,cab); } -void RMFT2::driveLoco(byte speed) { - if (loco<=0) return; // Prevent broadcast! - //if (diag) DIAG(F("EXRAIL drive %d %d %d"),loco,speed,forward^invert); - /* TODO..... - power on appropriate track if DC or main if dcc - if (TrackManager::getMainPowerMode()==POWERMODE::OFF) { - TrackManager::setMainPower(POWERMODE::ON); - } - **********/ - - DCC::setThrottle(loco,speed, forward^invert); - speedo=speed; -} bool RMFT2::readSensor(uint16_t sensorId) { // Exrail operands are unsigned but we need the signed version as inserted by the macros. @@ -493,6 +484,15 @@ bool RMFT2::skipIfBlock() { } } +void RMFT2::pause() { + if (loco) + pauseSpeed=DCC::getThrottleSpeedByte(loco); +} +void RMFT2::resume() { + if (loco) + DCC::setThrottle(loco,pauseSpeed & 0x7f, pauseSpeed & 0x80); +} + void RMFT2::loop() { if (compileFeatures & FEATURE_SENSOR) EXRAILSensor::checkAll(); @@ -557,18 +557,15 @@ void RMFT2::loop2() { #endif case OPCODE_REV: - forward = false; - driveLoco(operand); + if (loco) DCC::setThrottle(loco,operand,invert); break; case OPCODE_FWD: - forward = true; - driveLoco(operand); - break; + if (loco) DCC::setThrottle(loco,operand,!invert); + break; case OPCODE_SPEED: - forward=DCC::getThrottleDirection(loco)^invert; - driveLoco(operand); + if (loco) DCC::setThrottle(loco,operand,DCC::getThrottleDirection(loco)); break; case OPCODE_MOMENTUM: @@ -584,12 +581,11 @@ void RMFT2::loop2() { case OPCODE_INVERT_DIRECTION: invert= !invert; - driveLoco(speedo); break; case OPCODE_RESERVE: if (getFlag(operand,SECTION_FLAG)) { - driveLoco(0); + if (loco) DCC::setThrottle(loco,0,DCC::getThrottleDirection(loco)); delayMe(500); return; } @@ -687,6 +683,10 @@ void RMFT2::loop2() { break; case OPCODE_PAUSE: + // all tasks save their speed bytes + pause(); + for (RMFT2 * t=next; t!=this;t=t->next) t->pause(); + DCC::estopAll(); // pause all locos on the track pausingTask=this; break; @@ -731,8 +731,8 @@ void RMFT2::loop2() { case OPCODE_RESUME: pausingTask=NULL; - driveLoco(speedo); - for (RMFT2 * t=next; t!=this;t=t->next) if (t->loco >0) t->driveLoco(t->speedo); + resume(); + for (RMFT2 * t=next; t!=this;t=t->next) t->resume(); break; case OPCODE_IF: // do next operand if sensor set @@ -843,8 +843,7 @@ void RMFT2::loop2() { case OPCODE_DRIVE: { - byte analogSpeed=IODevice::readAnalogue(operand) *127 / 1024; - if (speedo!=analogSpeed) driveLoco(analogSpeed); + // Non functional but reserved break; } @@ -935,8 +934,6 @@ void RMFT2::loop2() { } loco=progtrackLocoId; - speedo=0; - forward=true; invert=false; break; #endif @@ -958,16 +955,13 @@ void RMFT2::loop2() { { int newPc=routeLookup->find(getOperand(1)); if (newPc<0) break; - RMFT2* newtask=new RMFT2(newPc); // create new task - newtask->loco=operand; + new RMFT2(newPc,operand); // create new task } break; case OPCODE_SETLOCO: { loco=operand; - speedo=0; - forward=true; invert=false; } break; @@ -1091,7 +1085,8 @@ void RMFT2::loop2() { case OPCODE_ONROTATE: #endif case OPCODE_ONOVERLOAD: - + case OPCODE_ONBLOCKENTER: + case OPCODE_ONBLOCKEXIT: break; default: @@ -1286,9 +1281,11 @@ void RMFT2::activateEvent(int16_t addr, bool activate) { } 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); + } } void RMFT2::changeEvent(int16_t vpin, bool change) { diff --git a/EXRAIL2.h b/EXRAIL2.h index 02458f7..cb8d73b 100644 --- a/EXRAIL2.h +++ b/EXRAIL2.h @@ -161,8 +161,7 @@ class LookList { public: static void begin(); static void loop(); - RMFT2(int progCounter); - RMFT2(int route, uint16_t cab); + RMFT2(int progCounter, int16_t cab=0); ~RMFT2(); static void readLocoCallback(int16_t cv); static void createNewTask(int route, uint16_t cab); @@ -215,7 +214,6 @@ private: static RMFT2 * loopTask; static RMFT2 * pausingTask; void delayMe(long millisecs); - void driveLoco(byte speedo); bool skipIfBlock(); bool readLoco(); void loop2(); @@ -224,6 +222,8 @@ private: void printMessage2(const FSH * msg); void thrungeString(uint32_t strfar, thrunger mode, byte id=0); uint16_t getOperand(byte n); + void pause(); + void resume(); static bool diag; static const HIGHFLASH3 byte RouteCode[]; @@ -271,9 +271,8 @@ private: byte taskId; BlinkState blinkState; // includes AT_TIMEOUT flag. uint16_t loco; - bool forward; bool invert; - byte speedo; + byte pauseSpeed; int onEventStartPosition; byte stackDepth; int callStack[MAX_STACK_DEPTH]; diff --git a/EXRAIL2MacroReset.h b/EXRAIL2MacroReset.h index 8ed7111..cff2a50 100644 --- a/EXRAIL2MacroReset.h +++ b/EXRAIL2MacroReset.h @@ -108,6 +108,8 @@ #undef ONACTIVATE #undef ONACTIVATEL #undef ONAMBER +#undef ONBLOCKENTER +#undef ONBLOCKEXIT #undef ONDEACTIVATE #undef ONDEACTIVATEL #undef ONCLOSE @@ -278,6 +280,8 @@ #define ONACTIVATE(addr,subaddr) #define ONACTIVATEL(linear) #define ONAMBER(signal_id) +#define ONBLOCKENTER(blockid) +#define ONBLOCKEXIT(blockid) #define ONTIME(value) #define ONCLOCKTIME(hours,mins) #define ONCLOCKMINS(mins) diff --git a/EXRAIL2Parser.cpp b/EXRAIL2Parser.cpp index 780c125..4e6c5ac 100644 --- a/EXRAIL2Parser.cpp +++ b/EXRAIL2Parser.cpp @@ -210,6 +210,14 @@ void RMFT2::ComandFilter(Print * stream, byte & opcode, byte & paramCount, int16 default: break; } + + case 'K': // Block enter + case 'k': // Block exit + if (paramCount!=2) break; + blockEvent(p[0],p[1],opcode=='K'); + opcode=0; + break; + default: // other commands pass through break; } @@ -228,11 +236,9 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) { ); } else { - StringFormatter::send(stream,F("\nID=%d,PC=%d,LOCO=%d%c,SPEED=%d%c"), + StringFormatter::send(stream,F("\nID=%d,PC=%d,LOCO=%d %c"), (int)(task->taskId),task->progCounter,task->loco, - task->invert?'I':' ', - task->speedo, - task->forward?'F':'R' + task->invert?'I':' ' ); } task=task->next; @@ -276,19 +282,27 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) { switch (p[0]) { case "PAUSE"_hk: // if (paramCount!=1) return false; - DCC::estopAll(); // pause all locos on the track + { // pause all tasks + RMFT2 * task=loopTask; + while(task) { + task->pause(); + task=task->next; + if (task==loopTask) break; + } + } + DCC::estopAll(); // stop all locos on the track pausingTask=(RMFT2 *)1; // Impossible task address return true; case "RESUME"_hk: // if (paramCount!=1) return false; pausingTask=NULL; - { + { // resume all tasks RMFT2 * task=loopTask; while(task) { - if (task->loco) task->driveLoco(task->speedo); - task=task->next; - if (task==loopTask) break; + task->resume(); + task=task->next; + if (task==loopTask) break; } } return true; @@ -301,8 +315,7 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) { uint16_t cab=(paramCount==2)? 0 : p[1]; int pc=routeLookup->find(route); if (pc<0) return false; - RMFT2* task=new RMFT2(pc); - task->loco=cab; + new RMFT2(pc,cab); } return true; diff --git a/EXRAILMacros.h b/EXRAILMacros.h index f7041aa..ca13b28 100644 --- a/EXRAILMacros.h +++ b/EXRAILMacros.h @@ -222,6 +222,10 @@ bool exrailHalSetup() { #define ONBUTTON(vpin) | FEATURE_SENSOR #undef ONSENSOR #define ONSENSOR(vpin) | FEATURE_SENSOR +#undef ONBLOCKENTER +#define ONBLOCKENTER(blockid) | FEATURE_BLOCK +#undef ONBLOCKEXIT +#define ONBLOCKEXIT(blockid) | FEATURE_BLOCK const byte RMFT2::compileFeatures = 0 #include "myAutomation.h" @@ -556,6 +560,8 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup]; #define ONACTIVATE(addr,subaddr) OPCODE_ONACTIVATE,V(addr<<2|subaddr), #define ONACTIVATEL(linear) OPCODE_ONACTIVATE,V(linear+3), #define ONAMBER(signal_id) OPCODE_ONAMBER,V(signal_id), +#define ONBLOCKENTER(block_id) OPCODE_ONBLOCKENTER,V(block_id), +#define ONBLOCKEXIT(block_id) OPCODE_ONBLOCKEXIT,V(block_id), #define ONCLOSE(turnout_id) OPCODE_ONCLOSE,V(turnout_id), #define ONLCC(sender,event) OPCODE_ONLCC,V(event),\ OPCODE_PAD,V((((uint64_t)sender)>>32)&0xFFFF),\