-
Notifications
You must be signed in to change notification settings - Fork 1
/
ili9163.c
340 lines (273 loc) · 9.54 KB
/
ili9163.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
#include "ili9163.h"
#include <stdio.h> // printf
#include <stdarg.h> // va_list, va_start, va_arg, va_end
uint16_t frameBuffer[BUFSIZE] = {0};
extern SPI_HandleTypeDef DISP_SPI;
extern uint8_t SPI_DMA_FL;
void ILI9163_writeCommand(uint8_t address) {
HAL_GPIO_WritePin(DISP_CS_Port, DISP_CS_Pin, 0);
HAL_GPIO_WritePin(DISP_DC_Port, DISP_DC_Pin, 0);
HAL_SPI_Transmit(&DISP_SPI, &address, 1, 0);
HAL_GPIO_WritePin(DISP_CS_Port, DISP_CS_Pin, 1);
}
void ILI9163_writeData(uint8_t data) {
HAL_GPIO_WritePin(DISP_CS_Port, DISP_CS_Pin, 0);
HAL_GPIO_WritePin(DISP_DC_Port, DISP_DC_Pin, 1);
HAL_SPI_Transmit(&DISP_SPI, &data, 1, 0);
HAL_GPIO_WritePin(DISP_CS_Port, DISP_CS_Pin, 1);
}
void ILI9163_writeData16(uint16_t word) {
HAL_GPIO_WritePin(DISP_CS_Port, DISP_CS_Pin, 0);
HAL_GPIO_WritePin(DISP_DC_Port, DISP_DC_Pin, 1);
uint8_t data [2] = {(word >> 8) & 0x00FF, word & 0x00FF};
HAL_SPI_Transmit(&DISP_SPI, data, 2, 0);
HAL_GPIO_WritePin(DISP_CS_Port, DISP_CS_Pin, 1);
}
void ILI9163_setAddress(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) {
ILI9163_writeCommand(ILI9163_CMD_SET_COLUMN_ADDRESS);
ILI9163_writeData16(x1);
ILI9163_writeData16(x2);
ILI9163_writeCommand(ILI9163_CMD_SET_PAGE_ADDRESS);
ILI9163_writeData16(y1);
ILI9163_writeData16(y2);
ILI9163_writeCommand(ILI9163_CMD_WRITE_MEMORY_START);
}
void ILI9163_reset(void)
{
HAL_GPIO_WritePin(DISP_RST_Port, DISP_RST_Pin, 0);
HAL_Delay(50);
HAL_GPIO_WritePin(DISP_RST_Port, DISP_RST_Pin, 1);
HAL_Delay(100);
}
void ILI9163_init(int rotation) {
HAL_GPIO_WritePin(DISP_CS_Port, DISP_CS_Pin, 1);
HAL_GPIO_WritePin(DISP_RST_Port, DISP_RST_Pin, 1);
ILI9163_reset(); // Hardware reset the LCD
ILI9163_writeCommand(ILI9163_CMD_EXIT_SLEEP_MODE);
HAL_Delay(5); // Wait for the screen to wake up
ILI9163_writeCommand(ILI9163_CMD_SET_PIXEL_FORMAT);
ILI9163_writeData(0x05); // 16 bpp
ILI9163_writeCommand(ILI9163_CMD_SET_GAMMA_CURVE);
ILI9163_writeData(0x04); // Gamma curve 3
ILI9163_writeCommand(ILI9163_CMD_GAM_R_SEL);
ILI9163_writeData(0x01); // Gamma curve enable
ILI9163_writeCommand(ILI9163_CMD_POSITIVE_GAMMA_CORRECT);
ILI9163_writeData(0x3f);
ILI9163_writeData(0x25);
ILI9163_writeData(0x1c);
ILI9163_writeData(0x1e);
ILI9163_writeData(0x20);
ILI9163_writeData(0x12);
ILI9163_writeData(0x2a);
ILI9163_writeData(0x90);
ILI9163_writeData(0x24);
ILI9163_writeData(0x11);
ILI9163_writeData(0x00);
ILI9163_writeData(0x00);
ILI9163_writeData(0x00);
ILI9163_writeData(0x00);
ILI9163_writeData(0x00);
ILI9163_writeCommand(ILI9163_CMD_NEGATIVE_GAMMA_CORRECT);
ILI9163_writeData(0x20);
ILI9163_writeData(0x20);
ILI9163_writeData(0x20);
ILI9163_writeData(0x20);
ILI9163_writeData(0x05);
ILI9163_writeData(0x00);
ILI9163_writeData(0x15);
ILI9163_writeData(0xa7);
ILI9163_writeData(0x3d);
ILI9163_writeData(0x18);
ILI9163_writeData(0x25);
ILI9163_writeData(0x2a);
ILI9163_writeData(0x2b);
ILI9163_writeData(0x2b);
ILI9163_writeData(0x3a);
ILI9163_writeCommand(ILI9163_CMD_FRAME_RATE_CONTROL1);
ILI9163_writeData(0x08); // DIVA = 8
ILI9163_writeData(0x02); // VPA = 8
ILI9163_writeCommand(ILI9163_CMD_FRAME_RATE_CONTROL2);
ILI9163_writeData(0x08); // DIVA = 8
ILI9163_writeData(0x02); // VPA = 8
ILI9163_writeCommand(ILI9163_CMD_FRAME_RATE_CONTROL3);
ILI9163_writeData(0x08); // DIVA = 8
ILI9163_writeData(0x02); // VPA = 8
ILI9163_writeCommand(ILI9163_CMD_DISPLAY_INVERSION);
ILI9163_writeData(0x07); // NLA = 1, NLB = 1, NLC = 1 (all on Frame Inversion)
/* This bit may cause minor compatibility issues, so it's commented out. Beware.
ILI9163_writeCommand(ILI9163_CMD_POWER_CONTROL1);
ILI9163_writeData(0x0a); // VRH = 10: GVDD = 4.30
ILI9163_writeData(0x02); // VC = 2: VCI1 = 2.65
ILI9163_writeCommand(ILI9163_CMD_POWER_CONTROL2);
ILI9163_writeData(0x02); // BT = 2: AVDD = 2xVCI1, VCL = -1xVCI1, VGH = 5xVCI1, VGL = -2xVCI1
ILI9163_writeCommand(ILI9163_CMD_VCOM_CONTROL1);
ILI9163_writeData(0x24); // VMH = 80: VCOMH voltage = 4.5
ILI9163_writeData(0x48); // VML = 91: VCOML voltage = -0.225
ILI9163_writeCommand(ILI9163_CMD_VCOM_OFFSET_CONTROL);
ILI9163_writeData(0x40); // nVM = 0, VMF = 64: VCOMH output = VMH, VCOML output = VML
*/
ILI9163_writeCommand(ILI9163_CMD_SET_COLUMN_ADDRESS);
ILI9163_writeData(0x00); // XSH
ILI9163_writeData(0x00); // XSL
ILI9163_writeData(0x00); // XEH
ILI9163_writeData(ILI9163_HEIGHT-1); // XEL (128 pixels x)
ILI9163_writeCommand(ILI9163_CMD_SET_PAGE_ADDRESS);
ILI9163_writeData(0x00);
ILI9163_writeData(0x00);
ILI9163_writeData(0x00);
ILI9163_writeData(ILI9163_WIDTH-1); // 160 pixels y
ILI9163_writeCommand(ILI9163_CMD_SET_ADDRESS_MODE);
if(rotation)
ILI9163_writeData(0x80 | 0x20 | 0x08);
else
ILI9163_writeData(0x40 | 0x20 | 0x08);
ILI9163_writeCommand(ILI9163_CMD_ENTER_NORMAL_MODE);
ILI9163_writeCommand(ILI9163_CMD_SET_DISPLAY_ON);
ILI9163_writeCommand(ILI9163_CMD_WRITE_MEMORY_START);
}
void ILI9163_newFrame()
{
for(uint32_t i= 0; i < (ILI9163_WIDTH*ILI9163_HEIGHT); i++)
frameBuffer[i] = 0xFFFF;
}
void ILI9163_render()
{
ILI9163_setAddress(0, 0, ILI9163_WIDTH, ILI9163_HEIGHT);
HAL_GPIO_WritePin(DISP_CS_Port, DISP_CS_Pin, 0);
HAL_GPIO_WritePin(DISP_DC_Port, DISP_DC_Pin, 1);
HAL_SPI_Transmit_DMA(&DISP_SPI, (uint8_t*)frameBuffer, BUFSIZE*2);
SPI_DMA_FL=0;
while(!SPI_DMA_FL) {} // This can be commented out if your thread sends new frames slower than SPI transmits them. Otherwise, memory havoc. See README.md
}
void ILI9163_drawPixel(uint8_t x, uint8_t y, uint16_t color) {
if ((x < 0) || (x >= ILI9163_WIDTH) || (y < 0) || (y >= ILI9163_HEIGHT)) return;
frameBuffer[((x)+(y*ILI9163_WIDTH))] = color;// >> 8;
//frameBuffer[((x*2)+(y*2*ILI9163_WIDTH))+1] = color & 0xFF;
}
void ILI9163_fillRect(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color)
{
for(uint8_t x = x1; x < x2; x++)
for(uint8_t y = y1; y < y2; y++)
ILI9163_drawPixel(x, y, color);
}
void ILI9163_drawRect(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2, uint8_t thickness, uint16_t color) {
ILI9163_fillRect(x1, y1, x2, y1+thickness, color);
ILI9163_fillRect(x1, y2-thickness, x2, y2, color);
ILI9163_fillRect(x1, y1, x1+thickness, y2, color);
ILI9163_fillRect(x2-thickness, y1, x2, y2, color);
}
void ILI9163_drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color) {
uint16_t dy = y1 - y0;
uint16_t dx = x1 - x0;
uint16_t stepx, stepy;
if (dy < 0) {
dy = -dy; stepy = -1;
}
else stepy = 1;
if (dx < 0) {
dx = -dx; stepx = -1;
}
else stepx = 1;
dy <<= 1;
dx <<= 1;
ILI9163_drawPixel(x0, y0, color);
if (dx > dy) {
int fraction = dy - (dx >> 1);
while (x0 != x1) {
if (fraction >= 0) {
y0 += stepy;
fraction -= dx;
}
x0 += stepx;
fraction += dy;
ILI9163_drawPixel(x0, y0, color);
}
} else {
int fraction = dx - (dy >> 1);
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
ILI9163_drawPixel(x0, y0, color);
}
}
}
void ILI9163_fillCircle(uint8_t centerX, uint8_t centerY, uint8_t radius, uint16_t color) {
for(int y=-radius; y<=radius; y++)
for(int x=-radius; x<=radius; x++)
if(x*x+y*y <= radius*radius)
ILI9163_drawPixel(centerX+x, centerY+y, color);
}
void ILI9163_drawCircle(uint8_t centerX, uint8_t centerY, uint8_t radius, uint16_t color) { // From the Adafruit GFX library
radius--; // inner outline
int16_t f = 1 - radius;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * radius;
int16_t x = 0;
int16_t y = radius;
ILI9163_drawPixel(centerX, centerY + radius, color);
ILI9163_drawPixel(centerX, centerY - radius, color);
ILI9163_drawPixel(centerX + radius, centerY, color);
ILI9163_drawPixel(centerX - radius, centerY, color);
while (x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
ILI9163_drawPixel(centerX + x, centerY + y, color);
ILI9163_drawPixel(centerX - x, centerY + y, color);
ILI9163_drawPixel(centerX + x, centerY - y, color);
ILI9163_drawPixel(centerX - x, centerY - y, color);
ILI9163_drawPixel(centerX + y, centerY + x, color);
ILI9163_drawPixel(centerX - y, centerY + x, color);
ILI9163_drawPixel(centerX + y, centerY - x, color);
ILI9163_drawPixel(centerX - y, centerY - x, color);
}
}
void ILI9163_fillDisplay(uint16_t color) {
ILI9163_fillRect(0,0, ILI9163_WIDTH, ILI9163_HEIGHT, color);
}
void ILI9163_drawChar(uint8_t x, uint8_t y, char ch, FontDef font, uint16_t color) {
uint16_t i, b, j;
for(i = 0; i < font.height; i++) {
b = font.data[(ch - 32) * font.height + i];
for(j = 0; j < font.width; j++) {
if((b << j) & 0x8000) {
ILI9163_drawPixel(x + j, y + i, color);
}
}
}
}
void ILI9163_drawString(uint8_t x, uint8_t y, FontDef font, uint16_t color, const char *string) {
while(*string) {
if(x + font.width >= ILI9163_WIDTH) {
x = 0;
y += font.height;
if(y + font.height >= ILI9163_HEIGHT)
break;
if(*string == ' ') {
// skip spaces in the beginning of the new line
string++;
continue;
}
}
ILI9163_drawChar(x, y, *string, font, color);
x += font.width;
string++;
}
}
void ILI9163_drawStringF(uint8_t x, uint8_t y, FontDef font, uint16_t color, char *szFormat, ...) {
char szBuffer[64];
va_list pArgs;
va_start(pArgs, szFormat);
vsnprintf(szBuffer, 63, szFormat, pArgs);
va_end(pArgs);
ILI9163_drawString(x, y, font, color, szBuffer);
}