refactor: added OT bus reset; increased timings for changing OT status

This commit is contained in:
Yurii
2025-01-05 01:39:36 +03:00
parent 7e31de6c71
commit 6efa3a52fe
2 changed files with 38 additions and 23 deletions

View File

@@ -3,15 +3,17 @@
class CustomOpenTherm : public OpenTherm { class CustomOpenTherm : public OpenTherm {
public: public:
typedef std::function<void()> YieldCallback; typedef std::function<void(unsigned int)> DelayCallback;
typedef std::function<void(unsigned long, byte)> BeforeSendRequestCallback; typedef std::function<void(unsigned long, byte)> BeforeSendRequestCallback;
typedef std::function<void(unsigned long, unsigned long, OpenThermResponseStatus, byte)> AfterSendRequestCallback; typedef std::function<void(unsigned long, unsigned long, OpenThermResponseStatus, byte)> 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() {}
CustomOpenTherm* setYieldCallback(YieldCallback callback = nullptr) { CustomOpenTherm* setDelayCallback(DelayCallback callback = nullptr) {
this->yieldCallback = callback; this->delayCallback = callback;
return this; return this;
} }
@@ -28,14 +30,28 @@ public:
return this; 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) { unsigned long sendRequest(unsigned long request, byte attempts = 5, byte _attempt = 0) {
_attempt++; _attempt++;
while (!this->isReady()) { while (!this->isReady()) {
if (this->yieldCallback) { if (this->delayCallback) {
this->yieldCallback(); this->delayCallback(150);
} else {
::yield();
} }
this->process(); this->process();
@@ -51,17 +67,13 @@ public:
_response = 0; _response = 0;
} else { } else {
while (true) { do {
this->process(); if (this->delayCallback) {
this->delayCallback(150);
if (this->status == OpenThermStatus::READY || this->status == OpenThermStatus::DELAY) {
break;
} else if (this->yieldCallback) {
this->yieldCallback();
} else {
::yield();
} }
}
this->process();
} while (this->status != OpenThermStatus::READY && this->status != OpenThermStatus::DELAY);
_response = this->getLastResponse(); _response = this->getLastResponse();
_responseStatus = this->getLastResponseStatus(); _responseStatus = this->getLastResponseStatus();
@@ -151,7 +163,8 @@ public:
} }
protected: protected:
YieldCallback yieldCallback; DelayCallback delayCallback;
BeforeSendRequestCallback beforeSendRequestCallback; BeforeSendRequestCallback beforeSendRequestCallback;
AfterSendRequestCallback afterSendRequestCallback; AfterSendRequestCallback afterSendRequestCallback;
int _outPin;
}; };

View File

@@ -98,8 +98,8 @@ protected:
} }
}); });
this->instance->setYieldCallback([this]() { this->instance->setDelayCallback([this](unsigned int time) {
this->delay(25); this->delay(time);
}); });
this->instance->begin(); 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")); Log.sinfoln(FPSTR(L_OT), F("Connected"));
vars.slave.connected = true; 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")); Log.swarningln(FPSTR(L_OT), F("Disconnected"));
// Mark sensors as disconnected // Mark sensors as disconnected
@@ -248,6 +248,8 @@ protected:
vars.slave.diag.active = false; vars.slave.diag.active = false;
vars.slave.diag.code = 0; vars.slave.diag.code = 0;
this->instance->reset();
return; return;
} }