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