diff --git a/CommandDistributor.cpp b/CommandDistributor.cpp index 2068dc6..1dbcc6e 100644 --- a/CommandDistributor.cpp +++ b/CommandDistributor.cpp @@ -47,7 +47,6 @@ #ifdef CD_HANDLE_RING // wifi or ethernet ring streams with multiple client types RingStream * CommandDistributor::ring=0; -// byte CommandDistributor::ringClient=NO_CLIENT; CommandDistributor::clientType CommandDistributor::clients[8]={ NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE}; @@ -57,7 +56,6 @@ void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream if (Diag::WIFI && Diag::CMD) DIAG(F("Parse C=%d T=%d B=%s"),clientId, clients[clientId], buffer); ring=stream; - // ringClient=stream->peekTargetMark(); // First check if the client is not known // yet and in that case determinine type @@ -78,7 +76,6 @@ void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream else if (clients[clientId] == WITHROTTLE_TYPE) WiThrottle::getThrottle(clientId)->parse(ring, buffer); - // ringClient=NO_CLIENT; } void CommandDistributor::forget(byte clientId) { @@ -100,24 +97,24 @@ void CommandDistributor::broadcastToClients(clientType type) { // before merging broadcasts in the ring, then reinstate it in case // the process continues to output to its client. if (ring) { - if ((rememberClient = ring->peekTargetMark()) != NO_CLIENT) { - DIAG(F("CD precommit client %d"), rememberClient); - ring->commit(); - } - /* loop through ring clients */ - for (byte clientId=0; clientIdmark(clientId); - ring->print(broadcastBufferWriter->getString()); - DIAG(F("CD commit client %d"), clientId); + if ((rememberClient = ring->peekTargetMark()) != RingStream::NO_CLIENT) { + //DIAG(F("CD precommit client %d"), rememberClient); ring->commit(); } - } - if (ring->peekTargetMark()!=NO_CLIENT) { - DIAG(F("CD postmark client %d"), rememberClient); - ring->mark(rememberClient); - } + /* loop through ring clients */ + for (byte clientId=0; clientIdmark(clientId); + ring->print(broadcastBufferWriter->getString()); + //DIAG(F("CD commit client %d"), clientId); + ring->commit(); + } + } + if (ring->peekTargetMark()!=RingStream::NO_CLIENT) { + //DIAG(F("CD postmark client %d"), rememberClient); + ring->mark(rememberClient); + } } #endif } diff --git a/CommandDistributor.h b/CommandDistributor.h index c5b6ad7..3c3ec7b 100644 --- a/CommandDistributor.h +++ b/CommandDistributor.h @@ -47,8 +47,6 @@ private: static StringBuffer * broadcastBufferWriter; #ifdef CD_HANDLE_RING static RingStream * ring; - static const byte NO_CLIENT=255; //XXX remove later - // static byte ringClient; static clientType clients[8]; #endif }; diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index 0cbf056..7dca071 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -143,6 +143,10 @@ void loop() #if WIFI_ON WifiInterface::loop(); #endif //WIFI_ON +#else //ARDUINO_ARCH_ESP32 +#ifndef WIFI_TASK_ON_CORE0 + WifiESP::loop(); +#endif #endif //ARDUINO_ARCH_ESP32 #if ETHERNET_ON EthernetInterface::loop(); diff --git a/RingStream.cpp b/RingStream.cpp index fcf6ed7..e639f5d 100644 --- a/RingStream.cpp +++ b/RingStream.cpp @@ -143,7 +143,7 @@ int RingStream::freeSpace() { // mark start of message with client id (0...9) void RingStream::mark(uint8_t b) { - DIAG(F("RS mark client %d at %d core %d"), b, _pos_write, xPortGetCoreID()); + //DIAG(F("RS mark client %d at %d core %d"), b, _pos_write, xPortGetCoreID()); _ringClient = b; _mark=_pos_write; write(b); // client id @@ -165,14 +165,14 @@ void RingStream::info() { bool RingStream::commit() { _flashInsert=NULL; // prepared for first read if (_overflow) { - DIAG(F("RingStream(%d) commit(%d) OVERFLOW"),_len, _count); + //DIAG(F("RingStream(%d) commit(%d) OVERFLOW"),_len, _count); // just throw it away _pos_write=_mark; _overflow=false; return false; // commit failed } if (_count==0) { - DIAG(F("RS commit count=0 rewind back to %d core %d"), _mark, xPortGetCoreID()); + //DIAG(F("RS commit count=0 rewind back to %d core %d"), _mark, xPortGetCoreID()); // ignore empty response _pos_write=_mark; _ringClient = NO_CLIENT; //XXX make else clause later @@ -188,7 +188,7 @@ bool RingStream::commit() { { char s[_count+2]; strncpy(s, (const char*)&(_buffer[_mark+1]), _count); s[_count]=0; - DIAG(F("RS commit count=%d core %d \"%s\""), _count, xPortGetCoreID(), s); + //DIAG(F("RS commit count=%d core %d \"%s\""), _count, xPortGetCoreID(), s); } _ringClient = NO_CLIENT; return true; // commit worked diff --git a/RingStream.h b/RingStream.h index a3aad49..4f51a65 100644 --- a/RingStream.h +++ b/RingStream.h @@ -52,6 +52,7 @@ class RingStream : public Print { if ((_pos_read==_pos_write) && !_overflow) return -1; // empty return _buffer[_pos_read]; }; + static const byte NO_CLIENT=255; private: int _len; int _pos_write; @@ -61,7 +62,6 @@ class RingStream : public Print { int _count; byte * _buffer; char * _flashInsert; - static const byte NO_CLIENT=255; // must be same as in CommandDistributor byte _ringClient = NO_CLIENT; }; diff --git a/WiThrottle.cpp b/WiThrottle.cpp index e6b018f..46ee81f 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -611,12 +611,12 @@ byte WiThrottle::stashClient; char WiThrottle::stashThrottleChar; void WiThrottle::getLocoCallback(int16_t locoid) { - DIAG(F("LocoCallback mark client %d"), stashClient); + //DIAG(F("LocoCallback mark client %d"), stashClient); stashStream->mark(stashClient); if (locoid<=0) { StringFormatter::send(stashStream,F("HMNo loco found on prog track\n")); - DIAG(F("LocoCallback commit (noloco)")); + //DIAG(F("LocoCallback commit (noloco)")); stashStream->commit(); // done here, commit and return return; } @@ -627,7 +627,7 @@ void WiThrottle::getLocoCallback(int16_t locoid) { locoid = locoid ^ LONG_ADDR_MARKER; // remove marker bit to get real long addr if (locoid <= HIGHEST_SHORT_ADDR ) { // out of range for long addr StringFormatter::send(stashStream,F("HMLong addr %d <= %d unsupported\n"), locoid, HIGHEST_SHORT_ADDR); - DIAG(F("LocoCallback commit (error)")); + //DIAG(F("LocoCallback commit (error)")); stashStream->commit(); // done here, commit and return return; } @@ -641,7 +641,7 @@ void WiThrottle::getLocoCallback(int16_t locoid) { stashInstance->multithrottle(stashStream, (byte *)addcmd); TrackManager::setMainPower(POWERMODE::ON); TrackManager::setJoin(true); // <1 JOIN> so we can drive loco away - DIAG(F("LocoCallback commit success")); + //DIAG(F("LocoCallback commit success")); stashStream->commit(); CommandDistributor::broadcastPower(); diff --git a/WifiESP32.cpp b/WifiESP32.cpp index 164374f..21b94ea 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -95,14 +95,15 @@ public: static std::vector clients; // a list to hold all clients static WiFiServer *server = NULL; static RingStream *outboundRing = new RingStream(2048); -//static RingStream *eventRing = new RingStream(2048); static bool APmode = false; +#ifdef WIFI_TASK_ON_CORE0 void wifiLoop(void *){ for(;;){ WifiESP::loop(); } } +#endif bool WifiESP::setup(const char *SSid, const char *password, @@ -196,6 +197,7 @@ bool WifiESP::setup(const char *SSid, server->begin(); // server started here +#ifdef WIFI_TASK_ON_CORE0 //start loop task if (pdPASS != xTaskCreatePinnedToCore( wifiLoop, /* Task function. */ @@ -211,7 +213,10 @@ bool WifiESP::setup(const char *SSid, // report server started after wifiLoop creation // when everything looks good - DIAG(F("Server up port %d"),port); + DIAG(F("Server starting (core 0) port %d"),port); +#else + DIAG(F("Server will be started on port %d"),port); +#endif return true; } @@ -261,7 +266,7 @@ void WifiESP::loop() { cmd[len]=0; outboundRing->mark(clientId); CommandDistributor::parse(clientId,cmd,outboundRing); - if (outboundRing->peekTargetMark()!=255) //XXX fix 255 later + if (outboundRing->peekTargetMark()!=RingStream::NO_CLIENT) outboundRing->commit(); } }