ESPHome  2024.5.5
display.cpp
Go to the documentation of this file.
1 #include "display.h"
2 #include "display_color_utils.h"
3 #include <utility>
4 #include "esphome/core/hal.h"
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace display {
9 
10 static const char *const TAG = "display";
11 
12 const Color COLOR_OFF(0, 0, 0, 0);
13 const Color COLOR_ON(255, 255, 255, 255);
14 
15 void Display::fill(Color color) { this->filled_rectangle(0, 0, this->get_width(), this->get_height(), color); }
16 void Display::clear() { this->fill(COLOR_OFF); }
17 void Display::set_rotation(DisplayRotation rotation) { this->rotation_ = rotation; }
18 void 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 
39 void 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 
43 void 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 
54 void 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;
66  case COLOR_BITNESS_565:
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;
74  case COLOR_BITNESS_888:
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 
88 void 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 void 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 }
98 void 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 }
104 void 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 }
110 void 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 }
133 void 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 }
159 void HOT Display::triangle(int x1, int y1, int x2, int y2, int x3, int y3, Color color) {
160  this->line(x1, y1, x2, y2, color);
161  this->line(x1, y1, x3, y3, color);
162  this->line(x2, y2, x3, y3, color);
163 }
164 void Display::sort_triangle_points_by_y_(int *x1, int *y1, int *x2, int *y2, int *x3, int *y3) {
165  if (*y1 > *y2) {
166  int x_temp = *x1, y_temp = *y1;
167  *x1 = *x2, *y1 = *y2;
168  *x2 = x_temp, *y2 = y_temp;
169  }
170  if (*y1 > *y3) {
171  int x_temp = *x1, y_temp = *y1;
172  *x1 = *x3, *y1 = *y3;
173  *x3 = x_temp, *y3 = y_temp;
174  }
175  if (*y2 > *y3) {
176  int x_temp = *x2, y_temp = *y2;
177  *x2 = *x3, *y2 = *y3;
178  *x3 = x_temp, *y3 = y_temp;
179  }
180 }
181 void Display::filled_flat_side_triangle_(int x1, int y1, int x2, int y2, int x3, int y3, Color color) {
182  // y2 must be equal to y3 (same horizontal line)
183 
184  // Initialize Bresenham's algorithm for side 1
185  int s1_current_x = x1;
186  int s1_current_y = y1;
187  bool s1_axis_swap = false;
188  int s1_dx = abs(x2 - x1);
189  int s1_dy = abs(y2 - y1);
190  int s1_sign_x = ((x2 - x1) >= 0) ? 1 : -1;
191  int s1_sign_y = ((y2 - y1) >= 0) ? 1 : -1;
192  if (s1_dy > s1_dx) { // swap values
193  int tmp = s1_dx;
194  s1_dx = s1_dy;
195  s1_dy = tmp;
196  s1_axis_swap = true;
197  }
198  int s1_error = 2 * s1_dy - s1_dx;
199 
200  // Initialize Bresenham's algorithm for side 2
201  int s2_current_x = x1;
202  int s2_current_y = y1;
203  bool s2_axis_swap = false;
204  int s2_dx = abs(x3 - x1);
205  int s2_dy = abs(y3 - y1);
206  int s2_sign_x = ((x3 - x1) >= 0) ? 1 : -1;
207  int s2_sign_y = ((y3 - y1) >= 0) ? 1 : -1;
208  if (s2_dy > s2_dx) { // swap values
209  int tmp = s2_dx;
210  s2_dx = s2_dy;
211  s2_dy = tmp;
212  s2_axis_swap = true;
213  }
214  int s2_error = 2 * s2_dy - s2_dx;
215 
216  // Iterate on side 1 and allow side 2 to be processed to match the advance of the y-axis.
217  for (int i = 0; i <= s1_dx; i++) {
218  if (s1_current_x <= s2_current_x) {
219  this->horizontal_line(s1_current_x, s1_current_y, s2_current_x - s1_current_x + 1, color);
220  } else {
221  this->horizontal_line(s2_current_x, s2_current_y, s1_current_x - s2_current_x + 1, color);
222  }
223 
224  // Bresenham's #1
225  // Side 1 s1_current_x and s1_current_y calculation
226  while (s1_error >= 0) {
227  if (s1_axis_swap) {
228  s1_current_x += s1_sign_x;
229  } else {
230  s1_current_y += s1_sign_y;
231  }
232  s1_error = s1_error - 2 * s1_dx;
233  }
234  if (s1_axis_swap) {
235  s1_current_y += s1_sign_y;
236  } else {
237  s1_current_x += s1_sign_x;
238  }
239  s1_error = s1_error + 2 * s1_dy;
240 
241  // Bresenham's #2
242  // Side 2 s2_current_x and s2_current_y calculation
243  while (s2_current_y != s1_current_y) {
244  while (s2_error >= 0) {
245  if (s2_axis_swap) {
246  s2_current_x += s2_sign_x;
247  } else {
248  s2_current_y += s2_sign_y;
249  }
250  s2_error = s2_error - 2 * s2_dx;
251  }
252  if (s2_axis_swap) {
253  s2_current_y += s2_sign_y;
254  } else {
255  s2_current_x += s2_sign_x;
256  }
257  s2_error = s2_error + 2 * s2_dy;
258  }
259  }
260 }
261 void Display::filled_triangle(int x1, int y1, int x2, int y2, int x3, int y3, Color color) {
262  // Sort the three points by y-coordinate ascending, so [x1,y1] is the topmost point
263  this->sort_triangle_points_by_y_(&x1, &y1, &x2, &y2, &x3, &y3);
264 
265  if (y2 == y3) { // Check for special case of a bottom-flat triangle
266  this->filled_flat_side_triangle_(x1, y1, x2, y2, x3, y3, color);
267  } else if (y1 == y2) { // Check for special case of a top-flat triangle
268  this->filled_flat_side_triangle_(x3, y3, x1, y1, x2, y2, color);
269  } else { // General case: split the no-flat-side triangle in a top-flat triangle and bottom-flat triangle
270  int x_temp = (int) (x1 + ((float) (y2 - y1) / (float) (y3 - y1)) * (x3 - x1)), y_temp = y2;
271  this->filled_flat_side_triangle_(x1, y1, x2, y2, x_temp, y_temp, color);
272  this->filled_flat_side_triangle_(x3, y3, x2, y2, x_temp, y_temp, color);
273  }
274 }
275 void HOT Display::get_regular_polygon_vertex(int vertex_id, int *vertex_x, int *vertex_y, int center_x, int center_y,
276  int radius, int edges, RegularPolygonVariation variation,
277  float rotation_degrees) {
278  if (edges >= 2) {
279  // Given the orientation of the display component, an angle is measured clockwise from the x axis.
280  // For a regular polygon, the human reference would be the top of the polygon,
281  // hence we rotate the shape by 270° to orient the polygon up.
282  rotation_degrees += ROTATION_270_DEGREES;
283  // Convert the rotation to radians, easier to use in trigonometrical calculations
284  float rotation_radians = rotation_degrees * PI / 180;
285  // A pointy top variation means the first vertex of the polygon is at the top center of the shape, this requires no
286  // additional rotation of the shape.
287  // A flat top variation means the first point of the polygon has to be rotated so that the first edge is horizontal,
288  // this requires to rotate the shape by π/edges radians counter-clockwise so that the first point is located on the
289  // left side of the first horizontal edge.
290  rotation_radians -= (variation == VARIATION_FLAT_TOP) ? PI / edges : 0.0;
291 
292  float vertex_angle = ((float) vertex_id) / edges * 2 * PI + rotation_radians;
293  *vertex_x = (int) round(cos(vertex_angle) * radius) + center_x;
294  *vertex_y = (int) round(sin(vertex_angle) * radius) + center_y;
295  }
296 }
297 
298 void HOT Display::regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation,
299  float rotation_degrees, Color color, RegularPolygonDrawing drawing) {
300  if (edges >= 2) {
301  int previous_vertex_x, previous_vertex_y;
302  for (int current_vertex_id = 0; current_vertex_id <= edges; current_vertex_id++) {
303  int current_vertex_x, current_vertex_y;
304  get_regular_polygon_vertex(current_vertex_id, &current_vertex_x, &current_vertex_y, x, y, radius, edges,
305  variation, rotation_degrees);
306  if (current_vertex_id > 0) { // Start drawing after the 2nd vertex coordinates has been calculated
307  if (drawing == DRAWING_FILLED) {
308  this->filled_triangle(x, y, previous_vertex_x, previous_vertex_y, current_vertex_x, current_vertex_y, color);
309  } else if (drawing == DRAWING_OUTLINE) {
310  this->line(previous_vertex_x, previous_vertex_y, current_vertex_x, current_vertex_y, color);
311  }
312  }
313  previous_vertex_x = current_vertex_x;
314  previous_vertex_y = current_vertex_y;
315  }
316  }
317 }
318 void HOT Display::regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation, Color color,
319  RegularPolygonDrawing drawing) {
320  regular_polygon(x, y, radius, edges, variation, ROTATION_0_DEGREES, color, drawing);
321 }
322 void HOT Display::regular_polygon(int x, int y, int radius, int edges, Color color, RegularPolygonDrawing drawing) {
323  regular_polygon(x, y, radius, edges, VARIATION_POINTY_TOP, ROTATION_0_DEGREES, color, drawing);
324 }
325 void Display::filled_regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation,
326  float rotation_degrees, Color color) {
327  regular_polygon(x, y, radius, edges, variation, rotation_degrees, color, DRAWING_FILLED);
328 }
329 void Display::filled_regular_polygon(int x, int y, int radius, int edges, RegularPolygonVariation variation,
330  Color color) {
331  regular_polygon(x, y, radius, edges, variation, ROTATION_0_DEGREES, color, DRAWING_FILLED);
332 }
333 void Display::filled_regular_polygon(int x, int y, int radius, int edges, Color color) {
335 }
336 
337 void Display::print(int x, int y, BaseFont *font, Color color, TextAlign align, const char *text, Color background) {
338  int x_start, y_start;
339  int width, height;
340  this->get_text_bounds(x, y, text, font, align, &x_start, &y_start, &width, &height);
341  font->print(x_start, y_start, this, color, text, background);
342 }
343 
344 void Display::vprintf_(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format,
345  va_list arg) {
346  char buffer[256];
347  int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
348  if (ret > 0)
349  this->print(x, y, font, color, align, buffer, background);
350 }
351 
352 void Display::image(int x, int y, BaseImage *image, Color color_on, Color color_off) {
353  this->image(x, y, image, ImageAlign::TOP_LEFT, color_on, color_off);
354 }
355 
356 void Display::image(int x, int y, BaseImage *image, ImageAlign align, Color color_on, Color color_off) {
357  auto x_align = ImageAlign(int(align) & (int(ImageAlign::HORIZONTAL_ALIGNMENT)));
358  auto y_align = ImageAlign(int(align) & (int(ImageAlign::VERTICAL_ALIGNMENT)));
359 
360  switch (x_align) {
361  case ImageAlign::RIGHT:
362  x -= image->get_width();
363  break;
365  x -= image->get_width() / 2;
366  break;
367  case ImageAlign::LEFT:
368  default:
369  break;
370  }
371 
372  switch (y_align) {
373  case ImageAlign::BOTTOM:
374  y -= image->get_height();
375  break;
377  y -= image->get_height() / 2;
378  break;
379  case ImageAlign::TOP:
380  default:
381  break;
382  }
383 
384  image->draw(x, y, this, color_on, color_off);
385 }
386 
387 #ifdef USE_GRAPH
388 void Display::graph(int x, int y, graph::Graph *graph, Color color_on) { graph->draw(this, x, y, color_on); }
389 void Display::legend(int x, int y, graph::Graph *graph, Color color_on) { graph->draw_legend(this, x, y, color_on); }
390 #endif // USE_GRAPH
391 
392 #ifdef USE_QR_CODE
393 void Display::qr_code(int x, int y, qr_code::QrCode *qr_code, Color color_on, int scale) {
394  qr_code->draw(this, x, y, color_on, scale);
395 }
396 #endif // USE_QR_CODE
397 
398 #ifdef USE_GRAPHICAL_DISPLAY_MENU
399 void Display::menu(int x, int y, graphical_display_menu::GraphicalDisplayMenu *menu, int width, int height) {
400  Rect rect(x, y, width, height);
401  menu->draw(this, &rect);
402 }
403 #endif // USE_GRAPHICAL_DISPLAY_MENU
404 
405 void Display::get_text_bounds(int x, int y, const char *text, BaseFont *font, TextAlign align, int *x1, int *y1,
406  int *width, int *height) {
407  int x_offset, baseline;
408  font->measure(text, width, &x_offset, &baseline, height);
409 
410  auto x_align = TextAlign(int(align) & 0x18);
411  auto y_align = TextAlign(int(align) & 0x07);
412 
413  switch (x_align) {
414  case TextAlign::RIGHT:
415  *x1 = x - *width;
416  break;
418  *x1 = x - (*width) / 2;
419  break;
420  case TextAlign::LEFT:
421  default:
422  // LEFT
423  *x1 = x;
424  break;
425  }
426 
427  switch (y_align) {
428  case TextAlign::BOTTOM:
429  *y1 = y - *height;
430  break;
431  case TextAlign::BASELINE:
432  *y1 = y - baseline;
433  break;
435  *y1 = y - (*height) / 2;
436  break;
437  case TextAlign::TOP:
438  default:
439  *y1 = y;
440  break;
441  }
442 }
443 void Display::print(int x, int y, BaseFont *font, Color color, const char *text, Color background) {
444  this->print(x, y, font, color, TextAlign::TOP_LEFT, text, background);
445 }
446 void Display::print(int x, int y, BaseFont *font, TextAlign align, const char *text) {
447  this->print(x, y, font, COLOR_ON, align, text);
448 }
449 void Display::print(int x, int y, BaseFont *font, const char *text) {
450  this->print(x, y, font, COLOR_ON, TextAlign::TOP_LEFT, text);
451 }
452 void Display::printf(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format,
453  ...) {
454  va_list arg;
455  va_start(arg, format);
456  this->vprintf_(x, y, font, color, background, align, format, arg);
457  va_end(arg);
458 }
459 void Display::printf(int x, int y, BaseFont *font, Color color, TextAlign align, const char *format, ...) {
460  va_list arg;
461  va_start(arg, format);
462  this->vprintf_(x, y, font, color, COLOR_OFF, align, format, arg);
463  va_end(arg);
464 }
465 void Display::printf(int x, int y, BaseFont *font, Color color, const char *format, ...) {
466  va_list arg;
467  va_start(arg, format);
468  this->vprintf_(x, y, font, color, COLOR_OFF, TextAlign::TOP_LEFT, format, arg);
469  va_end(arg);
470 }
471 void Display::printf(int x, int y, BaseFont *font, TextAlign align, const char *format, ...) {
472  va_list arg;
473  va_start(arg, format);
474  this->vprintf_(x, y, font, COLOR_ON, COLOR_OFF, align, format, arg);
475  va_end(arg);
476 }
477 void Display::printf(int x, int y, BaseFont *font, const char *format, ...) {
478  va_list arg;
479  va_start(arg, format);
480  this->vprintf_(x, y, font, COLOR_ON, COLOR_OFF, TextAlign::TOP_LEFT, format, arg);
481  va_end(arg);
482 }
483 void Display::set_writer(display_writer_t &&writer) { this->writer_ = writer; }
484 void Display::set_pages(std::vector<DisplayPage *> pages) {
485  for (auto *page : pages)
486  page->set_parent(this);
487 
488  for (uint32_t i = 0; i < pages.size() - 1; i++) {
489  pages[i]->set_next(pages[i + 1]);
490  pages[i + 1]->set_prev(pages[i]);
491  }
492  pages[0]->set_prev(pages[pages.size() - 1]);
493  pages[pages.size() - 1]->set_next(pages[0]);
494  this->show_page(pages[0]);
495 }
497  this->previous_page_ = this->page_;
498  this->page_ = page;
499  if (this->previous_page_ != this->page_) {
500  for (auto *t : on_page_change_triggers_)
501  t->process(this->previous_page_, this->page_);
502  }
503 }
507  if (this->auto_clear_enabled_) {
508  this->clear();
509  }
510  if (this->show_test_card_) {
511  this->test_card();
512  } else if (this->page_ != nullptr) {
513  this->page_->get_writer()(*this);
514  } else if (this->writer_.has_value()) {
515  (*this->writer_)(*this);
516  }
517  this->clear_clipping_();
518 }
520  if ((this->from_ == nullptr || this->from_ == from) && (this->to_ == nullptr || this->to_ == to))
521  this->trigger(from, to);
522 }
523 void Display::strftime(int x, int y, BaseFont *font, Color color, TextAlign align, const char *format, ESPTime time) {
524  char buffer[64];
525  size_t ret = time.strftime(buffer, sizeof(buffer), format);
526  if (ret > 0)
527  this->print(x, y, font, color, align, buffer);
528 }
529 void Display::strftime(int x, int y, BaseFont *font, Color color, const char *format, ESPTime time) {
530  this->strftime(x, y, font, color, TextAlign::TOP_LEFT, format, time);
531 }
532 void Display::strftime(int x, int y, BaseFont *font, TextAlign align, const char *format, ESPTime time) {
533  this->strftime(x, y, font, COLOR_ON, align, format, time);
534 }
535 void Display::strftime(int x, int y, BaseFont *font, const char *format, ESPTime time) {
536  this->strftime(x, y, font, COLOR_ON, TextAlign::TOP_LEFT, format, time);
537 }
538 
540  if (!this->clipping_rectangle_.empty()) {
541  Rect r = this->clipping_rectangle_.back();
542  rect.shrink(r);
543  }
544  this->clipping_rectangle_.push_back(rect);
545 }
547  if (this->clipping_rectangle_.empty()) {
548  ESP_LOGE(TAG, "clear: Clipping is not set.");
549  } else {
550  this->clipping_rectangle_.pop_back();
551  }
552 }
554  if (this->clipping_rectangle_.empty()) {
555  ESP_LOGE(TAG, "add: Clipping is not set.");
556  } else {
557  this->clipping_rectangle_.back().extend(add_rect);
558  }
559 }
561  if (this->clipping_rectangle_.empty()) {
562  ESP_LOGE(TAG, "add: Clipping is not set.");
563  } else {
564  this->clipping_rectangle_.back().shrink(add_rect);
565  }
566 }
568  if (this->clipping_rectangle_.empty()) {
569  return Rect();
570  } else {
571  return this->clipping_rectangle_.back();
572  }
573 }
575 bool Display::clip(int x, int y) {
576  if (x < 0 || x >= this->get_width() || y < 0 || y >= this->get_height())
577  return false;
578  if (!this->get_clipping().inside(x, y))
579  return false;
580  return true;
581 }
582 bool Display::clamp_x_(int x, int w, int &min_x, int &max_x) {
583  min_x = std::max(x, 0);
584  max_x = std::min(x + w, this->get_width());
585 
586  if (!this->clipping_rectangle_.empty()) {
587  const auto &rect = this->clipping_rectangle_.back();
588  if (!rect.is_set())
589  return false;
590 
591  min_x = std::max(min_x, (int) rect.x);
592  max_x = std::min(max_x, (int) rect.x2());
593  }
594 
595  return min_x < max_x;
596 }
597 bool Display::clamp_y_(int y, int h, int &min_y, int &max_y) {
598  min_y = std::max(y, 0);
599  max_y = std::min(y + h, this->get_height());
600 
601  if (!this->clipping_rectangle_.empty()) {
602  const auto &rect = this->clipping_rectangle_.back();
603  if (!rect.is_set())
604  return false;
605 
606  min_y = std::max(min_y, (int) rect.y);
607  max_y = std::min(max_y, (int) rect.y2());
608  }
609 
610  return min_y < max_y;
611 }
612 
613 const uint8_t TESTCARD_FONT[3][8] PROGMEM = {{0x41, 0x7F, 0x7F, 0x09, 0x19, 0x7F, 0x66, 0x00}, // 'R'
614  {0x1C, 0x3E, 0x63, 0x41, 0x51, 0x73, 0x72, 0x00}, // 'G'
615  {0x41, 0x7F, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00}}; // 'B'
616 
618  int w = get_width(), h = get_height(), image_w, image_h;
619  this->clear();
620  this->show_test_card_ = false;
621  if (this->get_display_type() == DISPLAY_TYPE_COLOR) {
622  Color r(255, 0, 0), g(0, 255, 0), b(0, 0, 255);
623  image_w = std::min(w - 20, 310);
624  image_h = std::min(h - 20, 255);
625 
626  int shift_x = (w - image_w) / 2;
627  int shift_y = (h - image_h) / 2;
628  int line_w = (image_w - 6) / 6;
629  int image_c = image_w / 2;
630  for (auto i = 0; i <= image_h; i++) {
631  int c = esp_scale(i, image_h);
632  this->horizontal_line(shift_x + 0, shift_y + i, line_w, r.fade_to_white(c));
633  this->horizontal_line(shift_x + line_w, shift_y + i, line_w, r.fade_to_black(c)); //
634 
635  this->horizontal_line(shift_x + image_c - line_w, shift_y + i, line_w, g.fade_to_white(c));
636  this->horizontal_line(shift_x + image_c, shift_y + i, line_w, g.fade_to_black(c));
637 
638  this->horizontal_line(shift_x + image_w - (line_w * 2), shift_y + i, line_w, b.fade_to_white(c));
639  this->horizontal_line(shift_x + image_w - line_w, shift_y + i, line_w, b.fade_to_black(c));
640  }
641  this->rectangle(shift_x, shift_y, image_w, image_h, Color(127, 127, 0));
642 
643  uint16_t shift_r = shift_x + line_w - (8 * 3);
644  uint16_t shift_g = shift_x + image_c - (8 * 3);
645  uint16_t shift_b = shift_x + image_w - line_w - (8 * 3);
646  shift_y = h / 2 - (8 * 3);
647  for (auto i = 0; i < 8; i++) {
648  uint8_t ftr = progmem_read_byte(&TESTCARD_FONT[0][i]);
649  uint8_t ftg = progmem_read_byte(&TESTCARD_FONT[1][i]);
650  uint8_t ftb = progmem_read_byte(&TESTCARD_FONT[2][i]);
651  for (auto k = 0; k < 8; k++) {
652  if ((ftr & (1 << k)) != 0) {
653  this->filled_rectangle(shift_r + (i * 6), shift_y + (k * 6), 6, 6, COLOR_OFF);
654  }
655  if ((ftg & (1 << k)) != 0) {
656  this->filled_rectangle(shift_g + (i * 6), shift_y + (k * 6), 6, 6, COLOR_OFF);
657  }
658  if ((ftb & (1 << k)) != 0) {
659  this->filled_rectangle(shift_b + (i * 6), shift_y + (k * 6), 6, 6, COLOR_OFF);
660  }
661  }
662  }
663  }
664  this->rectangle(0, 0, w, h, Color(127, 0, 127));
665  this->filled_rectangle(0, 0, 10, 10, Color(255, 0, 255));
666  this->stop_poller();
667 }
668 
670 void DisplayPage::show() { this->parent_->show_page(this); }
671 void DisplayPage::show_next() { this->next_->show(); }
672 void DisplayPage::show_prev() { this->prev_->show(); }
673 void DisplayPage::set_parent(Display *parent) { this->parent_ = parent; }
674 void DisplayPage::set_prev(DisplayPage *prev) { this->prev_ = prev; }
675 void DisplayPage::set_next(DisplayPage *next) { this->next_ = next; }
676 const display_writer_t &DisplayPage::get_writer() const { return this->writer_; }
677 
678 } // namespace display
679 } // namespace esphome
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 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
optional< display_writer_t > writer_
Definition: display.h:659
std::vector< DisplayOnPageChangeTrigger * > on_page_change_triggers_
Definition: display.h:662
void set_pages(std::vector< DisplayPage *> pages)
Definition: display.cpp:484
bool clamp_x_(int x, int w, int &min_x, int &max_x)
Definition: display.cpp:582
void sort_triangle_points_by_y_(int *x1, int *y1, int *x2, int *y2, int *x3, int *y3)
Definition: display.cpp:164
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:18
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:405
void set_next(DisplayPage *next)
Definition: display.cpp:675
void set_parent(Display *parent)
Definition: display.cpp:673
uint16_t x
Definition: tt21100.cpp:17
Color fade_to_black(uint8_t amnt)
Definition: color.h:155
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:181
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
void set_rotation(DisplayRotation rotation)
Internal method to set the display rotation with.
Definition: display.cpp:17
const uint8_t TESTCARD_FONT [3][8] PROGMEM
Definition: display.cpp:613
Color fade_to_white(uint8_t amnt)
Definition: color.h:154
const Color COLOR_OFF(0, 0, 0, 0)
Turn the pixel OFF.
Definition: display.h:190
void extend_clipping(Rect rect)
Add a rectangular region to the invalidation region.
Definition: display.cpp:553
STL namespace.
void shrink_clipping(Rect rect)
substract a rectangular region to the invalidation region
Definition: display.cpp:560
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 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,y3] with the given color.
Definition: display.cpp:159
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
bool has_value() const
Definition: optional.h:87
virtual void fill(Color color)
Fill the entire screen with the given color.
Definition: display.cpp:15
DisplayPage(display_writer_t writer)
Definition: display.cpp:669
Rect get_clipping() const
Get the current the clipping rectangle.
Definition: display.cpp:567
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:393
virtual int get_width()
Get the calculated width of the display in pixels with rotation applied.
Definition: display.h:215
const float ROTATION_270_DEGREES
Definition: display.h:164
bool clamp_y_(int y, int h, int &min_y, int &max_y)
Definition: display.cpp:597
virtual void draw(int x, int y, Display *display, Color color_on, Color color_off)=0
uint16_t y
Definition: tt21100.cpp:18
const display_writer_t & get_writer() const
Definition: display.cpp:676
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:261
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
virtual void print(int x, int y, Display *display, Color color, const char *text, Color background)=0
TextAlign
TextAlign is used to tell the display class how to position a piece of text.
Definition: display.h:52
DisplayPage * page_
Definition: display.h:660
std::function< void(Display &)> display_writer_t
Definition: display.h:180
void process(DisplayPage *from, DisplayPage *to)
Definition: display.cpp:519
void start_clipping(Rect rect)
Set the clipping rectangle for further drawing.
Definition: display.cpp:539
ImageAlign
ImageAlign is used to tell the display class how to position a image.
Definition: display.h:102
void clear()
Clear the entire screen by filling it with OFF pixels.
Definition: display.cpp:16
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 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:337
void draw_legend(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color)
Definition: graph.cpp:335
const float ROTATION_0_DEGREES
Definition: display.h:160
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
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:389
void menu(int x, int y, graphical_display_menu::GraphicalDisplayMenu *menu, int width, int height)
Definition: display.cpp:399
void draw(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color, int scale)
Definition: qr_code.cpp:36
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:325
void vprintf_(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format, va_list arg)
Definition: display.cpp:344
display_writer_t writer_
Definition: display.h:681
DisplayRotation rotation_
Definition: display.h:658
void end_clipping()
Reset the invalidation region.
Definition: display.cpp:546
virtual int get_height()
Get the calculated height of the display in pixels with rotation applied.
Definition: display.h:217
DisplayPage * previous_page_
Definition: display.h:661
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
uint8_t progmem_read_byte(const uint8_t *addr)
Definition: core.cpp:55
const Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
Definition: display.h:192
virtual DisplayType get_display_type()=0
Get the type of display that the buffer corresponds to.
uint8_t h
Definition: bl0939.h:21
virtual int get_width() const =0
bool clip(int x, int y)
Check if pixel is within region of display.
Definition: display.cpp:575
void void void void void void strftime(int x, int y, BaseFont *font, Color color, 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:523
uint16_t length
Definition: tt21100.cpp:12
void draw_pixel_at(int x, int y)
Set a single pixel at the specified coordinates to default color.
Definition: display.h:225
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,y] with font.
Definition: display.cpp:452
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
static Color to_color(uint32_t colorcode, ColorOrder color_order, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
void draw(display::Display *display, const display::Rect *bounds)
void draw(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color)
Definition: graph.cpp:59
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:275
virtual void measure(const char *str, int *width, int *x_offset, int *baseline, int *height)=0
std::vector< Rect > clipping_rectangle_
Definition: display.h:664
void set_writer(display_writer_t &&writer)
Internal method to set the display writer lambda.
Definition: display.cpp:483
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:388
void shrink(Rect rect)
Definition: rect.cpp:42
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:352
void show_page(DisplayPage *page)
Definition: display.cpp:496
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:298
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&#39;s buffer...
Definition: display.cpp:54
void set_prev(DisplayPage *prev)
Definition: display.cpp:674
virtual int get_height() const =0