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