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