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