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>
This commit is contained in:
cjkas 2026-10-11 13:37:48 +02:00
parent 8881289b1d
commit 3a8736f4d4
6 changed files with 76 additions and 127 deletions

15
src/ShadeMotion.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef SHADE_MOTION_H
#define SHADE_MOTION_H
#include <Arduino.h>
// Shade position maths. Kept free of the shade state, radio and network so it
// can be unit tested on the PC (pio test -e native).
// Where a shade (or its tilt) is after travelling elapsedMs from startPos
// (0-100). dir > 0 moves toward 100 and dir < 0 toward 0. travelMs is the
// time a full travel takes in that direction, 0 meaning it gets there at once.
float travelPosition(float startPos, int8_t dir, uint32_t elapsedMs, int32_t travelMs);
// A position as reported to clients: -1 when unset, otherwise the whole
// percentage, counted from the other end when flipped.
int8_t displayPosition(float pos, bool flip);
#endif