13 static const char *
const TAG =
"display";
22 ESP_LOGE(TAG,
"Could not allocate buffer for display!");
73 const int32_t dx = abs(x2 - x1), sx = x1 < x2 ? 1 : -1;
74 const int32_t dy = -abs(y2 - y1), sy = y1 < y2 ? 1 : -1;
75 int32_t err = dx + dy;
79 if (x1 == x2 && y1 == y2)
94 for (
int i = x; i < x + width; i++)
99 for (
int i = y; i < y + height; i++)
110 for (
int i = y1; i < y1 + height; i++) {
117 int err = 2 - 2 * radius;
128 if (-dx == dy && e2 <= dx) {
138 int dx = -int32_t(radius);
140 int err = 2 - 2 * radius;
148 int hline_width = 2 * (-dx) + 1;
149 this->
horizontal_line(center_x + dx, center_y + dy, hline_width, color);
150 this->
horizontal_line(center_x + dx, center_y - dy, hline_width, color);
154 if (-dx == dy && e2 <= dx) {
165 int x_start, y_start;
167 this->
get_text_bounds(x, y, text, font, align, &x_start, &y_start, &width, &height);
171 while (text[i] !=
'\0') {
176 ESP_LOGW(TAG,
"Encountered character without representation in font: '%c'", text[i]);
178 uint8_t glyph_width = font->
get_glyphs()[0].glyph_data_->width;
179 for (
int glyph_x = 0; glyph_x < glyph_width; glyph_x++) {
180 for (
int glyph_y = 0; glyph_y < height; glyph_y++)
181 this->
draw_pixel_at(glyph_x + x_at, glyph_y + y_start, color);
191 int scan_x1, scan_y1, scan_width, scan_height;
192 glyph.
scan_area(&scan_x1, &scan_y1, &scan_width, &scan_height);
194 for (
int glyph_x = scan_x1; glyph_x < scan_x1 + scan_width; glyph_x++) {
195 for (
int glyph_y = scan_y1; glyph_y < scan_y1 + scan_height; glyph_y++) {
197 this->
draw_pixel_at(glyph_x + x_at, glyph_y + y_start, color);
209 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
211 this->
print(x, y, font, color, align, buffer);
217 for (
int img_x = 0; img_x < image->
get_width(); img_x++) {
218 for (
int img_y = 0; img_y < image->
get_height(); img_y++) {
224 for (
int img_x = 0; img_x < image->
get_width(); img_x++) {
225 for (
int img_y = 0; img_y < image->
get_height(); img_y++) {
231 for (
int img_x = 0; img_x < image->
get_width(); img_x++) {
232 for (
int img_y = 0; img_y < image->
get_height(); img_y++) {
238 for (
int img_x = 0; img_x < image->
get_width(); img_x++) {
239 for (
int img_y = 0; img_y < image->
get_height(); img_y++) {
246 for (
int img_x = 0; img_x < image->
get_width(); img_x++) {
247 for (
int img_y = 0; img_y < image->
get_height(); img_y++) {
264 qr_code->
draw(
this, x, y, color_on, scale);
266 #endif // USE_QR_CODE 269 int *width,
int *height) {
270 int x_offset, baseline;
271 font->
measure(text, width, &x_offset, &baseline, height);
273 auto x_align =
TextAlign(
int(align) & 0x18);
274 auto y_align =
TextAlign(
int(align) & 0x07);
281 *x1 = x - (*width) / 2;
298 *y1 = y - (*height) / 2;
310 this->
print(x, y, font, COLOR_ON, align, text);
317 va_start(arg, format);
318 this->
vprintf_(x, y, font, color, align, format, arg);
323 va_start(arg, format);
329 va_start(arg, format);
330 this->
vprintf_(x, y, font, COLOR_ON, align, format, arg);
335 va_start(arg, format);
341 for (
auto *page : pages)
342 page->set_parent(
this);
344 for (uint32_t i = 0; i < pages.size() - 1; i++) {
345 pages[i]->set_next(pages[i + 1]);
346 pages[i + 1]->set_prev(pages[i]);
348 pages[0]->set_prev(pages[pages.size() - 1]);
349 pages[pages.size() - 1]->set_next(pages[0]);
366 if (this->
page_ !=
nullptr) {
373 if ((this->from_ ==
nullptr || this->from_ == from) && (this->to_ ==
nullptr || this->to_ == to))
374 this->trigger(from, to);
380 size_t ret = time.
strftime(buffer,
sizeof(buffer), format);
382 this->
print(x, y, font, color, align, buffer);
388 this->
strftime(x, y, font, COLOR_ON, align, format, time);
396 const int x_data = x - this->glyph_data_->offset_x;
397 const int y_data = y - this->glyph_data_->offset_y;
398 if (x_data < 0 || x_data >= this->glyph_data_->width || y_data < 0 || y_data >= this->glyph_data_->height)
400 const uint32_t width_8 = ((this->glyph_data_->width + 7u) / 8u) * 8u;
401 const uint32_t pos = x_data + y_data * width_8;
402 return progmem_read_byte(this->glyph_data_->data + (pos / 8u)) & (0x80 >> (pos % 8u));
408 for (uint32_t i = 0;; i++) {
409 if (this->glyph_data_->a_char[i] ==
'\0')
413 if (this->glyph_data_->a_char[i] > str[i])
415 if (this->glyph_data_->a_char[i] < str[i])
422 for (uint32_t i = 0;; i++) {
423 if (this->glyph_data_->a_char[i] ==
'\0')
425 if (str[i] != this->glyph_data_->a_char[i])
432 *x1 = this->glyph_data_->offset_x;
433 *y1 = this->glyph_data_->offset_y;
434 *width = this->glyph_data_->width;
435 *height = this->glyph_data_->height;
439 int hi = this->glyphs_.size() - 1;
441 int mid = (lo + hi + 1) / 2;
442 if (this->glyphs_[mid].compare_to(str)) {
448 *match_length = this->glyphs_[lo].match_length(str);
449 if (*match_length <= 0)
453 void Font::measure(
const char *str,
int *width,
int *x_offset,
int *baseline,
int *height) {
454 *baseline = this->baseline_;
455 *height = this->bottom_;
458 bool has_char =
false;
460 while (str[i] !=
'\0') {
462 int glyph_n = this->match_next_glyph(str + i, &match_length);
465 if (!this->get_glyphs().empty())
466 x += this->get_glyphs()[0].glyph_data_->width;
471 const Glyph &glyph = this->glyphs_[glyph_n];
486 Font::Font(
const GlyphData *data,
int data_nr,
int baseline,
int bottom) : baseline_(baseline), bottom_(bottom) {
487 for (
int i = 0; i < data_nr; ++i)
488 glyphs_.emplace_back(data + i);
492 if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
494 const uint32_t width_8 = ((this->width_ + 7u) / 8u) * 8u;
495 const uint32_t pos = x + y * width_8;
499 if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
501 const uint32_t pos = (x + y * this->width_) * 3;
505 return Color(color32);
508 if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
510 const uint32_t pos = (x + y * this->width_) * 2;
513 auto r = (rgb565 & 0xF800) >> 11;
514 auto g = (rgb565 & 0x07E0) >> 5;
515 auto b = rgb565 & 0x001F;
516 return Color((r << 3) | (r >> 2), (g << 2) | (g >> 4), (b << 3) | (b >> 2));
519 if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
521 const uint32_t pos = (x + y * this->width_);
523 return Color(gray | gray << 8 | gray << 16 | gray << 24);
529 : width_(width), height_(height), type_(type), data_start_(data_start) {}
532 if (x < 0 || x >= this->
width_ || y < 0 || y >= this->
height_)
534 const uint32_t width_8 = ((this->
width_ + 7u) / 8u) * 8u;
535 const uint32_t frame_index = this->
height_ * width_8 * this->current_frame_;
536 if (frame_index >= (uint32_t)(this->
width_ * this->
height_ * this->animation_frame_count_))
538 const uint32_t pos = x + y * width_8 + frame_index;
542 if (x < 0 || x >= this->
width_ || y < 0 || y >= this->
height_)
544 const uint32_t frame_index = this->
width_ * this->
height_ * this->current_frame_;
545 if (frame_index >= (uint32_t)(this->
width_ * this->
height_ * this->animation_frame_count_))
547 const uint32_t pos = (x + y * this->
width_ + frame_index) * 3;
551 return Color(color32);
554 if (x < 0 || x >= this->
width_ || y < 0 || y >= this->
height_)
556 const uint32_t frame_index = this->
width_ * this->
height_ * this->current_frame_;
557 if (frame_index >= (uint32_t)(this->
width_ * this->
height_ * this->animation_frame_count_))
559 const uint32_t pos = (x + y * this->
width_ + frame_index) * 2;
562 auto r = (rgb565 & 0xF800) >> 11;
563 auto g = (rgb565 & 0x07E0) >> 5;
564 auto b = rgb565 & 0x001F;
565 return Color((r << 3) | (r >> 2), (g << 2) | (g >> 4), (b << 3) | (b >> 2));
568 if (x < 0 || x >= this->
width_ || y < 0 || y >= this->
height_)
570 const uint32_t frame_index = this->
width_ * this->
height_ * this->current_frame_;
571 if (frame_index >= (uint32_t)(this->
width_ * this->
height_ * this->animation_frame_count_))
573 const uint32_t pos = (x + y * this->
width_ + frame_index);
575 return Color(gray | gray << 8 | gray << 16 | gray << 24);
578 :
Image(data_start, width, height, type), current_frame_(0), animation_frame_count_(animation_frame_count) {}
void printf(int x, int y, Font *font, Color color, 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.
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.
void void void void void strftime(int x, int y, Font *font, Color color, TextAlign align, const char *format, time::ESPTime time) __attribute__((format(strftime
Evaluate the strftime-format format and print the result with the anchor point at [x...
virtual Color get_grayscale_pixel(int x, int y) const
void show_page(DisplayPage *page)
virtual void draw_absolute_pixel_internal(int x, int y, Color color)=0
void draw(display::DisplayBuffer *buff, uint16_t x_offset, uint16_t y_offset, Color color, int scale)
void set_pages(std::vector< DisplayPage *> pages)
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.
Image(const uint8_t *data_start, int width, int height, ImageType type)
void scan_area(int *x1, int *y1, int *width, int *height) const
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...
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...
void set_next(DisplayPage *next)
void set_rotation(DisplayRotation rotation)
Internal method to set the display rotation with.
Color get_rgb565_pixel(int x, int y) const override
void clear()
Clear the entire screen by filling it with OFF pixels.
optional< display_writer_t > writer_
const Color COLOR_OFF(0, 0, 0, 0)
Turn the pixel OFF.
An STL allocator that uses SPI RAM.
int get_current_frame() const
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...
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.
virtual Color get_color_pixel(int x, int y) const
virtual int get_width_internal()=0
bool get_pixel(int x, int y) const
DisplayPage(display_writer_t writer)
void vprintf_(int x, int y, Font *font, Color color, TextAlign align, const char *format, va_list arg)
const uint8_t * data_start_
void print(int x, int y, Font *font, Color color, TextAlign align, const char *text)
Print text with the anchor point at [x,y] with font.
const display_writer_t & get_writer() const
void init_internal_(uint32_t buffer_length)
bool compare_to(const char *str) const
DisplayPage * previous_page_
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.
ImageType get_type() const
int animation_frame_count_
virtual void fill(Color color)
Fill the entire screen with the given color.
Color get_grayscale_pixel(int x, int y) const override
TextAlign
TextAlign is used to tell the display class how to position a piece of text.
void measure(const char *str, int *width, int *x_offset, int *baseline, int *height)
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.
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.
A more user-friendly version of struct tm from time.h.
void process(DisplayPage *from, DisplayPage *to)
const std::vector< Glyph > & get_glyphs() const
int match_length(const char *str) const
Color get_color_pixel(int x, int y) const override
int match_next_glyph(const char *str, int *match_length)
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...
int get_animation_frame_count() const
std::vector< Glyph > glyphs_
DisplayRotation rotation_
Application App
Global storage of Application pointer - only one Application can exist.
void set_writer(display_writer_t &&writer)
Internal method to set the display writer lambda.
void void void void void void void void void image(int x, int y, Image *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.
std::vector< DisplayOnPageChangeTrigger * > on_page_change_triggers_
void swap(optional< T > &x, optional< T > &y)
uint8_t progmem_read_byte(const uint8_t *addr)
const Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
void draw(display::DisplayBuffer *buff, uint16_t x_offset, uint16_t y_offset, Color color)
virtual int get_height_internal()=0
Font(const GlyphData *data, int data_nr, int baseline, int bottom)
Construct the font with the given glyphs.
void get_text_bounds(int x, int y, const char *text, Font *font, TextAlign align, int *x1, int *y1, int *width, int *height)
Get the text bounds of the given string.
virtual Color get_rgb565_pixel(int x, int y) const
const GlyphData * glyph_data_
const char * get_char() const
std::function< void(DisplayBuffer &)> display_writer_t
void draw_legend(display::DisplayBuffer *buff, uint16_t x_offset, uint16_t y_offset, Color color)
void set_parent(DisplayBuffer *parent)
void draw_pixel_at(int x, int y, Color color=COLOR_ON)
Set a single pixel at the specified coordinates to the given color.
Animation(const uint8_t *data_start, int width, int height, uint32_t animation_frame_count, ImageType type)
int get_height()
Get the height of the image in pixels with rotation applied.
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+...
virtual bool get_pixel(int x, int y) const
void set_prev(DisplayPage *prev)
int get_width()
Get the width of the image in pixels with rotation applied.
bool get_pixel(int x, int y) const override