-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFR0664.cpp
More file actions
276 lines (224 loc) · 5.64 KB
/
DFR0664.cpp
File metadata and controls
276 lines (224 loc) · 5.64 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
#include "DFR0664.h"
#include "DFR0664_cmd.h"
#include "DFR0664_platform_impl.h"
// might be necessary
#ifdef CHECK_BEGIN
#define ASSERT_BEGIN() do { if (begin) return; } while (0)
#else
#define ASSERT_BEGIN()
#endif
#ifndef abs
#define abs(x) ((x)>0 ?(x) : -(x))
#endif
DFR0664::DFR0664(uint8_t dc, uint8_t cs, uint8_t rst) {
_dc = dc;
_cs = cs;
_rst = rst;
pin_output(dc);
pin_high(dc);
pin_output(cs);
pin_high(cs);
pin_output(rst);
pin_high(rst);
}
void DFR0664::begin() {
pin_low(_rst);
delay_ms(100);
pin_high(_rst);
delay_ms(100);
spi_begin(FREQ);
delay_ms(100);
// Init commands, check datasheet for more info
{
uint8_t param;
sendCommand(0x01);
delay_ms(150);
sendCommand(0x11);
delay_ms(120);
param = 0x55;
sendCommand(0x3A, ¶m, 1);
param = 0x00;
sendCommand(0x36, ¶m, 1);
sendCommand(0x21);
sendCommand(0x13);
sendCommand(0x29);
}
_begun = true;
fillScreen(0);
}
void DFR0664::sendCommand(uint8_t cmd) {
pin_low(_cs);
pin_low(_dc);
spi_transfer(cmd);
pin_high(_cs);
pin_high(_dc);
}
void DFR0664::sendCommand(uint8_t cmd, uint8_t* data, uint32_t len) {
sendCommand(cmd);
pin_low(_cs);
for (int i = 0; i < len; i++) {
spi_transfer(data[i]);
}
pin_high(_cs);
}
void DFR0664::sendCommand_ArgsOnFlash(uint8_t cmd, const uint8_t* data, uint32_t len) {
sendCommand(cmd);
pin_low(_cs);
for (int i = 0; i < len; i++) {
spi_transfer(flash_read(data + i));
}
pin_high(_cs);
}
void DFR0664::sendData8(uint8_t data) {
pin_low(_cs);
spi_transfer(data);
pin_high(_cs);
}
void DFR0664::sendData16(uint16_t data) {
pin_low(_cs);
spi_transfer(data >> 8);
spi_transfer(data);
pin_high(_cs);
}
void DFR0664::setDrawingArea(ucoord_t x, ucoord_t y, ucoord_t w, ucoord_t h) {
sendCommand(RAWSET);
sendData16(x);
sendData16(x + w - 1);
sendCommand(COLSET);
sendData16(y);
sendData16(y + h - 1);
sendCommand(RAMWR); // used by later functions
}
void DFR0664::sendColor(color_t color, uint32_t n) {
uint8_t l = color >> 8;
uint8_t r = color;
pin_low(_cs);
while (n--) {
spi_transfer(l);
spi_transfer(r);
}
pin_high(_cs);
}
#define INLINED_SP
#ifndef INLINED_SP
void DFR0664::setPixel(ucoord_t x, ucoord_t y, color_t color) {
ASSERT_BEGIN();
setDrawingArea(x, y, 1, 1);
sendData16(color);
}
#else
// fully inlined version of setPixel. Approximately 15 us faster. Not absolutely necessary
void DFR0664::setPixel(ucoord_t x, ucoord_t y, color_t color) {
ASSERT_BEGIN();
pin_low(_cs);
pin_low(_dc);
spi_transfer(RAWSET);
pin_high(_cs);
pin_high(_dc);
uint8_t l = x >> 8;
uint8_t r = x;
pin_low(_cs);
spi_transfer(l);
spi_transfer(r);
pin_high(_cs);
pin_low(_cs);
spi_transfer(l);
spi_transfer(r);
pin_high(_cs);
pin_low(_cs);
pin_low(_dc);
spi_transfer(COLSET);
pin_high(_cs);
pin_high(_dc);
l = y >> 8;
r = y;
pin_low(_cs);
spi_transfer(l);
spi_transfer(r);
pin_high(_cs);
pin_low(_cs);
spi_transfer(l);
spi_transfer(r);
pin_high(_cs);
pin_low(_cs);
pin_low(_dc);
spi_transfer(RAMWR);
pin_high(_cs);
pin_high(_dc);
pin_low(_cs);
spi_transfer(color >> 8);
spi_transfer(color);
pin_high(_cs);
}
#endif
void DFR0664::fillRect(ucoord_t x, ucoord_t y, ucoord_t w, ucoord_t h, color_t color) {
ASSERT_BEGIN();
setDrawingArea(x, y, w, h);
sendColor(color, (uint32_t)w * (uint32_t)h); // oh btw it took me 2 hours to realize w * h was converted into a uint32_t AFTER the multiplication, which made an integer overflow if you had to fill more than 65535 pixels. C++ quirks you learn with embedded programming...
}
void DFR0664::fillScreen(color_t color) {
ASSERT_BEGIN();
setDrawingArea(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
sendColor(color, (uint32_t)SCREEN_WIDTH * (uint32_t)SCREEN_HEIGHT);
}
void DFR0664::fillPixels(ucoord_t x, ucoord_t y, ucoord_t w, ucoord_t h, color_t* data) {
ASSERT_BEGIN();
setDrawingArea(x, y, w, h);
uint32_t len = (uint32_t)w * (uint32_t)h;
pin_low(_cs);
while (len--)
{
color_t col = *data++;
spi_transfer(col >> 8);
spi_transfer(col);
}
pin_high(_cs);
}
void DFR0664::drawVLine(ucoord_t x, ucoord_t y, ucoord_t len, color_t color) {
ASSERT_BEGIN();
setDrawingArea(x, y, 1, len);
sendColor(color, len);
}
void DFR0664::drawHLine(ucoord_t x, ucoord_t y, ucoord_t len, color_t color) {
ASSERT_BEGIN();
setDrawingArea(x, y, len, 1);
sendColor(color, len);
}
void DFR0664::drawLine(coord_t x0, coord_t y0, coord_t x1, coord_t y1, color_t color) {
ASSERT_BEGIN();
// Bresenham's drawing algorithm (https://gist.github.com/bert/1085538)
coord_t dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
coord_t dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
coord_t err = dx + dy, e2;
while (1)
{
setPixel(x0, y0, color);
if (x0 == x1 && y0 == y1)
break;
e2 = 2 * err;
if (e2 >= dy)
{
err += dy;
x0 += sx;
}
if (e2 <= dx)
{
err += dx;
y0 += sy;
}
}
}
void DFR0664::setRotation(uint8_t mode) {
ASSERT_BEGIN();
uint8_t reg = 0;
if (mode == 1) {
reg = 0b1100000;
}
else if (mode == 2) {
reg = 0b11000000;
}
else if (mode == 3) {
reg = 0b10100000;
}
sendCommand(MADCTL, ®, 1);
}