From 65c8db72fbae7f6812303d2cd106befb3a7dd500 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 26 Feb 2021 17:17:00 +0300 Subject: [PATCH] lights: fix rtcmem implementation shifts ref. https://github.com/xoseperez/espurna/issues/2422#issuecomment-786171934 stop including mireds into the brightness when unpacking uint64 value clamp brightness via the helper function --- code/espurna/light.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/code/espurna/light.cpp b/code/espurna/light.cpp index e2ec0a06..cf359708 100644 --- a/code/espurna/light.cpp +++ b/code/espurna/light.cpp @@ -1041,7 +1041,7 @@ struct LightRtcmem { // - aabbccddee are channels (from 0 to 5 respectively) explicit LightRtcmem(uint64_t value) { _mireds = (value >> (8ull * 6ull)) & 0xffffull; - _brightness = (value >> (8ull * 5ull)); + _brightness = (value >> (8ull * 5ull)) & 0xffull; _channels[4] = static_cast((value >> (8ull * 4ull))); _channels[3] = static_cast((value >> (8ull * 3ull))); @@ -1065,8 +1065,8 @@ struct LightRtcmem { {} uint64_t serialize() const { - return (static_cast(_mireds) << (8ull * 6ull)) - | (static_cast(_brightness) << (8ull * 5ull)) + return ((static_cast(_mireds) & 0xffffull) << (8ull * 6ull)) + | ((static_cast(_brightness) & 0xffull) << (8ull * 5ull)) | (static_cast(_channels[4]) << (8ull * 4ull)) | (static_cast(_channels[3]) << (8ull * 3ull)) | (static_cast(_channels[2]) << (8ull * 2ull)) @@ -1125,8 +1125,8 @@ void _lightRestoreRtcmem() { _light_channels[channel].inputValue = channels[channel]; } - _light_brightness = light.brightness(); - _light_mireds = light.mireds(); + _light_mireds = light.mireds(); // channels are already set + lightBrightness(light.brightness()); } void _lightSaveSettings() { @@ -1150,8 +1150,8 @@ void _lightRestoreSettings() { _light_channels[channel].inputValue = value; } - _light_brightness = getSetting("brightness", Light::BrightnessMax); _light_mireds = getSetting("mireds", _light_mireds); + lightBrightness(getSetting("brightness", Light::BrightnessMax)); } bool _lightParsePayload(const char* payload) {