refactor: move sun and wind automation into ShadeSensors

The sun and wind timers were spread over processFrame, checkMovement and
processInternalCommand. ShadeSensors now holds them and decides when the
weather extends or retracts a shade, when turning the sun flag on moves
it, and when wind blocks a remote Down. Each caller still sets the targets
itself, so no shade moves differently yet.

The timers are now 32-bit like millis(). As 64-bit values holding a 32-bit
millis() they fired at once after the rollover every 49.7 days. The debug
logs of each timer go: they were compiled out at CORE_DEBUG_LEVEL 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-13 21:12:34 +02:00
parent 0eb0e63631
commit 8276c1c3cd
6 changed files with 319 additions and 129 deletions

77
src/ShadeSensors.cpp Normal file
View file

@ -0,0 +1,77 @@
#include "ShadeSensors.h"
// Whether a timer started at start has run timeoutMs by now. The unsigned
// subtraction keeps working across the millis() rollover.
static bool due(uint32_t start, uint32_t now, uint32_t timeoutMs) {
return start != 0 && now - start >= timeoutMs;
}
void ShadeSensors::clear() { *this = ShadeSensors(); }
void ShadeSensors::onSensorFrame(bool wasSunny, bool wasWindy, bool sunny, bool windy, uint32_t now) {
if(sunny) {
this->noSunStart = 0;
this->noSunDone = true;
}
else {
this->sunStart = 0;
this->sunDone = true;
}
if(windy) {
this->noWindStart = 0;
this->noWindDone = true;
this->windLast = now;
}
else {
this->windStart = 0;
this->windDone = true;
}
if(sunny && !wasSunny) {
this->sunStart = now;
this->sunDone = false;
}
else if(!sunny && wasSunny) {
this->noSunStart = now;
this->noSunDone = false;
}
if(windy && !wasWindy) {
this->windStart = now;
this->windDone = false;
}
else if(!windy && wasWindy) {
this->noWindStart = now;
this->noWindDone = false;
}
}
SensorMove ShadeSensors::check(bool sunFlag, bool sunny, bool windy, uint32_t now) {
SensorMove move = SensorMove::none;
if(sunFlag) {
if(sunny && !windy) {
// Sunny and calm, so the shade should be extended.
if(this->noWindDone && !this->sunDone && due(this->sunStart, now, SOMFY_SUN_TIMEOUT)) {
this->sunDone = true;
move = SensorMove::extend;
}
if(!this->noWindDone && due(this->noWindStart, now, SOMFY_NO_WIND_TIMEOUT)) {
this->noWindDone = true;
move = SensorMove::extend;
}
}
if(!sunny && !this->noSunDone && due(this->noSunStart, now, SOMFY_NO_SUN_TIMEOUT)) {
this->noSunDone = true;
move = SensorMove::retract;
}
}
if(windy && !this->windDone && due(this->windStart, now, SOMFY_WIND_TIMEOUT)) {
this->windDone = true;
move = SensorMove::retract;
}
return move;
}
SensorMove ShadeSensors::onSunFlag(bool sunny, bool windy) const {
if(windy) return SensorMove::none;
if(sunny && this->sunDone) return SensorMove::extend;
if(!sunny && this->noSunDone) return SensorMove::retract;
return SensorMove::none;
}
bool ShadeSensors::windBlocksDown(uint32_t now) const {
return this->windLast != 0 && now - this->windLast < SOMFY_NO_WIND_REMOTE_TIMEOUT;
}

52
src/ShadeSensors.h Normal file
View file

@ -0,0 +1,52 @@
#ifndef SHADE_SENSORS_H
#define SHADE_SENSORS_H
#include <stdint.h>
// Sun and wind automation for a shade with a sun sensor. Kept free of the
// shade, radio and network so it can be unit tested on the PC
// (pio test -e native).
#define SECS_TO_MILLIS(x) ((x) * 1000)
#define MINS_TO_MILLIS(x) SECS_TO_MILLIS((x) * 60)
// How long the sensor must report a change before the shade follows it.
#define SOMFY_SUN_TIMEOUT MINS_TO_MILLIS(2)
#define SOMFY_NO_SUN_TIMEOUT MINS_TO_MILLIS(20)
#define SOMFY_WIND_TIMEOUT SECS_TO_MILLIS(2)
#define SOMFY_NO_WIND_TIMEOUT MINS_TO_MILLIS(12)
// A remote Down is ignored this long after the last windy sensor frame.
#define SOMFY_NO_WIND_REMOTE_TIMEOUT SECS_TO_MILLIS(30)
// What the weather tells a shade to do.
enum class SensorMove : uint8_t {
none,
extend, // To the favorite, or all the way down when there is none.
retract // All the way up.
};
// The sensor timers of one shade. Times are millis(); a start of 0 means the
// timer isn't running.
struct ShadeSensors {
uint32_t sunStart = 0;
uint32_t noSunStart = 0;
uint32_t windStart = 0;
uint32_t noWindStart = 0;
uint32_t windLast = 0; // Last sensor frame that reported wind.
bool sunDone = true;
bool noSunDone = true;
bool windDone = true;
bool noWindDone = true;
void clear();
// A sensor frame arrived. was* is what the shade knew before it, sunny and
// windy what the frame reports.
void onSensorFrame(bool wasSunny, bool wasWindy, bool sunny, bool windy, uint32_t now);
// Fires the timers that are due, each once. Sun only moves the shade while
// the sun flag is on; wind always does.
SensorMove check(bool sunFlag, bool sunny, bool windy, uint32_t now);
// Where turning the sun flag on sends the shade right away. Nothing while
// it's windy or while a timer is still running.
SensorMove onSunFlag(bool sunny, bool windy) const;
// Whether a remote Down is ignored because the sensor reported wind lately.
bool windBlocksDown(uint32_t now) const;
};
#endif

View file

@ -11,22 +11,13 @@
#define SOMFY_MAX_ROOMS 16
#define SOMFY_MAX_REPEATERS 7
#define SECS_TO_MILLIS(x) ((x) * 1000)
#define MINS_TO_MILLIS(x) SECS_TO_MILLIS((x) * 60)
#define SOMFY_SUN_TIMEOUT MINS_TO_MILLIS(2)
#define SOMFY_NO_SUN_TIMEOUT MINS_TO_MILLIS(20)
#define SOMFY_WIND_TIMEOUT SECS_TO_MILLIS(2)
#define SOMFY_NO_WIND_TIMEOUT MINS_TO_MILLIS(12)
#define SOMFY_NO_WIND_REMOTE_TIMEOUT SECS_TO_MILLIS(30)
#include "SomfyFrame.h"
#include "TxBuffer.h"
#include "RadioScheduler.h"
#include "ShadeTypes.h"
#include "ShadeMotion.h"
#include "ShadeSensors.h"
#include "Transceiver.h"
enum class somfy_flags_t : byte {
@ -127,15 +118,7 @@ class SomfyShade : public SomfyRemote {
uint8_t shadeId = 255;
uint64_t moveStart = 0;
uint64_t tiltStart = 0;
uint64_t noSunStart = 0;
uint64_t sunStart = 0;
uint64_t windStart = 0;
uint64_t windLast = 0;
uint64_t noWindStart = 0;
bool noSunDone = true;
bool sunDone = true;
bool windDone = true;
bool noWindDone = true;
ShadeSensors sensors;
float startPos = 0.0f;
float startTiltPos = 0.0f;
bool settingMyPos = false;

View file

@ -19,15 +19,7 @@ void SomfyShade::clear() {
this->setRemoteAddress(0);
this->moveStart = 0;
this->tiltStart = 0;
this->noSunStart = 0;
this->sunStart = 0;
this->windStart = 0;
this->windLast = 0;
this->noWindStart = 0;
this->noSunDone = true;
this->sunDone = true;
this->windDone = true;
this->noWindDone = true;
this->sensors.clear();
this->startPos = 0.0f;
this->startTiltPos = 0.0f;
this->settingMyPos = false;
@ -160,49 +152,16 @@ void SomfyShade::checkMovement() {
case TxHoldCheck::none:
break;
}
if (sunFlag) {
if (isSunny && !isWindy) { // It is sunny and there is no wind so we should be extended
if (this->noWindDone
&& !this->sunDone
&& this->sunStart
&& (curTime - this->sunStart) >= SOMFY_SUN_TIMEOUT)
{
this->p_target(this->myPos >= 0 ? this->myPos : 100.0f);
//this->target = this->myPos >= 0 ? this->myPos : 100.0f;
this->sunDone = true;
ESP_LOGD(TAG, "[%u] Sun -> done", this->shadeId);
}
if (!this->noWindDone
&& this->noWindStart
&& (curTime - this->noWindStart) >= SOMFY_NO_WIND_TIMEOUT)
{
this->p_target(this->myPos >= 0 ? this->myPos : 100.0f);
//this->target = this->myPos >= 0 ? this->myPos : 100.0f;
this->noWindDone = true;
ESP_LOGD(TAG, "[%u] No Wind -> done", this->shadeId);
}
}
if (!isSunny
&& !this->noSunDone
&& this->noSunStart
&& (curTime - this->noSunStart) >= SOMFY_NO_SUN_TIMEOUT)
{
switch(this->sensors.check(sunFlag, isSunny, isWindy, (uint32_t)curTime)) {
case SensorMove::extend:
this->p_target(this->myPos >= 0 ? this->myPos : 100.0f);
break;
case SensorMove::retract:
if(this->tiltType == tilt_types::tiltonly) this->p_tiltTarget(0.0f);
this->p_target(0.0f);
this->noSunDone = true;
ESP_LOGD(TAG, "[%u] No Sun -> done", this->shadeId);
}
}
if (isWindy
&& !this->windDone
&& this->windStart
&& (curTime - this->windStart) >= SOMFY_WIND_TIMEOUT)
{
if(this->tiltType == tilt_types::tiltonly) this->p_tiltTarget(0.0f);
this->p_target(0.0f);
this->windDone = true;
ESP_LOGD(TAG, "[%u] Wind -> done", this->shadeId);
break;
case SensorMove::none:
break;
}
if(!tilt_first && this->direction > 0) {
@ -616,51 +575,7 @@ void SomfyShade::processFrame(somfy_frame_t &frame, bool internal) {
this->flags &= ~(static_cast<uint8_t>(somfy_flags_t::DemoMode));
const bool isSunny = this->flags & static_cast<uint8_t>(somfy_flags_t::Sunny);
const bool isWindy = this->flags & static_cast<uint8_t>(somfy_flags_t::Windy);
if (isSunny)
{
this->noSunStart = 0;
this->noSunDone = true;
}
else
{
this->sunStart = 0;
this->sunDone = true;
}
if (isWindy)
{
this->noWindStart = 0;
this->noWindDone = true;
this->windLast = curTime;
}
else
{
this->windStart = 0;
this->windDone = true;
}
if (isSunny && !wasSunny)
{
this->sunStart = curTime;
this->sunDone = false;
ESP_LOGD(TAG, "[%u] Sun -> start", this->shadeId);
}
else if (!isSunny && wasSunny)
{
this->noSunStart = curTime;
this->noSunDone = false;
ESP_LOGD(TAG, "[%u] No Sun -> start", this->shadeId);
}
if (isWindy && !wasWindy)
{
this->windStart = curTime;
this->windDone = false;
ESP_LOGD(TAG, "[%u] Wind -> start", this->shadeId);
}
else if (!isWindy && wasWindy)
{
this->noWindStart = curTime;
this->noWindDone = false;
ESP_LOGD(TAG, "[%u] No Wind -> start", this->shadeId);
}
this->sensors.onSensorFrame(wasSunny, wasWindy, isSunny, isWindy, (uint32_t)curTime);
this->emitState();
somfy.updateGroupFlags();
}
@ -691,23 +606,24 @@ void SomfyShade::processFrame(somfy_frame_t &frame, bool internal) {
if(this->lastFrame.rollingCode & 0x8000) return; // Some sensors send bogus frames with a rollingCode >= 32768 that cause them to change the state.
{
const bool isWindy = this->flags & static_cast<uint8_t>(somfy_flags_t::Windy);
const bool isSunny = this->flags & static_cast<uint8_t>(somfy_flags_t::Sunny);
//this->flags |= static_cast<uint8_t>(somfy_flags_t::SunFlag);
this->p_sunFlag(true);
if (!isWindy)
{
const bool isSunny = this->flags & static_cast<uint8_t>(somfy_flags_t::Sunny);
if (isSunny && this->sunDone) {
switch(this->sensors.onSunFlag(isSunny, isWindy)) {
case SensorMove::extend:
if(this->tiltType == tilt_types::tiltonly)
this->p_tiltTarget(this->myTiltPos >= 0 ? this->myTiltPos : 100.0f);
else
this->p_target(this->myPos >= 0 ? this->myPos : 100.0f);
}
else if (!isSunny && this->noSunDone) {
break;
case SensorMove::retract:
if(this->tiltType == tilt_types::tiltonly)
this->p_tiltTarget(0.0f);
else
this->p_target(0.0f);
}
break;
case SensorMove::none:
break;
}
somfy.isDirty = true;
this->emitState();
@ -755,7 +671,7 @@ void SomfyShade::processFrame(somfy_frame_t &frame, bool internal) {
this->emitCommand(cmd, internal ? "internal" : "remote", frame.remoteAddress);
return;
}
if (!this->windLast || (curTime - this->windLast) >= SOMFY_NO_WIND_REMOTE_TIMEOUT) {
if (!this->sensors.windBlocksDown((uint32_t)curTime)) {
if(this->tiltType == tilt_types::tiltmotor || this->tiltType == tilt_types::euromode) {
// Wait another half seccond just in case we are potentially processing a tilt.
if(!internal) this->lastFrame.await = curTime + 500;
@ -967,7 +883,7 @@ void SomfyShade::processInternalCommand(somfy_commands cmd, uint8_t repeat) {
}
break;
case somfy_commands::Down:
if (!this->windLast || (curTime - this->windLast) >= SOMFY_NO_WIND_REMOTE_TIMEOUT) {
if (!this->sensors.windBlocksDown((uint32_t)curTime)) {
if(this->tiltType == tilt_types::tiltmotor) {
if(repeat >= TILT_REPEATS)
this->p_tiltTarget(100.0f);
@ -1088,15 +1004,18 @@ void SomfyShade::processInternalCommand(somfy_commands cmd, uint8_t repeat) {
case somfy_commands::SunFlag:
if(this->hasSunSensor()) {
const bool isWindy = this->flags & static_cast<uint8_t>(somfy_flags_t::Windy);
const bool isSunny = this->flags & static_cast<uint8_t>(somfy_flags_t::Sunny);
this->p_sunFlag(true);
//this->flags |= static_cast<uint8_t>(somfy_flags_t::SunFlag);
if (!isWindy)
{
const bool isSunny = this->flags & static_cast<uint8_t>(somfy_flags_t::Sunny);
if (isSunny && this->sunDone)
switch(this->sensors.onSunFlag(isSunny, isWindy)) {
case SensorMove::extend:
this->p_target(this->myPos >= 0 ? this->myPos : 100.0f);
else if (!isSunny && this->noSunDone)
break;
case SensorMove::retract:
this->p_target(0.0f);
break;
case SensorMove::none:
break;
}
somfy.isDirty = true;
this->emitState();