ESPSomfy-RTS/src/SomfyFrame.h
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

72 lines
1.9 KiB
C

#ifndef SOMFY_FRAME_H
#define SOMFY_FRAME_H
#include <Arduino.h>
// The RTS frame codec and command names. Kept free of the radio, storage and
// network so it can be unit tested on the PC (pio test -e native).
enum class radio_proto : byte { // Ordinal byte 0-255
RTS = 0x00,
RTW = 0x01,
RTV = 0x02,
GP_Relay = 0x08,
GP_Remote = 0x09
};
enum class somfy_commands : byte {
Unknown0 = 0x0,
My = 0x1,
Up = 0x2,
MyUp = 0x3,
Down = 0x4,
MyDown = 0x5,
UpDown = 0x6,
MyUpDown = 0x7,
Prog = 0x8,
SunFlag = 0x9,
Flag = 0xA,
StepDown = 0xB,
Toggle = 0xC,
UnknownD = 0xD,
Sensor = 0xE,
RTWProto = 0xF, // RTW Protocol
// Command extensions for 80 bit frames
StepUp = 0x8B,
Favorite = 0xC1,
Stop = 0xF1
};
String translateSomfyCommand(const somfy_commands cmd);
somfy_commands translateSomfyCommand(const String& string);
// The command with up and down swapped, for motors wired the other way round.
somfy_commands flipCommand(somfy_commands cmd);
struct somfy_rx_t;
struct somfy_frame_t {
bool valid = false;
bool processed = false;
bool synonym = false;
radio_proto proto = radio_proto::RTS;
int rssi = 0;
byte lqi = 0x0;
somfy_commands cmd;
uint32_t remoteAddress = 0;
uint16_t rollingCode = 0;
uint8_t encKey = 0xA7;
uint8_t checksum = 0;
uint8_t hwsync = 0;
uint8_t repeats = 0;
uint32_t await = 0;
uint8_t bitLength = 56;
uint16_t pulseCount = 0;
uint8_t stepSize = 0;
uint8_t rawCmd = 0;
void print();
void encode80BitFrame(byte *frame, uint8_t repeat);
byte calc80Checksum(byte b0, byte b1, byte b2);
byte encode80Byte7(byte start, uint8_t repeat);
void encodeFrame(byte *frame);
void decodeFrame(byte* frame);
void decodeFrame(somfy_rx_t *rx);
bool isRepeat(somfy_frame_t &f);
bool isSynonym(somfy_frame_t &f);
void copy(somfy_frame_t &f);
};
#endif