From 802205ad911641bb14941eb0b9ac76ed2d5adce6 Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 14:46:31 +0100 Subject: [PATCH 01/14] Update appInterface.h --- src/appInterface.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/appInterface.h b/src/appInterface.h index a8e37a75..83a4f769 100644 --- a/src/appInterface.h +++ b/src/appInterface.h @@ -17,6 +17,7 @@ class IApp { virtual bool saveSettings() = 0; virtual bool readSettings(const char *path) = 0; virtual bool eraseSettings(bool eraseWifi) = 0; + virtual void setOnUpdate() = 0; virtual void setRebootFlag() = 0; virtual const char *getVersion() = 0; virtual statistics_t *getStatistics() = 0; From 139129ea8b0c3fbac9b4dd0ecc74227eb1dd1842 Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 14:49:49 +0100 Subject: [PATCH 02/14] Update app.h --- src/app.h | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/app.h b/src/app.h index d51c9209..d3709b89 100644 --- a/src/app.h +++ b/src/app.h @@ -18,10 +18,8 @@ #include "config/settings.h" #include "defines.h" #include "utils/crc.h" -#include "utils/ahoyTimer.h" #include "utils/scheduler.h" -#include "hm/CircularBuffer.h" #include "hm/hmSystem.h" #include "hm/payload.h" #include "wifi/ahoywifi.h" @@ -61,10 +59,6 @@ class app : public IApp, public ah::Scheduler { void loopWifi(void); void onWifi(bool gotIp); void regularTickers(void); - void handleIntr(void); - void cbMqtt(char* topic, byte* payload, unsigned int length); - void saveValues(void); - bool getWifiApActive(void); uint32_t getUptime() { return Scheduler::getUptime(); @@ -99,6 +93,10 @@ class app : public IApp, public ah::Scheduler { mWifi.getAvailNetworks(obj); } + void setOnUpdate() { + onWifi(false); + } + void setRebootFlag() { once(std::bind(&app::tickReboot, this), 3, "rboot"); } @@ -206,6 +204,9 @@ class app : public IApp, public ah::Scheduler { void tickReboot(void) { DPRINTLN(DBG_INFO, F("Rebooting...")); + onWifi(false); + ah::Scheduler::resetTicker(); + WiFi.disconnect(); ESP.restart(); } @@ -247,13 +248,9 @@ class app : public IApp, public ah::Scheduler { settings_t *mConfig; uint8_t mSendLastIvId; - uint8_t mSendTickerId; statistics_t mStat; - // timer - uint32_t mRxTicker; - // mqtt PubMqttType mMqtt; bool mMqttReconnect; From 5fb45f1b20b1e7de0f3f4edb98ffe6339bdeeccc Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 14:53:01 +0100 Subject: [PATCH 03/14] Update app.cpp --- src/app.cpp | 83 +++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 51 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 0d88847e..f5478f0f 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -82,40 +82,26 @@ void app::loop(void) { void app::loopStandard(void) { ah::Scheduler::loop(); - mSys->Radio.loop(); + if (mSys->Radio.loop()) { + while (!mSys->Radio.mBufCtrl.empty()) { + packet_t *p = &mSys->Radio.mBufCtrl.front(); + + if (mConfig->serial.debug) { + DPRINT(DBG_INFO, "RX " + String(p->len) + "B Ch" + String(p->ch) + " | "); + mSys->Radio.dumpBuf(p->packet, p->len); + } + mStat.frmCnt++; + + mPayload.add(p); + mSys->Radio.mBufCtrl.pop(); + yield(); + } + mPayload.process(true); + } mPayload.loop(); - yield(); - - if (ah::checkTicker(&mRxTicker, 4)) { - bool rxRdy = mSys->Radio.switchRxCh(); - - if (!mSys->BufCtrl.empty()) { - uint8_t len; - packet_t *p = mSys->BufCtrl.getBack(); - - if (mSys->Radio.checkPaketCrc(p->packet, &len, p->rxCh)) { - if (mConfig->serial.debug) { - DPRINT(DBG_INFO, "RX " + String(len) + "B Ch" + String(p->rxCh) + " | "); - mSys->Radio.dumpBuf(NULL, p->packet, len); - } - mStat.frmCnt++; - - if (0 != len) - mPayload.add(p, len); - } - mSys->BufCtrl.popBack(); - } - yield(); - - if (rxRdy) - mPayload.process(true); - } - -#if !defined(AP_ONLY) if(mMqttEnabled) mMqtt.loop(); -#endif } //----------------------------------------------------------------------------- @@ -131,12 +117,14 @@ void app::onWifi(bool gotIp) { regularTickers(); // reinstall regular tickers if (gotIp) { mInnerLoopCb = std::bind(&app::loopStandard, this); - mSendTickerId = every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend"); + every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend"); mMqttReconnect = true; mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers! once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2"); - if(WIFI_AP == WiFi.getMode()) + if(WIFI_AP == WiFi.getMode()) { + mMqttEnabled = false; everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL"); + } } else { mInnerLoopCb = std::bind(&app::loopWifi, this); @@ -232,6 +220,15 @@ void app::tickComm(void) { once(std::bind(&app::tickComm, this), 1, "mqCom"); // MQTT not connected, retry } +//----------------------------------------------------------------------------- +void app::tickMidnight(void) { + // only used and enabled by MQTT (see setup()) + uint32_t nxtTrig = mTimestamp - ((mTimestamp - 1) % 86400) + 86400; // next midnight + onceAt(std::bind(&app::tickMidnight, this), nxtTrig, "mid2"); + + mMqtt.tickerMidnight(); +} + //----------------------------------------------------------------------------- void app::tickSend(void) { if(!mSys->Radio.isChipConnected()) { @@ -239,9 +236,9 @@ void app::tickSend(void) { return; } if (mIVCommunicationOn) { - if (!mSys->BufCtrl.empty()) { + if (!mSys->Radio.mBufCtrl.empty()) { if (mConfig->serial.debug) - DPRINTLN(DBG_DEBUG, F("recbuf not empty! #") + String(mSys->BufCtrl.getFill())); + DPRINTLN(DBG_DEBUG, F("recbuf not empty! #") + String(mSys->Radio.mBufCtrl.size())); } int8_t maxLoop = MAX_NUM_INVERTERS; @@ -264,21 +261,6 @@ void app::tickSend(void) { updateLed(); } -//----------------------------------------------------------------------------- -void app::tickMidnight(void) { - // only used and enabled by MQTT (see setup()) - uint32_t nxtTrig = mTimestamp - ((mTimestamp - 1) % 86400) + 86400; // next midnight - onceAt(std::bind(&app::tickMidnight, this), nxtTrig, "mid2"); - - mMqtt.tickerMidnight(); -} - -//----------------------------------------------------------------------------- -void app::handleIntr(void) { - DPRINTLN(DBG_VERBOSE, F("app::handleIntr")); - mSys->Radio.handleIntr(); -} - //----------------------------------------------------------------------------- void app::resetSystem(void) { snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); @@ -290,8 +272,7 @@ void app::resetSystem(void) { mSunrise = 0; mSunset = 0; - mRxTicker = 0; - mSendTickerId = 0xff; // invalid id + mMqttEnabled = false; mSendLastIvId = 0; mShowRebootRequest = false; From 447a43388eb20a7a9250af0d4c5e5c68ce49a62a Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:01:08 +0100 Subject: [PATCH 04/14] Update main.cpp --- src/main.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c585d0f2..42ed55f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,21 +7,11 @@ #include "app.h" #include "config/config.h" - app myApp; -//----------------------------------------------------------------------------- -IRAM_ATTR void handleIntr(void) { - myApp.handleIntr(); -} - - //----------------------------------------------------------------------------- void setup() { myApp.setup(); - - // TODO: move to HmRadio - attachInterrupt(digitalPinToInterrupt(myApp.getIrqPin()), handleIntr, FALLING); } From 975ee667f2dd29a269200ad82a42c179e6522689 Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:03:26 +0100 Subject: [PATCH 05/14] Update defines.h --- src/defines.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/defines.h b/src/defines.h index 5c626e02..d5f0fcb3 100644 --- a/src/defines.h +++ b/src/defines.h @@ -17,7 +17,8 @@ //------------------------------------- typedef struct { - uint8_t rxCh; + uint8_t ch; + uint8_t len; uint8_t packet[MAX_RF_PAYLOAD_SIZE]; } packet_t; From 7177d54d736e11fa5cc2e574ab6fe9232c39260a Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:10:15 +0100 Subject: [PATCH 06/14] Update pubMqtt.h --- src/publisher/pubMqtt.h | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 021a29eb..938499ea 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -15,7 +15,6 @@ #endif #include "../utils/dbg.h" -#include "../utils/ahoyTimer.h" #include "../config/config.h" #include #include @@ -40,8 +39,7 @@ class PubMqtt { mRxCnt = 0; mTxCnt = 0; mSubscriptionCb = NULL; - mIvAvail = true; - memset(mLastIvState, 0xff, MAX_NUM_INVERTERS); + memset(mLastIvState, MQTT_STATUS_NOT_AVAIL_NOT_PROD, MAX_NUM_INVERTERS); } ~PubMqtt() { } @@ -70,6 +68,7 @@ class PubMqtt { void loop() { #if defined(ESP8266) mClient.loop(); + yield(); #endif } @@ -408,13 +407,12 @@ class PubMqtt { bool processIvStatus() { // returns true if all inverters are available - bool allAvail = true; - bool first = true; + bool allAvail = true; // shows if all enabled inverters are available + bool anyAvail = false; // shows if at least one enabled inverter is available bool changed = false; char topic[7 + MQTT_TOPIC_LEN], val[40]; Inverter<> *iv; record_t<> *rec; - bool totalComplete = true; for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { iv = mSys->getInverterByPos(id); @@ -422,32 +420,21 @@ class PubMqtt { continue; // skip to next inverter rec = iv->getRecordStruct(RealTimeRunData_Debug); - if(first) - mIvAvail = false; - first = false; // inverter status - uint8_t status = MQTT_STATUS_AVAIL_PROD; - if ((!iv->isAvailable(*mUtcTimestamp)) || (!iv->config->enabled)) { - status = MQTT_STATUS_NOT_AVAIL_NOT_PROD; - if(iv->config->enabled) { // only change all-avail if inverter is enabled! - totalComplete = false; + uint8_t status = MQTT_STATUS_NOT_AVAIL_NOT_PROD; + if (iv->config->enabled) { + if (iv->isAvailable(*mUtcTimestamp)) + status = (iv->isProducing(*mUtcTimestamp)) ? MQTT_STATUS_AVAIL_PROD : MQTT_STATUS_AVAIL_NOT_PROD; + else // inverter is enabled but not available allAvail = false; - } - } - else { - mIvAvail = true; - if (!iv->isProducing(*mUtcTimestamp)) { - if (MQTT_STATUS_AVAIL_PROD == status) - status = MQTT_STATUS_AVAIL_NOT_PROD; - } } if(mLastIvState[id] != status) { mLastIvState[id] = status; changed = true; - if(mCfgMqtt->rstValsNotAvail) + if((MQTT_STATUS_NOT_AVAIL_NOT_PROD == status) && (mCfgMqtt->rstValsNotAvail)) zeroValues(iv); snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->config->name); @@ -461,12 +448,12 @@ class PubMqtt { } if(changed) { - snprintf(val, 32, "%d", ((allAvail) ? MQTT_STATUS_ONLINE : ((mIvAvail) ? MQTT_STATUS_PARTIAL : MQTT_STATUS_OFFLINE))); + snprintf(val, 32, "%d", ((allAvail) ? MQTT_STATUS_ONLINE : ((anyAvail) ? MQTT_STATUS_PARTIAL : MQTT_STATUS_OFFLINE))); publish("status", val, true); sendIvData(false); // false prevents loop of same function } - return totalComplete; + return allAvail; } void sendAlarmData() { @@ -494,7 +481,7 @@ class PubMqtt { memset(total, 0, sizeof(float) * 4); for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { Inverter<> *iv = mSys->getInverterByPos(id); - if (NULL == iv) + if ((NULL == iv) || (MQTT_STATUS_NOT_AVAIL_NOT_PROD == mLastIvState[id])) continue; // skip to next inverter record_t<> *rec = iv->getRecordStruct(mSendList.front()); @@ -626,7 +613,6 @@ class PubMqtt { std::queue mSendList; std::queue mAlarmList; subscriptionCb mSubscriptionCb; - bool mIvAvail; // shows if at least one inverter is available bool mReconnectRequest; uint8_t mLastIvState[MAX_NUM_INVERTERS]; uint16_t mIntervalTimeout; From 8f49aae4f35d5fd6629d57afc82105a25e47837e Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:15:53 +0100 Subject: [PATCH 07/14] Delete ahoyTimer.h --- src/utils/ahoyTimer.h | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/utils/ahoyTimer.h diff --git a/src/utils/ahoyTimer.h b/src/utils/ahoyTimer.h deleted file mode 100644 index 08c09016..00000000 --- a/src/utils/ahoyTimer.h +++ /dev/null @@ -1,27 +0,0 @@ -//----------------------------------------------------------------------------- -// 2022 Ahoy, https://ahoydtu.de -// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ -//----------------------------------------------------------------------------- - -#ifndef __AHOY_TIMER_H__ -#define __AHOY_TIMER_H__ - -#include - -namespace ah { - inline bool checkTicker(uint32_t *ticker, uint32_t interval) { - uint32_t mil = millis(); - if(mil >= *ticker) { - *ticker = mil + interval; - return true; - } - else if((mil + interval) < (*ticker)) { - *ticker = mil + interval; - return true; - } - - return false; - } -} - -#endif /*__AHOY_TIMER_H__*/ From 48c4884e80c0e1ab8253da7d0db5d608c00dc793 Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:22:34 +0100 Subject: [PATCH 08/14] Update web.h --- src/web/web.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/web/web.h b/src/web/web.h index 5641ef0d..89d2dfb0 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -18,7 +18,6 @@ #include "../appInterface.h" #include "../hm/hmSystem.h" -#include "../utils/ahoyTimer.h" #include "../utils/helper.h" #include "html/h/index_html.h" @@ -132,6 +131,8 @@ class Web { } void showUpdate2(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { + mApp->setOnUpdate(); + if(!index) { Serial.printf("Update Start: %s\n", filename.c_str()); #ifndef ESP32 @@ -246,7 +247,7 @@ class Web { AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), html); response->addHeader("Connection", "close"); request->send(response); - if(reboot) + //if(reboot) mApp->setRebootFlag(); } @@ -263,7 +264,7 @@ class Web { AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), html); response->addHeader("Connection", "close"); request->send(response); - if(reboot) + //if(reboot) mApp->setRebootFlag(); } @@ -550,8 +551,8 @@ class Web { mConfig->mqtt.port = request->arg("mqttPort").toInt(); mConfig->mqtt.interval = request->arg("mqttInterval").toInt(); mConfig->mqtt.rstYieldMidNight = (request->arg("mqttRstMid") == "on"); - mConfig->mqtt.rstValsNotAvail = (request->arg("mqttRstComStop") == "on"); - mConfig->mqtt.rstValsCommStop = (request->arg("mqttRstNotAvail") == "on"); + mConfig->mqtt.rstValsNotAvail = (request->arg("mqttRstNotAvail") == "on"); + mConfig->mqtt.rstValsCommStop = (request->arg("mqttRstComStop") == "on"); // serial console if(request->arg("serIntvl") != "") { From 5056d7c729f2babd5df113188e864b01a23f30ee Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:25:54 +0100 Subject: [PATCH 09/14] Update ahoywifi.h --- src/wifi/ahoywifi.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/wifi/ahoywifi.h b/src/wifi/ahoywifi.h index 321dbb86..ac8c0dd0 100644 --- a/src/wifi/ahoywifi.h +++ b/src/wifi/ahoywifi.h @@ -34,6 +34,7 @@ class ahoywifi { DISCONNECTED = 0, CONNECTING, CONNECTED, + IN_AP_MODE, GOT_IP } WiFiStatus_t; @@ -41,6 +42,8 @@ class ahoywifi { void setupAp(void); void setupStation(void); void sendNTPpacket(IPAddress& address); + void sortRSSI(int *sort, int n); + void getBSSIDs(void); void connectionEvent(WiFiStatus_t status); #if defined(ESP8266) void onConnect(const WiFiEventStationModeConnected& event); @@ -50,8 +53,7 @@ class ahoywifi { void onWiFiEvent(WiFiEvent_t event); #endif void welcome(String msg); - void sortRSSI(int *sort, int n); - void getBSSIDs(void); + settings_t *mConfig; appWifiCb mAppWifiCb; @@ -67,11 +69,8 @@ class ahoywifi { uint8_t mCnt; uint32_t *mUtcTimestamp; - uint8_t mLoopCnt; - bool mScanActive; - uint8_t mLastApClients; uint8_t mScanCnt; - + bool mScanActive; std::list mBSSIDList; }; From bbee9abd28a35b4e40effe3ae3df8e9da7732707 Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:28:22 +0100 Subject: [PATCH 10/14] Update ahoywifi.cpp --- src/wifi/ahoywifi.cpp | 64 ++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/src/wifi/ahoywifi.cpp b/src/wifi/ahoywifi.cpp index 4ca7f290..8384c118 100644 --- a/src/wifi/ahoywifi.cpp +++ b/src/wifi/ahoywifi.cpp @@ -25,8 +25,6 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) { mStaConn = DISCONNECTED; mCnt = 0; mScanActive = false; - mLastApClients = 0; - mScanCnt = 0; #if defined(ESP8266) wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1)); @@ -66,44 +64,31 @@ void ahoywifi::tickWifiLoop() { #if !defined(AP_ONLY) if(mStaConn != GOT_IP) { if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists - if(WIFI_AP_STA == WiFi.getMode()) { - // first time switch to AP Mode - if(mScanActive && (mLastApClients != WiFi.softAPgetStationNum())) - mScanActive = false; - - // scan is finished - if(!mScanActive) { - WiFi.mode(WIFI_AP); - mDns.start(53, "*", mApIp); - } - - // only once a client connects to AP - if(mLastApClients != WiFi.softAPgetStationNum()) { - mLastApClients = WiFi.softAPgetStationNum(); + if(mStaConn != IN_AP_MODE) { + mStaConn = IN_AP_MODE; + // first time switch to AP Mode + if (mScanActive) { WiFi.scanDelete(); - mAppWifiCb(false); - DBGPRINTLN(F("AP client connected")); - welcome(mApIp.toString()); + mScanActive = false; } + DBGPRINTLN(F("AP client connected")); + welcome(mApIp.toString()); + WiFi.mode(WIFI_AP); + mDns.start(53, "*", mApIp); + mAppWifiCb(true); } mDns.processNextRequest(); return; } - else if(WIFI_AP == WiFi.getMode()) { - mLastApClients = 0; + else if(mStaConn == IN_AP_MODE) { mCnt = 0; - DPRINTLN(DBG_INFO, "DNS stop"); mDns.stop(); WiFi.mode(WIFI_AP_STA); + mStaConn = DISCONNECTED; } mCnt++; - uint8_t timeout = 10; // seconds - if (mStaConn == CONNECTED) // connected but no ip - timeout = 20; - - - if(!mScanActive && mBSSIDList.empty() && ((mCnt % timeout) == 0)) { // start scanning APs with the given SSID + if(!mScanActive && mBSSIDList.empty()) { // start scanning APs with the given SSID DBGPRINT(F("scanning APs with SSID ")); DBGPRINTLN(String(mConfig->sys.stationSsid)); mScanCnt = 0; @@ -115,14 +100,20 @@ void ahoywifi::tickWifiLoop() { #endif return; } + + uint8_t timeout = 10; // seconds + + if (mStaConn == CONNECTED) // connected but no ip + timeout = 20; + DBGPRINT(F("reconnect in ")); DBGPRINT(String(timeout-mCnt)); DBGPRINTLN(F(" seconds")); if(mScanActive) { getBSSIDs(); - //if(!mScanActive) // scan completed - // if ((mCnt % timeout) < 8) - // mCnt = timeout - 2; + if(!mScanActive) // scan completed + if ((mCnt % timeout) < timeout - 2) + mCnt = timeout - 2; } if((mCnt % timeout) == 0) { // try to reconnect after x sec without connection if(mStaConn != CONNECTED) @@ -166,8 +157,6 @@ void ahoywifi::setupAp(void) { WiFi.mode(WIFI_AP_STA); WiFi.softAPConfig(mApIp, mApIp, IPAddress(255, 255, 255, 0)); WiFi.softAP(WIFI_AP_SSID, WIFI_AP_PWD); - - mDns.start(53, "*", mApIp); } @@ -266,10 +255,10 @@ void ahoywifi::sortRSSI(int *sort, int n) { //----------------------------------------------------------------------------- void ahoywifi::scanAvailNetworks(void) { - if(-2 == WiFi.scanComplete()) { + if(!mScanActive) { mScanActive = true; if(WIFI_AP == WiFi.getMode()) - WiFi.mode(WIFI_AP_STA); + WiFi.mode(WIFI_AP_STA); WiFi.scanNetworks(true); } } @@ -291,12 +280,14 @@ void ahoywifi::getAvailNetworks(JsonObject obj) { } mScanActive = false; WiFi.scanDelete(); + if(mStaConn == IN_AP_MODE) + WiFi.mode(WIFI_AP); } //----------------------------------------------------------------------------- void ahoywifi::getBSSIDs() { int n = WiFi.scanComplete(); - if (n < 0){ + if (n < 0) { mScanCnt++; if (mScanCnt < 20) return; @@ -336,7 +327,6 @@ void ahoywifi::connectionEvent(WiFiStatus_t status) { mScanActive = false; } welcome(WiFi.localIP().toString() + F(" (Station)")); - mDns.stop(); WiFi.mode(WIFI_STA); DBGPRINTLN(F("[WiFi] AP disabled")); mAppWifiCb(true); From 155d602e6e56e5d5dbf6bba2d6eeccc3aaa35f13 Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:29:24 +0100 Subject: [PATCH 11/14] Delete CircularBuffer.h --- src/hm/CircularBuffer.h | 161 ---------------------------------------- 1 file changed, 161 deletions(-) delete mode 100644 src/hm/CircularBuffer.h diff --git a/src/hm/CircularBuffer.h b/src/hm/CircularBuffer.h deleted file mode 100644 index 65c9e768..00000000 --- a/src/hm/CircularBuffer.h +++ /dev/null @@ -1,161 +0,0 @@ -/* - CircularBuffer - An Arduino circular buffering library for arbitrary types. - - Created by Ivo Pullens, Emmission, 2014 -- www.emmission.nl - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef CircularBuffer_h -#define CircularBuffer_h - -#if defined(ESP8266) || defined(ESP32) -#define DISABLE_IRQ noInterrupts() -#define RESTORE_IRQ interrupts() -#else -#define DISABLE_IRQ \ - uint8_t sreg = SREG; \ - cli(); - -#define RESTORE_IRQ \ - SREG = sreg; -#endif - -template -class CircularBuffer { - - typedef BUFFERTYPE BufferType; - BufferType Buffer[BUFFERSIZE]; - - public: - CircularBuffer() : m_buff(Buffer) { - m_size = BUFFERSIZE; - clear(); - } - - /** Clear all entries in the circular buffer. */ - void clear(void) - { - m_front = 0; - m_fill = 0; - } - - /** Test if the circular buffer is empty */ - inline bool empty(void) const - { - return !m_fill; - } - - /** Return the number of records stored in the buffer */ - inline uint8_t available(void) const - { - return m_fill; - } - - /** Test if the circular buffer is full */ - inline bool full(void) const - { - return m_fill == m_size; - } - - inline uint8_t getFill(void) const { - return m_fill; - } - - /** Aquire record on front of the buffer, for writing. - * After filling the record, it has to be pushed to actually - * add it to the buffer. - * @return Pointer to record, or NULL when buffer is full. - */ - BUFFERTYPE* getFront(void) const - { - DISABLE_IRQ; - BUFFERTYPE* f = NULL; - if (!full()) - f = get(m_front); - RESTORE_IRQ; - return f; - } - - /** Push record to front of the buffer - * @param record Record to push. If record was aquired previously (using getFront) its - * data will not be copied as it is already present in the buffer. - * @return True, when record was pushed successfully. - */ - bool pushFront(BUFFERTYPE* record) - { - bool ok = false; - DISABLE_IRQ; - if (!full()) - { - BUFFERTYPE* f = get(m_front); - if (f != record) - *f = *record; - m_front = (m_front+1) % m_size; - m_fill++; - ok = true; - } - RESTORE_IRQ; - return ok; - } - - /** Aquire record on back of the buffer, for reading. - * After reading the record, it has to be pop'ed to actually - * remove it from the buffer. - * @return Pointer to record, or NULL when buffer is empty. - */ - BUFFERTYPE* getBack(void) const - { - BUFFERTYPE* b = NULL; - DISABLE_IRQ; - if (!empty()) - b = get(back()); - RESTORE_IRQ; - return b; - } - - /** Remove record from back of the buffer. - * @return True, when record was pop'ed successfully. - */ - bool popBack(void) - { - bool ok = false; - DISABLE_IRQ; - if (!empty()) - { - m_fill--; - ok = true; - } - RESTORE_IRQ; - return ok; - } - - protected: - inline BUFFERTYPE * get(const uint8_t idx) const - { - return &(m_buff[idx]); - } - inline uint8_t back(void) const - { - return (m_front - m_fill + m_size) % m_size; - } - - uint8_t m_size; // Total number of records that can be stored in the buffer. - BUFFERTYPE* const m_buff; - volatile uint8_t m_front; // Index of front element (not pushed yet). - volatile uint8_t m_fill; // Amount of records currently pushed. -}; - -#endif // CircularBuffer_h From 6159c0755973bf458fa4916cca684fab703ec32d Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:41:47 +0100 Subject: [PATCH 12/14] Update hmRadio.h --- src/hm/hmRadio.h | 329 +++++++++++++++++++---------------------------- 1 file changed, 131 insertions(+), 198 deletions(-) diff --git a/src/hm/hmRadio.h b/src/hm/hmRadio.h index ee84263b..ea440b8c 100644 --- a/src/hm/hmRadio.h +++ b/src/hm/hmRadio.h @@ -9,28 +9,10 @@ #include "../utils/dbg.h" #include #include "../utils/crc.h" -#ifndef DISABLE_IRQ - #if defined(ESP8266) || defined(ESP32) - #define DISABLE_IRQ noInterrupts() - #define RESTORE_IRQ interrupts() - #else - #define DISABLE_IRQ \ - uint8_t sreg = SREG; \ - cli(); - #define RESTORE_IRQ \ - SREG = sreg; - #endif -#endif -//#define CHANNEL_HOP // switch between channels or use static channel to send +#define SPI_SPEED 1000000 -#define DEFAULT_RECV_CHANNEL 3 -#define SPI_SPEED 1000000 - -#define DUMMY_RADIO_ID ((uint64_t)0xDEADBEEF01ULL) - -#define RF_CHANNELS 5 -#define RF_LOOP_CNT 300 +#define RF_CHANNELS 5 #define TX_REQ_INFO 0x15 #define TX_REQ_DEVCONTROL 0x51 @@ -61,11 +43,12 @@ const char* const rf24AmpPowerNames[] = {"MIN", "LOW", "HIGH", "MAX"}; #define BIT_CNT(x) ((x)<<3) +static volatile bool mIrqRcvd; //----------------------------------------------------------------------------- // HM Radio class //----------------------------------------------------------------------------- -template +template class HmRadio { public: HmRadio() : mNrf24(CE_PIN, CS_PIN, SPI_SPEED) { @@ -84,23 +67,22 @@ class HmRadio { mRfChLst[3] = 61; mRfChLst[4] = 75; + // default channels mTxChIdx = 2; // Start TX with 40 mRxChIdx = 0; // Start RX with 03 - mRxLoopCnt = RF_LOOP_CNT; - mSendCnt = 0; - mRetransmits = 0; + mSendCnt = 0; + mRetransmits = 0; - mSerialDebug = false; - mIrqRcvd = false; + mSerialDebug = false; + mIrqRcvd = false; } ~HmRadio() {} - void setup(BUFFER *ctrl, uint8_t ampPwr = RF24_PA_LOW, uint8_t irq = IRQ_PIN, uint8_t ce = CE_PIN, uint8_t cs = CS_PIN) { + void setup(uint8_t ampPwr = RF24_PA_LOW, uint8_t irq = IRQ_PIN, uint8_t ce = CE_PIN, uint8_t cs = CS_PIN) { DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setup")); pinMode(irq, INPUT_PULLUP); - mBufCtrl = ctrl; - + attachInterrupt(digitalPinToInterrupt(irq), []()IRAM_ATTR{ mIrqRcvd = true; }, FALLING); uint32_t dtuSn = 0x87654321; uint32_t chipID = 0; // will be filled with last 3 bytes of MAC @@ -121,27 +103,22 @@ class HmRadio { DTU_RADIO_ID = ((uint64_t)(((dtuSn >> 24) & 0xFF) | ((dtuSn >> 8) & 0xFF00) | ((dtuSn << 8) & 0xFF0000) | ((dtuSn << 24) & 0xFF000000)) << 8) | 0x01; mNrf24.begin(ce, cs); - mNrf24.setRetries(0, 0); + mNrf24.setRetries(3, 15); // 3*250us + 250us and 15 loops -> 15ms - mNrf24.setChannel(DEFAULT_RECV_CHANNEL); + mNrf24.setChannel(mRfChLst[mRxChIdx]); mNrf24.setDataRate(RF24_250KBPS); + mNrf24.setAutoAck(true); + mNrf24.enableDynamicPayloads(); mNrf24.setCRCLength(RF24_CRC_16); - mNrf24.setAutoAck(false); - mNrf24.setPayloadSize(MAX_RF_PAYLOAD_SIZE); mNrf24.setAddressWidth(5); mNrf24.openReadingPipe(1, DTU_RADIO_ID); - mNrf24.enableDynamicPayloads(); - // enable only receiving interrupts - mNrf24.maskIRQ(true, true, false); + // enable all receiving interrupts + mNrf24.maskIRQ(false, false, false); DPRINT(DBG_INFO, F("RF24 Amp Pwr: RF24_PA_")); DPRINTLN(DBG_INFO, String(rf24AmpPowerNames[ampPwr])); mNrf24.setPALevel(ampPwr & 0x03); - mNrf24.startListening(); - - - mTxCh = setDefaultChannels(); if(mNrf24.isChipConnected()) { DPRINTLN(DBG_INFO, F("Radio Config:")); @@ -151,77 +128,70 @@ class HmRadio { DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring")); } - void loop(void) { - if(mIrqRcvd) { - DISABLE_IRQ; - mIrqRcvd = false; - bool tx_ok, tx_fail, rx_ready; - mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // resets the IRQ pin to HIGH - RESTORE_IRQ; - uint8_t pipe, len; - packet_t *p; - while(mNrf24.available(&pipe)) { - if(!mBufCtrl->full()) { - p = mBufCtrl->getFront(); - p->rxCh = mRfChLst[mRxChIdx]; - len = mNrf24.getPayloadSize(); - if(len > MAX_RF_PAYLOAD_SIZE) - len = MAX_RF_PAYLOAD_SIZE; + bool loop(void) { + if (!mIrqRcvd) + return false; // nothing to do + mIrqRcvd = false; + bool tx_ok, tx_fail, rx_ready; + mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // resets the IRQ pin to HIGH + mNrf24.flush_tx(); // empty TX FIFO + //DBGPRINTLN("TX whatHappened Ch" + String(mRfChLst[mTxChIdx]) + " " + String(tx_ok) + String(tx_fail) + String(rx_ready)); - mNrf24.read(p->packet, len); - mBufCtrl->pushFront(p); - yield(); + // start listening on the default RX channel + mRxChIdx = 0; + mNrf24.setChannel(mRfChLst[mRxChIdx]); + mNrf24.startListening(); + + //uint32_t debug_ms = millis(); + uint16_t cnt = 300; // that is 60 times 5 channels + while (0 < cnt--) { + uint32_t startMillis = millis(); + while (millis()-startMillis < 4) { // listen 4ms to each channel + if (mIrqRcvd) { + mIrqRcvd = false; + if (getReceived()) { // everything received + //DBGPRINTLN("RX finished Cnt: " + String(300-cnt) + " time used: " + String(millis()-debug_ms)+ " ms"); + mNrf24.stopListening(); + return true; + } } - else - break; + yield(); } - mNrf24.flush_rx(); // drop the packet - RESTORE_IRQ; + switchRxCh(); // switch to next RX channel + yield(); } + // not finished but time is over + //DBGPRINTLN("RX not finished: 300 time used: " + String(millis()-debug_ms)+ " ms"); + mNrf24.stopListening(); + return true; } + bool isChipConnected(void) { + //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:isChipConnected")); + return mNrf24.isChipConnected(); + } void enableDebug() { mSerialDebug = true; } - void handleIntr(void) { - mIrqRcvd = true; - } - - uint8_t setDefaultChannels(void) { - //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setDefaultChannels")); - mTxChIdx = 2; // Start TX with 40 - mRxChIdx = 0; // Start RX with 03 - return mRfChLst[mTxChIdx]; - } - void sendControlPacket(uint64_t invId, uint8_t cmd, uint16_t *data, bool isRetransmit) { DPRINTLN(DBG_INFO, F("sendControlPacket cmd: 0x") + String(cmd, HEX)); - sendCmdPacket(invId, TX_REQ_DEVCONTROL, SINGLE_FRAME, false); - uint8_t cnt = 0; - mTxBuf[10 + cnt++] = cmd; // cmd -> 0 on, 1 off, 2 restart, 11 active power, 12 reactive power, 13 power factor - mTxBuf[10 + cnt++] = 0x00; + initPacket(invId, TX_REQ_DEVCONTROL, SINGLE_FRAME); + uint8_t cnt = 10; + mTxBuf[cnt++] = cmd; // cmd -> 0 on, 1 off, 2 restart, 11 active power, 12 reactive power, 13 power factor + mTxBuf[cnt++] = 0x00; if(cmd >= ActivePowerContr && cmd <= PFSet) { // ActivePowerContr, ReactivePowerContr, PFSet - mTxBuf[10 + cnt++] = ((data[0] * 10) >> 8) & 0xff; // power limit - mTxBuf[10 + cnt++] = ((data[0] * 10) ) & 0xff; // power limit - mTxBuf[10 + cnt++] = ((data[1] ) >> 8) & 0xff; // setting for persistens handlings - mTxBuf[10 + cnt++] = ((data[1] ) ) & 0xff; // setting for persistens handling + mTxBuf[cnt++] = ((data[0] * 10) >> 8) & 0xff; // power limit + mTxBuf[cnt++] = ((data[0] * 10) ) & 0xff; // power limit + mTxBuf[cnt++] = ((data[1] ) >> 8) & 0xff; // setting for persistens handlings + mTxBuf[cnt++] = ((data[1] ) ) & 0xff; // setting for persistens handling } - - // crc control data - uint16_t crc = ah::crc16(&mTxBuf[10], cnt); - mTxBuf[10 + cnt++] = (crc >> 8) & 0xff; - mTxBuf[10 + cnt++] = (crc ) & 0xff; - - // crc over all - mTxBuf[10 + cnt] = ah::crc8(mTxBuf, 10 + cnt); - - sendPacket(invId, mTxBuf, 10 + cnt + 1, isRetransmit, true); + sendPacket(invId, cnt, isRetransmit, true); } void sendTimePacket(uint64_t invId, uint8_t cmd, uint32_t ts, uint16_t alarmMesId, bool isRetransmit) { DPRINTLN(DBG_DEBUG, F("sendTimePacket 0x") + String(cmd, HEX)); - sendCmdPacket(invId, TX_REQ_INFO, ALL_FRAMES, isRetransmit, false); + initPacket(invId, TX_REQ_INFO, ALL_FRAMES); mTxBuf[10] = cmd; // cid mTxBuf[11] = 0x00; CP_U32_LittleEndian(&mTxBuf[12], ts); @@ -229,61 +199,16 @@ class HmRadio { mTxBuf[18] = (alarmMesId >> 8) & 0xff; mTxBuf[19] = (alarmMesId ) & 0xff; } - uint16_t crc = ah::crc16(&mTxBuf[10], 14); - mTxBuf[24] = (crc >> 8) & 0xff; - mTxBuf[25] = (crc ) & 0xff; - mTxBuf[26] = ah::crc8(mTxBuf, 26); - - sendPacket(invId, mTxBuf, 27, isRetransmit, true); + sendPacket(invId, 24, isRetransmit, true); } - void sendCmdPacket(uint64_t invId, uint8_t mid, uint8_t pid, bool isRetransmit, bool calcCrc = true) { - DPRINTLN(DBG_VERBOSE, F("sendCmdPacket, mid: ") + String(mid, HEX) + F(" pid: ") + String(pid, HEX)); - memset(mTxBuf, 0, MAX_RF_PAYLOAD_SIZE); - mTxBuf[0] = mid; // message id - CP_U32_BigEndian(&mTxBuf[1], (invId >> 8)); - CP_U32_BigEndian(&mTxBuf[5], (DTU_RADIO_ID >> 8)); - mTxBuf[9] = pid; - if(calcCrc) { - mTxBuf[10] = ah::crc8(mTxBuf, 10); - sendPacket(invId, mTxBuf, 11, isRetransmit, false); - } + void sendCmdPacket(uint64_t invId, uint8_t mid, uint8_t pid, bool isRetransmit) { + initPacket(invId, mid, pid); + sendPacket(invId, 10, isRetransmit, false); } - bool checkPaketCrc(uint8_t buf[], uint8_t *len, uint8_t rxCh) { - //DPRINTLN(DBG_INFO, F("hmRadio.h:checkPaketCrc")); - *len = (buf[0] >> 2); - if(*len > (MAX_RF_PAYLOAD_SIZE - 2)) - *len = MAX_RF_PAYLOAD_SIZE - 2; - for(uint8_t i = 1; i < (*len + 1); i++) { - buf[i-1] = (buf[i] << 1) | (buf[i+1] >> 7); - } - - uint8_t crc = ah::crc8(buf, *len-1); - bool valid = (crc == buf[*len-1]); - - return valid; - } - - bool switchRxCh(uint16_t addLoop = 0) { - if(!mNrf24.isChipConnected()) - return true; - mRxLoopCnt += addLoop; - if(mRxLoopCnt != 0) { - mRxLoopCnt--; - DISABLE_IRQ; - mNrf24.stopListening(); - mNrf24.setChannel(getRxNxtChannel()); - mNrf24.startListening(); - RESTORE_IRQ; - } - return (0 == mRxLoopCnt); // receive finished - } - - void dumpBuf(const char *info, uint8_t buf[], uint8_t len) { + void dumpBuf(uint8_t buf[], uint8_t len) { //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:dumpBuf")); - if(NULL != info) - DBGPRINT(String(info)); for(uint8_t i = 0; i < len; i++) { DHEX(buf[i]); DBGPRINT(" "); @@ -291,11 +216,6 @@ class HmRadio { DBGPRINTLN(""); } - bool isChipConnected(void) { - //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:isChipConnected")); - return mNrf24.isChipConnected(); - } - uint8_t getDataRate(void) { if(!mNrf24.isChipConnected()) return 3; // unkown @@ -306,7 +226,7 @@ class HmRadio { return mNrf24.isPVariant(); } - + std::queue mBufCtrl; uint32_t mSendCnt; uint32_t mRetransmits; @@ -314,78 +234,91 @@ class HmRadio { bool mSerialDebug; private: - void sendPacket(uint64_t invId, uint8_t buf[], uint8_t len, bool isRetransmit, bool clear=false) { + bool getReceived(void) { + bool tx_ok, tx_fail, rx_ready; + mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // resets the IRQ pin to HIGH + DBGPRINTLN("RX whatHappened Ch" + String(mRfChLst[mRxChIdx]) + " " + String(tx_ok) + String(tx_fail) + String(rx_ready)); + + bool isLastPackage = false; + while(mNrf24.available()) { + uint8_t len; + len = mNrf24.getDynamicPayloadSize(); // if payload size > 32, corrupt payload has been flushed + if (len > 0) { + packet_t p; + p.ch = mRfChLst[mRxChIdx]; + p.len = len; + mNrf24.read(p.packet, len); + mBufCtrl.push(p); + if (p.packet[0] == (TX_REQ_INFO + ALL_FRAMES)) // response from get information command + isLastPackage = (p.packet[9] > 0x81); // > 0x81 indicates last packet received + else if (p.packet[0] != 0x00) // ignore fragment number zero + isLastPackage = true; // response from dev control command + yield(); + } + } + return isLastPackage; + } + + void switchRxCh() { + mNrf24.stopListening(); + // get next channel index + if(++mRxChIdx >= RF_CHANNELS) + mRxChIdx = 0; + mNrf24.setChannel(mRfChLst[mRxChIdx]); + mNrf24.startListening(); + } + + void initPacket(uint64_t invId, uint8_t mid, uint8_t pid) { + DPRINTLN(DBG_VERBOSE, F("initPacket, mid: ") + String(mid, HEX) + F(" pid: ") + String(pid, HEX)); + memset(mTxBuf, 0, MAX_RF_PAYLOAD_SIZE); + mTxBuf[0] = mid; // message id + CP_U32_BigEndian(&mTxBuf[1], (invId >> 8)); + CP_U32_BigEndian(&mTxBuf[5], (DTU_RADIO_ID >> 8)); + mTxBuf[9] = pid; + } + + void sendPacket(uint64_t invId, uint8_t len, bool isRetransmit, bool clear=false) { //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendPacket")); //DPRINTLN(DBG_VERBOSE, "sent packet: #" + String(mSendCnt)); - //dumpBuf("SEN ", buf, len); + + // append crc's + if (len > 10) { + // crc control data + uint16_t crc = ah::crc16(&mTxBuf[10], len - 10); + mTxBuf[len++] = (crc >> 8) & 0xff; + mTxBuf[len++] = (crc ) & 0xff; + } + // crc over all + mTxBuf[len++] = ah::crc8(mTxBuf, len); + if(mSerialDebug) { DPRINT(DBG_INFO, "TX " + String(len) + "B Ch" + String(mRfChLst[mTxChIdx]) + " | "); - dumpBuf(NULL, buf, len); + dumpBuf(mTxBuf, len); } - DISABLE_IRQ; - mNrf24.stopListening(); - - if(clear) - mRxLoopCnt = RF_LOOP_CNT; - mNrf24.setChannel(mRfChLst[mTxChIdx]); - mTxCh = getTxNxtChannel(); // switch channel for next packet - mNrf24.openWritingPipe(invId); // TODO: deprecated - mNrf24.setCRCLength(RF24_CRC_16); - mNrf24.enableDynamicPayloads(); - mNrf24.setAutoAck(true); - mNrf24.setRetries(3, 15); // 3*250us and 15 loops -> 11.25ms - mNrf24.write(buf, len); + mNrf24.openWritingPipe(reinterpret_cast(&invId)); + mNrf24.startWrite(mTxBuf, len, false); // false = request ACK response - // Try to avoid zero payload acks (has no effect) - mNrf24.openWritingPipe(DUMMY_RADIO_ID); // TODO: why dummy radio id?, deprecated - mRxChIdx = 0; - mNrf24.setChannel(mRfChLst[mRxChIdx]); - mNrf24.setAutoAck(false); - mNrf24.setRetries(0, 0); - mNrf24.disableDynamicPayloads(); - mNrf24.setCRCLength(RF24_CRC_DISABLED); - mNrf24.startListening(); + // switch TX channel for next packet + if(++mTxChIdx >= RF_CHANNELS) + mTxChIdx = 0; - RESTORE_IRQ; if(isRetransmit) mRetransmits++; else mSendCnt++; } - uint8_t getTxNxtChannel(void) { - - if(++mTxChIdx >= RF_CHANNELS) - mTxChIdx = 0; - return mRfChLst[mTxChIdx]; - } - - uint8_t getRxNxtChannel(void) { - - if(++mRxChIdx >= RF_CHANNELS) - mRxChIdx = 0; - return mRfChLst[mRxChIdx]; - } - uint64_t DTU_RADIO_ID; - uint8_t mTxCh; - uint8_t mTxChIdx; - uint8_t mRfChLst[RF_CHANNELS]; + uint8_t mTxChIdx; uint8_t mRxChIdx; - uint16_t mRxLoopCnt; RF24 mNrf24; - BUFFER *mBufCtrl; uint8_t mTxBuf[MAX_RF_PAYLOAD_SIZE]; - - DevControlCmdType DevControlCmd; - - volatile bool mIrqRcvd; }; #endif /*__RADIO_H__*/ From 9638414ce29abcf80d51b6cb95c7cd1708ad8867 Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:44:19 +0100 Subject: [PATCH 13/14] Update hmSystem.h --- src/hm/hmSystem.h | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/hm/hmSystem.h b/src/hm/hmSystem.h index 85976cbd..58099d40 100644 --- a/src/hm/hmSystem.h +++ b/src/hm/hmSystem.h @@ -8,19 +8,11 @@ #include "hmInverter.h" #include "hmRadio.h" -#include "CircularBuffer.h" -typedef CircularBuffer BufferType; -typedef HmRadio RadioType; - -template > +template > class HmSystem { public: - typedef RADIO RadioType; - RadioType Radio; - typedef BUFFER BufferType; - BufferType BufCtrl; - //DevControlCmdType DevControlCmd; + HmRadio<> Radio; HmSystem() { mNumInv = 0; @@ -30,11 +22,11 @@ class HmSystem { } void setup() { - Radio.setup(&BufCtrl); + Radio.setup(); } void setup(uint8_t ampPwr, uint8_t irqPin, uint8_t cePin, uint8_t csPin) { - Radio.setup(&BufCtrl, ampPwr, irqPin, cePin, csPin); + Radio.setup(ampPwr, irqPin, cePin, csPin); } void addInverters(cfgInst_t *config) { From c241f356a0549cd2d9514ad07c2b843f02ff3c85 Mon Sep 17 00:00:00 2001 From: beegee3 <119520001+beegee3@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:47:29 +0100 Subject: [PATCH 14/14] Update payload.h --- src/hm/payload.h | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/hm/payload.h b/src/hm/payload.h index 7e66eb76..72166390 100644 --- a/src/hm/payload.h +++ b/src/hm/payload.h @@ -46,7 +46,8 @@ class Payload { reset(i); } mSerialDebug = false; - mHighPrioIv = NULL; + mHighPrioIv = NULL; + mCbAlarm = NULL; } void enableSerialDebug(bool enable) { @@ -117,7 +118,7 @@ class Payload { } } - void add(packet_t *p, uint8_t len) { + void add(packet_t *p) { Inverter<> *iv = mSys->findInverter(&p->packet[1]); if(NULL == iv) @@ -132,8 +133,8 @@ class Payload { } else { DPRINTLN(DBG_DEBUG, "PID: 0x" + String(*pid, HEX)); if ((*pid & 0x7F) < MAX_PAYLOAD_ENTRIES) { - memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->packet[10], len - 11); - mPayload[iv->id].len[(*pid & 0x7F) - 1] = len - 11; + memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->packet[10], p->len - 11); + mPayload[iv->id].len[(*pid & 0x7F) - 1] = p->len - 11; mPayload[iv->id].gotFragment = true; } @@ -194,21 +195,24 @@ class Payload { if (mPayload[iv->id].retransmits < mMaxRetrans) { mPayload[iv->id].retransmits++; if(false == mPayload[iv->id].gotFragment) { + /* DPRINTLN(DBG_WARN, F("nothing received: Request Complete Retransmit")); mPayload[iv->id].txCmd = iv->getQueuedCmd(); DPRINTLN(DBG_INFO, F("(#") + String(iv->id) + F(") sendTimePacket 0x") + String(mPayload[iv->id].txCmd, HEX)); mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].txCmd, mPayload[iv->id].ts, iv->alarmMesIndex, true); + */ + DPRINTLN(DBG_WARN, F("(#") + String(iv->id) + F(") nothing received")); + mPayload[iv->id].retransmits = mMaxRetrans; } else { for (uint8_t i = 0; i < (mPayload[iv->id].maxPackId - 1); i++) { if (mPayload[iv->id].len[i] == 0) { DPRINTLN(DBG_WARN, F("Frame ") + String(i + 1) + F(" missing: Request Retransmit")); - mSys->Radio.sendCmdPacket(iv->radioId.u64, TX_REQ_INFO, (SINGLE_FRAME + i), true, true); + mSys->Radio.sendCmdPacket(iv->radioId.u64, TX_REQ_INFO, (SINGLE_FRAME + i), true); break; // only request retransmit one frame per loop } yield(); } } - mSys->Radio.switchRxCh(100); } } } @@ -241,13 +245,13 @@ class Payload { if (mSerialDebug) { DPRINT(DBG_INFO, F("Payload (") + String(payloadLen) + "): "); - mSys->Radio.dumpBuf(NULL, payload, payloadLen); + mSys->Radio.dumpBuf(payload, payloadLen); } if (NULL == rec) { DPRINTLN(DBG_ERROR, F("record is NULL!")); } else if ((rec->pyldLen == payloadLen) || (0 == rec->pyldLen)) { - if (mPayload[iv->id].txId == (TX_REQ_INFO + 0x80)) + if (mPayload[iv->id].txId == (TX_REQ_INFO + ALL_FRAMES)) mStat->rxSuccess++; rec->ts = mPayload[iv->id].ts; @@ -266,7 +270,8 @@ class Payload { code = iv->parseAlarmLog(i++, payload, payloadLen, &start, &end); if(0 == code) break; - (mCbAlarm)(code, start, end); + if (NULL != mCbAlarm) + (mCbAlarm)(code, start, end); yield(); } } @@ -278,9 +283,7 @@ class Payload { iv->setQueuedCmdFinished(); } } - yield(); - } } @@ -290,7 +293,8 @@ class Payload { } void notify(uint16_t code, uint32_t start, uint32_t endTime) { - (mCbAlarm)(code, start, endTime); + if (NULL != mCbAlarm) + (mCbAlarm)(code, start, endTime); } bool build(uint8_t id, bool *complete) {