1
0
mirror of https://github.com/esphome/esphome.git synced 2025-02-21 20:38:16 +00:00

110 lines
3.7 KiB
C
Raw Normal View History

2021-09-09 01:10:02 +04:00
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "remote_base.h"
2022-11-22 18:53:48 -05:00
#include <array>
#include <utility>
2022-11-24 13:12:55 +13:00
#include <vector>
2021-09-09 01:10:02 +04:00
namespace esphome {
namespace remote_base {
class MideaData {
public:
// Make default
MideaData() {}
2021-09-09 01:10:02 +04:00
// Make from initializer_list
MideaData(std::initializer_list<uint8_t> data) {
std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin());
}
2021-09-09 01:10:02 +04:00
// Make from vector
MideaData(const std::vector<uint8_t> &data) {
std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin());
2021-09-09 01:10:02 +04:00
}
// Default copy constructor
MideaData(const MideaData &) = default;
uint8_t *data() { return this->data_.data(); }
const uint8_t *data() const { return this->data_.data(); }
uint8_t size() const { return this->data_.size(); }
2021-09-09 01:10:02 +04:00
bool is_valid() const { return this->data_[OFFSET_CS] == this->calc_cs_(); }
void finalize() { this->data_[OFFSET_CS] = this->calc_cs_(); }
bool is_compliment(const MideaData &rhs) const;
std::string to_string() const { return format_hex_pretty(this->data_.data(), this->data_.size()); }
2021-09-09 01:10:02 +04:00
// compare only 40-bits
bool operator==(const MideaData &rhs) const {
return std::equal(this->data_.begin(), this->data_.begin() + OFFSET_CS, rhs.data_.begin());
}
2021-09-09 01:10:02 +04:00
enum MideaDataType : uint8_t {
MIDEA_TYPE_CONTROL = 0xA1,
2021-09-09 01:10:02 +04:00
MIDEA_TYPE_SPECIAL = 0xA2,
MIDEA_TYPE_FOLLOW_ME = 0xA4,
};
MideaDataType type() const { return static_cast<MideaDataType>(this->data_[0]); }
template<typename T> T to() const { return T(*this); }
uint8_t &operator[](size_t idx) { return this->data_[idx]; }
const uint8_t &operator[](size_t idx) const { return this->data_[idx]; }
2021-09-09 01:10:02 +04:00
protected:
uint8_t get_value_(uint8_t idx, uint8_t mask = 255, uint8_t shift = 0) const {
return (this->data_[idx] >> shift) & mask;
}
void set_value_(uint8_t idx, uint8_t value, uint8_t mask = 255, uint8_t shift = 0) {
this->data_[idx] &= ~(mask << shift);
this->data_[idx] |= (value << shift);
2021-09-09 01:10:02 +04:00
}
void set_mask_(uint8_t idx, bool state, uint8_t mask = 255) { this->set_value_(idx, state ? mask : 0, mask); }
2021-09-09 01:10:02 +04:00
static const uint8_t OFFSET_CS = 5;
// 48-bits data
std::array<uint8_t, 6> data_;
2021-09-09 01:10:02 +04:00
// Calculate checksum
uint8_t calc_cs_() const;
};
class MideaProtocol : public RemoteProtocol<MideaData> {
public:
void encode(RemoteTransmitData *dst, const MideaData &src) override;
2021-09-09 01:10:02 +04:00
optional<MideaData> decode(RemoteReceiveData src) override;
void dump(const MideaData &data) override;
};
class MideaBinarySensor : public RemoteReceiverBinarySensorBase {
public:
bool matches(RemoteReceiveData src) override {
auto data = MideaProtocol().decode(src);
return data.has_value() && data.value() == this->data_;
}
ESP-IDF support and generic target platforms (#2303) * Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
2021-09-20 11:47:51 +02:00
void set_code(const std::vector<uint8_t> &code) { this->data_ = code; }
2021-09-09 01:10:02 +04:00
protected:
MideaData data_;
};
using MideaTrigger = RemoteReceiverTrigger<MideaProtocol, MideaData>;
using MideaDumper = RemoteReceiverDumper<MideaProtocol, MideaData>;
template<typename... Ts> class MideaAction : public RemoteTransmitterActionBase<Ts...> {
ESP-IDF support and generic target platforms (#2303) * Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
2021-09-20 11:47:51 +02:00
TEMPLATABLE_VALUE(std::vector<uint8_t>, code)
2022-11-22 18:53:48 -05:00
void set_code_static(std::vector<uint8_t> code) { code_static_ = std::move(code); }
void set_code_template(std::function<std::vector<uint8_t>(Ts...)> func) { this->code_func_ = func; }
2021-09-09 01:10:02 +04:00
void encode(RemoteTransmitData *dst, Ts... x) override {
2022-11-22 18:53:48 -05:00
MideaData data;
if (!this->code_static_.empty()) {
data = MideaData(this->code_static_);
} else {
data = MideaData(this->code_func_(x...));
}
2021-09-09 01:10:02 +04:00
data.finalize();
MideaProtocol().encode(dst, data);
}
2022-11-22 18:53:48 -05:00
protected:
std::function<std::vector<uint8_t>(Ts...)> code_func_{};
std::vector<uint8_t> code_static_{};
2021-09-09 01:10:02 +04:00
};
} // namespace remote_base
} // namespace esphome