ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
magiquest_protocol.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4/* Based on protocol analysis from
5 * https://arduino-irremote.github.io/Arduino-IRremote/ir__MagiQuest_8hpp_source.html
6 */
7
8namespace esphome::remote_base {
9
10ESPHOME_LOG_TAG(TAG, "remote.magiquest");
11
12static constexpr uint32_t MAGIQUEST_UNIT = 288; // us
13// Half a unit is the widest window that still tells a one unit mark or space from a two unit one.
14static constexpr uint32_t MAGIQUEST_TOLERANCE = MAGIQUEST_UNIT / 2;
15static constexpr uint32_t MAGIQUEST_ONE_MARK = 2 * MAGIQUEST_UNIT;
16static constexpr uint32_t MAGIQUEST_ONE_SPACE = 2 * MAGIQUEST_UNIT;
17static constexpr uint32_t MAGIQUEST_ZERO_MARK = MAGIQUEST_UNIT;
18static constexpr uint32_t MAGIQUEST_ZERO_SPACE = 3 * MAGIQUEST_UNIT;
19static constexpr BitTimings MAGIQUEST_BIT_TIMINGS{.one_space = MAGIQUEST_ONE_SPACE,
20 .zero_space = MAGIQUEST_ZERO_SPACE,
22 .one_mark = MAGIQUEST_ONE_MARK,
23 .zero_mark = MAGIQUEST_ZERO_MARK};
24
25static bool checksum_is_valid(uint32_t wand_id, uint32_t magnitude_and_checksum) {
26 // The frame's six payload bytes, which start at the wand id's top bit, must sum to zero.
27 const uint32_t id_bytes = wand_id << 1;
28 uint8_t sum = 0;
29 for (uint8_t shift = 0; shift < 32; shift += 8) {
30 sum += (id_bytes >> shift) + (magnitude_and_checksum >> shift);
31 }
32 return sum == 0;
33}
34
36 // Still the old frame layout, which the decoder no longer reads; changing it would alter what existing configs send.
37 dst->reserve(101); // 2 start bits, 48 data bits, 1 stop bit
38 dst->set_carrier_frequency(38000);
39
40 // 2 start bits
41 dst->item(MAGIQUEST_ZERO_MARK, MAGIQUEST_ZERO_SPACE);
42 dst->item(MAGIQUEST_ZERO_MARK, MAGIQUEST_ZERO_SPACE);
43 const uint8_t bytes[6] = {static_cast<uint8_t>(data.wand_id >> 24), static_cast<uint8_t>(data.wand_id >> 16),
44 static_cast<uint8_t>(data.wand_id >> 8), static_cast<uint8_t>(data.wand_id),
45 static_cast<uint8_t>(data.magnitude >> 8), static_cast<uint8_t>(data.magnitude)};
46 dst->item_bytes<MAGIQUEST_BIT_TIMINGS>(bytes, sizeof(bytes));
47
48 dst->mark(MAGIQUEST_UNIT);
49}
51 src.set_tolerance(MAGIQUEST_TOLERANCE, TOLERANCE_MODE_TIME);
52
53 MagiQuestData data{};
54
55 // 8 bit header, always zero
56 for (uint8_t i = 0; i < 8; i++) {
57 if (!src.expect_item(MAGIQUEST_ZERO_MARK, MAGIQUEST_ZERO_SPACE)) {
58 return {};
59 }
60 }
61
62 // 31 bit wand id
63 for (uint32_t mask = 1 << 30; mask; mask >>= 1) {
64 if (src.expect_item(MAGIQUEST_ONE_MARK, MAGIQUEST_ONE_SPACE)) {
65 data.wand_id |= mask;
66 } else if (!src.expect_item(MAGIQUEST_ZERO_MARK, MAGIQUEST_ZERO_SPACE)) {
67 return {};
68 }
69 }
70
71 // 9 bit magnitude, then 8 bit checksum
72 uint32_t magnitude_and_checksum = 0;
73 for (uint32_t mask = 1 << 16; mask > 1; mask >>= 1) {
74 if (src.expect_item(MAGIQUEST_ONE_MARK, MAGIQUEST_ONE_SPACE)) {
75 magnitude_and_checksum |= mask;
76 } else if (!src.expect_item(MAGIQUEST_ZERO_MARK, MAGIQUEST_ZERO_SPACE)) {
77 return {};
78 }
79 }
80 // The last bit has no trailing space. A wrong or missing mark here is caught by the checksum.
81 if (src.expect_mark(MAGIQUEST_ONE_MARK)) {
82 magnitude_and_checksum |= 1;
83 }
84
85 if (!checksum_is_valid(data.wand_id, magnitude_and_checksum)) {
86 return {};
87 }
88 data.magnitude = magnitude_and_checksum >> 8;
89 return data;
90}
92 ESP_LOGI(TAG, "Received MagiQuest: wand_id=0x%08" PRIX32 ", magnitude=%u", data.wand_id, data.magnitude);
93}
94
95} // namespace esphome::remote_base
optional< MagiQuestData > decode(RemoteReceiveData src)
void dump(const MagiQuestData &data)
void encode(RemoteTransmitData *dst, const MagiQuestData &data)
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:64
void item(uint32_t mark, uint32_t space)
Definition remote_base.h:43
void item_bytes(const uint8_t *bytes, size_t len)
One item per bit of each byte.
Definition remote_base.h:49
ESPHOME_LOG_TAG(TAG, "remote.abbwelcome")
const void * src
Definition hal.h:68
static void uint32_t
A bit is a mark (one_mark or zero_mark, both bit_mark unless set) and a space (one_space or zero_spac...
Definition remote_base.h:29