TinyGPU
Loading...
Searching...
No Matches
DisplayDriver.h
Go to the documentation of this file.
1#pragma once
2#include <stddef.h>
3
4#include "TinyGPU/Font/Font5x7.h"
5#include "TinyGPU/Surface/ISurface.h"
6
7namespace tinygpu {
8
9/**
10 * @brief Abstract base class for display drivers.
11 *
12 * Defines the interface for initializing the display, setting the address
13 * window, and writing pixel data from a Surface. Specific display drivers (e.g.
14 * SPI, SDL) should inherit from this and implement the virtual methods.
15 */
16
17/// Common rotation values for rectangular display drivers (row/column
18/// exchange plus optional axis mirroring) - shared here, rather than
19/// duplicated as a nested enum per concrete driver (as ILI9341Driver/
20/// ILI9342Driver used to each define their own, identical, copy), so
21/// generic code holding only a DisplayDriver<RGB_T>& (e.g. an LCDBoard's
22/// display()) can call setRotation() without knowing the concrete driver
23/// type. Concrete drivers keep a `using Rotation = tinygpu::DisplayRotation;`
24/// alias so existing code written against e.g. ILI9341Driver<RGB565>::
25/// Rotation still compiles unchanged. Named DisplayRotation (not just
26/// Rotation) to avoid colliding with the differently-shaped touch
27/// tinygpu::Rotation (Deg0/Deg90/Deg180/Deg270) in TouchDriver.h.
28enum class DisplayRotation {
29 kNone = -1,
30 kPortrait = 0,
31 kLandscape = 1,
34};
35
36/// True for the landscape family (kLandscape/kLandscapeFlipped) - false
37/// for portrait (kPortrait/kPortraitFlipped) or kNone. The landscape
38/// family has its row/column addressing exchanged relative to portrait
39/// (MV bit, on MIPI-DCS-style controllers), which is what makes
40/// width()/height() swap between the two families - a same-family
41/// rotation (e.g. kLandscape to kLandscapeFlipped, a 180 degree flip)
42/// only changes scan direction, not addressable width/height.
44 return r == DisplayRotation::kLandscape ||
46}
47
48template <typename RGB_T = RGB565>
50 public:
51 virtual ~DisplayDriver() = default;
52 virtual bool begin() = 0;
53 virtual void end() {}
54 virtual bool writeData(ISurface<RGB_T>& surface) = 0;
55 virtual bool writeData(ISurface<RGB_T>& surface, size_t x, size_t y) = 0;
56
57 /// Changes the panel's rotation after begin(). Default no-op, for
58 /// drivers with no such concept (e.g. a fixed-orientation panel, or a
59 /// desktop/SDL backend) - concrete SPI drivers (ILI9341Driver,
60 /// ILI9342Driver) override this to reprogram MADCTL.
61 virtual void setRotation(DisplayRotation rotation) {}
62
63 /// Current addressable panel width/height in pixels - for a driver
64 /// that supports setRotation(), these reflect the *current* rotation
65 /// (landscape vs portrait swaps them; see isLandscapeFamily()), not
66 /// necessarily the panel's native/physical orientation. Callers that
67 /// need to lay out content (e.g. OutputTinyGPU's scale_to_fit target,
68 /// or clearScreen()'s band loop) should read these fresh rather than
69 /// caching their own copy, so a setRotation() call is picked up
70 /// automatically without needing separate bookkeeping.
71 virtual size_t width() const = 0;
72 virtual size_t height() const = 0;
73
74 void writeColor(size_t width, size_t height, RGB_T color) {
75 Sprite<RGB_T> tempSurface(width, 1, defaultFont);
76 tempSurface.begin();
77 for (int i = 0; i < width; ++i) tempSurface.setPixel(i, 0, color);
78
79 for (int j = 0; j < height; ++j) {
80 writeData(tempSurface, 0, j);
81 }
82 }
83
84 protected:
85 virtual bool setAddressWindow(size_t x, size_t y, size_t w, size_t h) = 0;
87};
88
89} // namespace tinygpu
Definition: DisplayDriver.h:49
virtual bool setAddressWindow(size_t x, size_t y, size_t w, size_t h)=0
virtual size_t height() const =0
void writeColor(size_t width, size_t height, RGB_T color)
Definition: DisplayDriver.h:74
virtual void setRotation(DisplayRotation rotation)
Definition: DisplayDriver.h:61
Font5x7< RGB_T > defaultFont
Definition: DisplayDriver.h:86
virtual bool writeData(ISurface< RGB_T > &surface, size_t x, size_t y)=0
virtual bool writeData(ISurface< RGB_T > &surface)=0
virtual size_t width() const =0
virtual ~DisplayDriver()=default
virtual void end()
Definition: DisplayDriver.h:53
virtual bool begin()=0
Fixed-size 5x7 bitmap font with ASCII and extended character support.
Definition: Font5x7.h:23
RGB color stored in 16-bit RGB565 format, byte-swapped from the conventional bit layout.
Definition: RGB565.h:24
Definition: DSIBusESP32.h:19
bool isLandscapeFamily(DisplayRotation r)
Definition: DisplayDriver.h:43
DisplayRotation
Abstract base class for display drivers.
Definition: DisplayDriver.h:28