ESPHome  2024.4.1
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 #include <cinttypes>
5 
6 namespace esphome {
7 namespace nextion {
8 static const char *const TAG = "nextion";
9 
10 // Sleep safe commands
11 void Nextion::soft_reset() { this->send_command_("rest"); }
12 
13 void Nextion::set_wake_up_page(uint8_t page_id) {
14  this->add_no_result_to_queue_with_set_internal_("wake_up_page", "wup", page_id, true);
15 }
16 
17 void Nextion::set_start_up_page(uint8_t page_id) { this->start_up_page_ = page_id; }
18 
19 void Nextion::set_touch_sleep_timeout(uint16_t timeout) {
20  if (timeout < 3) {
21  ESP_LOGD(TAG, "Sleep timeout out of bounds, range 3-65535");
22  return;
23  }
24 
25  this->add_no_result_to_queue_with_set_internal_("touch_sleep_timeout", "thsp", timeout, true);
26 }
27 
28 void Nextion::sleep(bool sleep) {
29  if (sleep) { // Set sleep
30  this->is_sleeping_ = true;
31  this->add_no_result_to_queue_with_set_internal_("sleep", "sleep", 1, true);
32  } else { // Turn off sleep. Wait for a sleep_wake return before setting sleep off
33  this->add_no_result_to_queue_with_set_internal_("sleep_wake", "sleep", 0, true);
34  }
35 }
36 // End sleep safe commands
37 
38 // Protocol reparse mode
39 void Nextion::set_protocol_reparse_mode(bool active_mode) {
40  const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
41  if (active_mode) { // Sets active protocol reparse mode
42  this->write_str(
43  "recmod=1"); // send_command_ cannot be used as Nextion might not be setup if incorrect reparse mode
44  this->write_array(to_send, sizeof(to_send));
45  } else { // Sets passive protocol reparse mode
46  this->write_str("DRAKJHSUYDGBNCJHGJKSHBDN"); // To exit active reparse mode this sequence must be sent
47  this->write_array(to_send, sizeof(to_send));
48  this->write_str("recmod=0"); // Sending recmode=0 twice is recommended
49  this->write_array(to_send, sizeof(to_send));
50  this->write_str("recmod=0");
51  this->write_array(to_send, sizeof(to_send));
52  }
53  this->write_str("connect");
54  this->write_array(to_send, sizeof(to_send));
55 }
56 void Nextion::set_exit_reparse_on_start(bool exit_reparse) { this->exit_reparse_on_start_ = exit_reparse; }
57 
58 // Set Colors - Background
59 void Nextion::set_component_background_color(const char *component, uint16_t color) {
60  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%" PRIu16, component, color);
61 }
62 
63 void Nextion::set_component_background_color(const char *component, const char *color) {
64  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%s", component, color);
65 }
66 
67 void Nextion::set_component_background_color(const char *component, Color color) {
68  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%d", component,
70 }
71 
72 // Set Colors - Background (pressed)
73 void Nextion::set_component_pressed_background_color(const char *component, uint16_t color) {
74  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%" PRIu16, component,
75  color);
76 }
77 
78 void Nextion::set_component_pressed_background_color(const char *component, const char *color) {
79  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%s", component, color);
80 }
81 
82 void Nextion::set_component_pressed_background_color(const char *component, Color color) {
83  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%d", component,
85 }
86 
87 // Set Colors - Foreground
88 void Nextion::set_component_foreground_color(const char *component, uint16_t color) {
89  this->add_no_result_to_queue_with_printf_("set_component_foreground_color", "%s.pco=%" PRIu16, component, color);
90 }
91 
92 void Nextion::set_component_foreground_color(const char *component, const char *color) {
93  this->add_no_result_to_queue_with_printf_("set_component_foreground_color", "%s.pco=%s", component, color);
94 }
95 
96 void Nextion::set_component_foreground_color(const char *component, Color color) {
97  this->add_no_result_to_queue_with_printf_("set_component_foreground_color", "%s.pco=%d", component,
99 }
100 
101 // Set Colors - Foreground (pressed)
102 void Nextion::set_component_pressed_foreground_color(const char *component, uint16_t color) {
103  this->add_no_result_to_queue_with_printf_("set_component_pressed_foreground_color", "%s.pco2=%" PRIu16, component,
104  color);
105 }
106 
107 void Nextion::set_component_pressed_foreground_color(const char *component, const char *color) {
108  this->add_no_result_to_queue_with_printf_("set_component_pressed_foreground_color", " %s.pco2=%s", component, color);
109 }
110 
111 void Nextion::set_component_pressed_foreground_color(const char *component, Color color) {
112  this->add_no_result_to_queue_with_printf_("set_component_pressed_foreground_color", "%s.pco2=%d", component,
114 }
115 
116 // Set Colors - Font
117 void Nextion::set_component_font_color(const char *component, uint16_t color) {
118  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%" PRIu16, component, color);
119 }
120 
121 void Nextion::set_component_font_color(const char *component, const char *color) {
122  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%s", component, color);
123 }
124 
125 void Nextion::set_component_font_color(const char *component, Color color) {
126  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%d", component,
128 }
129 
130 // Set Colors - Font (pressed)
131 void Nextion::set_component_pressed_font_color(const char *component, uint16_t color) {
132  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", "%s.pco2=%" PRIu16, component, color);
133 }
134 
135 void Nextion::set_component_pressed_font_color(const char *component, const char *color) {
136  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", " %s.pco2=%s", component, color);
137 }
138 
139 void Nextion::set_component_pressed_font_color(const char *component, Color color) {
140  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", "%s.pco2=%d", component,
142 }
143 
144 // Set picture
145 void Nextion::set_component_pic(const char *component, uint8_t pic_id) {
146  this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.pic=%d", component, pic_id);
147 }
148 
149 void Nextion::set_component_picc(const char *component, uint8_t pic_id) {
150  this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.picc=%d", component, pic_id);
151 }
152 
153 void Nextion::set_component_text_printf(const char *component, const char *format, ...) {
154  va_list arg;
155  va_start(arg, format);
156  char buffer[256];
157  int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
158  va_end(arg);
159  if (ret > 0)
160  this->set_component_text(component, buffer);
161 }
162 
163 // General Nextion
164 void Nextion::goto_page(const char *page) { this->add_no_result_to_queue_with_printf_("goto_page", "page %s", page); }
165 void Nextion::goto_page(uint8_t page) { this->add_no_result_to_queue_with_printf_("goto_page", "page %i", page); }
166 
167 void Nextion::set_backlight_brightness(float brightness) {
168  if (brightness < 0 || brightness > 1.0) {
169  ESP_LOGD(TAG, "Brightness out of bounds, percentage range 0-1.0");
170  return;
171  }
172  this->add_no_result_to_queue_with_printf_("backlight_brightness", "dim=%d", static_cast<int>(brightness * 100));
173 }
174 
175 void Nextion::set_auto_wake_on_touch(bool auto_wake) {
176  this->add_no_result_to_queue_with_set("auto_wake_on_touch", "thup", auto_wake ? 1 : 0);
177 }
178 
179 // General Component
180 void Nextion::set_component_font(const char *component, uint8_t font_id) {
181  this->add_no_result_to_queue_with_printf_("set_component_font", "%s.font=%d", component, font_id);
182 }
183 
184 void Nextion::hide_component(const char *component) {
185  this->add_no_result_to_queue_with_printf_("hide_component", "vis %s,0", component);
186 }
187 
188 void Nextion::show_component(const char *component) {
189  this->add_no_result_to_queue_with_printf_("show_component", "vis %s,1", component);
190 }
191 
192 void Nextion::enable_component_touch(const char *component) {
193  this->add_no_result_to_queue_with_printf_("enable_component_touch", "tsw %s,1", component);
194 }
195 
196 void Nextion::disable_component_touch(const char *component) {
197  this->add_no_result_to_queue_with_printf_("disable_component_touch", "tsw %s,0", component);
198 }
199 
200 void Nextion::set_component_picture(const char *component, uint8_t picture_id) {
201  this->add_no_result_to_queue_with_printf_("set_component_picture", "%s.pic=%d", component, picture_id);
202 }
203 
204 void Nextion::set_component_text(const char *component, const char *text) {
205  this->add_no_result_to_queue_with_printf_("set_component_text", "%s.txt=\"%s\"", component, text);
206 }
207 
208 void Nextion::set_component_value(const char *component, int value) {
209  this->add_no_result_to_queue_with_printf_("set_component_value", "%s.val=%d", component, value);
210 }
211 
212 void Nextion::add_waveform_data(int component_id, uint8_t channel_number, uint8_t value) {
213  this->add_no_result_to_queue_with_printf_("add_waveform_data", "add %d,%u,%u", component_id, channel_number, value);
214 }
215 
216 void Nextion::open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value) {
217  this->add_no_result_to_queue_with_printf_("open_waveform_channel", "addt %d,%u,%u", component_id, channel_number,
218  value);
219 }
220 
221 void Nextion::set_component_coordinates(const char *component, int x, int y) {
222  this->add_no_result_to_queue_with_printf_("set_component_coordinates command 1", "%s.xcen=%d", component, x);
223  this->add_no_result_to_queue_with_printf_("set_component_coordinates command 2", "%s.ycen=%d", component, y);
224 }
225 
226 // Drawing
227 void Nextion::display_picture(int picture_id, int x_start, int y_start) {
228  this->add_no_result_to_queue_with_printf_("display_picture", "pic %d, %d, %d", x_start, y_start, picture_id);
229 }
230 
231 void Nextion::fill_area(int x1, int y1, int width, int height, uint16_t color) {
232  this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%" PRIu16, x1, y1, width, height, color);
233 }
234 
235 void Nextion::fill_area(int x1, int y1, int width, int height, const char *color) {
236  this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%s", x1, y1, width, height, color);
237 }
238 
239 void Nextion::fill_area(int x1, int y1, int width, int height, Color color) {
240  this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%d", x1, y1, width, height,
242 }
243 
244 void Nextion::line(int x1, int y1, int x2, int y2, uint16_t color) {
245  this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%" PRIu16, x1, y1, x2, y2, color);
246 }
247 
248 void Nextion::line(int x1, int y1, int x2, int y2, const char *color) {
249  this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%s", x1, y1, x2, y2, color);
250 }
251 
252 void Nextion::line(int x1, int y1, int x2, int y2, Color color) {
253  this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%d", x1, y1, x2, y2,
255 }
256 
257 void Nextion::rectangle(int x1, int y1, int width, int height, uint16_t color) {
258  this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%" PRIu16, x1, y1, x1 + width, y1 + height,
259  color);
260 }
261 
262 void Nextion::rectangle(int x1, int y1, int width, int height, const char *color) {
263  this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%s", x1, y1, x1 + width, y1 + height, color);
264 }
265 
266 void Nextion::rectangle(int x1, int y1, int width, int height, Color color) {
267  this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%d", x1, y1, x1 + width, y1 + height,
269 }
270 
271 void Nextion::circle(int center_x, int center_y, int radius, uint16_t color) {
272  this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%" PRIu16, center_x, center_y, radius, color);
273 }
274 
275 void Nextion::circle(int center_x, int center_y, int radius, const char *color) {
276  this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%s", center_x, center_y, radius, color);
277 }
278 
279 void Nextion::circle(int center_x, int center_y, int radius, Color color) {
280  this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%d", center_x, center_y, radius,
282 }
283 
284 void Nextion::filled_circle(int center_x, int center_y, int radius, uint16_t color) {
285  this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%" PRIu16, center_x, center_y, radius, color);
286 }
287 
288 void Nextion::filled_circle(int center_x, int center_y, int radius, const char *color) {
289  this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%s", center_x, center_y, radius, color);
290 }
291 
292 void Nextion::filled_circle(int center_x, int center_y, int radius, Color color) {
293  this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%d", center_x, center_y, radius,
295 }
296 
297 void Nextion::qrcode(int x1, int y1, const char *content, int size, uint16_t background_color,
298  uint16_t foreground_color, int logo_pic, uint8_t border_width) {
299  this->add_no_result_to_queue_with_printf_("qrcode", "qrcode %d,%d,%d,%d,%d,%d,%d,\"%s\"", x1, y1, size,
300  background_color, foreground_color, logo_pic, border_width, content);
301 }
302 
303 void Nextion::qrcode(int x1, int y1, const char *content, int size, Color background_color, Color foreground_color,
304  int logo_pic, uint8_t border_width) {
306  "qrcode", "qrcode %d,%d,%d,%d,%d,%d,%d,\"%s\"", x1, y1, size, display::ColorUtil::color_to_565(background_color),
307  display::ColorUtil::color_to_565(foreground_color), logo_pic, border_width, content);
308 }
309 
311  this->add_no_result_to_queue_with_printf_("rtc0", "rtc0=%u", time.year);
312  this->add_no_result_to_queue_with_printf_("rtc1", "rtc1=%u", time.month);
313  this->add_no_result_to_queue_with_printf_("rtc2", "rtc2=%u", time.day_of_month);
314  this->add_no_result_to_queue_with_printf_("rtc3", "rtc3=%u", time.hour);
315  this->add_no_result_to_queue_with_printf_("rtc4", "rtc4=%u", time.minute);
316  this->add_no_result_to_queue_with_printf_("rtc5", "rtc5=%u", time.second);
317 }
318 
319 } // namespace nextion
320 } // 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 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 set_component_picture(const char *component, uint8_t picture_id)
Set the picture of an image 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:1027
uint16_t x
Definition: tt21100.cpp:17
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
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:29
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:1037
void set_exit_reparse_on_start(bool exit_reparse)
Sets if Nextion should exit the active reparse mode before the "connect" command is sent...
void show_component(const char *component) override
Show a component.
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 filled_circle(int center_x, int center_y, int radius, uint16_t color)
Draw a filled circled.
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:1000
void set_component_pressed_background_color(const char *component, uint16_t color)
Set the pressed background color of a component.
void set_component_pressed_foreground_color(const char *component, uint16_t color)
Set the pressed foreground color of a component.
void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value)
Add waveform data to a waveform component.
void fill_area(int x1, int y1, int width, int height, uint16_t color)
Fill a rectangle with a color.
void set_component_background_color(const char *component, uint16_t color)
Set the background color of a 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.
uint8_t second
seconds after the minute [0-60]
Definition: time.h:21
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:23
void set_component_font_color(const char *component, uint16_t color)
Set the font color of a component.
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:35
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 qrcode(int x1, int y1, const char *content, int size=200, uint16_t background_color=65535, uint16_t foreground_color=0, int logo_pic=-1, uint8_t border_width=8)
Draws a QR code in the screen.
void rectangle(int x1, int y1, int width, int height, uint16_t color)
Draw a rectangle outline.
void set_auto_wake_on_touch(bool auto_wake)
Sets if Nextion should auto-wake from sleep when touch press occurs.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:33
uint8_t hour
hours since midnight [0-23]
Definition: time.h:25
void set_component_foreground_color(const char *component, uint16_t color)
Set the foreground color of a component.
void set_component_pressed_font_color(const char *component, uint16_t color)
Set the pressed font color of a component.
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
void circle(int center_x, int center_y, int radius, uint16_t color)
Draw a circle outline.
void line(int x1, int y1, int x2, int y2, uint16_t color)
Draw a line on the screen.