mirror of
https://github.com/esphome/esphome.git
synced 2025-11-19 00:05:43 +00:00
43 lines
1.0 KiB
C++
43 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include "uart.h"
|
|
#include "esphome/core/automation.h"
|
|
|
|
#include <vector>
|
|
|
|
namespace esphome {
|
|
namespace uart {
|
|
|
|
template<typename... Ts> class UARTWriteAction : public Action<Ts...>, public Parented<UARTComponent> {
|
|
public:
|
|
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
|
|
this->data_func_ = func;
|
|
this->static_ = false;
|
|
}
|
|
void set_data_static(std::vector<uint8_t> &&data) {
|
|
this->data_static_ = std::move(data);
|
|
this->static_ = true;
|
|
}
|
|
void set_data_static(std::initializer_list<uint8_t> data) {
|
|
this->data_static_ = std::vector<uint8_t>(data);
|
|
this->static_ = true;
|
|
}
|
|
|
|
void play(Ts... x) override {
|
|
if (this->static_) {
|
|
this->parent_->write_array(this->data_static_);
|
|
} else {
|
|
auto val = this->data_func_(x...);
|
|
this->parent_->write_array(val);
|
|
}
|
|
}
|
|
|
|
protected:
|
|
bool static_{false};
|
|
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
|
|
std::vector<uint8_t> data_static_{};
|
|
};
|
|
|
|
} // namespace uart
|
|
} // namespace esphome
|