From 797028b2233e04057a37237fb0738c73e97940d3 Mon Sep 17 00:00:00 2001 From: peteGSX <97784652+peteGSX@users.noreply.github.com> Date: Tue, 19 Dec 2023 07:30:15 +1000 Subject: [PATCH 1/6] Ready to test --- IO_EXIOExpander.h | 56 ++++++++++++++++++++++++----------------------- version.h | 3 ++- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h index 675d66c..c8bcba0 100644 --- a/IO_EXIOExpander.h +++ b/IO_EXIOExpander.h @@ -22,13 +22,10 @@ * This device driver will configure the device on startup, along with * interacting with the device for all input/output duties. * -* To create EX-IOExpander devices, these are defined in myHal.cpp: +* To create EX-IOExpander devices, these are defined in myAutomation.h: * (Note the device driver is included by default) * -* void halSetup() { -* // EXIOExpander::create(vpin, num_vpins, i2c_address); -* EXIOExpander::create(800, 18, 0x65); -* } +* HAL(EXIOExpander,800,18,0x65) * * All pins on an EX-IOExpander device are allocated according to the pin map for the specific * device in use. There is no way for the device driver to sanity check pins are used for the @@ -98,25 +95,30 @@ private: _numAnaloguePins = receiveBuffer[2]; // See if we already have suitable buffers assigned - size_t digitalBytesNeeded = (_numDigitalPins + 7) / 8; - if (_digitalPinBytes < digitalBytesNeeded) { - // Not enough space, free any existing buffer and allocate a new one - if (_digitalPinBytes > 0) free(_digitalInputStates); - _digitalInputStates = (byte*) calloc(_digitalPinBytes, 1); - _digitalPinBytes = digitalBytesNeeded; - } - size_t analogueBytesNeeded = _numAnaloguePins * 2; - if (_analoguePinBytes < analogueBytesNeeded) { - // Free any existing buffers and allocate new ones. - if (_analoguePinBytes > 0) { - free(_analogueInputBuffer); - free(_analogueInputStates); - free(_analoguePinMap); + if (_numDigitalPins>0) { + size_t digitalBytesNeeded = (_numDigitalPins + 7) / 8; + if (_digitalPinBytes < digitalBytesNeeded) { + // Not enough space, free any existing buffer and allocate a new one + if (_digitalPinBytes > 0) free(_digitalInputStates); + _digitalInputStates = (byte*) calloc(_digitalPinBytes, 1); + _digitalPinBytes = digitalBytesNeeded; + } + } + + if (_numAnaloguePins>0) { + size_t analogueBytesNeeded = _numAnaloguePins * 2; + if (_analoguePinBytes < analogueBytesNeeded) { + // Free any existing buffers and allocate new ones. + if (_analoguePinBytes > 0) { + free(_analogueInputBuffer); + free(_analogueInputStates); + free(_analoguePinMap); + } + _analogueInputStates = (uint8_t*) calloc(analogueBytesNeeded, 1); + _analogueInputBuffer = (uint8_t*) calloc(analogueBytesNeeded, 1); + _analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1); + _analoguePinBytes = analogueBytesNeeded; } - _analogueInputStates = (uint8_t*) calloc(analogueBytesNeeded, 1); - _analogueInputBuffer = (uint8_t*) calloc(analogueBytesNeeded, 1); - _analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1); - _analoguePinBytes = analogueBytesNeeded; } } else { DIAG(F("EX-IOExpander I2C:%s ERROR configuring device"), _I2CAddress.toString()); @@ -124,8 +126,8 @@ private: return; } } - // We now need to retrieve the analogue pin map - if (status == I2C_STATUS_OK) { + // We now need to retrieve the analogue pin map if there are analogue pins + if (status == I2C_STATUS_OK && _numAnaloguePins>0) { commandBuffer[0] = EXIOINITA; status = I2CManager.read(_I2CAddress, _analoguePinMap, _numAnaloguePins, commandBuffer, 1); } @@ -239,7 +241,7 @@ private: // If we're not doing anything now, check to see if a new input transfer is due. if (_readState == RDS_IDLE) { - if (currentMicros - _lastDigitalRead > _digitalRefresh) { // Delay for digital read refresh + if (currentMicros - _lastDigitalRead > _digitalRefresh && _numDigitalPins>0) { // Delay for digital read refresh // Issue new read request for digital states. As the request is non-blocking, the buffer has to // be allocated from heap (object state). _readCommandBuffer[0] = EXIORDD; @@ -247,7 +249,7 @@ private: // non-blocking read _lastDigitalRead = currentMicros; _readState = RDS_DIGITAL; - } else if (currentMicros - _lastAnalogueRead > _analogueRefresh) { // Delay for analogue read refresh + } else if (currentMicros - _lastAnalogueRead > _analogueRefresh && _numAnaloguePins>0) { // Delay for analogue read refresh // Issue new read for analogue input states _readCommandBuffer[0] = EXIORDAN; I2CManager.read(_I2CAddress, _analogueInputBuffer, diff --git a/version.h b/version.h index b74cbd2..3da0307 100644 --- a/version.h +++ b/version.h @@ -3,7 +3,8 @@ #include "StringFormatter.h" -#define VERSION "5.2.15" +#define VERSION "5.2.16" +// 5.2.16 - Bugfix to allow for devices using the EX-IOExpander protocol to have no analogue or no digital pins // 5.2.15 - move call to CommandDistributor::broadcastPower() into the TrackManager::setTrackPower(*) functions // - add repeats to function packets that are not reminded in accordance with accessory packets // 5.2.14 - Reminder window DCC packet optimization From 198d762a212321ef632f2edf5cb6984c7254622d Mon Sep 17 00:00:00 2001 From: pmantoine Date: Fri, 22 Dec 2023 12:29:17 +0800 Subject: [PATCH 2/6] Add F439ZI serial setup in WifiInterface --- WifiInterface.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WifiInterface.cpp b/WifiInterface.cpp index 8b2251a..87d5437 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -68,7 +68,9 @@ Stream * WifiInterface::wifiStream; #define NUM_SERIAL 3 #define SERIAL1 Serial3 #define SERIAL3 Serial5 -#elif defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE) || defined(ARDUINO_NUCLEO_F412ZG) +#elif defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) \ + || defined(ARDUINO_NUCLEO_F446ZE) || defined(ARDUINO_NUCLEO_F412ZG) \ + || defined(ARDUINO_NUCLEO_F439ZI) #define NUM_SERIAL 2 #define SERIAL1 Serial6 #else From 041a6534da3c73693b232521096b7bda9fdd0717 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 24 Dec 2023 12:03:42 +0100 Subject: [PATCH 3/6] more diag and inUse tests --- WifiESP32.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/WifiESP32.cpp b/WifiESP32.cpp index f0a857f..ab5274b 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -80,16 +80,15 @@ public: }; bool recycle(WiFiClient c) { - if (inUse == true) return false; - - // return false here until we have - // implemented a LRU timer - // if (LRU too recent) return false; + if (wifi == c) { + if (inUse == true) + DIAG(F("WARNING: Duplicate")); + else + DIAG(F("Returning")); + inUse = true; + return true; + } return false; - - wifi = c; - inUse = true; - return true; }; WiFiClient wifi; bool inUse = true; @@ -299,14 +298,14 @@ void WifiESP::loop() { while (client = server->available()) { for (clientId=0; clientId=clients.size()) { NetworkClient nc(client); clients.push_back(nc); - DIAG(F("New client %d, %s"), clientId, client.remoteIP().toString().c_str()); + DIAG(F("New client %d, %s:%d"), clientId, client.remoteIP().toString().c_str(),client.remotePort()); } } } From e3bab887a289b4fef5ecb14a336b8558fb31137c Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 25 Dec 2023 17:32:39 +0100 Subject: [PATCH 4/6] simplify WifiESP32 --- WifiESP32.cpp | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/WifiESP32.cpp b/WifiESP32.cpp index ab5274b..1103dfc 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -74,12 +74,21 @@ class NetworkClient { public: NetworkClient(WiFiClient c) { wifi = c; + inUse = true; }; - bool ok() { - return (inUse && wifi.connected()); - }; + bool active(byte clientId) { + if (!inUse) + return false; + if(!wifi.connected()) { + DIAG(F("Remove client %d"), clientId); + CommandDistributor::forget(clientId); + wifi.stop(); + inUse = false; + return false; + } + return true; + } bool recycle(WiFiClient c) { - if (wifi == c) { if (inUse == true) DIAG(F("WARNING: Duplicate")); @@ -88,10 +97,16 @@ public: inUse = true; return true; } + if (inUse == false) { + wifi = c; + inUse = true; + return true; + } return false; }; WiFiClient wifi; - bool inUse = true; +private: + bool inUse; }; static std::vector clients; // a list to hold all clients @@ -281,18 +296,6 @@ void WifiESP::loop() { // really no good way to check for LISTEN especially in AP mode? wl_status_t wlStatus; if (APmode || (wlStatus = WiFi.status()) == WL_CONNECTED) { - // loop over all clients and remove inactive - for (clientId=0; clientIdhasClient()) { WiFiClient client; while (client = server->available()) { @@ -310,8 +313,9 @@ void WifiESP::loop() { } } // loop over all connected clients + // this removes as a side effect inactive clients when checking ::active() for (clientId=0; clientId 0) { // read data from client @@ -349,7 +353,7 @@ void WifiESP::loop() { } // buffer filled, end with '\0' so we can use it as C string buffer[count]='\0'; - if((unsigned int)clientId <= clients.size() && clients[clientId].ok()) { + if((unsigned int)clientId <= clients.size() && clients[clientId].active(clientId)) { if (Diag::CMD || Diag::WITHROTTLE) DIAG(F("SEND %d:%s"), clientId, buffer); clients[clientId].wifi.write(buffer,count); From e7d3d92c23417898cfc3772339b1679ed5ed8b78 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 25 Dec 2023 17:40:29 +0100 Subject: [PATCH 5/6] as no other tasks run on core1, yield() not necessary --- WifiESP32.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/WifiESP32.cpp b/WifiESP32.cpp index 1103dfc..c990495 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -386,8 +386,9 @@ void WifiESP::loop() { // prio task. On core1 this is not a problem // as there the wdt is disabled by the // arduio IDE startup routines. - if (xPortGetCoreID() == 0) + if (xPortGetCoreID() == 0) { feedTheDog0(); - yield(); + yield(); + } } #endif //ESP32 From bd44184f574b108d6eee700c635181e43e1b5588 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 25 Dec 2023 17:49:16 +0100 Subject: [PATCH 6/6] version 5.2.17 --- GITHUB_SHA.h | 2 +- version.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index 0a5110a..42646fe 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "devel-202312131041Z" +#define GITHUB_SHA "devel-202312251647Z" diff --git a/version.h b/version.h index 3da0307..bc79b3e 100644 --- a/version.h +++ b/version.h @@ -3,7 +3,8 @@ #include "StringFormatter.h" -#define VERSION "5.2.16" +#define VERSION "5.2.17" +// 5.2.17 - ESP32 simplify network logic // 5.2.16 - Bugfix to allow for devices using the EX-IOExpander protocol to have no analogue or no digital pins // 5.2.15 - move call to CommandDistributor::broadcastPower() into the TrackManager::setTrackPower(*) functions // - add repeats to function packets that are not reminded in accordance with accessory packets