ESPHome  2022.11.3
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  if (page_id > 255) {
14  ESP_LOGD(TAG, "Wake up page of bounds, range 0-255");
15  return;
16  }
17  this->add_no_result_to_queue_with_set_internal_("wake_up_page", "wup", page_id, true);
18 }
19 
20 void Nextion::set_touch_sleep_timeout(uint16_t timeout) {
21  if (timeout < 3 || timeout > 65535) {
22  ESP_LOGD(TAG, "Sleep timeout out of bounds, range 3-65535");
23  return;
24  }
25 
26  this->add_no_result_to_queue_with_set_internal_("touch_sleep_timeout", "thsp", timeout, true);
27 }
28 
29 void 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 // Set Colors
40 void Nextion::set_component_background_color(const char *component, uint32_t color) {
41  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%d", component, color);
42 }
43 
44 void Nextion::set_component_background_color(const char *component, const char *color) {
45  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%s", component, color);
46 }
47 
48 void Nextion::set_component_background_color(const char *component, Color color) {
49  this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%d", component,
51 }
52 
53 void Nextion::set_component_pressed_background_color(const char *component, uint32_t color) {
54  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%d", component, color);
55 }
56 
57 void Nextion::set_component_pressed_background_color(const char *component, const char *color) {
58  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%s", component, color);
59 }
60 
61 void Nextion::set_component_pressed_background_color(const char *component, Color color) {
62  this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%d", component,
64 }
65 
66 void Nextion::set_component_pic(const char *component, uint8_t pic_id) {
67  this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.pic=%d", component, pic_id);
68 }
69 
70 void Nextion::set_component_picc(const char *component, uint8_t pic_id) {
71  this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.picc=%d", component, pic_id);
72 }
73 
74 void Nextion::set_component_font_color(const char *component, uint32_t color) {
75  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%d", component, color);
76 }
77 
78 void Nextion::set_component_font_color(const char *component, const char *color) {
79  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%s", component, color);
80 }
81 
82 void Nextion::set_component_font_color(const char *component, Color color) {
83  this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%d", component,
85 }
86 
87 void Nextion::set_component_pressed_font_color(const char *component, uint32_t color) {
88  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", "%s.pco2=%d", component, color);
89 }
90 
91 void Nextion::set_component_pressed_font_color(const char *component, const char *color) {
92  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", " %s.pco2=%s", component, color);
93 }
94 
95 void Nextion::set_component_pressed_font_color(const char *component, Color color) {
96  this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", "%s.pco2=%d", component,
98 }
99 
100 void Nextion::set_component_text_printf(const char *component, const char *format, ...) {
101  va_list arg;
102  va_start(arg, format);
103  char buffer[256];
104  int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
105  va_end(arg);
106  if (ret > 0)
107  this->set_component_text(component, buffer);
108 }
109 
110 // General Nextion
111 void Nextion::goto_page(const char *page) { this->add_no_result_to_queue_with_printf_("goto_page", "page %s", page); }
112 
113 void Nextion::set_backlight_brightness(float brightness) {
114  if (brightness < 0 || brightness > 1.0) {
115  ESP_LOGD(TAG, "Brightness out of bounds, percentage range 0-1.0");
116  return;
117  }
118  this->add_no_result_to_queue_with_printf_("backlight_brightness", "dim=%d", static_cast<int>(brightness * 100));
119 }
120 
121 void Nextion::set_auto_wake_on_touch(bool auto_wake) {
122  this->add_no_result_to_queue_with_set("auto_wake_on_touch", "thup", auto_wake ? 1 : 0);
123 }
124 
125 // General Component
126 void Nextion::set_component_font(const char *component, uint8_t font_id) {
127  this->add_no_result_to_queue_with_printf_("set_component_font", "%s.font=%d", component, font_id);
128 }
129 
130 void Nextion::hide_component(const char *component) {
131  this->add_no_result_to_queue_with_printf_("hide_component", "vis %s,0", component);
132 }
133 
134 void Nextion::show_component(const char *component) {
135  this->add_no_result_to_queue_with_printf_("show_component", "vis %s,1", component);
136 }
137 
138 void Nextion::enable_component_touch(const char *component) {
139  this->add_no_result_to_queue_with_printf_("enable_component_touch", "tsw %s,1", component);
140 }
141 
142 void Nextion::disable_component_touch(const char *component) {
143  this->add_no_result_to_queue_with_printf_("disable_component_touch", "tsw %s,0", component);
144 }
145 
146 void Nextion::set_component_picture(const char *component, const char *picture) {
147  this->add_no_result_to_queue_with_printf_("set_component_picture", "%s.val=%s", component, picture);
148 }
149 
150 void Nextion::set_component_text(const char *component, const char *text) {
151  this->add_no_result_to_queue_with_printf_("set_component_text", "%s.txt=\"%s\"", component, text);
152 }
153 
154 void Nextion::set_component_value(const char *component, int value) {
155  this->add_no_result_to_queue_with_printf_("set_component_value", "%s.val=%d", component, value);
156 }
157 
158 void Nextion::add_waveform_data(int component_id, uint8_t channel_number, uint8_t value) {
159  this->add_no_result_to_queue_with_printf_("add_waveform_data", "add %d,%u,%u", component_id, channel_number, value);
160 }
161 
162 void Nextion::open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value) {
163  this->add_no_result_to_queue_with_printf_("open_waveform_channel", "addt %d,%u,%u", component_id, channel_number,
164  value);
165 }
166 
167 void Nextion::set_component_coordinates(const char *component, int x, int y) {
168  this->add_no_result_to_queue_with_printf_("set_component_coordinates command 1", "%s.xcen=%d", component, x);
169  this->add_no_result_to_queue_with_printf_("set_component_coordinates command 2", "%s.ycen=%d", component, y);
170 }
171 
172 // Drawing
173 void Nextion::display_picture(int picture_id, int x_start, int y_start) {
174  this->add_no_result_to_queue_with_printf_("display_picture", "pic %d, %d, %d", x_start, y_start, picture_id);
175 }
176 
177 void Nextion::fill_area(int x1, int y1, int width, int height, const char *color) {
178  this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%s", x1, y1, width, height, color);
179 }
180 
181 void Nextion::fill_area(int x1, int y1, int width, int height, Color color) {
182  this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%d", x1, y1, width, height,
184 }
185 
186 void Nextion::line(int x1, int y1, int x2, int y2, const char *color) {
187  this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%s", x1, y1, x2, y2, color);
188 }
189 
190 void Nextion::line(int x1, int y1, int x2, int y2, Color color) {
191  this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%d", x1, y1, x2, y2,
193 }
194 
195 void Nextion::rectangle(int x1, int y1, int width, int height, const char *color) {
196  this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%s", x1, y1, x1 + width, y1 + height, color);
197 }
198 
199 void Nextion::rectangle(int x1, int y1, int width, int height, Color color) {
200  this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%d", x1, y1, x1 + width, y1 + height,
202 }
203 
204 void Nextion::circle(int center_x, int center_y, int radius, const char *color) {
205  this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%s", center_x, center_y, radius, color);
206 }
207 
208 void Nextion::circle(int center_x, int center_y, int radius, Color color) {
209  this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%d", center_x, center_y, radius,
211 }
212 
213 void Nextion::filled_circle(int center_x, int center_y, int radius, const char *color) {
214  this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%s", center_x, center_y, radius, color);
215 }
216 
217 void Nextion::filled_circle(int center_x, int center_y, int radius, Color color) {
218  this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%d", center_x, center_y, radius,
220 }
221 
222 #ifdef USE_TIME
224  this->add_no_result_to_queue_with_printf_("rtc0", "rtc0=%u", time.year);
225  this->add_no_result_to_queue_with_printf_("rtc1", "rtc1=%u", time.month);
226  this->add_no_result_to_queue_with_printf_("rtc2", "rtc2=%u", time.day_of_month);
227  this->add_no_result_to_queue_with_printf_("rtc3", "rtc3=%u", time.hour);
228  this->add_no_result_to_queue_with_printf_("rtc4", "rtc4=%u", time.minute);
229  this->add_no_result_to_queue_with_printf_("rtc5", "rtc5=%u", time.second);
230 }
231 #endif
232 
233 } // namespace nextion
234 } // namespace esphome
void goto_page(const char *page)
Show the page with a given name.
uint8_t month
month; january=1 [1-12]
void line(int x1, int y1, int x2, int y2, const char *color)
Draw a line on the screen.
void set_component_pic(const char *component, uint8_t pic_id)
Set the picture id of a component.
void set_nextion_rtc_time(time::ESPTime time)
Send the current time to the nextion display.
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:1007
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.
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:1017
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.
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:980
uint8_t minute
minutes after the hour [0-59]
void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value)
Add waveform data to a waveform component.
A more user-friendly version of struct tm from time.h.
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.
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 second
seconds after the minute [0-60]
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.
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.
Definition: a4988.cpp:4
uint8_t day_of_month
day of the month [1-31]
uint8_t hour
hours since midnight [0-23]
void set_component_font_color(const char *component, uint32_t color)
Set the font color of a component.