ESPHome  2023.11.6
nextion_commands.cpp
Go to the documentation of this file.
1 #include "nextion.h"
2 #include "esphome/core/util.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace nextion {
7 static const char *const TAG = "nextion";
8 
9 // Sleep safe commands
10 void Nextion::soft_reset() { this->send_command_("rest"); }
11 
12 void Nextion::set_wake_up_page(uint8_t page_id) {
13  this->add_no_result_to_queue_with_set_internal_("wake_up_page", "wup", page_id, true);
14 }
15 
16 void Nextion::set_start_up_page(uint8_t page_id) { this->start_up_page_ = page_id; }
17 
18 void Nextion::set_touch_sleep_timeout(uint16_t timeout) {
19  if (timeout < 3) {
20  ESP_LOGD(TAG, "Sleep timeout out of bounds, range 3-65535");
21  return;
22  }
23 
24  this->add_no_result_to_queue_with_set_internal_("touch_sleep_timeout", "thsp", timeout, true);
25 }
26 
27 void Nextion::sleep(bool sleep) {
28  if (sleep) { // Set sleep
29  this->is_sleeping_ = true;
30  this->add_no_result_to_queue_with_set_internal_("sleep", "sleep", 1, true);
31  } else { // Turn off sleep. Wait for a sleep_wake return before setting sleep off
32  this->add_no_result_to_queue_with_set_internal_("sleep_wake", "sleep", 0, true);
33  }
34 }
35 // End sleep safe commands
36 
37 // Protocol reparse mode
38 void Nextion::set_protocol_reparse_mode(bool active_mode) {
39  const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
40  if (active_mode) { // Sets active protocol reparse mode
41  this->write_str(
42  "recmod=1"); // send_command_ cannot be used as Nextion might not be setup if incorrect reparse mode
43  this->write_array(to_send, sizeof(to_send));
44  } else { // Sets passive protocol reparse mode
45  this->write_str("DRAKJHSUYDGBNCJHGJKSHBDN"); // To exit active reparse mode this sequence must be sent
46  this->write_array(to_send, sizeof(to_send));
47  this->write_str("recmod=0"); // Sending recmode=0 twice is recommended
48  this->write_array(to_send, sizeof(to_send));
49  this->write_str("recmod=0");
50  this->write_array(to_send, sizeof(to_send));
51  }
52  this->write_str("connect");
53  this->write_array(to_send, sizeof(to_send));
54 }
55 
56 // Set Colors
57 void Nextion::set_component_background_color(const char *component, uint32_t color) {
58  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%d", component, color);
59 }
60 
61 void Nextion::set_component_background_color(const char *component, const char *color) {
62  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%s", component, color);
63 }
64 
65 void Nextion::set_component_background_color(const char *component, Color color) {
66  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%d", component,
68 }
69 
70 void Nextion::set_component_pressed_background_color(const char *component, uint32_t color) {
71  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%d", component, color);
72 }
73 
74 void Nextion::set_component_pressed_background_color(const char *component, const char *color) {
75  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%s", component, color);
76 }
77 
78 void Nextion::set_component_pressed_background_color(const char *component, Color color) {
79  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%d", component,
81 }
82 
83 void Nextion::set_component_pic(const char *component, uint8_t pic_id) {
84  this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.pic=%d", component, pic_id);
85 }
86 
87 void Nextion::set_component_picc(const char *component, uint8_t pic_id) {
88  this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.picc=%d", component, pic_id);
89 }
90 
91 void Nextion::set_component_font_color(const char *component, uint32_t color) {
92  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%d", component, color);
93 }
94 
95 void Nextion::set_component_font_color(const char *component, const char *color) {
96  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%s", component, color);
97 }
98 
99 void Nextion::set_component_font_color(const char *component, Color color) {
100  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%d", component,
102 }
103 
104 void Nextion::set_component_pressed_font_color(const char *component, uint32_t color) {
105  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", "%s.pco2=%d", component, color);
106 }
107 
108 void Nextion::set_component_pressed_font_color(const char *component, const char *color) {
109  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", " %s.pco2=%s", component, color);
110 }
111 
112 void Nextion::set_component_pressed_font_color(const char *component, Color color) {
113  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", "%s.pco2=%d", component,
115 }
116 
117 void Nextion::set_component_text_printf(const char *component, const char *format, ...) {
118  va_list arg;
119  va_start(arg, format);
120  char buffer[256];
121  int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
122  va_end(arg);
123  if (ret > 0)
124  this->set_component_text(component, buffer);
125 }
126 
127 // General Nextion
128 void Nextion::goto_page(const char *page) { this->add_no_result_to_queue_with_printf_("goto_page", "page %s", page); }
129 void Nextion::goto_page(uint8_t page) { this->add_no_result_to_queue_with_printf_("goto_page", "page %i", page); }
130 
131 void Nextion::set_backlight_brightness(float brightness) {
132  if (brightness < 0 || brightness > 1.0) {
133  ESP_LOGD(TAG, "Brightness out of bounds, percentage range 0-1.0");
134  return;
135  }
136  this->add_no_result_to_queue_with_printf_("backlight_brightness", "dim=%d", static_cast<int>(brightness * 100));
137 }
138 
139 void Nextion::set_auto_wake_on_touch(bool auto_wake) {
140  this->add_no_result_to_queue_with_set("auto_wake_on_touch", "thup", auto_wake ? 1 : 0);
141 }
142 
143 // General Component
144 void Nextion::set_component_font(const char *component, uint8_t font_id) {
145  this->add_no_result_to_queue_with_printf_("set_component_font", "%s.font=%d", component, font_id);
146 }
147 
148 void Nextion::hide_component(const char *component) {
149  this->add_no_result_to_queue_with_printf_("hide_component", "vis %s,0", component);
150 }
151 
152 void Nextion::show_component(const char *component) {
153  this->add_no_result_to_queue_with_printf_("show_component", "vis %s,1", component);
154 }
155 
156 void Nextion::enable_component_touch(const char *component) {
157  this->add_no_result_to_queue_with_printf_("enable_component_touch", "tsw %s,1", component);
158 }
159 
160 void Nextion::disable_component_touch(const char *component) {
161  this->add_no_result_to_queue_with_printf_("disable_component_touch", "tsw %s,0", component);
162 }
163 
164 void Nextion::set_component_picture(const char *component, const char *picture) {
165  this->add_no_result_to_queue_with_printf_("set_component_picture", "%s.val=%s", component, picture);
166 }
167 
168 void Nextion::set_component_text(const char *component, const char *text) {
169  this->add_no_result_to_queue_with_printf_("set_component_text", "%s.txt=\"%s\"", component, text);
170 }
171 
172 void Nextion::set_component_value(const char *component, int value) {
173  this->add_no_result_to_queue_with_printf_("set_component_value", "%s.val=%d", component, value);
174 }
175 
176 void Nextion::add_waveform_data(int component_id, uint8_t channel_number, uint8_t value) {
177  this->add_no_result_to_queue_with_printf_("add_waveform_data", "add %d,%u,%u", component_id, channel_number, value);
178 }
179 
180 void Nextion::open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value) {
181  this->add_no_result_to_queue_with_printf_("open_waveform_channel", "addt %d,%u,%u", component_id, channel_number,
182  value);
183 }
184 
185 void Nextion::set_component_coordinates(const char *component, int x, int y) {
186  this->add_no_result_to_queue_with_printf_("set_component_coordinates command 1", "%s.xcen=%d", component, x);
187  this->add_no_result_to_queue_with_printf_("set_component_coordinates command 2", "%s.ycen=%d", component, y);
188 }
189 
190 // Drawing
191 void Nextion::display_picture(int picture_id, int x_start, int y_start) {
192  this->add_no_result_to_queue_with_printf_("display_picture", "pic %d, %d, %d", x_start, y_start, picture_id);
193 }
194 
195 void Nextion::fill_area(int x1, int y1, int width, int height, const char *color) {
196  this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%s", x1, y1, width, height, color);
197 }
198 
199 void Nextion::fill_area(int x1, int y1, int width, int height, Color color) {
200  this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%d", x1, y1, width, height,
202 }
203 
204 void Nextion::line(int x1, int y1, int x2, int y2, const char *color) {
205  this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%s", x1, y1, x2, y2, color);
206 }
207 
208 void Nextion::line(int x1, int y1, int x2, int y2, Color color) {
209  this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%d", x1, y1, x2, y2,
211 }
212 
213 void Nextion::rectangle(int x1, int y1, int width, int height, const char *color) {
214  this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%s", x1, y1, x1 + width, y1 + height, color);
215 }
216 
217 void Nextion::rectangle(int x1, int y1, int width, int height, Color color) {
218  this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%d", x1, y1, x1 + width, y1 + height,
220 }
221 
222 void Nextion::circle(int center_x, int center_y, int radius, const char *color) {
223  this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%s", center_x, center_y, radius, color);
224 }
225 
226 void Nextion::circle(int center_x, int center_y, int radius, Color color) {
227  this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%d", center_x, center_y, radius,
229 }
230 
231 void Nextion::filled_circle(int center_x, int center_y, int radius, const char *color) {
232  this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%s", center_x, center_y, radius, color);
233 }
234 
235 void Nextion::filled_circle(int center_x, int center_y, int radius, Color color) {
236  this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%d", center_x, center_y, radius,
238 }
239 
241  this->add_no_result_to_queue_with_printf_("rtc0", "rtc0=%u", time.year);
242  this->add_no_result_to_queue_with_printf_("rtc1", "rtc1=%u", time.month);
243  this->add_no_result_to_queue_with_printf_("rtc2", "rtc2=%u", time.day_of_month);
244  this->add_no_result_to_queue_with_printf_("rtc3", "rtc3=%u", time.hour);
245  this->add_no_result_to_queue_with_printf_("rtc4", "rtc4=%u", time.minute);
246  this->add_no_result_to_queue_with_printf_("rtc5", "rtc5=%u", time.second);
247 }
248 
249 } // namespace nextion
250 } // namespace esphome
void goto_page(const char *page)
Show the page with a given name.
void set_nextion_rtc_time(ESPTime time)
Send the current time to the nextion display.
void write_str(const char *str)
Definition: uart.h:27
void line(int x1, int y1, int x2, int y2, const char *color)
Draw a line on the screen.
void set_protocol_reparse_mode(bool active_mode)
Sets Nextion Protocol Reparse mode between active or passive.
void set_component_pic(const char *component, uint8_t pic_id)
Set the picture id of a component.
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void hide_component(const char *component) override
Hide a component.
void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override
Definition: nextion.cpp:991
void circle(int center_x, int center_y, int radius, const char *color)
Draw a circle outline.
void set_component_pressed_background_color(const char *component, uint32_t color)
Set the pressed background color of a component.
void filled_circle(int center_x, int center_y, int radius, const char *color)
Draw a filled circled.
uint16_t x
Definition: tt21100.cpp:17
A more user-friendly version of struct tm from time.h.
Definition: time.h:12
bool send_command_(const std::string &command)
Manually send a raw command to the display and don&#39;t wait for an acknowledgement packet.
Definition: nextion.cpp:28
void set_component_coordinates(const char *component, int x, int y)
Set the coordinates of a component on screen.
void set_component_picc(const char *component, uint8_t pic_id)
Set the background picture id of component.
void sleep(bool sleep)
Sets Nextion mode between sleep and awake.
void disable_component_touch(const char *component)
Disable touch for a component.
bool void add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, int state_value, bool is_sleep_safe=false)
Definition: nextion.cpp:1001
void set_component_picture(const char *component, const char *picture)
Set the picture of an image component.
void set_component_pressed_font_color(const char *component, uint32_t color)
Set the pressed font color of a component.
void show_component(const char *component) override
Show a component.
void rectangle(int x1, int y1, int width, int height, const char *color)
Draw a rectangle outline.
uint16_t y
Definition: tt21100.cpp:18
void void set_component_value(const char *component, int value)
Set the integer value of a component.
void open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value)
bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
Sends a formatted command to the nextion.
Definition: nextion.cpp:964
void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value)
Add waveform data to a waveform component.
void set_component_text_printf(const char *component, const char *format,...) __attribute__((format(printf
Set the text of a component to a formatted string.
void set_component_background_color(const char *component, uint32_t color)
Set the background color of a component.
uint8_t second
seconds after the minute [0-60]
Definition: time.h:16
void display_picture(int picture_id, int x_start, int y_start)
Display a picture at coordinates.
void set_component_text(const char *component, const char *text)
Set the text of a component to a static string.
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:18
void fill_area(int x1, int y1, int width, int height, const char *color)
Fill a rectangle with a color.
void set_component_font(const char *component, uint8_t font_id) override
Set the font id for a component.
void enable_component_touch(const char *component)
Enable touch for a component.
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
void soft_reset()
Softreset the Nextion.
uint16_t year
year
Definition: time.h:30
void set_touch_sleep_timeout(uint16_t timeout)
Set the touch sleep timeout of the display.
void set_wake_up_page(uint8_t page_id=255)
Sets which page Nextion loads when exiting sleep mode.
void set_auto_wake_on_touch(bool auto_wake)
Sets if Nextion should auto-wake from sleep when touch press occurs.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_start_up_page(uint8_t page_id=255)
Sets which page Nextion loads when connecting to ESPHome.
uint8_t month
month; january=1 [1-12]
Definition: time.h:28
uint8_t hour
hours since midnight [0-23]
Definition: time.h:20
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:24
void set_component_font_color(const char *component, uint32_t color)
Set the font color of a component.