ESPHome  2024.3.1
raw_protocol.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "remote_base.h"
5 
6 #include <cinttypes>
7 #include <vector>
8 
9 namespace esphome {
10 namespace remote_base {
11 
13  public:
14  bool matches(RemoteReceiveData src) override {
15  for (size_t i = 0; i < this->len_; i++) {
16  auto val = this->data_[i];
17  if (val < 0) {
18  if (!src.expect_space(static_cast<uint32_t>(-val)))
19  return false;
20  } else {
21  if (!src.expect_mark(static_cast<uint32_t>(val)))
22  return false;
23  }
24  }
25  return true;
26  }
27  void set_data(const int32_t *data) { data_ = data; }
28  void set_len(size_t len) { len_ = len; }
29 
30  protected:
31  const int32_t *data_;
32  size_t len_;
33 };
34 
35 class RawTrigger : public Trigger<RawTimings>, public Component, public RemoteReceiverListener {
36  protected:
37  bool on_receive(RemoteReceiveData src) override {
38  this->trigger(src.get_raw_data());
39  return false;
40  }
41 };
42 
43 template<typename... Ts> class RawAction : public RemoteTransmitterActionBase<Ts...> {
44  public:
45  void set_code_template(std::function<RawTimings(Ts...)> func) { this->code_func_ = func; }
46  void set_code_static(const int32_t *code, size_t len) {
47  this->code_static_ = code;
48  this->code_static_len_ = len;
49  }
50  TEMPLATABLE_VALUE(uint32_t, carrier_frequency);
51 
52  void encode(RemoteTransmitData *dst, Ts... x) override {
53  if (this->code_static_ != nullptr) {
54  for (size_t i = 0; i < this->code_static_len_; i++) {
55  auto val = this->code_static_[i];
56  if (val < 0) {
57  dst->space(static_cast<uint32_t>(-val));
58  } else {
59  dst->mark(static_cast<uint32_t>(val));
60  }
61  }
62  } else {
63  dst->set_data(this->code_func_(x...));
64  }
65  dst->set_carrier_frequency(this->carrier_frequency_.value(x...));
66  }
67 
68  protected:
69  std::function<RawTimings(Ts...)> code_func_{nullptr};
70  const int32_t *code_static_{nullptr};
71  int32_t code_static_len_{0};
72 };
73 
75  public:
76  bool dump(RemoteReceiveData src) override;
77  bool is_secondary() override { return true; }
78 };
79 
80 } // namespace remote_base
81 } // namespace esphome
void set_data(const RawTimings &data)
Definition: remote_base.h:32
const RawTimings & get_raw_data() const
Definition: remote_base.h:48
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:29
uint16_t x
Definition: tt21100.cpp:17
bool on_receive(RemoteReceiveData src) override
Definition: raw_protocol.h:37
mopeka_std_values val[4]
std::vector< int32_t > RawTimings
Definition: remote_base.h:18
bool matches(RemoteReceiveData src) override
Definition: raw_protocol.h:14
void set_code_template(std::function< RawTimings(Ts...)> func)
Definition: raw_protocol.h:45
void encode(RemoteTransmitData *dst, Ts... x) override
Definition: raw_protocol.h:52
std::string size_t len
Definition: helpers.h:292
void set_code_static(const int32_t *code, size_t len)
Definition: raw_protocol.h:46
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_data(const int32_t *data)
Definition: raw_protocol.h:27