ESPSomfy-RTS/src/ShadeMotion.cpp
cjkas 3a8736f4d4 refactor: share the shade travel maths in ShadeMotion.cpp
checkMovement worked out the position four times, nearly copy-pasted,
for lift down, lift up, tilt down and tilt up.  All four now call
travelPosition(), which keeps the same arithmetic, including how the
elapsed time wraps with millis().  Tilt up with no tilt time still
stops the tilt direction in the caller.

transformPosition and transformCommand keep their behaviour through
the new displayPosition() and flipCommand() helpers.  The native env
builds ShadeMotion.cpp so all of this can be unit tested on the PC.
No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 13:37:48 +02:00

31 lines
1.2 KiB
C++

#include <algorithm>
#include "ShadeMotion.h"
float travelPosition(float startPos, int8_t dir, uint32_t elapsedMs, int32_t travelMs) {
// Work in time travelled from the end we are moving away from: the start
// position is turned into the time it would take to get there, the elapsed
// time is added and the total is turned back into a position. The addition
// wraps like the uint32 millis() so it keeps working across a rollover.
if(dir > 0) {
if(travelMs == 0) return 100.0f;
int32_t ms = (int32_t)floor((startPos / 100) * travelMs);
ms += elapsedMs;
ms = std::min(travelMs, ms);
if(ms >= travelMs) return 100.0f;
return std::min(std::max(0.0f, (float)ms / (float)travelMs), 1.0f) * 100;
}
if(dir < 0) {
if(travelMs == 0) return 0.0f;
int32_t ms = travelMs - (int32_t)floor((startPos / 100) * travelMs);
ms += elapsedMs;
ms = std::min(travelMs, ms);
if(ms >= travelMs) return 0.0f;
float pos = (1.0f - std::min(std::max(0.0f, (float)ms / (float)travelMs), 1.0f)) * 100;
return pos <= 0.0f ? 0.0f : pos;
}
return startPos;
}
int8_t displayPosition(float pos, bool flip) {
if(pos < 0) return -1;
return static_cast<int8_t>(flip && pos >= 0.00f ? floor(100.0f - pos) : floor(pos));
}