roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
ili9341.h
Go to the documentation of this file.
1#pragma once
2
6#include "roo_threads.h"
7#include "roo_threads/thread.h"
8
9namespace roo_display {
10
11namespace ili9341 {
12
13static const int16_t kDefaultWidth = 240;
14static const int16_t kDefaultHeight = 320;
15
16static const uint32_t kSpiFrequency = 40 * 1000 * 1000;
17
19
20enum Command {
21 NOP = 0x00,
22 SWRESET = 0x01,
23
24 RDDIDIF = 0x04,
25 RDDST = 0x09,
26 RDDPM = 0x0A,
27 RDDMADCTL = 0x0B,
28 RDDCOLMOD = 0x0C,
29 RDDIM = 0x0D,
30 RDDSM = 0x0E,
31 RDDSDR = 0x0F,
32
33 SLPIN = 0x10,
34 SLPOUT = 0x11,
35 GAMSET = 0x26,
36 DISPOFF = 0x28,
37 DISPON = 0x29,
38
39 CASET = 0x2A,
40 PASET = 0x2B,
41 RAMWR = 0x2C,
42
43 MADCTL = 0x36,
44 PIXSET = 0x3A,
45
46 FRMCTR1 = 0xB1,
47 FRMCTR2 = 0xB2,
48 FRMCTR3 = 0xB3,
49 INVTR = 0xB4,
50 PRCTR = 0xB5,
51 DISCTRL = 0xB6,
52
53 PWCTRL1 = 0xC0,
54 PWCTRL2 = 0xC1,
55 PWCTRL3 = 0xC2,
56 VMCTRL1 = 0xC5,
57 VMCTRL2 = 0xC7,
58
59 PGAMCTRL = 0xE0,
60 NGAMCTRL = 0xE1,
61};
62
63enum MadCtl {
64 MY = 0x80,
65 MX = 0x40,
66 MV = 0x20,
67 ML = 0x10,
68 BGR = 0x08,
69 MH = 0x04
70};
71
72template <typename Transport>
74 public:
76 static constexpr ByteOrder byte_order = roo_io::kBigEndian;
77
80 : transport_(),
81 width_(width),
82 height_(height),
83 last_x0_(-1),
84 last_x1_(-1),
85 last_y0_(-1),
86 last_y1_(-1) {}
87
90 : transport_(std::move(transport)),
91 width_(width),
92 height_(height),
93 last_x0_(-1),
94 last_x1_(-1),
95 last_y0_(-1),
96 last_y1_(-1) {}
97
98 int16_t width() const { return width_; }
99 int16_t height() const { return height_; }
100
101 void begin() {
102 transport_.beginWriteOnlyTransaction();
103 transport_.begin();
104 }
105
106 void end() {
107 transport_.end();
108 transport_.endTransaction();
109 }
110
111 void init() {
112 transport_.init();
113 begin();
114
115 writeCommand(0xEF, {0x03, 0x80, 0x02});
116 writeCommand(0xCF, {0x00, 0xC1, 0x30});
117 writeCommand(0xED, {0x64, 0x03, 0x12, 0x81});
118 writeCommand(0xE8, {0x85, 0x00, 0x78});
119 writeCommand(0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02});
120 writeCommand(0xF7, {0x20});
121 writeCommand(0xEA, {0x00, 0x00});
122
123 writeCommand(PWCTRL1, {0x23}); // VRH[5:0]
124 writeCommand(PWCTRL2, {0x10}); // SAP[2:0];BT[3:0]
125
126 writeCommand(VMCTRL1, {0x3E, 0x28});
127 writeCommand(VMCTRL2, {0x86});
128
129 writeCommand(MADCTL, {0x48}); // portrait mode
130
131 writeCommand(PIXSET, {0x55});
132
133 // 0x18 79Hz, 0x1B default 70Hz, 0x13 100Hz
134 writeCommand(FRMCTR1, {0x00, 0x13});
135
136 writeCommand(DISCTRL, {0x08, 0x82, 0x27});
137
138 writeCommand(0xF2, {0x00}); // 3Gamma Function Disable
139
140 // Set Gamma curve.
141 writeCommand(GAMSET, {0x01});
142
143 // Set Gamma.
144 writeCommand(PGAMCTRL, {0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1,
145 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00});
146 writeCommand(NGAMCTRL, {0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1,
147 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F});
148
149 writeCommand(SLPOUT);
150
151 end();
152 sleep_ms(120);
153
154 begin();
155 writeCommand(DISPON);
156 end();
157 }
158
159 void setOrientation(Orientation orientation) {
160 uint8_t d = BGR | (orientation.isXYswapped() ? MV : 0) |
161 (orientation.isTopToBottom() ? 0 : MY) |
162 (orientation.isRightToLeft() ? 0 : MX);
163 writeCommand(MADCTL);
164 transport_.write(d);
165 }
166
168 __attribute__((always_inline)) {
169 if (last_x0_ != x0 || last_x1_ != x1) {
170 writeCommand(CASET);
171 transport_.write16x2(x0, x1);
172 last_x0_ = x0;
173 last_x1_ = x1;
174 }
175 if (last_y0_ != y0 || last_y1_ != y1) {
176 writeCommand(PASET);
177 transport_.write16x2(y0, y1);
178 last_y0_ = y0;
179 last_y1_ = y1;
180 }
181 }
182
183 void startRamWrite() __attribute__((always_inline)) { writeCommand(RAMWR); }
184
185 void flush() __attribute__((always_inline)) { transport_.flush(); }
186
187 void ramWrite(const roo::byte* data, size_t pixel_count)
188 __attribute__((always_inline)) {
189 transport_.writeBytes(data, pixel_count * 2);
190 }
191
192 void ramFill(const roo::byte* data, size_t pixel_count)
193 __attribute__((always_inline)) {
194 transport_.fill16(data, pixel_count);
195 }
196
197 void ramWriteAsyncBlit(const roo::byte* data, size_t row_stride_bytes,
198 size_t row_bytes, size_t row_count)
199 __attribute__((always_inline)) {
200 transport_.async_blit(data, row_stride_bytes, row_bytes, row_count);
201 }
202
203 private:
204 void writeCommand(uint8_t c) __attribute__((always_inline)) {
205 transport_.cmdBegin();
206 transport_.write(c);
207 transport_.cmdEnd();
208 }
209
210 void writeCommand(uint8_t c, const std::initializer_list<uint8_t>& d) {
211 writeCommand(c);
212 for (uint8_t i : d) transport_.write(i);
213 }
214
215 void sleep_ms(uint32_t ms) {
216 roo::this_thread::sleep_for(roo_time::Millis(ms));
217 }
218
219 Transport transport_;
220 int16_t width_;
221 int16_t height_;
222 uint16_t last_x0_, last_x1_, last_y0_, last_y1_;
223};
224
225} // namespace ili9341
226
227template <typename Transport>
229
230template <int pinCS, int pinDC, int pinRST, typename Spi = DefaultSpi,
234
235} // namespace roo_display
Represents the orientation of a display device.
Definition orientation.h:25
bool isRightToLeft() const
Return whether horizontal direction is right-to-left.
bool isTopToBottom() const
Return whether vertical direction is top-to-bottom.
bool isXYswapped() const
Return whether x maps to the vertical direction.
Definition orientation.h:90
16-bit RGB565 color mode (opaque).
Ili9341Target(Transport transport, uint16_t width=ili9341::kDefaultWidth, uint16_t height=ili9341::kDefaultHeight)
Definition ili9341.h:88
void ramFill(const roo::byte *data, size_t pixel_count) __attribute__((always_inline))
Definition ili9341.h:192
void flush() __attribute__((always_inline))
Definition ili9341.h:185
void startRamWrite() __attribute__((always_inline))
Definition ili9341.h:183
void ramWrite(const roo::byte *data, size_t pixel_count) __attribute__((always_inline))
Definition ili9341.h:187
Ili9341Target(uint16_t width=ili9341::kDefaultWidth, uint16_t height=ili9341::kDefaultHeight)
Definition ili9341.h:78
void ramWriteAsyncBlit(const roo::byte *data, size_t row_stride_bytes, size_t row_bytes, size_t row_count) __attribute__((always_inline))
Definition ili9341.h:197
static constexpr ByteOrder byte_order
Definition ili9341.h:76
void setOrientation(Orientation orientation)
Definition ili9341.h:159
void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) __attribute__((always_inline))
Definition ili9341.h:167
static const int16_t kDefaultHeight
Definition ili9341.h:14
static const uint32_t kSpiFrequency
Definition ili9341.h:16
SpiSettings< kSpiFrequency, kSpiMsbFirst, kSpiMode0 > DefaultSpiSettings
Definition ili9341.h:18
static const int16_t kDefaultWidth
Definition ili9341.h:13
Defines 140 opaque HTML named colors.
roo_io::ByteOrder ByteOrder
Definition byte_order.h:7