Add Roomba IR protocol (#4595)

Co-authored-by: Richard Forro <r@f.rf>
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
rforro 2024-04-24 05:21:08 +02:00 committed by GitHub
parent a7079f8fba
commit e2b0d561bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 130 additions and 0 deletions

View file

@ -881,6 +881,45 @@ async def pronto_action(var, config, args):
cg.add(var.set_data(template_)) cg.add(var.set_data(template_))
# Roomba
(
RoombaData,
RoombaBinarySensor,
RoombaTrigger,
RoombaAction,
RoombaDumper,
) = declare_protocol("Roomba")
ROOMBA_SCHEMA = cv.Schema({cv.Required(CONF_DATA): cv.hex_uint8_t})
@register_binary_sensor("roomba", RoombaBinarySensor, ROOMBA_SCHEMA)
def roomba_binary_sensor(var, config):
cg.add(
var.set_data(
cg.StructInitializer(
RoombaData,
("data", config[CONF_DATA]),
)
)
)
@register_trigger("roomba", RoombaTrigger, RoombaData)
def roomba_trigger(var, config):
pass
@register_dumper("roomba", RoombaDumper)
def roomba_dumper(var, config):
pass
@register_action("roomba", RoombaAction, ROOMBA_SCHEMA)
async def roomba_action(var, config, args):
template_ = await cg.templatable(config[CONF_DATA], args, cg.uint8)
cg.add(var.set_data(template_))
# Sony # Sony
SonyData, SonyBinarySensor, SonyTrigger, SonyAction, SonyDumper = declare_protocol( SonyData, SonyBinarySensor, SonyTrigger, SonyAction, SonyDumper = declare_protocol(
"Sony" "Sony"

View file

@ -0,0 +1,56 @@
#include "roomba_protocol.h"
#include "esphome/core/log.h"
namespace esphome {
namespace remote_base {
static const char *const TAG = "remote.roomba";
static const uint8_t NBITS = 8;
static const uint32_t BIT_ONE_HIGH_US = 3000;
static const uint32_t BIT_ONE_LOW_US = 1000;
static const uint32_t BIT_ZERO_HIGH_US = BIT_ONE_LOW_US;
static const uint32_t BIT_ZERO_LOW_US = BIT_ONE_HIGH_US;
void RoombaProtocol::encode(RemoteTransmitData *dst, const RoombaData &data) {
dst->set_carrier_frequency(38000);
dst->reserve(NBITS * 2u);
for (uint32_t mask = 1UL << (NBITS - 1); mask != 0; mask >>= 1) {
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<RoombaData> RoombaProtocol::decode(RemoteReceiveData src) {
RoombaData out{.data = 0};
for (uint8_t i = 0; i < (NBITS - 1); i++) {
out.data <<= 1UL;
if (src.expect_item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US)) {
out.data |= 1UL;
} else if (src.expect_item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US)) {
out.data |= 0UL;
} else {
return {};
}
}
// not possible to measure space on last bit, check only mark
out.data <<= 1UL;
if (src.expect_mark(BIT_ONE_HIGH_US)) {
out.data |= 1UL;
} else if (src.expect_mark(BIT_ZERO_HIGH_US)) {
out.data |= 0UL;
} else {
return {};
}
return out;
}
void RoombaProtocol::dump(const RoombaData &data) { ESP_LOGD(TAG, "Received Roomba: data=0x%02X", data.data); }
} // namespace remote_base
} // namespace esphome

View file

@ -0,0 +1,35 @@
#pragma once
#include "remote_base.h"
namespace esphome {
namespace remote_base {
struct RoombaData {
uint8_t data;
bool operator==(const RoombaData &rhs) const { return data == rhs.data; }
};
class RoombaProtocol : public RemoteProtocol<RoombaData> {
public:
void encode(RemoteTransmitData *dst, const RoombaData &data) override;
optional<RoombaData> decode(RemoteReceiveData src) override;
void dump(const RoombaData &data) override;
};
DECLARE_REMOTE_PROTOCOL(Roomba)
template<typename... Ts> class RoombaAction : public RemoteTransmitterActionBase<Ts...> {
public:
TEMPLATABLE_VALUE(uint8_t, data)
void encode(RemoteTransmitData *dst, Ts... x) override {
RoombaData data{};
data.data = this->data_.value(x...);
RoombaProtocol().encode(dst, data);
}
};
} // namespace remote_base
} // namespace esphome