diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 6af39b1..ab9ebfc 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -477,7 +477,10 @@ namespace ratgdo { { transmit(command); this->pref_.save(&this->rollingCodeCounter); - global_preferences->sync(); + if (!this->lastSyncedRollingCodeCounter || this->rollingCodeCounter - this->lastSyncedRollingCodeCounter > 2) { + this->lastSyncedRollingCodeCounter = this->rollingCodeCounter; + global_preferences->sync(); + } } void RATGDOComponent::register_child(RATGDOClient* obj) diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index 00a4552..0de83a9 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -82,9 +82,11 @@ namespace ratgdo { void loop() override; void dump_config() override; - uint32_t rollingCodeCounter; + uint32_t rollingCodeCounter{ 0}; + uint32_t lastSyncedRollingCodeCounter{ 0}; + uint16_t previousOpenings { 0 }; // number of times the door has been opened - uint16_t openings; // number of times the door has been opened + uint16_t openings{0}; // number of times the door has been opened uint8_t txRollingCode[CODE_LENGTH]; uint8_t rxRollingCode[CODE_LENGTH];