ESPHome  2024.7.2
roomba_protocol.cpp
Go to the documentation of this file.
1 #include "roomba_protocol.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote.roomba";
8 
9 static const uint8_t NBITS = 8;
10 static const uint32_t BIT_ONE_HIGH_US = 3000;
11 static const uint32_t BIT_ONE_LOW_US = 1000;
12 static const uint32_t BIT_ZERO_HIGH_US = BIT_ONE_LOW_US;
13 static 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 }
53 void RoombaProtocol::dump(const RoombaData &data) { ESP_LOGD(TAG, "Received Roomba: data=0x%02X", data.data); }
54 
55 } // namespace remote_base
56 } // namespace esphome
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
optional< RoombaData > decode(RemoteReceiveData src) override
void dump(const RoombaData &data) override
void encode(RemoteTransmitData *dst, const RoombaData &data) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74