forked from TangoCash/lcd4linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drv_ili9486_fb.c
461 lines (378 loc) · 9.58 KB
/
drv_ili9486_fb.c
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
/*
* Copyright (C) 2021 redblue
*
* This file is part of LCD4Linux.
*
* LCD4Linux is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* LCD4Linux is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* Driver use fbtft and fb_ili9486 linux kernel modules
* on orange pi/rapsberry pi hardware
*/
/*
* Hardware for testing: orange pi pc2 with TFT 3.5 Inch LCD Touch Screen SPI RGB Display
*/
/*
* TODO: tochscreen
*/
/*
*
* exported fuctions:
*
* struct DRIVER drv_ili9486_fb
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdint.h>
#include <syslog.h>
#include <byteswap.h>
#include "debug.h"
#include "cfg.h"
#include "qprintf.h"
#include "udelay.h"
#include "plugin.h"
#include "widget.h"
#include "widget_text.h"
#include "widget_icon.h"
#include "widget_bar.h"
#include "drv.h"
#include "drv_generic_graphic.h"
typedef enum { false = 0, true = !false } bool;
static char Name[] = "ili9486_fb";
/* Display data */
static int fd = -1, bpp = 0, stride_bpp_value = 0, xres = 0, yres = 0, stride = 0, backlight = 0;
static unsigned char * newLCD = NULL, * oldLCD = NULL;
#define WIDTH_MAX 480
#define HEIGHT_MAX 320
#define BPP_MAX 16
#define SET_BYTE(INPUT, VALUE, POSITION) (INPUT=(VALUE<<(POSITION<<3))|(INPUT&(0xFFFFFFFF^(0xFF<<(POSITION<<3)))))
static int ili9486_fb_open(const char *dev, int bpp_value, int xres_value, int yres_value)
{
bpp = bpp_value;
xres = xres_value;
yres = yres_value;
switch (bpp)
{
case 8:
stride_bpp_value = 2;
break;
case 15:
case 16:
stride_bpp_value = 4;
break;
case 24:
case 32:
stride_bpp_value = 6;
break;
default:
stride_bpp_value = (bpp + 7) / 8;
}
stride = xres * stride_bpp_value;
fd = open(dev, O_RDWR);
if (fd == -1) {
error("cannot open lcd device\n");
return -1;
}
return 0;
}
static int ili9486_fb_close()
{
if (newLCD)
{
free(newLCD);
newLCD = 0;
}
if (oldLCD)
{
free(oldLCD);
oldLCD = 0;
}
if (-1 != fd)
{
close(fd);
fd=-1;
}
return 0;
}
static int drv_ili9486_fb_open(const char *section)
{
char *dev;
char *size;
char *bpp_dev;
int bpp_value;
int xres_value;
int yres_value;
dev = cfg_get(section, "Port", NULL);
if (dev == NULL || *dev == '\0')
{
error("%s: no '%s.Port' entry from %s", Name, section, cfg_source());
return -1;
}
size = cfg_get(section, "Size", NULL);
if (size == NULL || *size == '\0')
{
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
return -1;
}
if (sscanf(size, "%dx%d", &xres_value, &yres_value) != 2 || xres_value < 1 || yres_value < 1 || xres_value > WIDTH_MAX || yres_value > HEIGHT_MAX) {
error("%s: bad %s.Size '%s' from %s", Name, section, size, cfg_source());
free(size);
return -1;
}
bpp_dev = cfg_get(section, "Bpp", NULL);
if (bpp_dev == NULL || *bpp_dev == '\0')
{
error("%s: no '%s.Bpp' entry from %s", Name, section, cfg_source());
return -1;
}
if (sscanf(bpp_dev, "%d", &bpp_value) != 1 || bpp_value < 1 || bpp_value > BPP_MAX) {
error("%s: bad %s.Bpp '%s' from %s", Name, section, bpp_dev, cfg_source());
free(bpp_dev);
return -1;
}
int h = ili9486_fb_open(dev, bpp_value, xres_value, yres_value);
if (h == -1)
{
error("%s: cannot open ili9486 device %s", Name, dev);
return -1;
}
return 0;
}
static int drv_ili9486_fb_close(void)
{
ili9486_fb_close();
return 0;
}
static void drv_ili9486_fb_set_pixel(int x, int y, RGBA pix)
{
long int location;
unsigned char red = pix.R;
unsigned char green = pix.G;
unsigned char blue = pix.B;
uint32_t data = NULL;
SET_BYTE(data, blue, 0);
SET_BYTE(data, green, 1);
SET_BYTE(data, red, 2);
uint32_t colraw = ((data & 0x00FF0000) >> (16 + 8 - 5) << 11) | // red
((data & 0x0000FF00) >> ( 8 + 8 - 6) << 5) | // green
((data & 0x000000FF) >> ( 0 + 8 - 5) << 0); // blue;
unsigned char col1 = colraw & 0x0000FF;
unsigned char col2 = (colraw & 0x00FF00) >> 8;
location = (x * xres + y) * stride_bpp_value / 2;
*(newLCD + location + 0) = col1;
*(newLCD + location + 1) = col2;
}
static void drv_ili9486_fb_blit(const int row, const int col, const int height, const int width)
{
bool refreshAll = false;
int r, c;
for (r = row; r < row + height; r++)
{
for (c = col; c < col + width; c++)
{
drv_ili9486_fb_set_pixel(r, c, drv_generic_graphic_rgb(r, c));
}
}
for (r = row; r < row + height; r++)
{
for (c = col; c < col + width; c++)
{
if (newLCD != oldLCD)
{
refreshAll = true;
break;
}
}
}
if (refreshAll)
{
for (r = row; r < row + height; r++)
{
for (c = col; c < col + width; c++)
{
memcpy(oldLCD, newLCD, sizeof(newLCD)+1);
}
}
write(fd, newLCD + stride, stride * yres);
}
}
static int drv_ili9486_fb_backlight(int number)
{
return 0;
}
/* start graphic display */
static int drv_ili9486_fb_start(const char *section)
{
int i;
char *s;
s = cfg_get(section, "Font", "6x8");
if (s == NULL || *s == '\0') {
error("%s: no '%s.Font' entry from %s", Name, section, cfg_source());
return -1;
}
XRES = -1;
YRES = -1;
if (sscanf(s, "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
error("%s: bad Font '%s' from %s", Name, s, cfg_source());
return -1;
}
if (XRES < 6 || YRES < 8)
{
error("%s: bad Font '%s' from %s (must be at least 6x8)", Name, s, cfg_source());
return -1;
}
free(s);
// Get the backlight value (0 = off, 10 = max brightness)
if (cfg_number(section, "Backlight", 0, 0, 10, &i) > 0)
backlight = i;
else
backlight = 10;
/* open communication with the display */
if (drv_ili9486_fb_open(section) < 0)
{
return -1;
}
/* you surely want to allocate a framebuffer or something... */
newLCD = (unsigned char *)malloc(yres * stride);
if (newLCD)
memset(newLCD, 0, yres * stride);
if (newLCD == NULL) {
error("%s: newLCD buffer could not be allocated: malloc() failed", Name);
return -1;
}
oldLCD = (unsigned char *)malloc(yres * stride);
if (oldLCD)
memset(oldLCD, 0, yres * stride);
if (oldLCD == NULL) {
error("%s: oldLCD buffer could not be allocated: malloc() failed", Name);
return -1;
}
drv_ili9486_fb_backlight(backlight);
/* set width/height from ili9486 firmware specs */
DROWS = yres;
DCOLS = xres;
info("%s: init succesfully, xres %d, yres %d, bpp %d, stride %d", Name, xres, yres, bpp, stride);
return 0;
}
/****************************************/
/*** plugins ***/
/****************************************/
static void plugin_backlight(RESULT * result, RESULT * arg1)
{
int bl_on;
bl_on = (R2N(arg1) == 0 ? 0 : 1);
drv_ili9486_fb_backlight(bl_on);
SetResult(&result, R_NUMBER, &bl_on);
}
/****************************************/
/*** widget callbacks ***/
/****************************************/
/* using drv_generic_text_draw(W) */
/* using drv_generic_text_icon_draw(W) */
/* using drv_generic_text_bar_draw(W) */
/* using drv_generic_gpio_draw(W) */
/****************************************/
/*** exported functions ***/
/****************************************/
/* list models */
int drv_ili9486_fb_list(void)
{
info("ili9486 OLED driver");
return 0;
}
/* initialize driver & display */
int drv_ili9486_fb_init(const char *section, const int quiet)
{
int ret;
/* real worker functions */
drv_generic_graphic_real_blit = drv_ili9486_fb_blit;
/* start display */
if ((ret = drv_ili9486_fb_start(section)) != 0)
return ret;
/* initialize generic graphic driver */
if ((ret = drv_generic_graphic_init(section, Name)) != 0)
return ret;
drv_generic_graphic_clear();
if (!quiet)
{
char _buffer[40];
qprintf(_buffer, sizeof(_buffer), "%s %dx%d", Name, DCOLS, DROWS);
if (drv_generic_graphic_greet(_buffer, NULL))
{
sleep(3);
drv_generic_graphic_clear();
}
}
/* register plugins */
AddFunction("LCD::backlight", 1, plugin_backlight);
return 0;
}
/* close driver & display */
int drv_ili9486_fb_quit(const int quiet)
{
info("%s: shutting down.", Name);
/* clear display */
drv_generic_graphic_clear();
//read goodby message from /tmp/lcd/goodbye
char line1[80], value[80];
FILE *fp;
int i, size;
fp = fopen("/tmp/lcd/goodbye", "r");
if (!fp) {
debug("couldn't open file '/tmp/lcd/goodbye'");
line1[0] = '\0';
if (!quiet) {
drv_generic_graphic_greet("goodbye!", NULL);
}
} else {
i = 0;
while (!feof(fp) && i++ < 1) {
fgets(value, sizeof(value), fp);
size = strcspn(value, "\r\n");
strncpy(line1, value, size);
line1[size] = '\0';
/* more than 80 chars, chew up rest of line */
while (!feof(fp) && strchr(value, '\n') == NULL) {
fgets(value, sizeof(value), fp);
}
}
fclose(fp);
if (i <= 1) {
debug("'/tmp/lcd/goodbye' seems empty");
line1[0] = '\0';
}
/* remove the file */
debug("removing '/tmp/lcd/goodbye'");
unlink("/tmp/lcd/goodbye");
drv_generic_graphic_greet(NULL, line1);
}
drv_generic_graphic_quit();
debug("closing connection");
drv_ili9486_fb_close();
return (0);
}
DRIVER drv_ili9486_fb = {
.name = Name,
.list = drv_ili9486_fb_list,
.init = drv_ili9486_fb_init,
.quit = drv_ili9486_fb_quit,
};