mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-11-21 00:02:12 +01:00
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:
parent
0eb0e63631
commit
8276c1c3cd
6 changed files with 319 additions and 129 deletions
52
src/ShadeSensors.h
Normal file
52
src/ShadeSensors.h
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue