ESPHome  2024.12.2
nextion_component_base.h
Go to the documentation of this file.
1 #pragma once
2 #include <utility>
3 #include <vector>
4 #include "esphome/core/defines.h"
5 
6 namespace esphome {
7 namespace nextion {
8 
10  NO_RESULT = 0,
11  SENSOR = 1,
13  SWITCH = 3,
16 };
17 
18 static const char *const NEXTION_QUEUE_TYPE_STRINGS[] = {"NO_RESULT", "SENSOR", "BINARY_SENSOR",
19  "SWITCH", "TEXT_SENSOR", "WAVEFORM_SENSOR"};
20 
22 
23 class NextionQueue {
24  public:
25  virtual ~NextionQueue() = default;
27  uint32_t queue_time = 0;
28 };
29 
31  public:
32  virtual ~NextionComponentBase() = default;
33 
34  void set_variable_name(const std::string &variable_name, const std::string &variable_name_to_send = "") {
35  variable_name_ = variable_name;
36  if (variable_name_to_send.empty()) {
37  variable_name_to_send_ = variable_name;
38  } else {
39  variable_name_to_send_ = variable_name_to_send;
40  }
41  }
42 
43  virtual void update_component_settings(){};
44  virtual void update_component_settings(bool force_update){};
45 
46  virtual void update_component(){};
47  virtual void process_sensor(const std::string &variable_name, int state){};
48  virtual void process_touch(uint8_t page_id, uint8_t component_id, bool on){};
49  virtual void process_text(const std::string &variable_name, const std::string &text_value){};
50  virtual void process_bool(const std::string &variable_name, bool on){};
51 
52  virtual void set_state(float state){};
53  virtual void set_state(float state, bool publish){};
54  virtual void set_state(float state, bool publish, bool send_to_nextion){};
55 
56  virtual void set_state(bool state){};
57  virtual void set_state(bool state, bool publish){};
58  virtual void set_state(bool state, bool publish, bool send_to_nextion){};
59 
60  virtual void set_state(const std::string &state) {}
61  virtual void set_state(const std::string &state, bool publish) {}
62  virtual void set_state(const std::string &state, bool publish, bool send_to_nextion){};
63 
64  uint8_t get_component_id() { return this->component_id_; }
65  void set_component_id(uint8_t component_id) { component_id_ = component_id; }
66 
67  uint8_t get_wave_channel_id() { return this->wave_chan_id_; }
68  void set_wave_channel_id(uint8_t wave_chan_id) { this->wave_chan_id_ = wave_chan_id; }
69 
70  std::vector<uint8_t> get_wave_buffer() { return this->wave_buffer_; }
71  size_t get_wave_buffer_size() { return this->wave_buffer_.size(); }
72  void clear_wave_buffer(size_t buffer_sent) {
73  if (this->wave_buffer_.size() <= buffer_sent) {
74  this->wave_buffer_.clear();
75  } else {
76  this->wave_buffer_.erase(this->wave_buffer_.begin(), this->wave_buffer_.begin() + buffer_sent);
77  }
78  }
79 
80  std::string get_variable_name() { return this->variable_name_; }
81  std::string get_variable_name_to_send() { return this->variable_name_to_send_; }
83  virtual std::string get_queue_type_string() { return NEXTION_QUEUE_TYPE_STRINGS[this->get_queue_type()]; }
84  virtual void set_state_from_int(int state_value, bool publish, bool send_to_nextion){};
85  virtual void set_state_from_string(const std::string &state_value, bool publish, bool send_to_nextion){};
86  virtual void send_state_to_nextion(){};
87  bool get_needs_to_send_update() { return this->needs_to_send_update_; }
88  uint8_t get_wave_chan_id() { return this->wave_chan_id_; }
89  void set_wave_max_length(int wave_max_length) { this->wave_max_length_ = wave_max_length; }
90 
91  protected:
92  std::string variable_name_;
94 
95  uint8_t component_id_ = 0;
96  uint8_t wave_chan_id_ = UINT8_MAX;
97  std::vector<uint8_t> wave_buffer_;
98  int wave_max_length_ = 255;
99 
101 };
102 } // namespace nextion
103 } // namespace esphome
virtual void process_text(const std::string &variable_name, const std::string &text_value)
virtual void set_state(const std::string &state, bool publish)
virtual ~NextionQueue()=default
virtual void process_bool(const std::string &variable_name, bool on)
virtual void process_sensor(const std::string &variable_name, int state)
virtual void set_state(bool state, bool publish, bool send_to_nextion)
virtual void update_component_settings(bool force_update)
void set_wave_channel_id(uint8_t wave_chan_id)
virtual void set_state_from_string(const std::string &state_value, bool publish, bool send_to_nextion)
virtual void set_state(bool state, bool publish)
virtual void set_state(const std::string &state)
virtual void set_state(float state, bool publish, bool send_to_nextion)
virtual void set_state_from_int(int state_value, bool publish, bool send_to_nextion)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual void set_state(float state, bool publish)
virtual void set_state(const std::string &state, bool publish, bool send_to_nextion)
void set_variable_name(const std::string &variable_name, const std::string &variable_name_to_send="")
virtual void process_touch(uint8_t page_id, uint8_t component_id, bool on)
bool state
Definition: fan.h:34