ESPHome 2025.5.0
Loading...
Searching...
No Matches
roomba_protocol.cpp
Go to the documentation of this file.
1#include "roomba_protocol.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace remote_base {
6
7static const char *const TAG = "remote.roomba";
8
9static const uint8_t NBITS = 8;
10static const uint32_t BIT_ONE_HIGH_US = 3000;
11static const uint32_t BIT_ONE_LOW_US = 1000;
12static const uint32_t BIT_ZERO_HIGH_US = BIT_ONE_LOW_US;
13static const uint32_t BIT_ZERO_LOW_US = BIT_ONE_HIGH_US;
14
16 dst->set_carrier_frequency(38000);
17 dst->reserve(NBITS * 2u);
18
19 for (uint32_t mask = 1UL << (NBITS - 1); mask != 0; mask >>= 1) {
20 if (data.data & mask) {
21 dst->item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US);
22 } else {
23 dst->item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US);
24 }
25 }
26}
28 RoombaData out{.data = 0};
29
30 for (uint8_t i = 0; i < (NBITS - 1); i++) {
31 out.data <<= 1UL;
32 if (src.expect_item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US)) {
33 out.data |= 1UL;
34 } else if (src.expect_item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US)) {
35 out.data |= 0UL;
36 } else {
37 return {};
38 }
39 }
40
41 // not possible to measure space on last bit, check only mark
42 out.data <<= 1UL;
43 if (src.expect_mark(BIT_ONE_HIGH_US)) {
44 out.data |= 1UL;
45 } else if (src.expect_mark(BIT_ZERO_HIGH_US)) {
46 out.data |= 0UL;
47 } else {
48 return {};
49 }
50
51 return out;
52}
53void RoombaProtocol::dump(const RoombaData &data) { ESP_LOGD(TAG, "Received Roomba: data=0x%02X", data.data); }
54
55} // namespace remote_base
56} // namespace esphome
bool expect_item(uint32_t mark, uint32_t space)
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:34
void item(uint32_t mark, uint32_t space)
Definition remote_base.h:29
void encode(RemoteTransmitData *dst, const RoombaData &data) override
void dump(const RoombaData &data) override
optional< RoombaData > decode(RemoteReceiveData src) override
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7