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