ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
display.cpp
Go to the documentation of this file.
1#include "display.h"
2#include <utility>
3#include <numbers>
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7
8namespace esphome {
9namespace display {
10static const char *const TAG = "display";
11
12const Color COLOR_OFF(0, 0, 0, 0);
13const Color COLOR_ON(255, 255, 255, 255);
14
15void Display::fill(Color color) { this->filled_rectangle(0, 0, this->get_width(), this->get_height(), color); }
16void Display::clear() { this->fill(COLOR_OFF); }
17void Display::set_rotation(DisplayRotation rotation) { this->rotation_ = rotation; }
18
19void HOT Display::line(int x1, int y1, int x2, int y2, Color color) {
20 const int32_t dx = abs(x2 - x1), sx = x1 < x2 ? 1 : -1;
21 const int32_t dy = -abs(y2 - y1), sy = y1 < y2 ? 1 : -1;
22 int32_t err = dx + dy;
23
24 while (true) {
25 this->draw_pixel_at(x1, y1, color);
26 if (x1 == x2 && y1 == y2)
27 break;
28 int32_t e2 = 2 * err;
29 if (e2 >= dy) {
30 err += dy;
31 x1 += sx;
32 }
33 if (e2 <= dx) {
34 err += dx;
35 y1 += sy;
36 }
37 }
38}
39
40void Display::line_at_angle(int x, int y, int angle, int length, Color color) {
41 this->line_at_angle(x, y, angle, 0, length, color);
42}
43
44void Display::line_at_angle(int x, int y, int angle, int start_radius, int stop_radius, Color color) {
45 // Calculate start and end points
46 int x1 = (start_radius * cos(angle * M_PI / 180)) + x;
47 int y1 = (start_radius * sin(angle * M_PI / 180)) + y;
48 int x2 = (stop_radius * cos(angle * M_PI / 180)) + x;
49 int y2 = (stop_radius * sin(angle * M_PI / 180)) + y;
50
51 // Draw line
52 this->line(x1, y1, x2, y2, color);
53}
54
55void Display::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorOrder order,
56 ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
57 size_t line_stride = x_offset + w + x_pad; // length of each source line in pixels
58 uint32_t color_value;
59 for (int y = 0; y != h; y++) {
60 size_t source_idx = (y_offset + y) * line_stride + x_offset;
61 size_t source_idx_mod;
62 for (int x = 0; x != w; x++, source_idx++) {
63 switch (bitness) {
64 default:
65 color_value = ptr[source_idx];
66 break;
68 source_idx_mod = source_idx * 2;
69 if (big_endian) {
70 color_value = (ptr[source_idx_mod] << 8) + ptr[source_idx_mod + 1];
71 } else {
72 color_value = ptr[source_idx_mod] + (ptr[source_idx_mod + 1] << 8);
73 }
74 break;
76 source_idx_mod = source_idx * 3;
77 if (big_endian) {
78 color_value = (ptr[source_idx_mod + 0] << 16) + (ptr[source_idx_mod + 1] << 8) + ptr[source_idx_mod + 2];
79 } else {
80 color_value = ptr[source_idx_mod + 0] + (ptr[source_idx_mod + 1] << 8) + (ptr[source_idx_mod + 2] << 16);
81 }
82 break;
83 }
84 this->draw_pixel_at(x + x_start, y + y_start, ColorUtil::to_color(color_value, order, bitness));
85 }
86 }
87}
88
89void HOT Display::horizontal_line(int x, int y, int width, Color color) {
90 // Future: Could be made more efficient by manipulating buffer directly in certain rotations.
91 for (int i = x; i < x + width; i++)
92 this->draw_pixel_at(i, y, color);
93}
94
95void HOT Display::vertical_line(int x, int y, int height, Color color) {
96 // Future: Could be made more efficient by manipulating buffer directly in certain rotations.
97 for (int i = y; i < y + height; i++)
98 this->draw_pixel_at(x, i, color);
99}
100
101void Display::rectangle(int x1, int y1, int width, int height, Color color) {
102 this->horizontal_line(x1, y1, width, color);
103 this->horizontal_line(x1, y1 + height - 1, width, color);
104 this->vertical_line(x1, y1, height, color);
105 this->vertical_line(x1 + width - 1, y1, height, color);
106}
107
108void Display::filled_rectangle(int x1, int y1, int width, int height, Color color) {
109 // Future: Use vertical_line and horizontal_line methods depending on rotation to reduce memory accesses.
110 for (int i = y1; i < y1 + height; i++) {
111 this->horizontal_line(x1, i, width, color);
112 }
113}
114
115void HOT Display::circle(int center_x, int center_xy, int radius, Color color) {
116 int dx = -radius;
117 int dy = 0;
118 int err = 2 - 2 * radius;
119 int e2;
120
121 do {
122 this->draw_pixel_at(center_x - dx, center_xy + dy, color);
123 this->draw_pixel_at(center_x + dx, center_xy + dy, color);
124 this->draw_pixel_at(center_x + dx, center_xy - dy, color);
125 this->draw_pixel_at(center_x - dx, center_xy - dy, color);
126 e2 = err;
127 if (e2 < dy) {
128 err += ++dy * 2 + 1;
129 if (-dx == dy && e2 <= dx) {
130 e2 = 0;
131 }
132 }
133 if (e2 > dx) {
134 err += ++dx * 2 + 1;
135 }
136 } while (dx <= 0);
137}
138
139void Display::filled_circle(int center_x, int center_y, int radius, Color color) {
140 int dx = -int32_t(radius);
141 int dy = 0;
142 int err = 2 - 2 * radius;
143 int e2;
144
145 do {
146 this->draw_pixel_at(center_x - dx, center_y + dy, color);
147 this->draw_pixel_at(center_x + dx, center_y + dy, color);
148 this->draw_pixel_at(center_x + dx, center_y - dy, color);
149 this->draw_pixel_at(center_x - dx, center_y - dy, color);
150 int hline_width = 2 * (-dx) + 1;
151 this->horizontal_line(center_x + dx, center_y + dy, hline_width, color);
152 this->horizontal_line(center_x + dx, center_y - dy, hline_width, color);
153 e2 = err;
154 if (e2 < dy) {
155 err += ++dy * 2 + 1;
156 if (-dx == dy && e2 <= dx) {
157 e2 = 0;
158 }
159 }
160 if (e2 > dx) {
161 err += ++dx * 2 + 1;
162 }
163 } while (dx <= 0);
164}
165
166void Display::filled_ring(int center_x, int center_y, int radius1, int radius2, Color color) {
167 int rmax = radius1 > radius2 ? radius1 : radius2;
168 int rmin = radius1 < radius2 ? radius1 : radius2;
169 int dxmax = -int32_t(rmax), dxmin = -int32_t(rmin);
170 int dymax = 0, dymin = 0;
171 int errmax = 2 - 2 * rmax, errmin = 2 - 2 * rmin;
172 int e2max, e2min;
173 do {
174 // 8 dots for borders
175 this->draw_pixel_at(center_x - dxmax, center_y + dymax, color);
176 this->draw_pixel_at(center_x + dxmax, center_y + dymax, color);
177 this->draw_pixel_at(center_x - dxmin, center_y + dymin, color);
178 this->draw_pixel_at(center_x + dxmin, center_y + dymin, color);
179 this->draw_pixel_at(center_x + dxmax, center_y - dymax, color);
180 this->draw_pixel_at(center_x - dxmax, center_y - dymax, color);
181 this->draw_pixel_at(center_x + dxmin, center_y - dymin, color);
182 this->draw_pixel_at(center_x - dxmin, center_y - dymin, color);
183 if (dymin < rmin) {
184 // two parts - four lines
185 int hline_width = -(dxmax - dxmin) + 1;
186 this->horizontal_line(center_x + dxmax, center_y + dymax, hline_width, color);
187 this->horizontal_line(center_x - dxmin, center_y + dymax, hline_width, color);
188 this->horizontal_line(center_x + dxmax, center_y - dymax, hline_width, color);
189 this->horizontal_line(center_x - dxmin, center_y - dymax, hline_width, color);
190 } else {
191 // one part - top and bottom
192 int hline_width = 2 * (-dxmax) + 1;
193 this->horizontal_line(center_x + dxmax, center_y + dymax, hline_width, color);
194 this->horizontal_line(center_x + dxmax, center_y - dymax, hline_width, color);
195 }
196 e2max = errmax;
197 // tune external
198 if (e2max < dymax) {
199 errmax += ++dymax * 2 + 1;
200 if (-dxmax == dymax && e2max <= dxmax) {
201 e2max = 0;
202 }
203 }
204 if (e2max > dxmax) {
205 errmax += ++dxmax * 2 + 1;
206 }
207 // tune internal
208 while (dymin < dymax && dymin < rmin) {
209 e2min = errmin;
210 if (e2min < dymin) {
211 errmin += ++dymin * 2 + 1;
212 if (-dxmin == dymin && e2min <= dxmin) {
213 e2min = 0;
214 }
215 }
216 if (e2min > dxmin) {
217 errmin += ++dxmin * 2 + 1;
218 }
219 }
220 } while (dxmax <= 0);
221}
222
223void Display::filled_gauge(int center_x, int center_y, int radius1, int radius2, int progress, Color color) {
224 int rmax = radius1 > radius2 ? radius1 : radius2;
225 int rmin = radius1 < radius2 ? radius1 : radius2;
226 int dxmax = -int32_t(rmax), dxmin = -int32_t(rmin), upd_dxmax, upd_dxmin;
227 int dymax = 0, dymin = 0;
228 int errmax = 2 - 2 * rmax, errmin = 2 - 2 * rmin;
229 int e2max, e2min;
230 progress = std::max(0, std::min(progress, 100)); // 0..100
231 int draw_progress = progress > 50 ? (100 - progress) : progress;
232 float tan_a = (progress == 50) ? 65535 : tan(float(draw_progress) * M_PI / 100); // slope
233
234 do {
235 // outer dots
236 this->draw_pixel_at(center_x + dxmax, center_y - dymax, color);
237 this->draw_pixel_at(center_x - dxmax, center_y - dymax, color);
238 if (dymin < rmin) {
239 // side parts
240 int lhline_width = -(dxmax - dxmin) + 1;
241 if (progress >= 50) {
242 if (float(dymax) < float(-dxmax) * tan_a) {
243 upd_dxmax = ceil(float(dymax) / tan_a);
244 } else {
245 upd_dxmax = -dxmax;
246 }
247 this->horizontal_line(center_x + dxmax, center_y - dymax, lhline_width, color); // left
248 if (!dymax)
249 this->horizontal_line(center_x - dxmin, center_y, lhline_width, color); // right horizontal border
250 if (upd_dxmax > -dxmin) {
251 // right
252 int rhline_width = (upd_dxmax + dxmin) + 1;
253 this->horizontal_line(center_x - dxmin, center_y - dymax,
254 rhline_width > lhline_width ? lhline_width : rhline_width, color);
255 }
256 } else {
257 if (float(dymin) > float(-dxmin) * tan_a) {
258 upd_dxmin = ceil(float(dymin) / tan_a);
259 } else {
260 upd_dxmin = -dxmin;
261 }
262 lhline_width = -(dxmax + upd_dxmin) + 1;
263 if (!dymax)
264 this->horizontal_line(center_x - dxmin, center_y, lhline_width, color); // right horizontal border
265 if (lhline_width > 0)
266 this->horizontal_line(center_x + dxmax, center_y - dymax, lhline_width, color);
267 }
268 } else {
269 // top part
270 int hline_width = 2 * (-dxmax) + 1;
271 if (progress >= 50) {
272 if (dymax < float(-dxmax) * tan_a) {
273 upd_dxmax = ceil(float(dymax) / tan_a);
274 hline_width = -dxmax + upd_dxmax + 1;
275 }
276 } else {
277 if (dymax < float(-dxmax) * tan_a) {
278 upd_dxmax = ceil(float(dymax) / tan_a);
279 hline_width = -dxmax - upd_dxmax + 1;
280 } else {
281 hline_width = 0;
282 }
283 }
284 if (hline_width > 0)
285 this->horizontal_line(center_x + dxmax, center_y - dymax, hline_width, color);
286 }
287 e2max = errmax;
288 if (e2max < dymax) {
289 errmax += ++dymax * 2 + 1;
290 if (-dxmax == dymax && e2max <= dxmax) {
291 e2max = 0;
292 }
293 }
294 if (e2max > dxmax) {
295 errmax += ++dxmax * 2 + 1;
296 }
297 while (dymin <= dymax && dymin <= rmin && dxmin <= 0) {
298 this->draw_pixel_at(center_x + dxmin, center_y - dymin, color);
299 this->draw_pixel_at(center_x - dxmin, center_y - dymin, color);
300 e2min = errmin;
301 if (e2min < dymin) {
302 errmin += ++dymin * 2 + 1;
303 if (-dxmin == dymin && e2min <= dxmin) {
304 e2min = 0;
305 }
306 }
307 if (e2min > dxmin) {
308 errmin += ++dxmin * 2 + 1;
309 }
310 }
311 } while (dxmax <= 0);
312}
313
314void HOT Display::triangle(int x1, int y1, int x2, int y2, int x3, int y3, Color color) {
315 this->line(x1, y1, x2, y2, color);
316 this->line(x1, y1, x3, y3, color);
317 this->line(x2, y2, x3, y3, color);
318}
319
320void Display::sort_triangle_points_by_y_(int *x1, int *y1, int *x2, int *y2, int *x3, int *y3) {
321 if (*y1 > *y2) {
322 int x_temp = *x1, y_temp = *y1;
323 *x1 = *x2, *y1 = *y2;
324 *x2 = x_temp, *y2 = y_temp;
325 }
326 if (*y1 > *y3) {
327 int x_temp = *x1, y_temp = *y1;
328 *x1 = *x3, *y1 = *y3;
329 *x3 = x_temp, *y3 = y_temp;
330 }
331 if (*y2 > *y3) {
332 int x_temp = *x2, y_temp = *y2;
333 *x2 = *x3, *y2 = *y3;
334 *x3 = x_temp, *y3 = y_temp;
335 }
336}
337
338void Display::filled_flat_side_triangle_(int x1, int y1, int x2, int y2, int x3, int y3, Color color) {
339 // y2 must be equal to y3 (same horizontal line)
340
341 // Initialize Bresenham's algorithm for side 1
342 int s1_current_x = x1;
343 int s1_current_y = y1;
344 bool s1_axis_swap = false;
345 int s1_dx = abs(x2 - x1);
346 int s1_dy = abs(y2 - y1);
347 int s1_sign_x = ((x2 - x1) >= 0) ? 1 : -1;
348 int s1_sign_y = ((y2 - y1) >= 0) ? 1 : -1;
349 if (s1_dy > s1_dx) {
350 // swap values
351 int tmp = s1_dx;
352 s1_dx = s1_dy;
353 s1_dy = tmp;
354 s1_axis_swap = true;
355 }
356 int s1_error = 2 * s1_dy - s1_dx;
357
358 // Initialize Bresenham's algorithm for side 2
359 int s2_current_x = x1;
360 int s2_current_y = y1;
361 bool s2_axis_swap = false;
362 int s2_dx = abs(x3 - x1);
363 int s2_dy = abs(y3 - y1);
364 int s2_sign_x = ((x3 - x1) >= 0) ? 1 : -1;
365 int s2_sign_y = ((y3 - y1) >= 0) ? 1 : -1;
366 if (s2_dy > s2_dx) {
367 // swap values
368 int tmp = s2_dx;
369 s2_dx = s2_dy;
370 s2_dy = tmp;
371 s2_axis_swap = true;
372 }
373 int s2_error = 2 * s2_dy - s2_dx;
374
375 // Iterate on side 1 and allow side 2 to be processed to match the advance of the y-axis.
376 for (int i = 0; i <= s1_dx; i++) {
377 if (s1_current_x <= s2_current_x) {
378 this->horizontal_line(s1_current_x, s1_current_y, s2_current_x - s1_current_x + 1, color);
379 } else {
380 this->horizontal_line(s2_current_x, s2_current_y, s1_current_x - s2_current_x + 1, color);
381 }
382
383 // Bresenham's #1
384 // Side 1 s1_current_x and s1_current_y calculation
385 while (s1_error >= 0) {
386 if (s1_axis_swap) {
387 s1_current_x += s1_sign_x;
388 } else {
389 s1_current_y += s1_sign_y;
390 }
391 s1_error = s1_error - 2 * s1_dx;
392 }
393 if (s1_axis_swap) {
394 s1_current_y += s1_sign_y;
395 } else {
396 s1_current_x += s1_sign_x;
397 }
398 s1_error = s1_error + 2 * s1_dy;
399
400 // Bresenham's #2
401 // Side 2 s2_current_x and s2_current_y calculation
402 while (s2_current_y != s1_current_y) {
403 while (s2_error >= 0) {
404 if (s2_axis_swap) {
405 s2_current_x += s2_sign_x;
406 } else {
407 s2_current_y += s2_sign_y;
408 }
409 s2_error = s2_error - 2 * s2_dx;
410 }
411 if (s2_axis_swap) {
412 s2_current_y += s2_sign_y;
413 } else {
414 s2_current_x += s2_sign_x;
415 }
416 s2_error = s2_error + 2 * s2_dy;
417 }
418 }
419}
420
421void Display::filled_triangle(int x1, int y1, int x2, int y2, int x3, int y3, Color color) {
422 // Sort the three points by y-coordinate ascending, so [x1,y1] is the topmost point
423 this->sort_triangle_points_by_y_(&x1, &y1, &x2, &y2, &x3, &y3);
424
425 if (y2 == y3) {
426 // Check for special case of a bottom-flat triangle
427 this->filled_flat_side_triangle_(x1, y1, x2, y2, x3, y3, color);
428 } else if (y1 == y2) {
429 // Check for special case of a top-flat triangle
430 this->filled_flat_side_triangle_(x3, y3, x1, y1, x2, y2, color);
431 } else {
432 // General case: split the no-flat-side triangle in a top-flat triangle and bottom-flat triangle
433 int x_temp = (int) (x1 + ((float) (y2 - y1) / (float) (y3 - y1)) * (x3 - x1)), y_temp = y2;
434 this->filled_flat_side_triangle_(x1, y1, x2, y2, x_temp, y_temp, color);
435 this->filled_flat_side_triangle_(x3, y3, x2, y2, x_temp, y_temp, color);
436 }
437}
438
439void HOT Display::get_regular_polygon_vertex(int vertex_id, int *vertex_x, int *vertex_y, int center_x, int center_y,
440 int radius, int edges, RegularPolygonVariation variation,
441 float rotation_degrees) {
442 if (edges >= 2) {
443 // Given the orientation of the display component, an angle is measured clockwise from the x axis.
444 // For a regular polygon, the human reference would be the top of the polygon,
445 // hence we rotate the shape by 270° to orient the polygon up.
446 rotation_degrees += ROTATION_270_DEGREES;
447 // Convert the rotation to radians, easier to use in trigonometrical calculations
448 float rotation_radians = rotation_degrees * std::numbers::pi / 180;
449 // A pointy top variation means the first vertex of the polygon is at the top center of the shape, this requires no
450 // additional rotation of the shape.
451 // A flat top variation means the first point of the polygon has to be rotated so that the first edge is horizontal,
452 // this requires to rotate the shape by π/edges radians counter-clockwise so that the first point is located on the
453 // left side of the first horizontal edge.
454 rotation_radians -= (variation == VARIATION_FLAT_TOP) ? std::numbers::pi / edges : 0.0;
455
456 float vertex_angle = ((float) vertex_id) / edges * 2 * std::numbers::pi + rotation_radians;
457 *vertex_x = (int) round(cos(vertex_angle) * radius) + center_x;
458 *vertex_y = (int) round(sin(vertex_angle) * radius) + center_y;
459 }
460}
461
462void HOT Display::regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation,
463 float rotation_degrees, Color color, RegularPolygonDrawing drawing) {
464 if (edges >= 2) {
465 int previous_vertex_x, previous_vertex_y;
466 for (int current_vertex_id = 0; current_vertex_id <= edges; current_vertex_id++) {
467 int current_vertex_x, current_vertex_y;
468 get_regular_polygon_vertex(current_vertex_id, &current_vertex_x, &current_vertex_y, x, y, radius, edges,
469 variation, rotation_degrees);
470 if (current_vertex_id > 0) {
471 // Start drawing after the 2nd vertex coordinates has been calculated
472 if (drawing == DRAWING_FILLED) {
473 this->filled_triangle(x, y, previous_vertex_x, previous_vertex_y, current_vertex_x, current_vertex_y, color);
474 } else if (drawing == DRAWING_OUTLINE) {
475 this->line(previous_vertex_x, previous_vertex_y, current_vertex_x, current_vertex_y, color);
476 }
477 }
478 previous_vertex_x = current_vertex_x;
479 previous_vertex_y = current_vertex_y;
480 }
481 }
482}
483
484void HOT Display::regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation, Color color,
485 RegularPolygonDrawing drawing) {
486 regular_polygon(x, y, radius, edges, variation, ROTATION_0_DEGREES, color, drawing);
487}
488
489void HOT Display::regular_polygon(int x, int y, int radius, int edges, Color color, RegularPolygonDrawing drawing) {
490 regular_polygon(x, y, radius, edges, VARIATION_POINTY_TOP, ROTATION_0_DEGREES, color, drawing);
491}
492
493void Display::filled_regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation,
494 float rotation_degrees, Color color) {
495 regular_polygon(x, y, radius, edges, variation, rotation_degrees, color, DRAWING_FILLED);
496}
497
498void Display::filled_regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation,
499 Color color) {
500 regular_polygon(x, y, radius, edges, variation, ROTATION_0_DEGREES, color, DRAWING_FILLED);
501}
502
503void Display::filled_regular_polygon(int x, int y, int radius, int edges, Color color) {
505}
506
507void Display::print(int x, int y, BaseFont *font, Color color, TextAlign align, const char *text, Color background) {
508 int x_start, y_start;
509 int width, height;
510 this->get_text_bounds(x, y, text, font, align, &x_start, &y_start, &width, &height);
511 font->print(x_start, y_start, this, color, text, background);
512}
513
514void Display::vprintf_(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format,
515 va_list arg) {
516 char buffer[256];
517 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
518 if (ret > 0)
519 this->print(x, y, font, color, align, buffer, background);
520}
521
522void Display::image(int x, int y, BaseImage *image, Color color_on, Color color_off) {
523 this->image(x, y, image, ImageAlign::TOP_LEFT, color_on, color_off);
524}
525
526void Display::image(int x, int y, BaseImage *image, ImageAlign align, Color color_on, Color color_off) {
527 auto x_align = ImageAlign(int(align) & (int(ImageAlign::HORIZONTAL_ALIGNMENT)));
528 auto y_align = ImageAlign(int(align) & (int(ImageAlign::VERTICAL_ALIGNMENT)));
529
530 switch (x_align) {
532 x -= image->get_width();
533 break;
535 x -= image->get_width() / 2;
536 break;
537 case ImageAlign::LEFT:
538 default:
539 break;
540 }
541
542 switch (y_align) {
544 y -= image->get_height();
545 break;
547 y -= image->get_height() / 2;
548 break;
549 case ImageAlign::TOP:
550 default:
551 break;
552 }
553
554 image->draw(x, y, this, color_on, color_off);
555}
556
557#ifdef USE_GRAPH
558void Display::graph(int x, int y, graph::Graph *graph, Color color_on) { graph->draw(this, x, y, color_on); }
559void Display::legend(int x, int y, graph::Graph *graph, Color color_on) { graph->draw_legend(this, x, y, color_on); }
560#endif // USE_GRAPH
561
562#ifdef USE_QR_CODE
563void Display::qr_code(int x, int y, qr_code::QrCode *qr_code, Color color_on, int scale) {
564 qr_code->draw(this, x, y, color_on, scale);
565}
566#endif // USE_QR_CODE
567
568#ifdef USE_GRAPHICAL_DISPLAY_MENU
569void Display::menu(int x, int y, graphical_display_menu::GraphicalDisplayMenu *menu, int width, int height) {
570 Rect rect(x, y, width, height);
571 menu->draw(this, &rect);
572}
573#endif // USE_GRAPHICAL_DISPLAY_MENU
574
575void Display::get_text_bounds(int x, int y, const char *text, BaseFont *font, TextAlign align, int *x1, int *y1,
576 int *width, int *height) {
577 int x_offset, baseline;
578 font->measure(text, width, &x_offset, &baseline, height);
579
580 auto x_align = TextAlign(int(align) & 0x18);
581 auto y_align = TextAlign(int(align) & 0x07);
582
583 switch (x_align) {
584 case TextAlign::RIGHT:
585 *x1 = x - *width - x_offset;
586 break;
588 *x1 = x - (*width + x_offset) / 2;
589 break;
590 case TextAlign::LEFT:
591 default:
592 // LEFT
593 *x1 = x;
594 break;
595 }
596
597 switch (y_align) {
599 *y1 = y - *height;
600 break;
602 *y1 = y - baseline;
603 break;
605 *y1 = y - (*height) / 2;
606 break;
607 case TextAlign::TOP:
608 default:
609 *y1 = y;
610 break;
611 }
612}
613
614void Display::print(int x, int y, BaseFont *font, Color color, const char *text, Color background) {
615 this->print(x, y, font, color, TextAlign::TOP_LEFT, text, background);
616}
617
618void Display::print(int x, int y, BaseFont *font, TextAlign align, const char *text) {
619 this->print(x, y, font, COLOR_ON, align, text);
620}
621
622void Display::print(int x, int y, BaseFont *font, const char *text) {
623 this->print(x, y, font, COLOR_ON, TextAlign::TOP_LEFT, text);
624}
625
626void Display::printf(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format,
627 ...) {
628 va_list arg;
629 va_start(arg, format);
630 this->vprintf_(x, y, font, color, background, align, format, arg);
631 va_end(arg);
632}
633
634void Display::printf(int x, int y, BaseFont *font, Color color, TextAlign align, const char *format, ...) {
635 va_list arg;
636 va_start(arg, format);
637 this->vprintf_(x, y, font, color, COLOR_OFF, align, format, arg);
638 va_end(arg);
639}
640
641void Display::printf(int x, int y, BaseFont *font, Color color, const char *format, ...) {
642 va_list arg;
643 va_start(arg, format);
644 this->vprintf_(x, y, font, color, COLOR_OFF, TextAlign::TOP_LEFT, format, arg);
645 va_end(arg);
646}
647
648void Display::printf(int x, int y, BaseFont *font, TextAlign align, const char *format, ...) {
649 va_list arg;
650 va_start(arg, format);
651 this->vprintf_(x, y, font, COLOR_ON, COLOR_OFF, align, format, arg);
652 va_end(arg);
653}
654
655void Display::printf(int x, int y, BaseFont *font, const char *format, ...) {
656 va_list arg;
657 va_start(arg, format);
658 this->vprintf_(x, y, font, COLOR_ON, COLOR_OFF, TextAlign::TOP_LEFT, format, arg);
659 va_end(arg);
660}
661
662void Display::set_writer(display_writer_t &&writer) { this->writer_ = writer; }
663
664void Display::set_pages(std::vector<DisplayPage *> pages) {
665 for (auto *page : pages)
666 page->set_parent(this);
667
668 for (uint32_t i = 0; i < pages.size() - 1; i++) {
669 pages[i]->set_next(pages[i + 1]);
670 pages[i + 1]->set_prev(pages[i]);
671 }
672 pages[0]->set_prev(pages[pages.size() - 1]);
673 pages[pages.size() - 1]->set_next(pages[0]);
674 this->show_page(pages[0]);
675}
676
678 this->previous_page_ = this->page_;
679 this->page_ = page;
680 if (this->previous_page_ != this->page_) {
681 for (auto *t : on_page_change_triggers_)
682 t->process(this->previous_page_, this->page_);
683 }
684}
685
688
690 if (this->auto_clear_enabled_) {
691 this->clear();
692 }
693 if (this->show_test_card_) {
694 this->test_card();
695 } else if (this->page_ != nullptr) {
696 this->page_->get_writer()(*this);
697 } else if (this->writer_.has_value()) {
698 (*this->writer_)(*this);
699 }
700 this->clear_clipping_();
701}
702
704 if ((this->from_ == nullptr || this->from_ == from) && (this->to_ == nullptr || this->to_ == to))
705 this->trigger(from, to);
706}
707
708void Display::strftime(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format,
709 ESPTime time) {
710 char buffer[64];
711 size_t ret = time.strftime(buffer, sizeof(buffer), format);
712 if (ret > 0)
713 this->print(x, y, font, color, align, buffer, background);
714}
715
716void Display::strftime(int x, int y, BaseFont *font, Color color, TextAlign align, const char *format, ESPTime time) {
717 this->strftime(x, y, font, color, COLOR_OFF, align, format, time);
718}
719
720void Display::strftime(int x, int y, BaseFont *font, Color color, const char *format, ESPTime time) {
721 this->strftime(x, y, font, color, COLOR_OFF, TextAlign::TOP_LEFT, format, time);
722}
723
724void Display::strftime(int x, int y, BaseFont *font, TextAlign align, const char *format, ESPTime time) {
725 this->strftime(x, y, font, COLOR_ON, COLOR_OFF, align, format, time);
726}
727
728void Display::strftime(int x, int y, BaseFont *font, const char *format, ESPTime time) {
729 this->strftime(x, y, font, COLOR_ON, COLOR_OFF, TextAlign::TOP_LEFT, format, time);
730}
731
733 if (!this->clipping_rectangle_.empty()) {
734 Rect r = this->clipping_rectangle_.back();
735 rect.shrink(r);
736 }
737 this->clipping_rectangle_.push_back(rect);
738}
739
741 if (this->clipping_rectangle_.empty()) {
742 ESP_LOGE(TAG, "clear: Clipping is not set.");
743 } else {
744 this->clipping_rectangle_.pop_back();
745 }
746}
747
749 if (this->clipping_rectangle_.empty()) {
750 ESP_LOGE(TAG, "add: Clipping is not set.");
751 } else {
752 this->clipping_rectangle_.back().extend(add_rect);
753 }
754}
755
757 if (this->clipping_rectangle_.empty()) {
758 ESP_LOGE(TAG, "add: Clipping is not set.");
759 } else {
760 this->clipping_rectangle_.back().shrink(add_rect);
761 }
762}
763
765 if (this->clipping_rectangle_.empty()) {
766 return Rect();
767 } else {
768 return this->clipping_rectangle_.back();
769 }
770}
771
773
774bool Display::clip(int x, int y) {
775 if (x < 0 || x >= this->get_width() || y < 0 || y >= this->get_height())
776 return false;
777 if (!this->get_clipping().inside(x, y))
778 return false;
779 return true;
780}
781
782bool Display::clamp_x_(int x, int w, int &min_x, int &max_x) {
783 min_x = std::max(x, 0);
784 max_x = std::min(x + w, this->get_width());
785
786 if (!this->clipping_rectangle_.empty()) {
787 const auto &rect = this->clipping_rectangle_.back();
788 if (!rect.is_set())
789 return false;
790
791 min_x = std::max(min_x, (int) rect.x);
792 max_x = std::min(max_x, (int) rect.x2());
793 }
794
795 return min_x < max_x;
796}
797
798bool Display::clamp_y_(int y, int h, int &min_y, int &max_y) {
799 min_y = std::max(y, 0);
800 max_y = std::min(y + h, this->get_height());
801
802 if (!this->clipping_rectangle_.empty()) {
803 const auto &rect = this->clipping_rectangle_.back();
804 if (!rect.is_set())
805 return false;
806
807 min_y = std::max(min_y, (int) rect.y);
808 max_y = std::min(max_y, (int) rect.y2());
809 }
810
811 return min_y < max_y;
812}
813
814const uint8_t TESTCARD_FONT[3][8] PROGMEM = {{0x41, 0x7F, 0x7F, 0x09, 0x19, 0x7F, 0x66, 0x00}, // 'R'
815 {0x1C, 0x3E, 0x63, 0x41, 0x51, 0x73, 0x72, 0x00}, // 'G'
816 {0x41, 0x7F, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00}}; // 'B'
817
819 int w = get_width(), h = get_height(), image_w, image_h;
820 this->clear();
821 this->show_test_card_ = false;
822 image_w = std::min(w - 20, 310);
823 image_h = std::min(h - 20, 255);
824 int shift_x = (w - image_w) / 2;
825 int shift_y = (h - image_h) / 2;
826 int line_w = (image_w - 6) / 6;
827 int image_c = image_w / 2;
828 if (this->get_display_type() == DISPLAY_TYPE_COLOR) {
829 Color r(255, 0, 0), g(0, 255, 0), b(0, 0, 255);
830
831 for (auto i = 0; i != image_h; i++) {
832 int c = esp_scale(i, image_h);
833 this->horizontal_line(shift_x + 0, shift_y + i, line_w, r.fade_to_white(c));
834 this->horizontal_line(shift_x + line_w, shift_y + i, line_w, r.fade_to_black(c)); //
835
836 this->horizontal_line(shift_x + image_c - line_w, shift_y + i, line_w, g.fade_to_white(c));
837 this->horizontal_line(shift_x + image_c, shift_y + i, line_w, g.fade_to_black(c));
838
839 this->horizontal_line(shift_x + image_w - (line_w * 2), shift_y + i, line_w, b.fade_to_white(c));
840 this->horizontal_line(shift_x + image_w - line_w, shift_y + i, line_w, b.fade_to_black(c));
841 }
842 }
843 this->rectangle(shift_x, shift_y, image_w, image_h, Color(127, 127, 0));
844
845 uint16_t shift_r = shift_x + line_w - (8 * 3);
846 uint16_t shift_g = shift_x + image_c - (8 * 3);
847 uint16_t shift_b = shift_x + image_w - line_w - (8 * 3);
848 shift_y = h / 2 - (8 * 3);
849 for (auto i = 0; i < 8; i++) {
850 uint8_t ftr = progmem_read_byte(&TESTCARD_FONT[0][i]);
851 uint8_t ftg = progmem_read_byte(&TESTCARD_FONT[1][i]);
852 uint8_t ftb = progmem_read_byte(&TESTCARD_FONT[2][i]);
853 for (auto k = 0; k < 8; k++) {
854 if ((ftr & (1 << k)) != 0) {
855 this->filled_rectangle(shift_r + (i * 6), shift_y + (k * 6), 6, 6, COLOR_OFF);
856 }
857 if ((ftg & (1 << k)) != 0) {
858 this->filled_rectangle(shift_g + (i * 6), shift_y + (k * 6), 6, 6, COLOR_OFF);
859 }
860 if ((ftb & (1 << k)) != 0) {
861 this->filled_rectangle(shift_b + (i * 6), shift_y + (k * 6), 6, 6, COLOR_OFF);
862 }
863 }
864 }
865 this->filled_rectangle(0, 0, 10, 10, Color(255, 0, 255));
866 this->filled_rectangle(w - 10, 0, 10, 10, Color(255, 0, 255));
867 this->filled_rectangle(0, h - 10, 10, 10, Color(255, 0, 255));
868 this->filled_rectangle(w - 10, h - 10, 10, 10, Color(255, 0, 255));
869 this->rectangle(0, 0, w, h, Color(255, 255, 255));
870 this->stop_poller();
871}
872
873DisplayPage::DisplayPage(display_writer_t writer) : writer_(std::move(writer)) {}
874
875void DisplayPage::show() { this->parent_->show_page(this); }
876
878 if (this->next_ == nullptr) {
879 ESP_LOGE(TAG, "no next page");
880 return;
881 }
882 this->next_->show();
883}
884
886 if (this->prev_ == nullptr) {
887 ESP_LOGE(TAG, "no previous page");
888 return;
889 }
890 this->prev_->show();
891}
892
893void DisplayPage::set_parent(Display *parent) { this->parent_ = parent; }
894void DisplayPage::set_prev(DisplayPage *prev) { this->prev_ = prev; }
895void DisplayPage::set_next(DisplayPage *next) { this->next_ = next; }
896const display_writer_t &DisplayPage::get_writer() const { return this->writer_; }
897
898const LogString *text_align_to_string(TextAlign textalign) {
899 switch (textalign) {
901 return LOG_STR("TOP_LEFT");
903 return LOG_STR("TOP_CENTER");
905 return LOG_STR("TOP_RIGHT");
907 return LOG_STR("CENTER_LEFT");
909 return LOG_STR("CENTER");
911 return LOG_STR("CENTER_RIGHT");
913 return LOG_STR("BASELINE_LEFT");
915 return LOG_STR("BASELINE_CENTER");
917 return LOG_STR("BASELINE_RIGHT");
919 return LOG_STR("BOTTOM_LEFT");
921 return LOG_STR("BOTTOM_CENTER");
923 return LOG_STR("BOTTOM_RIGHT");
924 default:
925 return LOG_STR("UNKNOWN");
926 }
927}
928} // namespace display
929} // namespace esphome
uint8_t h
Definition bl0906.h:2
virtual void measure(const char *str, int *width, int *x_offset, int *baseline, int *height)=0
virtual void print(int x, int y, Display *display, Color color, const char *text, Color background)=0
virtual int get_height() const =0
virtual int get_width() const =0
virtual void draw(int x, int y, Display *display, Color color_on, Color color_off)=0
static Color to_color(uint32_t colorcode, ColorOrder color_order, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
void show_page(DisplayPage *page)
Definition display.cpp:677
bool clip(int x, int y)
Check if pixel is within region of display.
Definition display.cpp:774
void get_regular_polygon_vertex(int vertex_id, int *vertex_x, int *vertex_y, int center_x, int center_y, int radius, int edges, RegularPolygonVariation variation=VARIATION_POINTY_TOP, float rotation_degrees=ROTATION_0_DEGREES)
Get the specified vertex (x,y) coordinates for the regular polygon inscribed in the circle centered o...
Definition display.cpp:439
display_writer_t writer_
Definition display.h:791
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:16
void end_clipping()
Reset the invalidation region.
Definition display.cpp:740
void start_clipping(Rect rect)
Set the clipping rectangle for further drawing.
Definition display.cpp:732
void set_pages(std::vector< DisplayPage * > pages)
Definition display.cpp:664
void vprintf_(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format, va_list arg)
Definition display.cpp:514
virtual int get_height()
Get the calculated height of the display in pixels with rotation applied.
Definition display.h:328
virtual void fill(Color color)
Fill the entire screen with the given color.
Definition display.cpp:15
void horizontal_line(int x, int y, int width, Color color=COLOR_ON)
Draw a horizontal line from the point [x,y] to [x+width,y] with the given color.
Definition display.cpp:89
void sort_triangle_points_by_y_(int *x1, int *y1, int *x2, int *y2, int *x3, int *y3)
Definition display.cpp:320
virtual int get_width()
Get the calculated width of the display in pixels with rotation applied.
Definition display.h:326
void circle(int center_x, int center_xy, int radius, Color color=COLOR_ON)
Draw the outline of a circle centered around [center_x,center_y] with the radius radius with the give...
Definition display.cpp:115
void filled_triangle(int x1, int y1, int x2, int y2, int x3, int y3, Color color=COLOR_ON)
Fill a triangle contained between the points [x1,y1], [x2,y2] and [x3,y3] with the given color.
Definition display.cpp:421
void print(int x, int y, BaseFont *font, Color color, TextAlign align, const char *text, Color background=COLOR_OFF)
Print text with the anchor point at [x,y] with font.
Definition display.cpp:507
void set_rotation(DisplayRotation rotation)
Internal method to set the display rotation with.
Definition display.cpp:17
void filled_regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation=VARIATION_POINTY_TOP, float rotation_degrees=ROTATION_0_DEGREES, Color color=COLOR_ON)
Fill a regular polygon inscribed in the circle centered on [x,y] with the given radius and color.
Definition display.cpp:493
void qr_code(int x, int y, qr_code::QrCode *qr_code, Color color_on=COLOR_ON, int scale=1)
Draw the qr_code with the top-left corner at [x,y] to the screen.
Definition display.cpp:563
bool clamp_x_(int x, int w, int &min_x, int &max_x)
Definition display.cpp:782
void line(int x1, int y1, int x2, int y2, Color color=COLOR_ON)
Draw a straight line from the point [x1,y1] to [x2,y2] with the given color.
Definition display.cpp:19
bool clamp_y_(int y, int h, int &min_y, int &max_y)
Definition display.cpp:798
void filled_gauge(int center_x, int center_y, int radius1, int radius2, int progress, Color color=COLOR_ON)
Fill a half-ring "gauge" centered around [center_x,center_y] between two circles with the radius1 and...
Definition display.cpp:223
virtual DisplayType get_display_type()=0
Get the type of display that the buffer corresponds to.
void void void void void void void void void void void image(int x, int y, BaseImage *image, Color color_on=COLOR_ON, Color color_off=COLOR_OFF)
Draw the image with the top-left corner at [x,y] to the screen.
Definition display.cpp:522
void legend(int x, int y, graph::Graph *graph, Color color_on=COLOR_ON)
Draw the legend for graph with the top-left corner at [x,y] to the screen.
Definition display.cpp:559
void rectangle(int x1, int y1, int width, int height, Color color=COLOR_ON)
Draw the outline of a rectangle with the top left point at [x1,y1] and the bottom right point at [x1+...
Definition display.cpp:101
DisplayPage * previous_page_
Definition display.h:793
void filled_circle(int center_x, int center_y, int radius, Color color=COLOR_ON)
Fill a circle centered around [center_x,center_y] with the radius radius with the given color.
Definition display.cpp:139
void void void void void void strftime(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format, ESPTime time) __attribute__((format(strftime
Evaluate the strftime-format format and print the result with the anchor point at [x,...
Definition display.cpp:708
void triangle(int x1, int y1, int x2, int y2, int x3, int y3, Color color=COLOR_ON)
Draw the outline of a triangle contained between the points [x1,y1], [x2,y2] and [x3,...
Definition display.cpp:314
void printf(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format,...) __attribute__((format(printf
Evaluate the printf-format format and print the result with the anchor point at [x,...
Definition display.cpp:626
DisplayPage * page_
Definition display.h:792
void set_writer(display_writer_t &&writer)
Internal method to set the display writer lambda.
Definition display.cpp:662
void draw_pixel_at(int x, int y)
Set a single pixel at the specified coordinates to default color.
Definition display.h:336
void vertical_line(int x, int y, int height, Color color=COLOR_ON)
Draw a vertical line from the point [x,y] to [x,y+width] with the given color.
Definition display.cpp:95
void graph(int x, int y, graph::Graph *graph, Color color_on=COLOR_ON)
Draw the graph with the top-left corner at [x,y] to the screen.
Definition display.cpp:558
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:764
void filled_ring(int center_x, int center_y, int radius1, int radius2, Color color=COLOR_ON)
Fill a ring centered around [center_x,center_y] between two circles with the radius1 and radius2 with...
Definition display.cpp:166
void extend_clipping(Rect rect)
Add a rectangular region to the invalidation region.
Definition display.cpp:748
void menu(int x, int y, graphical_display_menu::GraphicalDisplayMenu *menu, int width, int height)
Definition display.cpp:569
void line_at_angle(int x, int y, int angle, int length, Color color=COLOR_ON)
Draw a straight line at the given angle based on the origin [x, y] for a specified length with the gi...
Definition display.cpp:40
void get_text_bounds(int x, int y, const char *text, BaseFont *font, TextAlign align, int *x1, int *y1, int *width, int *height)
Get the text bounds of the given string.
Definition display.cpp:575
DisplayRotation rotation_
Definition display.h:790
std::vector< DisplayOnPageChangeTrigger * > on_page_change_triggers_
Definition display.h:794
virtual void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorOrder order, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Given an array of pixels encoded in the nominated format, draw these into the display's buffer.
Definition display.cpp:55
void regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation=VARIATION_POINTY_TOP, float rotation_degrees=ROTATION_0_DEGREES, Color color=COLOR_ON, RegularPolygonDrawing drawing=DRAWING_OUTLINE)
Draw the outline of a regular polygon inscribed in the circle centered on [x,y] with the given radius...
Definition display.cpp:462
void filled_flat_side_triangle_(int x1, int y1, int x2, int y2, int x3, int y3, Color color)
This method fills a triangle using only integer variables by using a modified bresenham algorithm.
Definition display.cpp:338
void shrink_clipping(Rect rect)
substract a rectangular region to the invalidation region
Definition display.cpp:756
void filled_rectangle(int x1, int y1, int width, int height, Color color=COLOR_ON)
Fill a rectangle with the top left point at [x1,y1] and the bottom right point at [x1+width,...
Definition display.cpp:108
std::vector< Rect > clipping_rectangle_
Definition display.h:796
void process(DisplayPage *from, DisplayPage *to)
Definition display.cpp:703
display_writer_t writer_
Definition display.h:813
void set_next(DisplayPage *next)
Definition display.cpp:895
const display_writer_t & get_writer() const
Definition display.cpp:896
void set_parent(Display *parent)
Definition display.cpp:893
DisplayPage(display_writer_t writer)
Definition display.cpp:873
void set_prev(DisplayPage *prev)
Definition display.cpp:894
void shrink(Rect rect)
Definition rect.cpp:42
void draw(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color)
Definition graph.cpp:56
void draw_legend(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color)
Definition graph.cpp:335
void draw(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color, int scale)
Definition qr_code.cpp:38
const Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
Definition display.h:303
const LogString * text_align_to_string(TextAlign textalign)
Definition display.cpp:898
ImageAlign
ImageAlign is used to tell the display class how to position a image.
Definition display.h:103
const float ROTATION_270_DEGREES
Definition display.h:163
TextAlign
TextAlign is used to tell the display class how to position a piece of text.
Definition display.h:53
const uint8_t TESTCARD_FONT[3][8] PROGMEM
Definition display.cpp:814
const Color COLOR_OFF(0, 0, 0, 0)
Turn the pixel OFF.
Definition display.h:301
const float ROTATION_0_DEGREES
Definition display.h:159
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:49
Color fade_to_white(uint8_t amnt)
Definition color.cpp:19
Color fade_to_black(uint8_t amnt)
Definition color.cpp:21
A more user-friendly version of struct tm from time.h.
Definition time.h:15
size_t strftime(char *buffer, size_t buffer_len, const char *format)
Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument.
Definition time.cpp:15
std::string print()
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6