roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
TFT_eSPI_adapter.h
Go to the documentation of this file.
1#pragma once
2
3// If you want to use this adapter, include TFT_eSPI.h before including this
4// file. You must have configured TFT_eSPI first, by editing the User_Setup.h as
5// needed.
6//
7// Example main.cpp:
8//
9// #include "Arduino.h"
10// #include "TFT_eSPI.h"
11//
12// using namespace roo_display;
13//
14// TFT_eSPI_Adapter device(Orientation().rotateLeft());
15// Display display(device);
16//
17// // (The rest is identical to regular roo_display usage, except you do NOT
18// // initialize the SPI directly).
19
20#ifdef TFT_ESPI_VERSION
21
28
29namespace roo_display {
30
31class TFT_eSPI_Adapter : public DisplayDevice {
32 public:
33 using ColorMode = Rgb565;
34 static constexpr ColorPixelOrder pixel_order = ColorPixelOrder::kMsbFirst;
35 static constexpr ByteOrder byte_order = roo_io::kBigEndian;
36
37 TFT_eSPI_Adapter(uint16_t width, uint16_t height)
38 : TFT_eSPI_Adapter(Orientation(), width, height) {}
39
40 TFT_eSPI_Adapter(Orientation orientation = Orientation())
41 : TFT_eSPI_Adapter(orientation, TFT_WIDTH, TFT_HEIGHT) {}
42
43 TFT_eSPI_Adapter(Orientation orientation, uint16_t width, uint16_t height)
44 : DisplayDevice(orientation, width, height),
45 tft_(width, height),
46 bgcolor_(0xFF7F7F7F),
47 compactor_() {}
48
49 ~TFT_eSPI_Adapter() override {}
50
51 void init() override {
52 end();
53 tft_.init();
54 begin();
55 tft_.setRotation(orientation().getRotationCount());
56 }
57
58 void begin() override { tft_.startWrite(); }
59
60 void end() override { tft_.endWrite(); }
61
62 void setBgColorHint(Color bgcolor) override { bgcolor_ = bgcolor; }
63
64 void setAddress(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
65 BlendingMode mode) override {
66 blending_mode_ = mode;
67 tft_.setWindow(x0, y0, x1, y1);
68 }
69
70 void write(Color* color, uint32_t pixel_count) override {
71 uint16_t buffer[64];
72 tft_.setSwapBytes(true);
73 while (pixel_count > 64) {
74 color = processColorSequence(blending_mode_, color, buffer, 64);
75 tft_.pushPixels(buffer, 64);
76 pixel_count -= 64;
77 }
78 processColorSequence(blending_mode_, color, buffer, pixel_count);
79 tft_.pushPixels(buffer, pixel_count);
80 tft_.setSwapBytes(false);
81 }
82
83 void fill(Color color, uint32_t pixel_count) override {
84 ApplyBlendingOverBackground(blending_mode_, bgcolor_, &color, 1);
85 tft_.pushColor(to_raw_color(color), pixel_count);
86 }
87
88 void writeRects(BlendingMode mode, Color* color, int16_t* x0, int16_t* y0,
89 int16_t* x1, int16_t* y1, uint16_t count) override {
90 while (count-- > 0) {
91 uint32_t pixel_count = (*x1 - *x0 + 1) * (*y1 - *y0 + 1);
92 TFT_eSPI_Adapter::setAddress(*x0++, *y0++, *x1++, *y1++,
94 Color mycolor = *color++;
95 ApplyBlendingOverBackground(mode, bgcolor_, &mycolor, 1);
96 uint16_t raw_color = to_raw_color(mycolor);
97 tft_.pushBlock(raw_color, pixel_count);
98 }
99 }
100
101 void fillRects(BlendingMode mode, Color color, int16_t* x0, int16_t* y0,
102 int16_t* x1, int16_t* y1, uint16_t count) override {
103 ApplyBlendingOverBackground(mode, bgcolor_, &color, 1);
104 uint16_t raw_color = to_raw_color(color);
105
106 while (count-- > 0) {
107 uint32_t pixel_count = (*x1 - *x0 + 1) * (*y1 - *y0 + 1);
108 TFT_eSPI_Adapter::setAddress(*x0++, *y0++, *x1++, *y1++,
110 tft_.pushBlock(raw_color, pixel_count);
111 }
112 }
113
114 void writePixels(BlendingMode mode, Color* colors, int16_t* xs, int16_t* ys,
115 uint16_t pixel_count) override {
116 compactor_.drawPixels(
117 xs, ys, pixel_count,
118 [this, mode, colors](int16_t offset, int16_t x, int16_t y,
120 int16_t count) {
121 switch (direction) {
122 case Compactor::RIGHT: {
123 TFT_eSPI_Adapter::setAddress(x, y, x + count - 1, y, mode);
124 break;
125 }
126 case Compactor::DOWN: {
127 TFT_eSPI_Adapter::setAddress(x, y, x, y + count - 1, mode);
128 break;
129 }
130 case Compactor::LEFT: {
131 TFT_eSPI_Adapter::setAddress(x - count + 1, y, x, y, mode);
132 std::reverse(colors + offset, colors + offset + count);
133 break;
134 }
135 case Compactor::UP: {
136 TFT_eSPI_Adapter::setAddress(x, y - count + 1, x, y, mode);
137 std::reverse(colors + offset, colors + offset + count);
138 break;
139 }
140 }
141 TFT_eSPI_Adapter::write(colors + offset, count);
142 });
143 }
144
145 const ColorFormat& getColorFormat() const override {
146 static const internal::ColorFormatImpl<Rgb565, roo_io::kBigEndian,
148 format(color_mode());
149 return format;
150 }
151
152 const Rgb565& color_mode() const {
153 static const Rgb565 mode;
154 return mode;
155 }
156
157 void fillPixels(BlendingMode mode, Color color, int16_t* xs, int16_t* ys,
158 uint16_t pixel_count) override {
159 ApplyBlendingOverBackground(mode, bgcolor_, &color, 1);
160 uint16_t raw_color = to_raw_color(color);
161 compactor_.drawPixels(
162 xs, ys, pixel_count,
163 [this, raw_color](int16_t offset, int16_t x, int16_t y,
164 Compactor::WriteDirection direction, int16_t count) {
165 switch (direction) {
166 case Compactor::RIGHT: {
167 TFT_eSPI_Adapter::setAddress(x, y, x + count - 1, y,
169 break;
170 }
171 case Compactor::DOWN: {
172 TFT_eSPI_Adapter::setAddress(x, y, x, y + count - 1,
174 break;
175 }
176 case Compactor::LEFT: {
177 TFT_eSPI_Adapter::setAddress(x - count + 1, y, x, y,
179 break;
180 }
181 case Compactor::UP: {
182 TFT_eSPI_Adapter::setAddress(x, y - count + 1, x, y,
184 break;
185 }
186 }
187 tft_.pushBlock(raw_color, count);
188 });
189 }
190
191 void orientationUpdated() override {
192 tft_.setRotation(orientation().getRotationCount());
193 }
194
195 static inline uint16_t to_raw_color(Color color)
196 __attribute__((always_inline)) {
197 return Rgb565().fromArgbColor(color);
198 }
199
200 protected:
201 TFT_eSPI tft_;
202
203 private:
204 Color* processColorSequence(BlendingMode mode, Color* src, uint16_t* dest,
205 uint32_t pixel_count) {
206 ApplyBlendingOverBackground(mode, bgcolor_, src, pixel_count);
207 while (pixel_count-- > 0) {
208 *dest++ = to_raw_color(*src++);
209 }
210 return src;
211 }
212
213 Color bgcolor_;
214 BlendingMode blending_mode_;
215 Compactor compactor_;
216};
217
218} // namespace roo_display
219
220#endif
Defines 140 opaque HTML named colors.
void ApplyBlendingOverBackground(BlendingMode mode, Color bg, Color *src, int16_t count)
Definition blending.h:588
BlendingMode
Porter-Duff style blending modes.
Definition blending.h:17
@ kSource
The new ARGB8888 value completely replaces the old one.
roo_io::ByteOrder ByteOrder
Definition byte_order.h:7
Color bgcolor
Definition smooth.cpp:889