From 6efa3a52fe0599ef1670ae517aadbad692c6b430 Mon Sep 17 00:00:00 2001 From: Yurii Date: Sun, 5 Jan 2025 01:39:36 +0300 Subject: [PATCH] refactor: added OT bus reset; increased timings for changing OT status --- lib/CustomOpenTherm/CustomOpenTherm.h | 51 +++++++++++++++++---------- src/OpenThermTask.h | 10 +++--- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/lib/CustomOpenTherm/CustomOpenTherm.h b/lib/CustomOpenTherm/CustomOpenTherm.h index 4ae34d8..fb7b4f6 100644 --- a/lib/CustomOpenTherm/CustomOpenTherm.h +++ b/lib/CustomOpenTherm/CustomOpenTherm.h @@ -3,15 +3,17 @@ class CustomOpenTherm : public OpenTherm { public: - typedef std::function YieldCallback; + typedef std::function DelayCallback; typedef std::function BeforeSendRequestCallback; typedef std::function AfterSendRequestCallback; - CustomOpenTherm(int inPin = 4, int outPin = 5, bool isSlave = false) : OpenTherm(inPin, outPin, isSlave) {} + CustomOpenTherm(int inPin = 4, int outPin = 5, bool isSlave = false) : OpenTherm(inPin, outPin, isSlave) { + this->_outPin = outPin; + } ~CustomOpenTherm() {} - CustomOpenTherm* setYieldCallback(YieldCallback callback = nullptr) { - this->yieldCallback = callback; + CustomOpenTherm* setDelayCallback(DelayCallback callback = nullptr) { + this->delayCallback = callback; return this; } @@ -28,14 +30,28 @@ public: return this; } + void reset() { + if (this->status == OpenThermStatus::NOT_INITIALIZED) { + return; + } + + this->end(); + this->status = OpenThermStatus::NOT_INITIALIZED; + + digitalWrite(this->_outPin, LOW); + if (this->delayCallback) { + this->delayCallback(1000); + } + + this->begin(); + } + unsigned long sendRequest(unsigned long request, byte attempts = 5, byte _attempt = 0) { _attempt++; while (!this->isReady()) { - if (this->yieldCallback) { - this->yieldCallback(); - } else { - ::yield(); + if (this->delayCallback) { + this->delayCallback(150); } this->process(); @@ -51,17 +67,13 @@ public: _response = 0; } else { - while (true) { - this->process(); - - if (this->status == OpenThermStatus::READY || this->status == OpenThermStatus::DELAY) { - break; - } else if (this->yieldCallback) { - this->yieldCallback(); - } else { - ::yield(); + do { + if (this->delayCallback) { + this->delayCallback(150); } - } + + this->process(); + } while (this->status != OpenThermStatus::READY && this->status != OpenThermStatus::DELAY); _response = this->getLastResponse(); _responseStatus = this->getLastResponseStatus(); @@ -151,7 +163,8 @@ public: } protected: - YieldCallback yieldCallback; + DelayCallback delayCallback; BeforeSendRequestCallback beforeSendRequestCallback; AfterSendRequestCallback afterSendRequestCallback; + int _outPin; }; diff --git a/src/OpenThermTask.h b/src/OpenThermTask.h index c034a69..037f126 100644 --- a/src/OpenThermTask.h +++ b/src/OpenThermTask.h @@ -98,8 +98,8 @@ protected: } }); - this->instance->setYieldCallback([this]() { - this->delay(25); + this->instance->setDelayCallback([this](unsigned int time) { + this->delay(time); }); this->instance->begin(); @@ -203,12 +203,12 @@ protected: ); } - if (!vars.slave.connected && millis() - this->lastSuccessResponse < 1150) { + if (!vars.slave.connected && millis() - this->lastSuccessResponse < 1325) { Log.sinfoln(FPSTR(L_OT), F("Connected")); vars.slave.connected = true; - } else if (vars.slave.connected && millis() - this->lastSuccessResponse > 1150) { + } else if (vars.slave.connected && millis() - this->lastSuccessResponse > 1325) { Log.swarningln(FPSTR(L_OT), F("Disconnected")); // Mark sensors as disconnected @@ -248,6 +248,8 @@ protected: vars.slave.diag.active = false; vars.slave.diag.code = 0; + this->instance->reset(); + return; }