ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
ili9341_adafruit.c
Go to the documentation of this file.
1 
41 #include "user_config.h"
42 
43 #include <stdint.h>
44 #include <stdarg.h>
45 #include <string.h>
46 #include <math.h>
47 
48 #include "display/font.h"
49 #include "display/ili9341.h"
51 
52 // ============================================================
53 
58 {
59  uint8_t data[15] = {0};
60 
61 // // POWER_ON_SEQ_CONTROL
62  data[0] = 0x39;
63  data[1] = 0x2C;
64  data[2] = 0x00;
65  data[3] = 0x34;
66  data[4] = 0x02;
67  tft_Cmd_Data_TX(0xCB, data, 5);
68 
69  data[0] = 0x00;
70  data[1] = 0XC1;
71  data[2] = 0X30;
72  tft_Cmd_Data_TX(0xCF, data, 3);
73 
74  data[0] = 0x85;
75  data[1] = 0x00;
76  data[2] = 0x78;
77  tft_Cmd_Data_TX(0xE8, data, 3);
78 
79  data[0] = 0x00;
80  data[1] = 0x00;
81  tft_Cmd_Data_TX(0xEA, data, 2);
82 
83  data[0] = 0x64;
84  data[1] = 0x03;
85  data[2] = 0X12;
86  data[3] = 0X81;
87  tft_Cmd_Data_TX(0xED, data, 4);
88 
89  data[0] = 0x20;
90  tft_Cmd_Data_TX(0xF7, data, 1);
91 
92  data[0] = 0x23; //VRH[5:0]
93  tft_Cmd_Data_TX(0xC0, data, 1); //Power control
94 
95  data[0] = 0x10; //SAP[2:0];BT[3:0]
96  tft_Cmd_Data_TX(0xC1, data, 1); //Power control
97 
98  data[0] = 0x3e; //Contrast
99  data[1] = 0x28;
100  tft_Cmd_Data_TX(0xC5, data, 2); //VCM control
101 
102  data[0] = 0x86; //--
103  tft_Cmd_Data_TX(0xC7, data, 1); //VCM control2
104 
105 // MG was 0x40
106  data[0] = 0x48; // column address order
107  tft_Cmd_Data_TX(0x36, data, 1); // Memory Access Control
108 
109  data[0] = 0x55;
110  tft_Cmd_Data_TX(0x3A, data, 1);
111 
112  data[0] = 0x00;
113  data[1] = 0x18;
114  tft_Cmd_Data_TX(0xB1, data, 2);
115 
116  data[0] = 0x08;
117  data[1] = 0x82;
118  data[2] = 0x27;
119  tft_Cmd_Data_TX(0xB6, data, 3); // Display Function Control
120 
121  data[0] = 0x00;
122  tft_Cmd_Data_TX(0xF2, data, 1); // 3Gamma Function Disable
123 
124  data[0] = 0x01;
125  tft_Cmd_Data_TX(0x26, data, 1); //Gamma curve selected
126 
127  data[0] = 0x0F;
128  data[1] = 0x31;
129  data[2] = 0x2B;
130  data[3] = 0x0C;
131  data[4] = 0x0E;
132  data[5] = 0x08;
133  data[6] = 0x4E;
134  data[7] = 0xF1;
135  data[8] = 0x37;
136  data[9] = 0x07;
137  data[10] = 0x10;
138  data[11] = 0x03;
139  data[12] = 0x0E;
140  data[13] = 0x09;
141  data[14] = 0x00;
142  tft_Cmd_Data_TX(0xE0, data, 15); //Set Gamma
143 
144  data[0] = 0x00;
145  data[1] = 0x0E;
146  data[2] = 0x14;
147  data[3] = 0x03;
148  data[4] = 0x11;
149  data[5] = 0x07;
150  data[6] = 0x31;
151  data[7] = 0xC1;
152  data[8] = 0x48;
153  data[9] = 0x08;
154  data[10] = 0x0F;
155  data[11] = 0x0C;
156  data[12] = 0x31;
157  data[13] = 0x36;
158  data[14] = 0x0F;
159  tft_Cmd_Data_TX(0xE1, data, 15); //Set Gamma
160 
161  tft_Cmd_Data_TX(0x11, 0, 0); //Exit Sleep
162 // MG was 120000
163  os_delay_us(150000);
164 
165  tft_Cmd_Data_TX(0x29, 0, 0); //Display on
166 // MG added
167  os_delay_us(150000);
168  tft_Cmd_Data_TX(0x2c, 0, 0);
169 }
170 
171 
172 // =======================================================================
173 
183 MEMSPACE
184 void tft_drawBitmap(window *win, int16_t x, int16_t y,
185 const uint16_t *bitmap, int16_t w, int16_t h)
186 {
187  tft_writeRect(win, x, y, w, h, (uint16_t *)bitmap);
188 }
189 
190 
191 // ====================================================
192 
200 MEMSPACE
201 void tft_drawCircle(window *win, int16_t x0, int16_t y0, int16_t r,
202 uint16_t color)
203 {
204  int16_t f = 1 - r;
205  int16_t ddF_x = 1;
206  int16_t ddF_y = -2 * r;
207  int16_t x = 0;
208  int16_t y = r;
209 
210  tft_drawPixel(win,x0 , y0+r, color);
211  tft_drawPixel(win,x0 , y0-r, color);
212  tft_drawPixel(win,x0+r, y0 , color);
213  tft_drawPixel(win,x0-r, y0 , color);
214 
215  while (x<y)
216  {
217  if (f >= 0)
218  {
219  y--;
220  ddF_y += 2;
221  f += ddF_y;
222  }
223  x++;
224  ddF_x += 2;
225  f += ddF_x;
226 
227  tft_drawPixel(win,x0 + x, y0 + y, color);
228  tft_drawPixel(win,x0 - x, y0 + y, color);
229  tft_drawPixel(win,x0 + x, y0 - y, color);
230  tft_drawPixel(win,x0 - x, y0 - y, color);
231  tft_drawPixel(win,x0 + y, y0 + x, color);
232  tft_drawPixel(win,x0 - y, y0 + x, color);
233  tft_drawPixel(win,x0 + y, y0 - x, color);
234  tft_drawPixel(win,x0 - y, y0 - x, color);
235  }
236 }
237 
238 
247 MEMSPACE
248 void tft_drawCircleHelper(window *win, int16_t x0, int16_t y0,
249 int16_t r, uint8_t cornername, uint16_t color)
250 {
251  int16_t f = 1 - r;
252  int16_t ddF_x = 1;
253  int16_t ddF_y = -2 * r;
254  int16_t x = 0;
255  int16_t y = r;
256 
257  while (x<y)
258  {
259  if (f >= 0)
260  {
261  y--;
262  ddF_y += 2;
263  f += ddF_y;
264  }
265  x++;
266  ddF_x += 2;
267  f += ddF_x;
268  if (cornername & 0x4)
269  {
270  tft_drawPixel(win,x0 + x, y0 + y, color);
271  tft_drawPixel(win,x0 + y, y0 + x, color);
272  }
273  if (cornername & 0x2)
274  {
275  tft_drawPixel(win,x0 + x, y0 - y, color);
276  tft_drawPixel(win,x0 + y, y0 - x, color);
277  }
278  if (cornername & 0x8)
279  {
280  tft_drawPixel(win,x0 - y, y0 + x, color);
281  tft_drawPixel(win,x0 - x, y0 + y, color);
282  }
283  if (cornername & 0x1)
284  {
285  tft_drawPixel(win,x0 - y, y0 - x, color);
286  tft_drawPixel(win,x0 - x, y0 - y, color);
287  }
288  }
289 }
290 
291 
301 MEMSPACE
302 void tft_fillCircleHelper(window *win, int16_t x0, int16_t y0, int16_t r,
303 uint8_t cornername, int16_t delta, uint16_t color)
304 {
305 
306  int16_t f = 1 - r;
307  int16_t ddF_x = 1;
308  int16_t ddF_y = -2 * r;
309  int16_t x = 0;
310  int16_t y = r;
311 
312  while (x<y)
313  {
314  if (f >= 0)
315  {
316  y--;
317  ddF_y += 2;
318  f += ddF_y;
319  }
320  x++;
321  ddF_x += 2;
322  f += ddF_x;
323 
324  if (cornername & 0x1)
325  {
326  tft_drawFastVLine(win,x0+x, y0-y, 2*y+1+delta, color);
327  tft_drawFastVLine(win,x0+y, y0-x, 2*x+1+delta, color);
328  }
329  if (cornername & 0x2)
330  {
331  tft_drawFastVLine(win,x0-x, y0-y, 2*y+1+delta, color);
332  tft_drawFastVLine(win,x0-y, y0-x, 2*x+1+delta, color);
333  }
334  }
335 }
336 
337 
345 MEMSPACE
346 void tft_fillCircle(window *win, int16_t x0, int16_t y0, int16_t r,
347 uint16_t color)
348 {
349  tft_drawFastVLine(win,x0, y0-r, 2*r+1, color);
350  tft_fillCircleHelper(win, x0, y0, r, 3, 0, color);
351 }
352 
353 
362 MEMSPACE
363 void tft_drawRect(window *win, int16_t x, int16_t y,
364 int16_t w, int16_t h,
365 uint16_t color)
366 {
367  tft_drawFastHLine(win,x, y, w, color);
368  tft_drawFastHLine(win,x, y+h-1, w, color);
369  tft_drawFastVLine(win,x, y, h, color);
370  tft_drawFastVLine(win,x+w-1, y, h, color);
371 }
372 
373 
383 MEMSPACE
384 void tft_drawRoundRect(window *win, int16_t x, int16_t y, int16_t w,
385 int16_t h, int16_t r, uint16_t color)
386 {
387 // smarter version
388  // Top
389  tft_drawFastHLine(win,x+r , y , w-2*r, color);
390  // Bottom
391  tft_drawFastHLine(win,x+r , y+h-1, w-2*r, color);
392  // Left
393  tft_drawFastVLine(win,x , y+r , h-2*r, color);
394  // Right
395  tft_drawFastVLine(win,x+w-1, y+r , h-2*r, color);
396 // draw four corners
397  tft_drawCircleHelper(win,x+r , y+r , r, 1, color);
398  tft_drawCircleHelper(win,x+w-r-1, y+r , r, 2, color);
399  tft_drawCircleHelper(win,x+w-r-1, y+h-r-1, r, 4, color);
400  tft_drawCircleHelper(win,x+r , y+h-r-1, r, 8, color);
401 }
402 
403 
413 MEMSPACE
414 void tft_fillRoundRect(window *win, int16_t x, int16_t y, int16_t w,
415 int16_t h, int16_t r, uint16_t color)
416 {
417 // smarter version
418  tft_fillRectWH(win,x+r, y, w-2*r, h, color);
419 
420 // draw four corners
421  tft_fillCircleHelper(win,x+w-r-1, y+r, r, 1, h-2*r-1, color);
422  tft_fillCircleHelper(win,x+r , y+r, r, 2, h-2*r-1, color);
423 }
424 
425 
436 MEMSPACE
437 void tft_drawTriangle(window *win, int16_t x0, int16_t y0,
438 int16_t x1, int16_t y1,
439 int16_t x2, int16_t y2, uint16_t color)
440 {
441  tft_drawLine(win,x0, y0, x1, y1, color);
442  tft_drawLine(win,x1, y1, x2, y2, color);
443  tft_drawLine(win,x2, y2, x0, y0, color);
444 }
445 
446 
457 MEMSPACE
458 void tft_fillTriangle ( window *win, int16_t x0, int16_t y0,
459 int16_t x1, int16_t y1,
460 int16_t x2, int16_t y2, uint16_t color)
461 {
462 
463  int16_t a, b, y, last;
464 
465 // Sort coordinates by Y order (y2 >= y1 >= y0)
466  if (y0 > y1)
467  {
468  SWAP(y0, y1); SWAP(x0, x1);
469  }
470  if (y1 > y2)
471  {
472  SWAP(y2, y1); SWAP(x2, x1);
473  }
474  if (y0 > y1)
475  {
476  SWAP(y0, y1); SWAP(x0, x1);
477  }
478 
479  if(y0 == y2) // Handle awkward all-on-same-line case as its own thing
480  {
481  a = b = x0;
482  if(x1 < a) a = x1;
483  else if(x1 > b) b = x1;
484  if(x2 < a) a = x2;
485  else if(x2 > b) b = x2;
486  tft_drawFastHLine(win,a, y0, b-a+1, color);
487  return;
488  }
489 
490  int16_t
491  dx01 = x1 - x0,
492  dy01 = y1 - y0,
493  dx02 = x2 - x0,
494  dy02 = y2 - y0,
495  dx12 = x2 - x1,
496  dy12 = y2 - y1,
497  sa = 0,
498  sb = 0;
499 
500 // For upper part of triangle, find scanline crossings for segments
501 // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
502 // is included here (and second loop will be skipped, avoiding a /0
503 // error there), otherwise scanline y1 is skipped here and handled
504 // in the second loop...which also avoids a /0 error here if y0=y1
505 // (flat-topped triangle).
506  if(y1 == y2) last = y1; // Include y1 scanline
507  else last = y1-1; // Skip it
508 
509  for(y=y0; y<=last; y++)
510  {
511  a = x0 + sa / dy01;
512  b = x0 + sb / dy02;
513  sa += dx01;
514  sb += dx02;
515 /* longhand:
516 a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
517 b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
518  */
519  if(a > b) SWAP(a,b);
520  tft_drawFastHLine(win,a, y, b-a+1, color);
521  }
522 
523 // For lower part of triangle, find scanline crossings for segments
524 // 0-2 and 1-2. This loop is skipped if y1=y2.
525  sa = dx12 * (y - y1);
526  sb = dx02 * (y - y0);
527  for(; y<=y2; y++)
528  {
529  a = x1 + sa / dy12;
530  b = x0 + sb / dy02;
531  sa += dx12;
532  sb += dx02;
533 /* longhand:
534 a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
535 b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
536  */
537  if(a > b) SWAP(a,b);
538  tft_drawFastHLine(win,a, y, b-a+1, color);
539  }
540 }
MEMSPACE void tft_drawTriangle(window *win, int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
Draw a triangle.
void tft_drawFastHLine(window *win, int16_t x, int16_t y, int16_t w, uint16_t color)
Fast virtical line drawing.
Definition: ili9341.c:1329
unsigned short uint16_t
Definition: send.c:18
Master include file for project Includes all project includes and defines here.
void tft_fillRectWH(window *win, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Partial window Fill with color We clip the window to the current view.
Definition: ili9341.c:627
#define SWAP(a, b)
Definition: ili9341.h:95
MEMSPACE void tft_fillCircleHelper(window *win, int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color)
Fill circle helper.
MEMSPACE void tft_configRegister(void)
Initialize ILI9341 display.
int16_t y[XYSTACK+2]
Definition: ili9341.c:372
MEMSPACE void tft_drawBitmap(window *win, int16_t x, int16_t y, const uint16_t *bitmap, int16_t w, int16_t h)
Draw bitmap Replaced by tft_writeRect()
void tft_writeRect(window *win, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t *color)
Write a rectangle pixel array.
Definition: ili9341.c:745
int16_t x[XYSTACK+2]
Definition: ili9341.c:371
void tft_drawFastVLine(window *win, int16_t x, int16_t y, int16_t h, uint16_t color)
Fast virtical line drawing.
Definition: ili9341.c:1314
MEMSPACE void tft_fillTriangle(window *win, int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
Fill a triangle.
void tft_drawPixel(window *win, int16_t x, int16_t y, int16_t color)
Pixel functions
Definition: ili9341.c:717
MEMSPACE void tft_drawRoundRect(window *win, int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color)
Draw a rounded rectangle.
MEMSPACE void tft_drawRect(window *win, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Draw a rectangle.
void tft_drawLine(window *win, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
Draw line From my blit test code testit.c 1984 - 1985 Mike Gore.
Definition: ili9341.c:1389
#define MEMSPACE
Definition: cpu.h:25
Definition: ili9341.h:34
void tft_Cmd_Data_TX(uint8_t cmd, uint8_t *data, int bytes)
Transmit 8 bit command and optionally send data buffer.
Definition: ili9341.c:72
MEMSPACE void tft_fillCircle(window *win, int16_t x0, int16_t y0, int16_t r, uint16_t color)
Fill circle.
ili9341 driver inspired by Adafruit ili9341 code All code in this file has been rewritten by Mike Gor...
unsigned char uint8_t
Definition: send.c:17
MEMSPACE void tft_drawCircleHelper(window *win, int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color)
Draw a circle helper.
MEMSPACE void tft_drawCircle(window *win, int16_t x0, int16_t y0, int16_t r, uint16_t color)
Draw a circle outline.
MEMSPACE void tft_fillRoundRect(window *win, int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color)
Fill a rounded rectangle.