roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
ili9486.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
8#include "roo_threads.h"
9#include "roo_threads/thread.h"
10
11namespace roo_display {
12
13namespace ili9486 {
14
15static const int16_t kDefaultWidth = 320;
16static const int16_t kDefaultHeight = 480;
17
18static const uint32_t kSpiFrequency = 20 * 1000 * 1000;
19
21
22enum Command {
23 NOP = 0x00,
24 SWRESET = 0x01,
25
26 SLPIN = 0x10,
27 SLPOUT = 0x11,
28
29 INVOFF = 0x20,
30
31 DISPOFF = 0x28,
32 DISPON = 0x29,
33 CASET = 0x2A,
34 PASET = 0x2B,
35 RAMWR = 0x2C,
36
37 MADCTL = 0x36,
38 PIXSET = 0x3A,
39
40 PWCTRL1 = 0xC0,
41 PWCTRL2 = 0xC1,
42 PWCTRL3 = 0xC2,
43 VMCTRL1 = 0xC5,
44 VMCTRL2 = 0xC7,
45
46 PGAMCTRL = 0xE0,
47 NGAMCTRL = 0xE1,
48};
49
50enum MadCtl {
51 MY = 0x80,
52 MX = 0x40,
53 MV = 0x20,
54 ML = 0x10,
55 BGR = 0x08,
56 MH = 0x04
57};
58
59template <typename Transport>
61 public:
63 static constexpr ByteOrder byte_order = roo_io::kBigEndian;
64
67 : transport_(),
68 width_(width),
69 height_(height),
70 last_x0_(-1),
71 last_x1_(-1),
72 last_y0_(-1),
73 last_y1_(-1) {}
74
77 : transport_(std::move(transport)),
78 width_(width),
79 height_(height),
80 last_x0_(-1),
81 last_x1_(-1),
82 last_y0_(-1),
83 last_y1_(-1) {}
84
85 int16_t width() const { return width_; }
86
87 int16_t height() const { return height_; }
88
89 void begin() {
90 transport_.beginWriteOnlyTransaction();
91 transport_.begin();
92 }
93
94 void end() {
95 transport_.end();
96 transport_.endTransaction();
97 }
98
99 void init() {
100 transport_.init();
101 // Soft reset.
102 begin();
103 writeCommand(SWRESET);
104 end();
105 sleep_ms(5);
106
107 begin();
108 writeCommand(SLPOUT); // Sleep out, also SW reset
109 sleep_ms(120);
110
111 writeCommand(PIXSET, {0x55}); // 16 bits / pixel
112
113 // The first byte affects shadows and the overall brightness; the second -
114 // lights.
115 writeCommand(PWCTRL1, {0x08, 0x08});
116
117 writeCommand(PWCTRL3, {0x44}); // The highest quality (2H / 8H)
118 writeCommand(VMCTRL1, {0x00, 0x00, 0x00, 0x00}); // VCOM Control 1
119
120 writeCommand(PGAMCTRL, {0x0F, 0x1F, 0x1C, 0x0C, 0x0F, 0x08, 0x48, 0x98,
121 0x37, 0x0A, 0x13, 0x04, 0x11, 0x0D, 0x00});
122 writeCommand(NGAMCTRL, {0x0F, 0x32, 0x1C, 0x0B, 0x0D, 0x05, 0x50, 0x75,
123 0x37, 0x06, 0x10, 0x03, 0x10, 0x10, 0x00});
124
125 writeCommand(INVOFF);
126 writeCommand(MADCTL, {0x48});
127 end();
128 sleep_ms(120);
129
130 begin();
131 writeCommand(DISPON);
132 end();
133 sleep_ms(120);
134 }
135
136 void setOrientation(Orientation orientation) {
137 writeCommand(MADCTL);
138 transport_.write16(BGR | (orientation.isXYswapped() ? MV : 0) |
139 (orientation.isTopToBottom() ? 0 : MY) |
140 (orientation.isRightToLeft() ? 0 : MX));
141 }
142
144 __attribute__((always_inline)) {
145 if (last_x0_ != x0 || last_x1_ != x1) {
146 writeCommand(CASET);
147 roo::byte xBin[] = {
148 roo::byte{0}, static_cast<roo::byte>(x0 >> 8),
149 roo::byte{0}, static_cast<roo::byte>(x0 >> 0),
150 roo::byte{0}, static_cast<roo::byte>(x1 >> 8),
151 roo::byte{0}, static_cast<roo::byte>(x1 >> 0),
152 };
153 transport_.writeBytes(xBin, 8);
154 last_x0_ = x0;
155 last_x1_ = x1;
156 }
157 if (last_y0_ != y0 || last_y1_ != y1) {
158 writeCommand(PASET);
159 roo::byte yBin[] = {
160 roo::byte{0}, static_cast<roo::byte>(y0 >> 8),
161 roo::byte{0}, static_cast<roo::byte>(y0 >> 0),
162 roo::byte{0}, static_cast<roo::byte>(y1 >> 8),
163 roo::byte{0}, static_cast<roo::byte>(y1 >> 0),
164 };
165 transport_.writeBytes(yBin, 8);
166 last_y0_ = y0;
167 last_y1_ = y1;
168 }
169 }
170
171 void startRamWrite() __attribute__((always_inline)) { writeCommand(RAMWR); }
172
173 void flush() __attribute__((always_inline)) { transport_.flush(); }
174
175 void ramWrite(const roo::byte* data, size_t pixel_count)
176 __attribute__((always_inline)) {
177 transport_.writeBytes(data, pixel_count * 2);
178 }
179
180 void ramFill(const roo::byte* data, size_t pixel_count)
181 __attribute__((always_inline)) {
182 transport_.fill16(data, pixel_count);
183 }
184
185 void ramWriteAsyncBlit(const roo::byte* data, size_t row_stride_bytes,
186 size_t row_bytes, size_t row_count)
187 __attribute__((always_inline)) {
188 transport_.async_blit(data, row_stride_bytes, row_bytes, row_count);
189 }
190
191 private:
192 void writeCommand(uint8_t c) __attribute__((always_inline)) {
193 transport_.cmdBegin();
194 transport_.write16(c);
195 transport_.cmdEnd();
196 }
197
198 void writeCommand(uint8_t c, const std::initializer_list<uint8_t>& d) {
199 writeCommand(c);
200 for (uint8_t i : d) transport_.write16(i);
201 }
202
203 void sleep_ms(uint32_t ms) {
204 roo::this_thread::sleep_for(roo_time::Millis(ms));
205 }
206
207 Transport transport_;
208 int16_t width_;
209 int16_t height_;
210 uint16_t last_x0_, last_x1_, last_y0_, last_y1_;
211};
212
213} // namespace ili9486
214
215template <typename Transport>
217
218template <int pinCS, int pinDC, int pinRST, typename Spi = DefaultSpi,
222
223} // 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).
void ramWrite(const roo::byte *data, size_t pixel_count) __attribute__((always_inline))
Definition ili9486.h:175
void flush() __attribute__((always_inline))
Definition ili9486.h:173
void ramFill(const roo::byte *data, size_t pixel_count) __attribute__((always_inline))
Definition ili9486.h:180
void startRamWrite() __attribute__((always_inline))
Definition ili9486.h:171
Ili9486Target(Transport transport, uint16_t width=kDefaultWidth, uint16_t height=kDefaultHeight)
Definition ili9486.h:75
Ili9486Target(uint16_t width=kDefaultWidth, uint16_t height=kDefaultHeight)
Definition ili9486.h:65
void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) __attribute__((always_inline))
Definition ili9486.h:143
void setOrientation(Orientation orientation)
Definition ili9486.h:136
void ramWriteAsyncBlit(const roo::byte *data, size_t row_stride_bytes, size_t row_bytes, size_t row_count) __attribute__((always_inline))
Definition ili9486.h:185
static constexpr ByteOrder byte_order
Definition ili9486.h:63
SpiSettings< kSpiFrequency, kSpiMsbFirst, kSpiMode0 > DefaultSpiSettings
Definition ili9486.h:20
static const int16_t kDefaultHeight
Definition ili9486.h:16
static const uint32_t kSpiFrequency
Definition ili9486.h:18
static const int16_t kDefaultWidth
Definition ili9486.h:15
Defines 140 opaque HTML named colors.
roo_io::ByteOrder ByteOrder
Definition byte_order.h:7