Repository navigation
Expand file tree
/
Copy pathPanelController.cpp
More file actions
278 lines (254 loc) · 8.22 KB
/
Copy pathPanelController.cpp
File metadata and controls
278 lines (254 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Portions derived from LovyanGFX (https://github.com/lovyan03/LovyanGFX),
// Copyright (c) lovyan03 and contributors, FreeBSD licence: Panel_LCD's
// rotation arithmetic and window encoding, Panel_GC9xxx's and Panel_SSD1351's
// windows, Panel_Device's reset timing. See NOTICE.
#include "PanelController.h"
#include "DekiGPIO.h" // from deki-gpio
#include <deki/LogSystem.h>
#include <utility>
namespace DekiDisplay
{
namespace
{
// COLMOD's pixel formats.
constexpr uint8_t kColmodRGB565 = 0x55;
constexpr uint8_t kColmodRGB666 = 0x66;
} // namespace
PanelController::PanelController(IPanelIO& io, DelayFn delay)
: m_IO(io),
m_Delay(delay),
m_Info(&GetControllerInfo(Controller::ST7789))
{
}
bool PanelController::Init(const PanelConfig& config, const InitSequence* init)
{
m_Config = config;
m_Info = &GetControllerInfo(config.controller);
if (m_Config.memoryWidth <= 0)
{
m_Config.memoryWidth = m_Info->memoryWidth;
}
if (m_Config.memoryHeight <= 0)
{
m_Config.memoryHeight = m_Info->memoryHeight;
}
// Reset as LovyanGFX does: low for 8 ms, high, then 64 ms for the
// controller to start. The waits stay without a reset line, giving a
// panel that has just been powered the same time.
DekiGpio::IDekiGPIO* gpio = DekiGpio::DekiGPIO::GetCurrent();
if (gpio && m_Config.resetPin >= 0)
{
gpio->SetOutput(m_Config.resetPin, false);
}
if (m_Delay)
{
m_Delay(8);
}
if (gpio && m_Config.resetPin >= 0)
{
gpio->Write(m_Config.resetPin, true);
}
if (m_Delay)
{
m_Delay(64);
}
const InitSequence defaults = InitSequenceFromTable(m_Info->defaultInit);
const InitSequence& steps = (init && !init->empty()) ? *init : defaults;
if (!m_IO.Begin())
{
DEKI_LOG_ERROR("PanelController: cannot take the bus to start the %s", m_Info->name);
return false;
}
bool ok = true;
// No reset line: a software reset puts the controller's registers back to
// their defaults, as the line would. Without it they keep what the
// firmware before this one set, since restarting the chip does not reset
// the panel: a byte order or gamma that was never in this start-up.
// A MIPI DCS controller takes commands 5 ms after it, and leaves sleep
// only 120 ms after it. Not sent twice: some start-ups begin with one.
const uint8_t softwareReset = m_Info->commands.softwareReset;
if (m_Config.resetPin < 0 && softwareReset != 0 && (steps.empty() || steps.front().cmd != softwareReset))
{
ok = m_IO.Command(softwareReset, nullptr, 0);
if (m_Delay)
{
m_Delay(120);
}
}
for (const InitStep& step : steps)
{
ok = m_IO.Command(step.cmd, step.params.data(), step.params.size()) && ok;
if (step.delayMs > 0 && m_Delay)
{
m_Delay(step.delayMs);
}
}
m_IO.End();
ok = SetInvert(m_Config.invert) && ok;
if (m_Info->commands.colmod != 0)
{
const uint8_t format = m_Config.pixels18 ? kColmodRGB666 : kColmodRGB565;
ok = m_IO.Command(m_Info->commands.colmod, &format, 1) && ok;
}
ok = SetRotation(m_Config.rotation) && ok;
if (!ok)
{
DEKI_LOG_ERROR("PanelController: the %s did not take its start-up", m_Info->name);
}
return ok;
}
bool PanelController::WriteInOwnOrientation()
{
const uint8_t screen = m_ScreenRotation;
const bool ok = SetRotation(0);
m_ScreenRotation = screen;
return ok;
}
bool PanelController::SetRotation(uint8_t rotation)
{
m_ScreenRotation = rotation & 7;
// Panel_LCD::setRotation with no offset rotation.
m_Rotation = rotation & 7;
const uint8_t r = m_Rotation;
int32_t ox = m_Config.offsetX;
int32_t oy = m_Config.offsetY;
int32_t pw = m_Config.panelWidth;
int32_t ph = m_Config.panelHeight;
int32_t mw = m_Config.memoryWidth;
int32_t mh = m_Config.memoryHeight;
if (r & 1)
{
std::swap(ox, oy);
std::swap(pw, ph);
std::swap(mw, mh);
}
m_Width = pw;
m_Height = ph;
if (m_Info->offsetsFollowRotation)
{
m_ColStart = (r & 2) ? mw - (pw + ox) : ox;
// Rotations 1, 2, 4 and 7 count rows from the other end of memory.
m_RowStart = ((1 << r) & 0b10010110) ? mh - (ph + oy) : oy;
}
else
{
// Panel_AMOLED: the offset as it is, only swapped with the axes.
m_ColStart = ox;
m_RowStart = oy;
}
ForgetWindow();
return SendMadctl();
}
bool PanelController::SendMadctl()
{
const uint8_t madctl = static_cast<uint8_t>(m_Info->madctl[m_Rotation] | (m_Config.rgbOrder ? 0 : m_Info->bgrBit));
return m_IO.Command(m_Info->commands.madctl, &madctl, 1);
}
bool PanelController::SetInvert(bool invert)
{
m_Config.invert = invert;
const ControllerCommands& c = m_Info->commands;
return m_IO.Command(invert ? c.invertOn : c.invertOff, nullptr, 0);
}
bool PanelController::SetSleep(bool sleep)
{
const ControllerCommands& c = m_Info->commands;
const bool ok = m_IO.Command(sleep ? c.sleepIn : c.sleepOut, nullptr, 0);
// The controller needs 120 ms after SLPOUT before the next SLPIN, and
// 5 ms after SLPIN before it is asleep.
if (m_Delay)
{
m_Delay(sleep ? 5 : 120);
}
return ok;
}
bool PanelController::SetDisplayOn(bool on)
{
const ControllerCommands& c = m_Info->commands;
return m_IO.Command(on ? c.displayOn : c.displayOff, nullptr, 0);
}
bool PanelController::SetBrightness(uint8_t level)
{
return HasBrightness() && m_IO.Command(m_Info->brightnessCmd, &level, 1);
}
bool PanelController::BeginWrite()
{
return m_IO.Begin();
}
void PanelController::EndWrite()
{
// With no chip select the panel reads everything as one write until a
// command: the NOP lets another device use the bus between writes.
if (!m_Config.hasChipSelect && m_Info->nopOnEnd)
{
m_IO.Command(m_Info->commands.nop, nullptr, 0);
}
m_IO.End();
}
// A window command's parameters: start and end, each in `bytes` bytes, high
// byte first. Returns how many it wrote.
static size_t PutWindow(uint8_t* out, int32_t start, int32_t end, uint8_t bytes)
{
if (bytes == 1)
{
out[0] = static_cast<uint8_t>(start);
out[1] = static_cast<uint8_t>(end);
return 2;
}
out[0] = static_cast<uint8_t>(start >> 8);
out[1] = static_cast<uint8_t>(start);
out[2] = static_cast<uint8_t>(end >> 8);
out[3] = static_cast<uint8_t>(end);
return 4;
}
bool PanelController::SetWindow(int32_t x0, int32_t y0, int32_t x1, int32_t y1)
{
if (x1 <= x0 || y1 <= y0)
{
return false;
}
const int32_t xs = x0 + m_ColStart;
const int32_t xe = x1 - 1 + m_ColStart;
const int32_t ys = y0 + m_RowStart;
const int32_t ye = y1 - 1 + m_RowStart;
const ControllerCommands& c = m_Info->commands;
const bool turned = (m_Rotation & 1) != 0;
// The SSD1351 turns its address counter: its column command takes rows.
const uint8_t columnsCmd = c.windowTurns && turned ? c.rows : c.columns;
const uint8_t rowsCmd = c.windowTurns && turned ? c.columns : c.rows;
bool ok = true;
uint8_t params[4];
if (xs != m_Xs || xe != m_Xe)
{
if (m_Info->openRowsBeforeColumns && turned)
{
// Panel_GC9xxx: turned, the GC9A01 takes a new column range only
// once the rows span everything.
const uint8_t all[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
ok = m_IO.Command(rowsCmd, all, 4);
m_Ys = m_Ye = -1;
}
const size_t n = PutWindow(params, xs, xe, c.windowBytes);
ok = ok && m_IO.Command(columnsCmd, params, n);
m_Xs = ok ? xs : -1;
m_Xe = ok ? xe : -1;
}
if (ok && (ys != m_Ys || ye != m_Ye))
{
const size_t n = PutWindow(params, ys, ye, c.windowBytes);
ok = m_IO.Command(rowsCmd, params, n);
m_Ys = ok ? ys : -1;
m_Ye = ok ? ye : -1;
}
return ok && m_IO.StartPixels(c.memoryWrite);
}
bool PanelController::SendCommand(uint8_t cmd, const uint8_t* params, size_t n)
{
ForgetWindow();
return m_IO.Command(cmd, params, n);
}
void PanelController::ForgetWindow()
{
m_Xs = m_Xe = m_Ys = m_Ye = -1;
}
} // namespace DekiDisplay