34constexpr uint8_t MARKER = 0x87;
35constexpr uint16_t SYNC_MARK_US = 6400;
36constexpr uint32_t FRAME_GAP_US = 63650;
37constexpr uint8_t WIRE_SIZE = 5;
40 .one_space = 750, .zero_space = 1460, .one_mark = 1460, .zero_mark = 750, .space_first =
true};
44constexpr uint8_t PIN_Y = 0x08;
45constexpr uint8_t PINCODE_BITS = 14;
46constexpr uint8_t PINCODE_MAP[PINCODE_BITS] PROGMEM = {
47 PIN_Y | 7, PIN_Y | 6, 7, 6, PIN_Y | 5, 5, PIN_Y | 4, 4, PIN_Y | 3, 3, PIN_Y | 2, PIN_Y | 1, PIN_Y | 0, 2,
51void encode_pin_fields(uint16_t pincode, uint8_t channel, uint8_t &plain0, uint8_t &plain1) {
55 for (uint8_t i = 0; i < PINCODE_BITS; ++i) {
56 if (((pincode >> (PINCODE_BITS - 1 - i)) & 1U) == 0)
59 (entry & PIN_Y ?
y :
x) |= 1U << (entry & 0x07);
63 plain0 = plain1 -
x + 1;
67void build_wire(uint8_t wire[WIRE_SIZE], uint8_t seed, uint8_t plain0, uint8_t plain1) {
68 uint16_t sum = MARKER + seed;
71 uint8_t
key = seed + (sum >> 8);
77 wire[0] = plain0 +
key;
91 encode_pin_fields(data.code & 0x3FFF, data.channel & 0x03, plain0, plain1);
93 for (uint8_t frame = 0; frame < data.repeats; ++frame) {
94 uint8_t wire[WIRE_SIZE];
97 build_wire(wire, seed, plain0, plain1);
98 dst->
mark(SYNC_MARK_US);
99 dst->
item_bytes<CARDIN_TIMINGS>(wire, WIRE_SIZE);
100 dst->
space(FRAME_GAP_US);
105 while (
src.is_valid()) {
107 if (!
src.expect_mark(SYNC_MARK_US)) {
112 uint8_t wire[WIRE_SIZE];
113 if (!
src.expect_bytes<CARDIN_TIMINGS>(wire, WIRE_SIZE) ||
119 uint8_t seed = wire[2] - MARKER;
122 key += (seed + MARKER) >> 8;
124 uint8_t plain1 = wire[1] -
key;
125 key += (plain1 +
key) >> 8;
127 uint8_t plain0 = wire[0] -
key;
130 uint8_t
x = plain1 - plain0 + 1;
135 for (
const uint8_t &slot : PINCODE_MAP) {
137 code = (code << 1) | (((entry & PIN_Y ?
y :
x) >> (entry & 0x07)) & 1U);
142 .channel =
static_cast<uint8_t
>(
x & 0x03),
152 char code[PINCODE_BITS + 1];
153 for (uint8_t i = 0; i < PINCODE_BITS; ++i)
154 code[i] = (data.code >> (PINCODE_BITS - 1 - i)) & 1U ?
'1' :
'0';
155 code[PINCODE_BITS] =
'\0';
156 ESP_LOGI(TAG,
"Received Cardin S438: code=%s channel=%c seed=0x%02X", code,
"BDAC"[data.channel & 0x03], data.seed);
void dump(const CardinS438Data &data)
optional< CardinS438Data > decode(RemoteReceiveData src)
void encode(RemoteTransmitData *dst, const CardinS438Data &data)
void space(uint32_t length)
void mark(uint32_t length)
void item_bytes(const uint8_t *bytes, size_t len)
One item per bit of each byte.
ESPHOME_LOG_TAG(TAG, "remote.abbwelcome")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
constexpr uint16_t sum16_bytes(const uint8_t *data, size_t len, uint16_t sum=0)
Add up the bytes of data with size len, truncated to 16 bits.
uint8_t progmem_read_byte(const uint8_t *addr)
A bit is a mark (one_mark or zero_mark, both bit_mark unless set) and a space (one_space or zero_spac...