ESPHome  2022.9.2
midea_protocol.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
5 #include "esphome/core/helpers.h"
6 #include "remote_base.h"
7 
8 namespace esphome {
9 namespace remote_base {
10 
11 class MideaData {
12  public:
13  // Make default
14  MideaData() {}
15  // Make from initializer_list
16  MideaData(std::initializer_list<uint8_t> data) {
17  std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin());
18  }
19  // Make from vector
20  MideaData(const std::vector<uint8_t> &data) {
21  std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin());
22  }
23  // Default copy constructor
24  MideaData(const MideaData &) = default;
25 
26  uint8_t *data() { return this->data_.data(); }
27  const uint8_t *data() const { return this->data_.data(); }
28  uint8_t size() const { return this->data_.size(); }
29  bool is_valid() const { return this->data_[OFFSET_CS] == this->calc_cs_(); }
30  void finalize() { this->data_[OFFSET_CS] = this->calc_cs_(); }
31  bool is_compliment(const MideaData &rhs) const;
32  std::string to_string() const { return format_hex_pretty(this->data_.data(), this->data_.size()); }
33  // compare only 40-bits
34  bool operator==(const MideaData &rhs) const {
35  return std::equal(this->data_.begin(), this->data_.begin() + OFFSET_CS, rhs.data_.begin());
36  }
37  enum MideaDataType : uint8_t {
41  };
42  MideaDataType type() const { return static_cast<MideaDataType>(this->data_[0]); }
43  template<typename T> T to() const { return T(*this); }
44  uint8_t &operator[](size_t idx) { return this->data_[idx]; }
45  const uint8_t &operator[](size_t idx) const { return this->data_[idx]; }
46 
47  protected:
48  uint8_t get_value_(uint8_t idx, uint8_t mask = 255, uint8_t shift = 0) const {
49  return (this->data_[idx] >> shift) & mask;
50  }
51  void set_value_(uint8_t idx, uint8_t value, uint8_t mask = 255, uint8_t shift = 0) {
52  this->data_[idx] &= ~(mask << shift);
53  this->data_[idx] |= (value << shift);
54  }
55  void set_mask_(uint8_t idx, bool state, uint8_t mask = 255) { this->set_value_(idx, state ? mask : 0, mask); }
56  static const uint8_t OFFSET_CS = 5;
57  // 48-bits data
58  std::array<uint8_t, 6> data_;
59  // Calculate checksum
60  uint8_t calc_cs_() const;
61 };
62 
63 class MideaProtocol : public RemoteProtocol<MideaData> {
64  public:
65  void encode(RemoteTransmitData *dst, const MideaData &src) override;
66  optional<MideaData> decode(RemoteReceiveData src) override;
67  void dump(const MideaData &data) override;
68 };
69 
71  public:
72  bool matches(RemoteReceiveData src) override {
73  auto data = MideaProtocol().decode(src);
74  return data.has_value() && data.value() == this->data_;
75  }
76  void set_code(const std::vector<uint8_t> &code) { this->data_ = code; }
77 
78  protected:
80 };
81 
84 
85 template<typename... Ts> class MideaAction : public RemoteTransmitterActionBase<Ts...> {
86  TEMPLATABLE_VALUE(std::vector<uint8_t>, code)
87  void set_code(const std::vector<uint8_t> &code) { code_ = code; }
88  void encode(RemoteTransmitData *dst, Ts... x) override {
89  MideaData data = this->code_.value(x...);
90  data.finalize();
91  MideaProtocol().encode(dst, data);
92  }
93 };
94 
95 } // namespace remote_base
96 } // namespace esphome
MideaDataType type() const
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
Definition: helpers.cpp:214
void set_code(const std::vector< uint8_t > &code)
bool operator==(const MideaData &rhs) const
uint8_t & operator[](size_t idx)
bool is_compliment(const MideaData &rhs) const
void encode(RemoteTransmitData *dst, const MideaData &src) override
void set_value_(uint8_t idx, uint8_t value, uint8_t mask=255, uint8_t shift=0)
static const uint8_t OFFSET_CS
uint8_t get_value_(uint8_t idx, uint8_t mask=255, uint8_t shift=0) const
bool matches(RemoteReceiveData src) override
MideaData(std::initializer_list< uint8_t > data)
std::array< uint8_t, 6 > data_
const uint8_t & operator[](size_t idx) const
std::string to_string() const
optional< MideaData > decode(RemoteReceiveData src) override
void set_mask_(uint8_t idx, bool state, uint8_t mask=255)
Definition: a4988.cpp:4
const uint8_t * data() const
MideaData(const std::vector< uint8_t > &data)
bool state
Definition: fan.h:34