ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
somfy_rts_protocol.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
4
5namespace esphome::remote_base {
6
7// Timings from https://pushstack.wordpress.com/somfy-rts-protocol/
8static constexpr uint8_t FRAME_SIZE_IN_BYTES = 7;
9static constexpr uint16_t HALF_BIT_US = 604;
10
11static constexpr uint16_t WAKEUP_HIGH_US = 9415;
12static constexpr uint32_t WAKEUP_LOW_US = 89565;
13
14static constexpr uint16_t HW_SYNC_US = 2560;
15
16static constexpr uint16_t SW_SYNC_HIGH_US = 4550;
17static constexpr uint16_t SW_SYNC_LOW_US = HALF_BIT_US;
18
19// Wakeup, two hardware syncs and a software sync (8), two entries per bit (112)
20static constexpr uint32_t FRAME_ENTRIES = 8 + FRAME_SIZE_IN_BYTES * 8 * 2;
21
23 dst->reserve(FRAME_ENTRIES);
25 dst->item(WAKEUP_HIGH_US, WAKEUP_LOW_US);
26 dst->item(HW_SYNC_US, HW_SYNC_US);
27 dst->item(HW_SYNC_US, HW_SYNC_US);
28 dst->item(SW_SYNC_HIGH_US, SW_SYNC_LOW_US);
29
30 uint8_t frame[FRAME_SIZE_IN_BYTES];
31 // Some non standard remotes put the control nibble in the key byte and send 0xF in the control nibble
32 if (data.control_in_key) {
33 frame[0] = (data.key & 0xF0) | (data.control & 0x0F);
34 frame[1] = 0xF0;
35 } else {
36 frame[0] = data.key;
37 frame[1] = (data.control & 0x0F) << 4;
38 }
39 put_be16(frame + 2, data.rolling_code);
40 put_be24(frame + 4, data.address);
41
42 uint8_t crc = 0;
43 for (uint8_t data_byte : frame) {
44 crc ^= (data_byte >> 4) ^ (data_byte & 0xF);
45 }
46 frame[1] |= (crc & 0x0F);
47
48 for (uint8_t i = 1; i < FRAME_SIZE_IN_BYTES; i++) {
49 frame[i] ^= frame[i - 1];
50 }
51
52 // Manchester, MSB first: a 1 is space then mark, a 0 is mark then space. Frames repeat with
53 // the 30 ms gap through the action's repeat option, not inside the frame.
54 for (uint8_t data_byte : frame) {
55 for (int8_t y = 7; y >= 0; y--) {
56 if (data_byte & (1 << y)) {
57 dst->space(HALF_BIT_US);
58 dst->mark(HALF_BIT_US);
59 } else {
60 dst->mark(HALF_BIT_US);
61 dst->space(HALF_BIT_US);
62 }
63 }
64 }
65}
66
67} // namespace esphome::remote_base
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:87
void item(uint32_t mark, uint32_t space)
Definition remote_base.h:66
void encode(RemoteTransmitData *dst, const SomfyRtsData &data)
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1296
constexpr uint8_t * put_be24(uint8_t *dst, uint32_t value)
Definition helpers.h:1301
static void uint32_t
uint16_t y
Definition tt21100.cpp:6