Sensorian  1.0
C API Reference Guide Library
TFT.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include "TFT.h"
11 #include "SPI.h"
12 #include "Font.h"
13 
17 
18 extern const unsigned char FontASCII8X16[];
19 
24 void TFT_Initialize(void)
25 {
26  CS_OUTPUT();
27  DC_OUTPUT();
28  RST_OUTPUT();
29  bcm2835_delay(100);
30 
31  CS_LOW();
32  bcm2835_delay(10);
33  RST_LOW();
34  bcm2835_delay(100);
35  RST_HIGH();
36  bcm2835_delay(100);
37 
39  bcm2835_delay(150);
41  bcm2835_delay(600);
42 
43  TFT_WriteCommand(FRMCTR1); // Set ST7735S Frame Rate
44  TFT_WriteData(0x01);
45  TFT_WriteData(0x2C);
46  TFT_WriteData(0x2D);
48  TFT_WriteData(0x01);
49  TFT_WriteData(0x2C);
50  TFT_WriteData(0x2D);
52  TFT_WriteData(0x01);
53  TFT_WriteData(0x2C);
54  TFT_WriteData(0x2D);
55  TFT_WriteData(0x01);
56  TFT_WriteData(0x2C);
57  TFT_WriteData(0x2D);
58 
59  TFT_WriteCommand(INVCTR); // Display Inversion Control
60  TFT_WriteData(0x07);
62  TFT_WriteData(0xA2);
63  TFT_WriteData(0x02);
64  TFT_WriteData(0x84);
66  TFT_WriteData(0XC5);
67  TFT_WriteCommand(PWCTR3); // Power Control 3 (in Normal mode/ Full colors)
68  TFT_WriteData(0x0A);
69  TFT_WriteData(0x00);
70  TFT_WriteCommand(PWCTR4); // Power Control 4 (in Idle mode/ 8-colors)
71  TFT_WriteData(0x8A);
72  TFT_WriteData(0x2A);
74  TFT_WriteData(0x8A);
75  TFT_WriteData(0xEE);
76 
77  TFT_WriteCommand(VMCTR1); // ST7735S Power Sequence
78  TFT_WriteData(0x0E);
80  TFT_WriteCommand(MADCTL); //MX, MY, RGB mode
81  TFT_WriteData(0xC0);
82  TFT_WriteCommand(COLMOD); //65k mode
83  TFT_WriteData(0x05);
84 
86  TFT_WriteData(0x00);
87  TFT_WriteData(0x7F);
89  TFT_WriteData(0x00);
90  TFT_WriteData(0x9F);
91 
92  TFT_WriteCommand(GMCTRP1); // ST7735S Gamma Sequence
93  TFT_WriteData(0x02);
94  TFT_WriteData(0x1c);
95  TFT_WriteData(0x07);
96  TFT_WriteData(0x12);
97 
98  TFT_WriteData(0x37);
99  TFT_WriteData(0x32);
100  TFT_WriteData(0x29);
101  TFT_WriteData(0x2d);
102 
103  TFT_WriteData(0x29);
104  TFT_WriteData(0x25);
105  TFT_WriteData(0x2B);
106  TFT_WriteData(0x39);
107 
108  TFT_WriteData(0x00);
109  TFT_WriteData(0x01);
110  TFT_WriteData(0x03);
111  TFT_WriteData(0x10);
112 
114  TFT_WriteData(0x03);
115  TFT_WriteData(0x1d);
116  TFT_WriteData(0x07);
117  TFT_WriteData(0x06);
118 
119  TFT_WriteData(0x2E);
120  TFT_WriteData(0x2C);
121  TFT_WriteData(0x29);
122  TFT_WriteData(0x2D);
123 
124  TFT_WriteData(0x2E);
125  TFT_WriteData(0x2E);
126  TFT_WriteData(0x37);
127  TFT_WriteData(0x3F);
128 
129  TFT_WriteData(0x00);
130  TFT_WriteData(0x00);
131  TFT_WriteData(0x02);
132  TFT_WriteData(0x10);
133 
134  TFT_WriteCommand(NORON); // Normal display on, no args, w/delay 10ms 0x13
135  bcm2835_delay(100); // End ST7735S Gamma Sequence
136  TFT_WriteCommand(DISPON); // Display on
137 }
138 
144 void TFT_WriteCommand(unsigned char command)
145 {
146  CS_LOW();
147  DC_LOW();
148  SPI_Write(command);
149  CS_HIGH();
150 }
151 
157 void TFT_WriteData(unsigned char datab)
158 {
159  CS_LOW();
160  DC_HIGH();
161  SPI_Write(datab);
162  CS_HIGH();
163 }
164 
170 void TFT_WriteDataWord(int wdata)
171 {
172  CS_LOW();
173  DC_HIGH();
174  SPI_Write(wdata>>8);
175  SPI_Write(wdata);
176  CS_HIGH();
177 }
178 
183 void TFT_RamAdress(void)
184 {
186  TFT_WriteData(0x00);
187  TFT_WriteData(0x00);
188  TFT_WriteData(0x00);
190 
192  TFT_WriteData(0x00);
193  TFT_WriteData(0x00);
194  TFT_WriteData(0x00);
197 }
198 
206 void TFT_SetPixel(unsigned char x_start,unsigned char y_start,unsigned int color)
207 {
209  TFT_WriteData(0x00);
210  TFT_WriteData(x_start); // XSTART
211  TFT_WriteData(0x00);
212  TFT_WriteData(x_start+1); // XEND
213 
214  TFT_WriteCommand(RASET); // Row addr set
215  TFT_WriteData(0x00);
216  TFT_WriteData(y_start); // YSTART
217  TFT_WriteData(0x00);
218  TFT_WriteData(y_start+1); // YEND
219 
221  TFT_WriteData(color>>8);
222  TFT_WriteData(color);
223 }
224 
229 void TFT_Sleep(void)
230 {
232  bcm2835_delay(5);
233 }
234 
239 void TFT_WakeUp(void)
240 {
242  bcm2835_delay(120);
243 }
244 
249 void TFT_TurnOffDisplay(void)
250 {
252 }
253 
258 void TFT_TurnOnDisplay(void)
259 {
261 }
262 
270 {
272  switch (mode)
273  {
274  case 0x00:
276  break;
277  case 0x01:
279  break;
280  case 0x02:
282  break;
283  case 0x03:
285  break;
286  case 0x04:
288  break;
289  case 0x05:
291  break;
292  case 0x06:
294  break;
295  case 0x07:
297  break;
298  }
299 }
300 
305 void TFT_InvertDisplay(void)
306 {
308 }
309 
314 void TFT_InvertDisplayOff(void)
315 {
317 }
318 
324 void TFT_Background(int color)
325 {
326  unsigned char i,j;
327  TFT_RamAdress();
328  for (i=0;i<160;i++)
329  for (j=0;j<128;j++)
330  TFT_WriteDataWord(color);
331 }
332 
342 void TFT_ShowPic(unsigned int picture[],unsigned char width, unsigned char height, unsigned int x, unsigned int y)
343 {
344  unsigned int i,j,k;
345  k=0;
346  for(i=0;i<width;i++)
347  {
348  for(j=0;j<height;j++)
349  {
350  TFT_SetPixel(i+x,j+y,picture[k]);
351  k++;
352  }
353  }
354 }
355 
363 void TFT_DisplayImage(Image_t *image, unsigned char x, unsigned char y)
364 {
365  unsigned int i,j,k;
366  k=0;
367  for(i=0;i<image->width;i++)
368  {
369  for(j=0;j<image->height;j++)
370  {
371  TFT_SetPixel(i+x,j+y,image->picture[k]);
372  k++;
373  }
374  }
375 }
376 
384 unsigned int TFT_Color565(unsigned char r, unsigned char g, unsigned char b)
385 {
386  return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
387 }
388 
399 void TFT_ASCII(char x, char y, int color, int background, char letter, char size)
400 {
401  unsigned char b,q,d,z;
402  char data;
403  for(q=0;q<5;q++)
404  {
405  data = font[(unsigned char)letter][q];
406  for(z=0;z<8*size;z++)
407  {
408  if((data&1)!=0){
409  for(d=0; d<size;d++)
410  {
411  for(b=0; b<size;b++)
412  {
413  TFT_SetPixel(x+(q*size)+d,y+(z*size)+b,color);
414  }
415  }
416  }else{
417  for(d=0; d<size;d++)
418  {
419  for(b=0; b<size;b++)
420  {
421  TFT_SetPixel(x+(q*size)+d,y+(z*size)+b,background);
422  }
423  }
424  }
425  data>>=1;
426  }
427  }
428 }
429 
440 void TFT_PrintString(char x, char y, int color, int background, char * message, char size)
441 {
442  while (*message)
443  {
444  TFT_ASCII(x,y,color,background,*message++, size);
445  x+=6*size;
446  if(x>120)
447  {
448  x=0;
449  y+=8*size;
450  }
451  }
452 }
453 
465 void TFT_PrintInteger(char x, char y, int color, int background,int integer, char size)
466 {
467  unsigned char tenthousands,thousands,hundreds,tens,ones;
468  tenthousands = integer / 10000;
469  TFT_ASCII(x,y,color,background,tenthousands+48, size);
470  thousands = ((integer - tenthousands*10000)) / 1000;
471  x+=6;
472  TFT_ASCII(x,y,color,background,thousands+48, size);
473  hundreds = (((integer - tenthousands*10000) - thousands*1000)-1) / 100;
474  x+=6;
475  TFT_ASCII(x,y,color,background,hundreds+48, size);
476  tens=(integer%100)/10;
477  x+=6;
478  TFT_ASCII(x,y,color,background,tens+48, size);
479  ones=integer%10;
480  x+=6;
481  TFT_ASCII(x,y,color,background,ones+48, size);
482 }
483 
#define RASET
Definition: TFT.h:43
void TFT_SetOrientation(orientation_t mode)
Sets the display text orientation. Mirrored modes are also supported on top of portrait and landscape...
Definition: TFT.c:269
#define SLEEP_OUT
Definition: TFT.h:34
void TFT_Background(int color)
This function paints the display background a specific color.
Definition: TFT.c:324
#define NORON
Definition: TFT.h:36
SPI library header.
#define DC_LOW()
Definition: TFT.h:132
#define FRMCTR3
Definition: TFT.h:62
void TFT_InvertDisplayOff(void)
Disables color inversion on the display.
Definition: TFT.c:314
#define GMCTRN1
Definition: TFT.h:78
const unsigned char font[]
Font header.
#define INVON
Definition: TFT.h:38
void TFT_TurnOnDisplay(void)
This function turns on the display from idle mode.
Definition: TFT.c:258
unsigned char width
Definition: TFT.h:159
void TFT_WriteCommand(unsigned char command)
This function writes a command byte to the display controller.
Definition: TFT.c:144
#define INVCTR
Definition: TFT.h:63
const unsigned char FontASCII8X16[]
unsigned int * picture
Definition: TFT.h:158
void TFT_ShowPic(unsigned int picture[], unsigned char width, unsigned char height, unsigned int x, unsigned int y)
This function paints an image with a specific heigh and width on the display at a specific coordinate...
Definition: TFT.c:342
#define PWCTR5
Definition: TFT.h:69
#define DC_OUTPUT()
Definition: TFT.h:131
void TFT_Sleep(void)
Puts the display in a low power mode.
Definition: TFT.c:229
#define CASET
Definition: TFT.h:42
#define RST_OUTPUT()
Definition: TFT.h:135
#define DC_HIGH()
Definition: TFT.h:133
void TFT_WakeUp(void)
Wakes the display from sleep mode.
Definition: TFT.c:239
#define RAMWR
Definition: TFT.h:44
#define WIDTH
Definition: TFT.h:107
#define INVOFF
Definition: TFT.h:37
#define MADCTL_M8
Definition: TFT.h:101
#define MADCTL_BGR
Definition: TFT.h:104
void TFT_SetPixel(unsigned char x_start, unsigned char y_start, unsigned int color)
This functions sets a specific pixel on the TFT display.
Definition: TFT.c:206
unsigned char SPI_Write(unsigned char data)
Writes a byte of data to the SPI bus.
Definition: SPI.c:53
void TFT_InvertDisplay(void)
Enables color inversion on the display.
Definition: TFT.c:305
void TFT_WriteData(unsigned char datab)
This function is used to write data to the TFT controller.
Definition: TFT.c:157
#define PWCTR1
Definition: TFT.h:65
#define MADCTL
Definition: TFT.h:51
#define DISPOFF
Definition: TFT.h:40
#define COLMOD
Definition: TFT.h:55
#define PWCTR4
Definition: TFT.h:68
orientation_t
Orientation type. Specific orientation modes of the device.
Definition: TFT.h:143
void TFT_PrintString(char x, char y, int color, int background, char *message, char size)
Orint a colored string at coordinates x,y with a specific font size.
Definition: TFT.c:440
#define CS_OUTPUT()
Definition: TFT.h:127
#define CS_LOW()
Definition: TFT.h:128
void TFT_TurnOffDisplay(void)
Blanks out the display.
Definition: TFT.c:249
#define PWCTR3
Definition: TFT.h:67
#define CS_HIGH()
Definition: TFT.h:129
#define MADCTL_ML
Definition: TFT.h:88
void TFT_DisplayImage(Image_t *image, unsigned char x, unsigned char y)
This function displays an image of type Image_t on screen.
Definition: TFT.c:363
#define VMCTR1
Definition: TFT.h:70
TFT Driver.
#define SLEEP_IN
Definition: TFT.h:33
void TFT_WriteDataWord(int wdata)
This function writes a 16 bit word on the display controller registers.
Definition: TFT.c:170
unsigned int TFT_Color565(unsigned char r, unsigned char g, unsigned char b)
Pass 8-bit (each) R,G,B, get back 16-bit packed color.
Definition: TFT.c:384
#define RST_LOW()
Definition: TFT.h:136
#define RST_HIGH()
Definition: TFT.h:137
#define MADCTL_MY
Definition: TFT.h:85
void TFT_ASCII(char x, char y, int color, int background, char letter, char size)
Plot an ASCII char on the display. A specific font is used.
Definition: TFT.c:399
#define DISPON
Definition: TFT.h:41
#define PWCTR2
Definition: TFT.h:66
unsigned char height
Definition: TFT.h:160
void TFT_PrintInteger(char x, char y, int color, int background, int integer, char size)
Prints an integer at coordinates x,y with a specific color on a specific background. The integer font size is deterined bu var size.
Definition: TFT.c:465
Image type. Contains image array with height and width dimensions.
Definition: TFT.h:156
#define GMCTRP1
Definition: TFT.h:77
#define MADCTL_MV
Definition: TFT.h:87
void TFT_Initialize(void)
This function intializes the display controller and prepares it for any subsequent operations...
Definition: TFT.c:24
#define SWRESET
Definition: TFT.h:24
void TFT_RamAdress(void)
Definition: TFT.c:183
#define MADCTL_MX
Definition: TFT.h:86
#define HEIGHT
Definition: TFT.h:108
#define FRMCTR1
Definition: TFT.h:60
#define FRMCTR2
Definition: TFT.h:61