ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
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
6namespace esphome::nextion {
7
8static const char *const TAG = "nextion";
9
10// Sleep safe commands
11void Nextion::soft_reset() { this->send_command_("rest"); }
12
13void Nextion::set_wake_up_page(uint8_t wake_up_page) {
14 this->wake_up_page_ = wake_up_page;
15 this->add_no_result_to_queue_with_set_internal_("wup", "wup", wake_up_page, true);
16}
17
18void Nextion::set_touch_sleep_timeout(const uint16_t touch_sleep_timeout) {
19 // Validate range: Nextion thsp command requires min 3, max 65535 seconds (0 disables)
20 if (touch_sleep_timeout != 0 && touch_sleep_timeout < 3) {
21 this->touch_sleep_timeout_ = 3; // Auto-correct to minimum valid value
22 } else {
23 this->touch_sleep_timeout_ = touch_sleep_timeout;
24 }
25
26 this->add_no_result_to_queue_with_set_internal_("thsp", "thsp", this->touch_sleep_timeout_, true);
27}
28
29void Nextion::sleep(bool sleep) {
30 if (sleep) { // Set sleep
31 this->is_sleeping_ = true;
32 this->add_no_result_to_queue_with_set_internal_("sleep", "sleep", 1, true);
33 } else { // Turn off sleep. Wait for a sleep_wake return before setting sleep off
34 this->add_no_result_to_queue_with_set_internal_("sleep_wake", "sleep", 0, true);
35 }
36}
37// End sleep safe commands
38
39// Protocol reparse mode
41 ESP_LOGV(TAG, "Reparse mode: %s", YESNO(active_mode));
42 this->connection_state_.ignore_is_setup_ = true; // if not in reparse mode setup will fail, so it should be ignored
43 bool all_commands_sent = true;
44 if (active_mode) { // Sets active protocol reparse mode
45 all_commands_sent &= this->send_command_("recmod=1");
46 } else { // Sets passive protocol reparse mode
47 all_commands_sent &=
48 this->send_command_("DRAKJHSUYDGBNCJHGJKSHBDN"); // To exit active reparse mode this sequence must be sent
49 all_commands_sent &= this->send_command_("recmod=0"); // Sending recmode=0 twice is recommended
50 all_commands_sent &= this->send_command_("recmod=0");
51 }
52 if (!this->connection_state_.nextion_reports_is_setup_) { // No need to connect if is already setup
53 all_commands_sent &= this->send_command_("connect");
54 }
55 this->connection_state_.ignore_is_setup_ = false;
56 return all_commands_sent;
57}
58
59// Set Colors - Background
60void Nextion::set_component_background_color(const char *component, uint16_t color) {
61 this->add_no_result_to_queue_with_printf_(".bco", "%s.bco=%" PRIu16, component, color);
62}
63
64void Nextion::set_component_background_color(const char *component, const char *color) {
65 this->add_no_result_to_queue_with_printf_(".bco", "%s.bco=%s", component, color);
66}
67
69 this->add_no_result_to_queue_with_printf_(".bco", "%s.bco=%d", component, display::ColorUtil::color_to_565(color));
70}
71
72// Set Colors - Background (pressed)
74 this->add_no_result_to_queue_with_printf_(".bco2", "%s.bco2=%" PRIu16, component, color);
75}
76
77void Nextion::set_component_pressed_background_color(const char *component, const char *color) {
78 this->add_no_result_to_queue_with_printf_(".bco2", "%s.bco2=%s", component, color);
79}
80
82 this->add_no_result_to_queue_with_printf_(".bco2", "%s.bco2=%d", component, display::ColorUtil::color_to_565(color));
83}
84
85// Set Colors - Foreground
86void Nextion::set_component_foreground_color(const char *component, uint16_t color) {
87 this->add_no_result_to_queue_with_printf_(".pco", "%s.pco=%" PRIu16, component, color);
88}
89
90void Nextion::set_component_foreground_color(const char *component, const char *color) {
91 this->add_no_result_to_queue_with_printf_(".pco", "%s.pco=%s", component, color);
92}
93
95 this->add_no_result_to_queue_with_printf_(".pco", "%s.pco=%d", component, display::ColorUtil::color_to_565(color));
96}
97
98// Set Colors - Foreground (pressed)
100 this->add_no_result_to_queue_with_printf_(".pco2", "%s.pco2=%" PRIu16, component, color);
101}
102
103void Nextion::set_component_pressed_foreground_color(const char *component, const char *color) {
104 this->add_no_result_to_queue_with_printf_(".pco2", "%s.pco2=%s", component, color);
105}
106
108 this->add_no_result_to_queue_with_printf_(".pco2", "%s.pco2=%d", component, display::ColorUtil::color_to_565(color));
109}
110
111// Set Colors - Font
112void Nextion::set_component_font_color(const char *component, uint16_t color) {
113 this->add_no_result_to_queue_with_printf_(".pco", "%s.pco=%" PRIu16, component, color);
114}
115
116void Nextion::set_component_font_color(const char *component, const char *color) {
117 this->add_no_result_to_queue_with_printf_(".pco", "%s.pco=%s", component, color);
118}
119
121 this->add_no_result_to_queue_with_printf_(".pco", "%s.pco=%d", component, display::ColorUtil::color_to_565(color));
122}
123
124// Set Colors - Font (pressed)
125void Nextion::set_component_pressed_font_color(const char *component, uint16_t color) {
126 this->add_no_result_to_queue_with_printf_(".pco2", "%s.pco2=%" PRIu16, component, color);
127}
128
129void Nextion::set_component_pressed_font_color(const char *component, const char *color) {
130 this->add_no_result_to_queue_with_printf_(".pco2", "%s.pco2=%s", component, color);
131}
132
134 this->add_no_result_to_queue_with_printf_(".pco2", "%s.pco2=%d", component, display::ColorUtil::color_to_565(color));
135}
136
137// Set picture
138void Nextion::set_component_pic(const char *component, uint16_t pic_id) {
139 this->add_no_result_to_queue_with_printf_(".pic", "%s.pic=%" PRIu16, component, pic_id);
140}
141
142void Nextion::set_component_picc(const char *component, uint16_t pic_id) {
143 this->add_no_result_to_queue_with_printf_(".picc", "%s.picc=%" PRIu16, component, pic_id);
144}
145
146// Set video
147void Nextion::set_component_vid(const char *component, uint8_t vid_id) {
148 this->add_no_result_to_queue_with_printf_(".vid", "%s.vid=%" PRIu8, component, vid_id);
149}
150
151void Nextion::set_component_drag(const char *component, bool drag) {
152 this->add_no_result_to_queue_with_printf_(".drag", "%s.drag=%i", component, drag ? 1 : 0);
153}
154
155void Nextion::set_component_aph(const char *component, uint8_t aph) {
156 this->add_no_result_to_queue_with_printf_(".aph", "%s.aph=%" PRIu8, component, aph);
157}
158
160 this->add_no_result_to_queue_with_printf_(".x", "%s.x=%" PRIu32, component, x);
161 this->add_no_result_to_queue_with_printf_(".y", "%s.y=%" PRIu32, component, y);
162}
163
164void Nextion::set_component_text_printf(const char *component, const char *format, ...) {
165 va_list arg;
166 va_start(arg, format);
167 char buffer[256];
168 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
169 va_end(arg);
170 if (ret > 0)
171 this->set_component_text(component, buffer);
172}
173
174// General Nextion
175void Nextion::goto_page(const char *page) { this->add_no_result_to_queue_with_printf_("page", "page %s", page); }
176void Nextion::goto_page(uint8_t page) { this->add_no_result_to_queue_with_printf_("page", "page %i", page); }
177
178void Nextion::set_backlight_brightness(float brightness) {
179 if (brightness < 0 || brightness > 1.0) {
180 ESP_LOGD(TAG, "Brightness out of bounds (0-1.0)");
181 return;
182 }
183 this->add_no_result_to_queue_with_printf_("dim", "dim=%d", static_cast<int>(brightness * 100));
184}
185
186void Nextion::set_auto_wake_on_touch(bool auto_wake_on_touch) {
187 this->connection_state_.auto_wake_on_touch_ = auto_wake_on_touch;
188 this->add_no_result_to_queue_with_set("thup", "thup", auto_wake_on_touch ? 1 : 0);
189}
190
191// General Component
192void Nextion::set_component_font(const char *component, uint8_t font_id) {
193 this->add_no_result_to_queue_with_printf_(".font", "%s.font=%" PRIu8, component, font_id);
194}
195
196void Nextion::set_component_visibility(const char *component, bool show) {
197 this->add_no_result_to_queue_with_printf_("vis", "vis %s,%d", component, show ? 1 : 0);
198}
199
200void Nextion::hide_component(const char *component) { this->set_component_visibility(component, false); }
201
202void Nextion::show_component(const char *component) { this->set_component_visibility(component, true); }
203
205 this->add_no_result_to_queue_with_printf_("tsw", "tsw %s,1", component);
206}
207
209 this->add_no_result_to_queue_with_printf_("tsw", "tsw %s,0", component);
210}
211
212void Nextion::set_component_text(const char *component, const char *text) {
213 this->add_no_result_to_queue_with_printf_(".txt", "%s.txt=\"%s\"", component, text);
214}
215
216void Nextion::set_component_value(const char *component, int32_t value) {
217 this->add_no_result_to_queue_with_printf_(".val", "%s.val=%" PRId32, component, value);
218}
219
220#ifdef USE_NEXTION_WAVEFORM
221void Nextion::add_waveform_data(uint8_t component_id, uint8_t channel_number, uint8_t value) {
222 this->add_no_result_to_queue_with_printf_("add", "add %" PRIu8 ",%" PRIu8 ",%" PRIu8, component_id, channel_number,
223 value);
224}
225
226void Nextion::open_waveform_channel(uint8_t component_id, uint8_t channel_number, uint8_t value) {
227 this->add_no_result_to_queue_with_printf_("addt", "addt %" PRIu8 ",%" PRIu8 ",%" PRIu8, component_id, channel_number,
228 value);
229}
230#endif // USE_NEXTION_WAVEFORM
231
232void Nextion::set_component_coordinates(const char *component, uint16_t x, uint16_t y) {
233 this->add_no_result_to_queue_with_printf_(".xcen", "%s.xcen=%" PRIu16, component, x);
234 this->add_no_result_to_queue_with_printf_(".ycen", "%s.ycen=%" PRIu16, component, y);
235}
236
237// Drawing
238void Nextion::display_picture(uint16_t picture_id, uint16_t x_start, uint16_t y_start) {
239 this->add_no_result_to_queue_with_printf_("pic", "pic %" PRIu16 ", %" PRIu16 ", %" PRIu16, x_start, y_start,
240 picture_id);
241}
242
243void Nextion::fill_area(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint16_t color) {
244 this->add_no_result_to_queue_with_printf_("fill", "fill %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, x1,
245 y1, width, height, color);
246}
247
248void Nextion::fill_area(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, const char *color) {
249 this->add_no_result_to_queue_with_printf_("fill", "fill %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%s", x1, y1,
250 width, height, color);
251}
252
253void Nextion::fill_area(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, Color color) {
254 this->add_no_result_to_queue_with_printf_("fill", "fill %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, x1,
255 y1, width, height, display::ColorUtil::color_to_565(color));
256}
257
258void Nextion::line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
259 this->add_no_result_to_queue_with_printf_("line", "line %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, x1,
260 y1, x2, y2, color);
261}
262
263void Nextion::line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, const char *color) {
264 this->add_no_result_to_queue_with_printf_("line", "line %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%s", x1, y1,
265 x2, y2, color);
266}
267
268void Nextion::line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, Color color) {
269 this->add_no_result_to_queue_with_printf_("line", "line %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, x1,
270 y1, x2, y2, display::ColorUtil::color_to_565(color));
271}
272
273void Nextion::rectangle(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint16_t color) {
274 this->add_no_result_to_queue_with_printf_("draw", "draw %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, x1,
275 y1, static_cast<uint16_t>(x1 + width), static_cast<uint16_t>(y1 + height),
276 color);
277}
278
279void Nextion::rectangle(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, const char *color) {
280 this->add_no_result_to_queue_with_printf_("draw", "draw %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%s", x1, y1,
281 static_cast<uint16_t>(x1 + width), static_cast<uint16_t>(y1 + height),
282 color);
283}
284
285void Nextion::rectangle(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, Color color) {
286 this->add_no_result_to_queue_with_printf_("draw", "draw %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, x1,
287 y1, static_cast<uint16_t>(x1 + width), static_cast<uint16_t>(y1 + height),
289}
290
291void Nextion::circle(uint16_t center_x, uint16_t center_y, uint16_t radius, uint16_t color) {
292 this->add_no_result_to_queue_with_printf_("cir", "cir %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, center_x,
293 center_y, radius, color);
294}
295
296void Nextion::circle(uint16_t center_x, uint16_t center_y, uint16_t radius, const char *color) {
297 this->add_no_result_to_queue_with_printf_("cir", "cir %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%s", center_x, center_y,
298 radius, color);
299}
300
301void Nextion::circle(uint16_t center_x, uint16_t center_y, uint16_t radius, Color color) {
302 this->add_no_result_to_queue_with_printf_("cir", "cir %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, center_x,
303 center_y, radius, display::ColorUtil::color_to_565(color));
304}
305
306void Nextion::filled_circle(uint16_t center_x, uint16_t center_y, uint16_t radius, uint16_t color) {
307 this->add_no_result_to_queue_with_printf_("cirs", "cirs %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, center_x,
308 center_y, radius, color);
309}
310
311void Nextion::filled_circle(uint16_t center_x, uint16_t center_y, uint16_t radius, const char *color) {
312 this->add_no_result_to_queue_with_printf_("cirs", "cirs %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%s", center_x, center_y,
313 radius, color);
314}
315
316void Nextion::filled_circle(uint16_t center_x, uint16_t center_y, uint16_t radius, Color color) {
317 this->add_no_result_to_queue_with_printf_("cirs", "cirs %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16, center_x,
318 center_y, radius, display::ColorUtil::color_to_565(color));
319}
320
321void Nextion::qrcode(uint16_t x1, uint16_t y1, const char *content, uint16_t size, uint16_t background_color,
322 uint16_t foreground_color, int32_t logo_pic, uint8_t border_width) {
324 "qrcode", "qrcode %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRId32 ",%" PRIu8 ",\"%s\"", x1,
325 y1, size, background_color, foreground_color, logo_pic, border_width, content);
326}
327
328void Nextion::qrcode(uint16_t x1, uint16_t y1, const char *content, uint16_t size, Color background_color,
329 Color foreground_color, int32_t logo_pic, uint8_t border_width) {
331 "qrcode", "qrcode %" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRId32 ",%" PRIu8 ",\"%s\"", x1,
332 y1, size, display::ColorUtil::color_to_565(background_color), display::ColorUtil::color_to_565(foreground_color),
333 logo_pic, border_width, content);
334}
335
337 this->add_no_result_to_queue_with_printf_("rtc0", "rtc0=%u", time.year);
338 this->add_no_result_to_queue_with_printf_("rtc1", "rtc1=%u", time.month);
339 this->add_no_result_to_queue_with_printf_("rtc2", "rtc2=%u", time.day_of_month);
340 this->add_no_result_to_queue_with_printf_("rtc3", "rtc3=%u", time.hour);
341 this->add_no_result_to_queue_with_printf_("rtc4", "rtc4=%u", time.minute);
342 this->add_no_result_to_queue_with_printf_("rtc5", "rtc5=%u", time.second);
343}
344
345} // namespace esphome::nextion
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void set_component_font_color(const char *component, uint16_t color)
Set the font color of a component.
void open_waveform_channel(uint8_t component_id, uint8_t channel_number, uint8_t value)
void set_component_visibility(const char *component, bool show) override
Set the visibility of a component.
void set_component_drag(const char *component, bool drag)
Set the drag availability of a component.
void set_component_foreground_color(const char *component, uint16_t color)
Set the foreground color of a component.
void set_component_font(const char *component, uint8_t font_id) override
Set the font id for a component.
bool send_command_(const std::string &command)
Manually send a raw command to the display and don't wait for an acknowledgement packet.
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_text(const char *component, const char *text)
Set the text of a component to a static string.
struct esphome::nextion::Nextion::@144 connection_state_
Status flags for Nextion display state management.
void set_wake_up_page(uint8_t wake_up_page=255)
Sets which page Nextion loads when exiting sleep mode.
void set_nextion_rtc_time(ESPTime time)
Send the current time to the nextion display.
void set_component_pressed_foreground_color(const char *component, uint16_t color)
Set the pressed foreground color of a component.
void soft_reset()
Softreset the Nextion.
void filled_circle(uint16_t center_x, uint16_t center_y, uint16_t radius, uint16_t color)
Draw a filled circled.
void circle(uint16_t center_x, uint16_t center_y, uint16_t radius, uint16_t color)
Draw a circle outline.
void enable_component_touch(const char *component)
Enable touch for a component.
void rectangle(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint16_t color)
Draw a rectangle outline.
void void set_component_value(const char *component, int32_t value)
Set the integer value of a component.
void set_auto_wake_on_touch(bool auto_wake_on_touch)
Sets if Nextion should auto-wake from sleep when touch press occurs.
void set_component_pic(const char *component, uint16_t pic_id)
Set the picture id of a component.
bool set_protocol_reparse_mode(bool active_mode)
Sets the Nextion display's protocol reparse mode.
void set_component_coordinates(const char *component, uint16_t x, uint16_t y)
Set the coordinates of a component on screen.
void set_component_picc(const char *component, uint16_t pic_id)
Set the background picture id of component.
void sleep(bool sleep)
Sets Nextion mode between sleep and awake.
bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
void set_touch_sleep_timeout(uint16_t touch_sleep_timeout=0)
Set the touch sleep timeout of the display using the thsp command.
void hide_component(const char *component) override
Hide a component.
void set_component_position(const char *component, uint32_t x, uint32_t y)
Set the position of a component.
void add_waveform_data(uint8_t component_id, uint8_t channel_number, uint8_t value)
Add waveform data to a waveform component.
void disable_component_touch(const char *component)
Disable touch for a component.
void set_component_pressed_font_color(const char *component, uint16_t color)
Set the pressed font color of a component.
void goto_page(const char *page)
Show the page with a given name.
bool void add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, int32_t state_value, bool is_sleep_safe=false)
void set_component_pressed_background_color(const char *component, uint16_t color)
Set the pressed background color of a component.
void set_component_vid(const char *component, uint8_t vid_id)
Set the video id of a component.
void set_component_background_color(const char *component, uint16_t color)
Set the background color of a component.
void line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Draw a line on the screen.
void qrcode(uint16_t x1, uint16_t y1, const char *content, uint16_t size=200, uint16_t background_color=65535, uint16_t foreground_color=0, int32_t logo_pic=-1, uint8_t border_width=8)
Draws a QR code in the screen.
void fill_area(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint16_t color)
Fill a rectangle with a color.
void display_picture(uint16_t picture_id, uint16_t x_start, uint16_t y_start)
Display a picture at coordinates.
void show_component(const char *component) override
Show a component.
void add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value) override
void set_component_aph(const char *component, uint8_t aph)
Set the opaqueness (fading) of a component.
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
const Component * component
Definition component.cpp:34
bool active_mode
const char int const __FlashStringHelper * format
Definition log.h:74
va_end(args)
uint16_t size
Definition helpers.cpp:25
size_t size_t const char va_start(args, fmt)
static void uint32_t
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6