From e36e867ec21cc605fc5acd9343142307847d1247 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Wed, 17 Aug 2022 02:11:51 +0200 Subject: [PATCH] ESP32: implement JOIN --- DCCACK.cpp | 22 ++++++++++++---------- DCCWaveform.cpp | 8 ++++---- GITHUB_SHA.h | 2 +- TrackManager.cpp | 23 +++++++++++++++++++++-- TrackManager.h | 3 +++ 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/DCCACK.cpp b/DCCACK.cpp index d159383..fa3d348 100644 --- a/DCCACK.cpp +++ b/DCCACK.cpp @@ -67,23 +67,25 @@ CALLBACK_STATE DCCACK::callbackState=READY; ACK_CALLBACK DCCACK::ackManagerCallback; void DCCACK::Setup(int cv, byte byteValueOrBitnum, ackOp const program[], ACK_CALLBACK callback) { + ackManagerRejoin=TrackManager::isJoined(); + if (ackManagerRejoin) { + // Change from JOIN must zero resets packet. + TrackManager::setJoin(false); + DCCWaveform::progTrack.clearResets(); + } + progDriver=TrackManager::getProgDriver(); if (progDriver==NULL) { - callback(-3); // we dont have a prog track! - return; - } + TrackManager::setJoin(ackManagerRejoin); + callback(-3); // we dont have a prog track! + return; + } if (!progDriver->canMeasureCurrent()) { + TrackManager::setJoin(ackManagerRejoin); callback(-2); // our prog track cant measure current return; } - ackManagerRejoin=TrackManager::isJoined(); - if (ackManagerRejoin ) { - // Change from JOIN must zero resets packet. - TrackManager::setJoin(false); - DCCWaveform::progTrack.clearResets(); - } - autoPowerOff=false; if (progDriver->getPower() == POWERMODE::OFF) { autoPowerOff=true; // power off afterwards diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 2883579..d7dc26a 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -213,10 +213,10 @@ void DCCWaveform::begin() { for(const auto& md: TrackManager::getMainDrivers()) { pinpair p = md->getSignalPin(); if(rmtMainChannel) { - // DIAG(F("added pins %d %d to MAIN channel"), p.pin, p.invpin); + //DIAG(F("added pins %d %d to MAIN channel"), p.pin, p.invpin); rmtMainChannel->addPin(p); // add pin to existing main channel } else { - // DIAG(F("new MAIN channel with pins %d %d"), p.pin, p.invpin); + //DIAG(F("new MAIN channel with pins %d %d"), p.pin, p.invpin); rmtMainChannel = new RMTChannel(p, true); /* create new main channel */ } } @@ -224,10 +224,10 @@ void DCCWaveform::begin() { if (md) { pinpair p = md->getSignalPin(); if (rmtProgChannel) { - // DIAG(F("added pins %d %d to PROG channel"), p.pin, p.invpin); + //DIAG(F("added pins %d %d to PROG channel"), p.pin, p.invpin); rmtProgChannel->addPin(p); // add pin to existing prog channel } else { - // DIAG(F("new PROGchannel with pins %d %d"), p.pin, p.invpin); + //DIAG(F("new PROGchannel with pins %d %d"), p.pin, p.invpin); rmtProgChannel = new RMTChannel(p, false); } } diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index 17197b8..78b65c0 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "PORTX-HAL-20220817" +#define GITHUB_SHA "PORTX-HAL-20220817-1" diff --git a/TrackManager.cpp b/TrackManager.cpp index 23a833d..dd9ad64 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -50,6 +50,9 @@ byte TrackManager::lastTrack=0; bool TrackManager::progTrackSyncMain=false; bool TrackManager::progTrackBoosted=false; int16_t TrackManager::joinRelay=UNUSED_PIN; +#ifdef ARDUINO_ARCH_ESP32 +byte TrackManager::tempProgTrack=MAX_TRACKS+1; +#endif // The setup call is done this way so that the tracks can be in a list @@ -141,11 +144,11 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr #ifdef ARDUINO_ARCH_ESP32 // remove pin from MUX matrix and turn it off pinpair p = track[trackToSet]->getSignalPin(); - // DIAG(F("Track=%c remove pin %d"),trackToSet+'A', p.pin); + //DIAG(F("Track=%c remove pin %d"),trackToSet+'A', p.pin); gpio_reset_pin((gpio_num_t)p.pin); pinMode(p.pin, OUTPUT); // gpio_reset_pin may reset to input if (p.invpin != UNUSED_PIN) { - DIAG(F("Track=%c remove ^pin %d"),trackToSet+'A', p.invpin); + //DIAG(F("Track=%c remove ^pin %d"),trackToSet+'A', p.invpin); gpio_reset_pin((gpio_num_t)p.invpin); pinMode(p.invpin, OUTPUT); // gpio_reset_pin may reset to input } @@ -384,6 +387,22 @@ void TrackManager::setJoinRelayPin(byte joinRelayPin) { } void TrackManager::setJoin(bool joined) { +#ifdef ARDUINO_ARCH_ESP32 + if (joined) { + FOR_EACH_TRACK(t) { + if (trackMode[t]==TRACK_MODE_PROG) { + tempProgTrack = t; + setTrackMode(t, TRACK_MODE_MAIN); + break; + } + } + } else { + if (tempProgTrack != MAX_TRACKS+1) { + setTrackMode(tempProgTrack, TRACK_MODE_PROG); + tempProgTrack = MAX_TRACKS+1; + } + } +#endif progTrackSyncMain=joined; if (joinRelay!=UNUSED_PIN) digitalWrite(joinRelay,joined?HIGH:LOW); } diff --git a/TrackManager.h b/TrackManager.h index b210221..45325f9 100644 --- a/TrackManager.h +++ b/TrackManager.h @@ -90,6 +90,9 @@ class TrackManager { static MotorDriver* track[MAX_TRACKS]; static TRACK_MODE trackMode[MAX_TRACKS]; static int16_t trackDCAddr[MAX_TRACKS]; // dc address if TRACK_MODE_DC or TRACK_MODE_DCX +#ifdef ARDUINO_ARCH_ESP32 + static byte tempProgTrack; // holds the prog track number during join +#endif }; #endif