-
Notifications
You must be signed in to change notification settings - Fork 1
/
ELDisplay_ILI9341Win.h
481 lines (408 loc) · 10.2 KB
/
ELDisplay_ILI9341Win.h
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#ifdef WIN32
#ifndef _ELDISPLAY_ILI9341WIN_
#define _ELDISPLAY_ILI9341WIN_
/*
Author: Brent Pease (embeddedlibraryfeedback@gmail.com)
The MIT License (MIT)
Copyright (c) 2015-FOREVER Brent Pease
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
ABOUT
*/
#include <Windows.h>
#include <Windowsx.h>
#include "ELDisplay.h"
class ITouchDriver_XPT2046;
static ITouchDriver_XPT2046* gTouchDriverXPT;
class ITouchDriver_XPT2046 : public ITouchDriver
{
public:
ITouchDriver_XPT2046(
uint8_t inCS)
{
gTouchDriverXPT = this;
mouseDown = false;
}
virtual bool
GetTouch(
SDisplayPoint& outTouchLoc)
{
outTouchLoc = mouseLoc;
return mouseDown;
}
bool mouseDown;
SDisplayPoint mouseLoc;
};
class CDisplayDriver_ILI9341 : public CModule, public IDisplayDriver
{
public:
MModule_Declaration(
CDisplayDriver_ILI9341,
EDisplayOrientation inDisplayOrientation,
uint8_t inCS,
uint8_t inDC,
uint8_t inMOSI,
uint8_t inClk,
uint8_t inMISO)
virtual int16_t
GetWidth(
void)
{
return displayWidth;
}
virtual int16_t
GetHeight(
void)
{
return displayHeight;
}
virtual void
BeginDrawing(
void)
{
drawingActive = true;
}
virtual void
EndDrawing(
void)
{
drawingActive = false;
}
virtual void
FillScreen(
SDisplayColor const& inColor)
{
MReturnOnError(!drawingActive);
hdc = GetDC(hwnd);
uint16_t color = inColor.GetRGB565();
for(int x = 0; x < displayWidth; ++x)
{
for(int y = 0; y < displayHeight; ++y)
{
displayMemory[y][x] = color;
uint8_t r = color >> 11;
uint8_t g = (color >> 5) & 0x3F;
uint8_t b = color & 0x1F;
SetPixel(hdc, x, y, RGB(r << 3, g << 2, b << 3));
}
}
ReleaseDC(hwnd, hdc);
}
virtual void
FillRect(
SDisplayRect const& inRect,
SDisplayColor const& inColor)
{
MReturnOnError(!drawingActive);
SDisplayRect clippedRect;
clippedRect.Intersect(displayPort, inRect);
if(clippedRect.IsEmpty())
{
return;
}
hdc = GetDC(hwnd);
uint16_t color = inColor.GetRGB565();
for(int x = inRect.topLeft.x; x < inRect.bottomRight.x; ++x)
{
for(int y = inRect.topLeft.y; y < inRect.bottomRight.y; ++y)
{
displayMemory[y][x] = color;
uint8_t r = color >> 11;
uint8_t g = (color >> 5) & 0x3F;
uint8_t b = color & 0x1F;
SetPixel(hdc, x, y, RGB(r << 3, g << 2, b << 3));
}
}
ReleaseDC(hwnd, hdc);
}
virtual void
DrawRect(
SDisplayRect const& inRect,
SDisplayColor const& inColor)
{
DrawHLine(inRect.topLeft, inRect.GetWidth(), inColor);
DrawHLine(SDisplayPoint(inRect.topLeft.x, inRect.bottomRight.y - 1), inRect.GetWidth(), inColor);
DrawVLine(inRect.topLeft, inRect.GetHeight(), inColor);
DrawVLine(SDisplayPoint(inRect.bottomRight.x - 1, inRect.topLeft.y), inRect.GetHeight(), inColor);
}
virtual void
DrawLine(
SDisplayPoint const& inPointA,
SDisplayPoint const& inPointB,
SDisplayColor const& inColor)
{
}
virtual void
DrawPixel(
SDisplayPoint const& inPoint,
SDisplayColor const& inColor)
{
MReturnOnError(!drawingActive);
uint16_t color = inColor.GetRGB565();
hdc = GetDC(hwnd);
displayMemory[inPoint.y][inPoint.x] = color;
uint8_t r = color >> 11;
uint8_t g = (color >> 5) & 0x3F;
uint8_t b = color & 0x1F;
SetPixel(hdc, inPoint.x, inPoint.y, RGB(r << 3, g << 2, b << 3));
ReleaseDC(hwnd, hdc);
}
virtual void
DrawContinuousStart(
SDisplayRect const& inRect,
SDisplayColor const& inFGColor,
SDisplayColor const& inBGColor)
{
MReturnOnError(!drawingActive);
SDisplayRect clippedRect;
clippedRect.Intersect(displayPort, inRect);
continuousTotalClip = clippedRect.IsEmpty();
if(continuousTotalClip)
{
return;
}
hdc = GetDC(hwnd);
continuousRect = inRect;
fgColor = inFGColor.GetRGB565();
bgColor = inBGColor.GetRGB565();
continuousX = inRect.topLeft.x;
continuousY = inRect.topLeft.y;
}
virtual void
DrawContinuousBits(
int16_t inPixelCount,
uint16_t inSrcBitStartIndex,
uint8_t const* inSrcBitData)
{
MReturnOnError(!drawingActive);
if(continuousTotalClip)
{
return;
}
while(inPixelCount-- > 0)
{
if(continuousX >= 0 && continuousX < displayWidth && continuousY >= 0 && continuousY < displayHeight)
{
if(inSrcBitData[inSrcBitStartIndex >> 3] & (1 << (7 - (inSrcBitStartIndex & 0x7))))
{
displayMemory[continuousY][continuousX] = fgColor;
uint8_t r = fgColor >> 11;
uint8_t g = (fgColor >> 5) & 0x3F;
uint8_t b = fgColor & 0x1F;
SetPixel(hdc, continuousX, continuousY, RGB(r << 3, g << 2, b << 3));
}
else
{
displayMemory[continuousY][continuousX] = bgColor;
uint8_t r = bgColor >> 11;
uint8_t g = (bgColor >> 5) & 0x3F;
uint8_t b = bgColor & 0x1F;
SetPixel(hdc, continuousX, continuousY, RGB(r << 3, g << 2, b << 3));
}
}
++continuousX;
if(continuousX >= continuousRect.bottomRight.x)
{
continuousX = continuousRect.topLeft.x;
++continuousY;
}
++inSrcBitStartIndex;
}
}
virtual void
DrawContinuousSolid(
int16_t inPixelCount,
bool inUseForeground)
{
MReturnOnError(!drawingActive);
if(continuousTotalClip)
{
return;
}
while(inPixelCount-- > 0)
{
if(continuousX >= 0 && continuousX < displayWidth && continuousY >= 0 && continuousY < displayHeight)
{
if(inUseForeground)
{
displayMemory[continuousY][continuousX] = fgColor;
uint8_t r = fgColor >> 11;
uint8_t g = (fgColor >> 5) & 0x3F;
uint8_t b = fgColor & 0x1F;
SetPixel(hdc, continuousX, continuousY, RGB(r << 3, g << 2, b << 3));
}
else
{
displayMemory[continuousY][continuousX] = bgColor;
uint8_t r = bgColor >> 11;
uint8_t g = (bgColor >> 5) & 0x3F;
uint8_t b = bgColor & 0x1F;
SetPixel(hdc, continuousX, continuousY, RGB(r << 3, g << 2, b << 3));
}
}
++continuousX;
if(continuousX >= continuousRect.bottomRight.x)
{
continuousX = continuousRect.topLeft.x;
++continuousY;
}
}
}
virtual void
DrawContinuousEnd(
void)
{
MReturnOnError(!drawingActive);
ReleaseDC(hwnd, hdc);
}
void
DrawHLine(
SDisplayPoint const& inStart,
int16_t inPixelsRight,
SDisplayColor const& inColor)
{
if(inStart.y < 0 || inStart.y >= displayHeight)
{
return;
}
hdc = GetDC(hwnd);
uint16_t color = inColor.GetRGB565();
for(int i = 0; i < inPixelsRight; ++i)
{
int16_t x = inStart.x + i;
if(x >= 0 && x < displayWidth)
{
displayMemory[inStart.y][x] = color;
uint8_t r = color >> 11;
uint8_t g = (color >> 5) & 0x3F;
uint8_t b = color & 0x1F;
SetPixel(hdc, x, inStart.y, RGB(r << 3, g << 2, b << 3));
}
}
ReleaseDC(hwnd, hdc);
}
void
DrawVLine(
SDisplayPoint const& inStart,
int16_t inPixelsDown,
SDisplayColor const& inColor)
{
if(inStart.x < 0 || inStart.x >= displayWidth)
{
return;
}
hdc = GetDC(hwnd);
uint16_t color = inColor.GetRGB565();
for(int i = 0; i < inPixelsDown; ++i)
{
int16_t y = inStart.y + i;
if(y >= 0 && y < displayHeight)
{
displayMemory[y][inStart.x] = color;
uint8_t r = color >> 11;
uint8_t g = (color >> 5) & 0x3F;
uint8_t b = color & 0x1F;
SetPixel(hdc, inStart.x, y, RGB(r << 3, g << 2, b << 3));
}
}
ReleaseDC(hwnd, hdc);
}
private:
CDisplayDriver_ILI9341(
EDisplayOrientation inDisplayOrientation,
uint8_t inCS,
uint8_t inDC,
uint8_t inMOSI,
uint8_t inClk,
uint8_t inMISO)
:
displayOrientation(inDisplayOrientation),
cs(inCS), dc(inDC), mosi(inMOSI), clk(inClk), miso(inMISO),
CModule(0, 0, NULL, 10 * 1000)
{
MReturnOnError(!((mosi == 11 || mosi == 7) && (miso == 12 || miso == 8) && (clk == 13 || clk == 14)));
me = this;
displayWidth = 320;
displayHeight = 240;
displayPort.topLeft.x = 0;
displayPort.topLeft.y = 0;
displayPort.bottomRight.x = displayWidth;
displayPort.bottomRight.y = displayHeight;
drawingActive = false;
WNDCLASSEX ex;
ex.cbSize = sizeof(WNDCLASSEX);
ex.style = CS_OWNDC;
ex.lpfnWndProc = WinProc;
ex.cbClsExtra = 0;
ex.cbWndExtra = 0;
ex.hInstance = GetModuleHandle(0);
ex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
ex.hCursor = LoadCursor(NULL, IDC_ARROW);
ex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
ex.lpszMenuName = NULL;
ex.lpszClassName = L"wndclass";
ex.hIconSm = NULL;
RegisterClassEx(&ex);
hwnd =
CreateWindowEx(
NULL,
L"wndclass",
L"Window",
WS_OVERLAPPED | WS_VISIBLE,
100, 100,
displayWidth + 50, displayHeight + 50,
NULL,
NULL,
GetModuleHandle(0),
this);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
}
virtual void
Update(
uint32_t inDeltaTimeUS)
{
MSG msg;
if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
static LRESULT CALLBACK
WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
static CDisplayDriver_ILI9341* me;
EDisplayOrientation displayOrientation;
int16_t displayWidth;
int16_t displayHeight;
uint8_t cs, dc, mosi, clk, miso;
uint8_t pcs_data, pcs_command;
bool drawingActive;
SDisplayRect displayPort;
bool continuousTotalClip;
int16_t continuousX;
int16_t continuousY;
SDisplayRect continuousRect;
uint16_t fgColor;
uint16_t bgColor;
uint16_t displayMemory[240][320];
HDC hdc;
HWND hwnd;
};
#endif
#endif