fix: read the rolling code from NVS only until it has been written

Splitting getNextRollingCode into peek and commit opened the ShadeCodes
namespace twice per command.  Once a code has been written for an
address lastRollingCode matches NVS, so peek uses it and only commit
opens NVS.  Changing the address drops back to reading NVS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-12 18:00:51 +02:00
parent a49fa5d953
commit 08d2cd7a0c
2 changed files with 9 additions and 2 deletions

View file

@ -3144,7 +3144,7 @@ bool SomfyRemote::toJSON(JsonObject &obj) {
return true; return true;
} }
*/ */
void SomfyRemote::setRemoteAddress(uint32_t address) { this->m_remoteAddress = address; snprintf(this->m_remotePrefId, sizeof(this->m_remotePrefId), "_%lu", (unsigned long)this->m_remoteAddress); } void SomfyRemote::setRemoteAddress(uint32_t address) { this->m_remoteAddress = address; this->m_rollingCodeSynced = false; snprintf(this->m_remotePrefId, sizeof(this->m_remotePrefId), "_%lu", (unsigned long)this->m_remoteAddress); }
uint32_t SomfyRemote::getRemoteAddress() { return this->m_remoteAddress; } uint32_t SomfyRemote::getRemoteAddress() { return this->m_remoteAddress; }
void SomfyShadeController::processFrame(somfy_frame_t &frame, bool internal) { void SomfyShadeController::processFrame(somfy_frame_t &frame, bool internal) {
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) { for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) {
@ -3681,8 +3681,10 @@ bool SomfyShadeController::deleteGroup(uint8_t groupId) {
bool SomfyShadeController::loadShadesFile(const char *filename) { return ShadeConfigFile::load(this, filename); } bool SomfyShadeController::loadShadesFile(const char *filename) { return ShadeConfigFile::load(this, filename); }
// The next rolling code is only read here. It is consumed by commitRollingCode // The next rolling code is only read here. It is consumed by commitRollingCode
// once the frame using it has been queued. // once the frame using it has been queued. NVS is only read until a code for
// this address has been written, after that lastRollingCode holds the same.
uint16_t SomfyRemote::peekNextRollingCode() { uint16_t SomfyRemote::peekNextRollingCode() {
if(this->m_rollingCodeSynced) return (uint16_t)(this->lastRollingCode + 1);
if(!pref.begin("ShadeCodes")) { if(!pref.begin("ShadeCodes")) {
ESP_LOGE(TAG, "peekNextRollingCode() pref.begin(ShadeCodes) FAILED"); ESP_LOGE(TAG, "peekNextRollingCode() pref.begin(ShadeCodes) FAILED");
} }
@ -3697,6 +3699,7 @@ void SomfyRemote::commitRollingCode(uint16_t code) {
pref.putUShort(this->m_remotePrefId, code); pref.putUShort(this->m_remotePrefId, code);
pref.end(); pref.end();
this->p_lastRollingCode(code); this->p_lastRollingCode(code);
this->m_rollingCodeSynced = true;
} }
uint16_t SomfyRemote::p_lastRollingCode(uint16_t code) { uint16_t SomfyRemote::p_lastRollingCode(uint16_t code) {
uint16_t old = this->lastRollingCode; uint16_t old = this->lastRollingCode;
@ -3709,6 +3712,7 @@ uint16_t SomfyRemote::setRollingCode(uint16_t code) {
pref.putUShort(this->m_remotePrefId, code); pref.putUShort(this->m_remotePrefId, code);
pref.end(); pref.end();
this->lastRollingCode = code; this->lastRollingCode = code;
this->m_rollingCodeSynced = true;
ESP_LOGD(TAG, "Setting Last Rolling code %d", this->lastRollingCode); ESP_LOGD(TAG, "Setting Last Rolling code %d", this->lastRollingCode);
} }
return code; return code;

View file

@ -198,6 +198,9 @@ class SomfyRemote {
protected: protected:
char m_remotePrefId[11] = ""; char m_remotePrefId[11] = "";
uint32_t m_remoteAddress = 0; uint32_t m_remoteAddress = 0;
// lastRollingCode is known to match NVS for this address, so the next code
// can be worked out without opening NVS.
bool m_rollingCodeSynced = false;
public: public:
radio_proto proto = radio_proto::RTS; radio_proto proto = radio_proto::RTS;
uint8_t gpioFlags = 0; uint8_t gpioFlags = 0;