mirror of
https://github.com/esphome/esphome.git
synced 2025-10-19 18:23:46 +01:00
[remote_base] Add Symphony IR protocol (encode/decode) with command_repeats support (#10777)
This commit is contained in:
@@ -1056,6 +1056,52 @@ async def sony_action(var, config, args):
|
||||
cg.add(var.set_nbits(template_))
|
||||
|
||||
|
||||
# Symphony
|
||||
SymphonyData, SymphonyBinarySensor, SymphonyTrigger, SymphonyAction, SymphonyDumper = (
|
||||
declare_protocol("Symphony")
|
||||
)
|
||||
SYMPHONY_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_DATA): cv.hex_uint32_t,
|
||||
cv.Required(CONF_NBITS): cv.int_range(min=1, max=32),
|
||||
cv.Optional(CONF_COMMAND_REPEATS, default=2): cv.uint8_t,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@register_binary_sensor("symphony", SymphonyBinarySensor, SYMPHONY_SCHEMA)
|
||||
def symphony_binary_sensor(var, config):
|
||||
cg.add(
|
||||
var.set_data(
|
||||
cg.StructInitializer(
|
||||
SymphonyData,
|
||||
("data", config[CONF_DATA]),
|
||||
("nbits", config[CONF_NBITS]),
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@register_trigger("symphony", SymphonyTrigger, SymphonyData)
|
||||
def symphony_trigger(var, config):
|
||||
pass
|
||||
|
||||
|
||||
@register_dumper("symphony", SymphonyDumper)
|
||||
def symphony_dumper(var, config):
|
||||
pass
|
||||
|
||||
|
||||
@register_action("symphony", SymphonyAction, SYMPHONY_SCHEMA)
|
||||
async def symphony_action(var, config, args):
|
||||
template_ = await cg.templatable(config[CONF_DATA], args, cg.uint32)
|
||||
cg.add(var.set_data(template_))
|
||||
template_ = await cg.templatable(config[CONF_NBITS], args, cg.uint32)
|
||||
cg.add(var.set_nbits(template_))
|
||||
template_ = await cg.templatable(config[CONF_COMMAND_REPEATS], args, cg.uint8)
|
||||
cg.add(var.set_repeats(template_))
|
||||
|
||||
|
||||
# Raw
|
||||
def validate_raw_alternating(value):
|
||||
assert isinstance(value, list)
|
||||
|
120
esphome/components/remote_base/symphony_protocol.cpp
Normal file
120
esphome/components/remote_base/symphony_protocol.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#include "symphony_protocol.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace remote_base {
|
||||
|
||||
static const char *const TAG = "remote.symphony";
|
||||
|
||||
// Reference implementation and timing details:
|
||||
// IRremoteESP8266 ir_Symphony.cpp
|
||||
// https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/ir_Symphony.cpp
|
||||
// The implementation below mirrors the constant bit-time mapping and
|
||||
// footer-gap handling used there.
|
||||
|
||||
// Symphony protocol timing specifications (tuned to handset captures)
|
||||
static const uint32_t BIT_ZERO_HIGH_US = 460; // short
|
||||
static const uint32_t BIT_ZERO_LOW_US = 1260; // long
|
||||
static const uint32_t BIT_ONE_HIGH_US = 1260; // long
|
||||
static const uint32_t BIT_ONE_LOW_US = 460; // short
|
||||
static const uint32_t CARRIER_FREQUENCY = 38000;
|
||||
|
||||
// IRremoteESP8266 reference: kSymphonyFooterGap = 4 * (mark + space)
|
||||
static const uint32_t FOOTER_GAP_US = 4 * (BIT_ZERO_HIGH_US + BIT_ZERO_LOW_US);
|
||||
// Typical inter-frame gap (~34.8 ms observed)
|
||||
static const uint32_t INTER_FRAME_GAP_US = 34760;
|
||||
|
||||
void SymphonyProtocol::encode(RemoteTransmitData *dst, const SymphonyData &data) {
|
||||
dst->set_carrier_frequency(CARRIER_FREQUENCY);
|
||||
ESP_LOGD(TAG, "Sending Symphony: data=0x%0*X nbits=%u repeats=%u", (data.nbits + 3) / 4, (uint32_t) data.data,
|
||||
data.nbits, data.repeats);
|
||||
// Each bit produces a mark+space (2 entries). We fold the inter-frame/footer gap
|
||||
// into the last bit's space of each frame to avoid over-length gaps.
|
||||
dst->reserve(data.nbits * 2u * data.repeats);
|
||||
|
||||
for (uint8_t repeats = 0; repeats < data.repeats; repeats++) {
|
||||
// Data bits (MSB first)
|
||||
for (uint32_t mask = 1UL << (data.nbits - 1); mask != 0; mask >>= 1) {
|
||||
const bool is_last_bit = (mask == 1);
|
||||
const bool is_last_frame = (repeats == (data.repeats - 1));
|
||||
if (is_last_bit) {
|
||||
// Emit last bit's mark; replace its space with the proper gap
|
||||
if (data.data & mask) {
|
||||
dst->mark(BIT_ONE_HIGH_US);
|
||||
} else {
|
||||
dst->mark(BIT_ZERO_HIGH_US);
|
||||
}
|
||||
dst->space(is_last_frame ? FOOTER_GAP_US : INTER_FRAME_GAP_US);
|
||||
} else {
|
||||
if (data.data & mask) {
|
||||
dst->item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US);
|
||||
} else {
|
||||
dst->item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
optional<SymphonyData> SymphonyProtocol::decode(RemoteReceiveData src) {
|
||||
auto is_valid_len = [](uint8_t nbits) -> bool { return nbits == 8 || nbits == 12 || nbits == 16; };
|
||||
|
||||
RemoteReceiveData s = src; // copy
|
||||
SymphonyData out{0, 0, 1};
|
||||
|
||||
for (; out.nbits < 32; out.nbits++) {
|
||||
if (s.expect_mark(BIT_ONE_HIGH_US)) {
|
||||
if (!s.expect_space(BIT_ONE_LOW_US)) {
|
||||
// Allow footer gap immediately after the last mark
|
||||
if (s.peek_space_at_least(FOOTER_GAP_US)) {
|
||||
uint8_t bits_with_this = out.nbits + 1;
|
||||
if (is_valid_len(bits_with_this)) {
|
||||
out.data = (out.data << 1UL) | 1UL;
|
||||
out.nbits = bits_with_this;
|
||||
return out;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
// Successfully consumed a '1' bit (mark + space)
|
||||
out.data = (out.data << 1UL) | 1UL;
|
||||
continue;
|
||||
} else if (s.expect_mark(BIT_ZERO_HIGH_US)) {
|
||||
if (!s.expect_space(BIT_ZERO_LOW_US)) {
|
||||
// Allow footer gap immediately after the last mark
|
||||
if (s.peek_space_at_least(FOOTER_GAP_US)) {
|
||||
uint8_t bits_with_this = out.nbits + 1;
|
||||
if (is_valid_len(bits_with_this)) {
|
||||
out.data = (out.data << 1UL) | 0UL;
|
||||
out.nbits = bits_with_this;
|
||||
return out;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
// Successfully consumed a '0' bit (mark + space)
|
||||
out.data = (out.data << 1UL) | 0UL;
|
||||
continue;
|
||||
} else {
|
||||
// Completed a valid-length frame followed by a footer gap
|
||||
if (is_valid_len(out.nbits) && s.peek_space_at_least(FOOTER_GAP_US)) {
|
||||
return out;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if (is_valid_len(out.nbits) && s.peek_space_at_least(FOOTER_GAP_US)) {
|
||||
return out;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void SymphonyProtocol::dump(const SymphonyData &data) {
|
||||
const int32_t hex_width = (data.nbits + 3) / 4; // pad to nibble width
|
||||
ESP_LOGI(TAG, "Received Symphony: data=0x%0*X, nbits=%d", hex_width, (uint32_t) data.data, data.nbits);
|
||||
}
|
||||
|
||||
} // namespace remote_base
|
||||
} // namespace esphome
|
44
esphome/components/remote_base/symphony_protocol.h
Normal file
44
esphome/components/remote_base/symphony_protocol.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "remote_base.h"
|
||||
|
||||
#include <cinttypes>
|
||||
|
||||
namespace esphome {
|
||||
namespace remote_base {
|
||||
|
||||
struct SymphonyData {
|
||||
uint32_t data;
|
||||
uint8_t nbits;
|
||||
uint8_t repeats{1};
|
||||
|
||||
bool operator==(const SymphonyData &rhs) const { return data == rhs.data && nbits == rhs.nbits; }
|
||||
};
|
||||
|
||||
class SymphonyProtocol : public RemoteProtocol<SymphonyData> {
|
||||
public:
|
||||
void encode(RemoteTransmitData *dst, const SymphonyData &data) override;
|
||||
optional<SymphonyData> decode(RemoteReceiveData src) override;
|
||||
void dump(const SymphonyData &data) override;
|
||||
};
|
||||
|
||||
DECLARE_REMOTE_PROTOCOL(Symphony)
|
||||
|
||||
template<typename... Ts> class SymphonyAction : public RemoteTransmitterActionBase<Ts...> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint32_t, data)
|
||||
TEMPLATABLE_VALUE(uint8_t, nbits)
|
||||
TEMPLATABLE_VALUE(uint8_t, repeats)
|
||||
|
||||
void encode(RemoteTransmitData *dst, Ts... x) override {
|
||||
SymphonyData data{};
|
||||
data.data = this->data_.value(x...);
|
||||
data.nbits = this->nbits_.value(x...);
|
||||
data.repeats = this->repeats_.value(x...);
|
||||
SymphonyProtocol().encode(dst, data);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace remote_base
|
||||
} // namespace esphome
|
@@ -143,6 +143,11 @@ on_sony:
|
||||
- logger.log:
|
||||
format: "on_sony: %lu %u"
|
||||
args: ["long(x.data)", "x.nbits"]
|
||||
on_symphony:
|
||||
then:
|
||||
- logger.log:
|
||||
format: "on_symphony: 0x%lX %u"
|
||||
args: ["long(x.data)", "x.nbits"]
|
||||
on_toshiba_ac:
|
||||
then:
|
||||
- logger.log:
|
||||
|
@@ -53,6 +53,12 @@ button:
|
||||
remote_transmitter.transmit_sony:
|
||||
data: 0xABCDEF
|
||||
nbits: 12
|
||||
- platform: template
|
||||
name: Symphony
|
||||
on_press:
|
||||
remote_transmitter.transmit_symphony:
|
||||
data: 0xE88
|
||||
nbits: 12
|
||||
- platform: template
|
||||
name: Panasonic
|
||||
on_press:
|
||||
|
Reference in New Issue
Block a user