ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
denon_protocol.cpp
Go to the documentation of this file.
1#include "denon_protocol.h"
2#include "esphome/core/log.h"
3
4namespace esphome::remote_base {
5
6ESPHOME_LOG_TAG(TAG, "remote.denon");
7
8// Timings from Denon's published Pronto codes
9static constexpr uint32_t CARRIER_FREQUENCY = 38000;
10static constexpr uint32_t BIT_MARK_US = 263;
11static constexpr uint32_t BIT_ZERO_SPACE_US = 789;
12static constexpr uint32_t BIT_ONE_SPACE_US = 1841;
13static constexpr uint32_t FRAME_GAP_US = 43572;
14
15// Each frame is 15 bits sent least significant first: 5 address, 8 command, 2 trailing bits; then a stop mark
16static constexpr uint8_t FRAME_BITS = 15;
17static constexpr uint8_t ADDRESS_BITS = 5;
18static constexpr uint16_t ADDRESS_MASK = (1U << ADDRESS_BITS) - 1;
19static constexpr uint8_t TRAILER_SHIFT = 13;
20// The second frame inverts the command and the trailing bits
21static constexpr uint16_t SECOND_FRAME_MASK = 0x7FE0;
22
23static void encode_frame(RemoteTransmitData *dst, uint16_t frame) {
24 for (uint8_t i = 0; i < FRAME_BITS; i++) {
25 dst->item(BIT_MARK_US, (frame >> i) & 1 ? BIT_ONE_SPACE_US : BIT_ZERO_SPACE_US);
26 }
27 dst->mark(BIT_MARK_US);
28}
29
31 dst->set_carrier_frequency(CARRIER_FREQUENCY);
32 dst->reserve(2 * (2 * FRAME_BITS + 1) + 1);
33 const uint16_t frame = (data.address & ADDRESS_MASK) | (data.command << ADDRESS_BITS);
34 encode_frame(dst, frame);
35 dst->space(FRAME_GAP_US);
36 encode_frame(dst, frame ^ SECOND_FRAME_MASK);
37}
38
40 uint16_t frame = 0;
41 for (uint8_t i = 0; i < FRAME_BITS; i++) {
42 if (src.expect_item(BIT_MARK_US, BIT_ONE_SPACE_US)) {
43 frame |= 1U << i;
44 } else if (!src.expect_item(BIT_MARK_US, BIT_ZERO_SPACE_US)) {
45 return {};
46 }
47 }
48 // Only the first frame of a pair ends in 00; the inverted second frame ends in 11
49 if (!src.expect_mark(BIT_MARK_US) || (frame >> TRAILER_SHIFT) != 0)
50 return {};
51 return DenonData{
52 .address = static_cast<uint8_t>(frame & ADDRESS_MASK),
53 .command = static_cast<uint8_t>(frame >> ADDRESS_BITS),
54 };
55}
56
57void DenonProtocol::dump(const DenonData &data) {
58 ESP_LOGI(TAG, "Received Denon: address=%u, command=%u", data.address, data.command);
59}
60
61} // namespace esphome::remote_base
optional< DenonData > decode(RemoteReceiveData src)
void dump(const DenonData &data)
void encode(RemoteTransmitData *dst, const DenonData &data)
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
ESPHOME_LOG_TAG(TAG, "remote.abbwelcome")
const void * src
Definition hal.h:68
static void uint32_t
Denon (Sharp-format) 15-bit IR code: the Device Code and Function # from Denon's IR code tables.