TinyGPU
Loading...
Searching...
No Matches
DisplayDriverTFTeSPI.h
Go to the documentation of this file.
1#pragma once
2#include <TFT_eSPI.h> // https://github.com/Bodmer/TFT_eSPI
3
4#include "TinyGPU/Drivers/DisplayDriver.h"
5#include "TinyGPU/Color/RGB565.h"
6
7namespace tinygpu {
8
9/**
10 * @brief Display driver that renders a TinyGPU surface through Bodmer's
11 * TFT_eSPI (https://github.com/Bodmer/TFT_eSPI) instead of one of this
12 * library's own SPI panel drivers (DisplayDriverSPI/QSPI/DSI) - useful if
13 * you already have a working TFT_eSPI `User_Setup.h` for your panel and
14 * would rather reuse it than duplicate the same init sequence/pin wiring
15 * here.
16 *
17 * TFT_eSPI is a separate, optional Arduino library - install it alongside
18 * TinyGPU and `#include <TinyGPU/Drivers/DisplayDriverTFTeSPI.h>`
19 * explicitly (this header is not pulled in by TinyGPU/DisplayDriver.h or
20 * TinyGPU.h) to opt in. All of the panel-specific configuration (which
21 * controller, which pins, SPI frequency, ...) is TFT_eSPI's own
22 * `User_Setup.h`/`User_Setup_Select.h` job, same as any other TFT_eSPI
23 * sketch - this driver only bridges TinyGPU's ISurface/writeData() calls
24 * onto TFT_eSPI's own API.
25 *
26 * Usage:
27 * TFT_eSPI tft;
28 * DisplayDriverTFTeSPI<RGB565> display(tft);
29 * display.begin(); // calls tft.init() and tft.setRotation()
30 * display.writeData(surface);
31 *
32 * Only a 16bpp RGB_T (RGB565) is supported, since that's the only pixel
33 * format TFT_eSPI's pushImage() accepts.
34 */
35template <typename RGB_T = RGB565>
36class DisplayDriverTFTeSPI : public DisplayDriver<RGB_T> {
37 static_assert(sizeof(RGB_T) == 2,
38 "DisplayDriverTFTeSPI assumes a 16bpp RGB_T (RGB565) - "
39 "TFT_eSPI::pushImage() only accepts that format.");
40
41 public:
42 /// Wraps an existing TFT_eSPI instance (ownership not taken - construct
43 /// and keep it alive for as long as this driver is used, the same way
44 /// DisplayDriverSPI takes its SPIClass& rather than owning one).
45 explicit DisplayDriverTFTeSPI(TFT_eSPI& tft,
47 : tft_(tft), rotation_(rotation) {}
48
49 /// Initializes TFT_eSPI (tft.init()) and applies the rotation passed to
50 /// the constructor, if any.
51 bool begin() override {
52 tft_.init();
55 }
56 return true;
57 }
58
59 bool writeData(ISurface<RGB_T>& surface) override {
60 return writeData(surface, 0, 0);
61 }
62
63 bool writeData(ISurface<RGB_T>& surface, size_t x, size_t y) override {
64 // RGB_T (RGB565) values are stored in the buffer already byte-swapped
65 // to the panel's wire order (see RGB565.h) - the same big-endian
66 // order TFT_eSPI::pushImage() expects by default (setSwapBytes(true)
67 // is only needed for sources in the *native*/little-endian 16-bit
68 // layout, e.g. raw LVGL buffers - not this one), so the buffer's raw
69 // memory can be handed to pushImage() as-is, with no per-pixel swap.
70 tft_.pushImage(static_cast<int32_t>(x), static_cast<int32_t>(y),
71 static_cast<int32_t>(surface.width()),
72 static_cast<int32_t>(surface.height()),
73 reinterpret_cast<const uint16_t*>(surface.data()));
74 return true;
75 }
76
77 /// Changes the panel's rotation - forwards directly to
78 /// TFT_eSPI::setRotation(), whose 0-3 values already match
79 /// DisplayRotation's own kPortrait/kLandscape/kPortraitFlipped/
80 /// kLandscapeFlipped numbering. kNone is a no-op (nothing to apply).
81 void setRotation(DisplayRotation rotation) override {
82 if (rotation == DisplayRotation::kNone) return;
83 rotation_ = rotation;
84 tft_.setRotation(static_cast<uint8_t>(rotation));
85 }
86
87 size_t width() const override { return static_cast<size_t>(tft_.width()); }
88 size_t height() const override {
89 return static_cast<size_t>(tft_.height());
90 }
91
92 protected:
95
96 bool setAddressWindow(size_t x, size_t y, size_t w, size_t h) override {
97 tft_.setAddrWindow(static_cast<int32_t>(x), static_cast<int32_t>(y),
98 static_cast<int32_t>(w), static_cast<int32_t>(h));
99 return true;
100 }
101};
102
103} // namespace tinygpu
Display driver that renders a TinyGPU surface through Bodmer's TFT_eSPI (https://github....
Definition: DisplayDriverTFTeSPI.h:36
size_t height() const override
Definition: DisplayDriverTFTeSPI.h:88
void setRotation(DisplayRotation rotation) override
Definition: DisplayDriverTFTeSPI.h:81
bool writeData(ISurface< RGB_T > &surface, size_t x, size_t y) override
Definition: DisplayDriverTFTeSPI.h:63
TFT_eSPI & tft_
Definition: DisplayDriverTFTeSPI.h:93
size_t width() const override
Definition: DisplayDriverTFTeSPI.h:87
DisplayRotation rotation_
Definition: DisplayDriverTFTeSPI.h:94
bool begin() override
Definition: DisplayDriverTFTeSPI.h:51
bool setAddressWindow(size_t x, size_t y, size_t w, size_t h) override
Definition: DisplayDriverTFTeSPI.h:96
DisplayDriverTFTeSPI(TFT_eSPI &tft, DisplayRotation rotation=DisplayRotation::kNone)
Definition: DisplayDriverTFTeSPI.h:45
bool writeData(ISurface< RGB_T > &surface) override
Definition: DisplayDriverTFTeSPI.h:59
Definition: DisplayDriver.h:49
RGB color stored in 16-bit RGB565 format, byte-swapped from the conventional bit layout.
Definition: RGB565.h:24
Definition: DSIBusESP32.h:19
DisplayRotation
Abstract base class for display drivers.
Definition: DisplayDriver.h:28