Better sync (#22)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Marius Muja 2023-06-26 12:41:31 -07:00 committed by GitHub
parent 12f3b8747a
commit 056b0ab8ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 27 deletions

View File

@ -21,7 +21,7 @@ namespace esphome {
namespace ratgdo { namespace ratgdo {
static const char* const TAG = "ratgdo"; static const char* const TAG = "ratgdo";
static const int SYNC_DELAY = 2000; static const int SYNC_DELAY = 1000;
// //
// MAX_CODES_WITHOUT_FLASH_WRITE is a bit of a guess // MAX_CODES_WITHOUT_FLASH_WRITE is a bit of a guess
// since we write the flash at most every every 5s // since we write the flash at most every every 5s
@ -32,8 +32,7 @@ namespace ratgdo {
// results in the rolling counter being behind what the GDO // results in the rolling counter being behind what the GDO
// expects. // expects.
// //
static const uint8_t MAX_CODES_WITHOUT_FLASH_WRITE = 5; static const uint8_t MAX_CODES_WITHOUT_FLASH_WRITE = 10;
static const uint32_t FLASH_WRITE_INTERVAL = 10000;
void IRAM_ATTR HOT RATGDOStore::isrObstruction(RATGDOStore* arg) void IRAM_ATTR HOT RATGDOStore::isrObstruction(RATGDOStore* arg)
{ {
@ -81,7 +80,7 @@ namespace ratgdo {
ESP_LOGV(TAG, "Syncing rolling code counter after reboot..."); ESP_LOGV(TAG, "Syncing rolling code counter after reboot...");
// many things happening at startup, use some delay for sync // many things happening at startup, use some delay for sync
set_timeout(SYNC_DELAY, std::bind(&RATGDOComponent::sync, this)); set_timeout(SYNC_DELAY, [=] { this->sync(); });
} }
void RATGDOComponent::loop() void RATGDOComponent::loop()
@ -343,9 +342,9 @@ namespace ratgdo {
sendRollingCodeChanged(); sendRollingCodeChanged();
} }
void RATGDOComponent::incrementRollingCodeCounter() void RATGDOComponent::incrementRollingCodeCounter(int delta)
{ {
this->rollingCodeCounter = (this->rollingCodeCounter + 1) & 0xfffffff; this->rollingCodeCounter = (this->rollingCodeCounter + delta) & 0xfffffff;
sendRollingCodeChanged(); sendRollingCodeChanged();
} }
@ -571,26 +570,24 @@ namespace ratgdo {
void RATGDOComponent::sync() void RATGDOComponent::sync()
{ {
if (this->rollingCodeCounter == 0) { // first time use // increment rolling code counter by some amount in case we crashed without writing to flash the latest value
this->rollingCodeCounter = 1; this->incrementRollingCodeCounter(MAX_CODES_WITHOUT_FLASH_WRITE);
// the opener only sends a reply when the rolling code > previous rolling code for a given remote id
// when used the first time there is no previous rolling code, so first command is ignored set_retry(
set_timeout(100, [=] { 300, 10, [=](auto r) {
transmit(command::GET_STATUS); if (this->doorState != DoorState::DOOR_STATE_UNKNOWN) { // have status
}); if (this->openings != 0) { // have openings
// send it twice since manual says it can take 3 button presses for door to open on first use return RetryResult::DONE;
set_timeout(200, [=] { } else {
transmit(command::GET_STATUS);
});
}
for (int i = 0; i <= MAX_CODES_WITHOUT_FLASH_WRITE; i++) {
set_timeout(300 + i * 100, [=] {
transmit(command::GET_STATUS);
});
}
set_timeout(400 + 100 * MAX_CODES_WITHOUT_FLASH_WRITE, [=] {
transmit(command::GET_OPENINGS); transmit(command::GET_OPENINGS);
}); return RetryResult::RETRY;
}
} else {
transmit(command::GET_STATUS);
return RetryResult::RETRY;
}
},
1.5f);
} }
void RATGDOComponent::openDoor() void RATGDOComponent::openDoor()

View File

@ -209,7 +209,7 @@ namespace ratgdo {
void printRollingCode(); void printRollingCode();
void getRollingCode(command::cmd command, uint32_t data, bool increment); void getRollingCode(command::cmd command, uint32_t data, bool increment);
uint16_t readRollingCode(); uint16_t readRollingCode();
void incrementRollingCodeCounter(); void incrementRollingCodeCounter(int delta = 1);
void sendRollingCodeChanged(); void sendRollingCodeChanged();
void setRollingCodeCounter(uint32_t counter); void setRollingCodeCounter(uint32_t counter);