ESPHome  2024.9.2
midea_protocol.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <vector>
5 
7 #include "esphome/core/helpers.h"
8 #include "remote_base.h"
9 
10 namespace esphome {
11 namespace remote_base {
12 
13 class MideaData {
14  public:
15  // Make default
16  MideaData() {}
17  // Make from initializer_list
18  MideaData(std::initializer_list<uint8_t> data) {
19  std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin());
20  }
21  // Make from vector
22  MideaData(const std::vector<uint8_t> &data) {
23  std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin());
24  }
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 
72 template<typename... Ts> class MideaAction : public RemoteTransmitterActionBase<Ts...> {
73  TEMPLATABLE_VALUE(std::vector<uint8_t>, code)
74 
75  void encode(RemoteTransmitData *dst, Ts... x) override {
76  MideaData data(this->code_.value(x...));
77  data.finalize();
78  MideaProtocol().encode(dst, data);
79  }
80 };
81 
82 } // namespace remote_base
83 } // 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:361
DECLARE_REMOTE_PROTOCOL(AEHA) template< typename... Ts > class AEHAAction
Definition: aeha_protocol.h:27
bool operator==(const MideaData &rhs) const
uint8_t & operator[](size_t idx)
uint16_t x
Definition: tt21100.cpp:17
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
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
void set_mask_(uint8_t idx, bool state, uint8_t mask=255)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint8_t * data() const
MideaData(const std::vector< uint8_t > &data)
bool state
Definition: fan.h:34