Dependents:   Telecommande_prologue

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TFT_4DGL_main.cpp Source File

TFT_4DGL_main.cpp

00001 //
00002 // TFT_4DGL is a class to drive 4D Systems TFT touch screens
00003 //
00004 // Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr>
00005 //
00006 // TFT_4DGL is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // TFT_4DGL is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with TFT_4DGL.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 #include "mbed.h"
00020 #include "TFT_4DGL.h"
00021 
00022 Serial pc(USBTX,USBRX);
00023 
00024 //******************************************************************************************************
00025 TFT_4DGL :: TFT_4DGL(PinName tx, PinName rx, PinName rst) : _cmd(tx, rx), _rst(rst) { // Constructor
00026 
00027 #if DEBUGMODE
00028     pc.baud(115200);
00029 
00030     pc.printf("\n\n\n");
00031     pc.printf("********************\n");
00032     pc.printf("TFT_4DGL CONSTRUCTOR\n");
00033     pc.printf("********************\n");
00034 #endif
00035 
00036     _rst = 1;    // put RESET pin to high to start TFT screen
00037 
00038     reset();
00039     autobaud();  // send autobaud command
00040     version();   // get version information
00041     cls();       // clear screen
00042 
00043     current_col         = 0;            // initial cursor col
00044     current_row         = 0;            // initial cursor row
00045     current_color       = WHITE;        // initial text color
00046     current_orientation = IS_PORTRAIT;  // initial screen orientation
00047 
00048     set_font(FONT_5X7);                 // initial font
00049     text_mode(OPAQUE);                  // initial texr mode
00050 }
00051 
00052 //******************************************************************************************************
00053 void TFT_4DGL :: writeBYTE(char c) { // send a BYTE command to screen
00054 
00055     _cmd.putc(c);
00056     wait_ms(1);
00057 
00058 #if DEBUGMODE
00059     pc.printf("   Char sent : 0x%02X\n",c);
00060 #endif
00061 
00062 }
00063 
00064 //******************************************************************************************************
00065 void TFT_4DGL :: freeBUFFER(void) {       // Clear serial buffer before writing command
00066 
00067     while (_cmd.readable()) _cmd.getc();  // clear buffer garbage
00068 }
00069 
00070 //******************************************************************************************************
00071 int TFT_4DGL :: writeCOMMAND(char *command, int number) { // send several BYTES making a command and return an answer
00072 
00073 #if DEBUGMODE
00074     pc.printf("\n");
00075     pc.printf("New COMMAND : 0x%02X\n", command[0]);
00076 #endif
00077     int i, resp = 0;
00078     freeBUFFER();
00079 
00080     for (i = 0; i < number; i++) writeBYTE(command[i]); // send command to serial port
00081 
00082     while (!_cmd.readable()) wait_ms(TEMPO);              // wait for screen answer
00083     if (_cmd.readable()) resp = _cmd.getc();           // read response if any
00084     switch (resp) {
00085         case ACK :                                     // if OK return   1
00086             resp =  1;
00087             break;
00088         case NAK :                                     // if NOK return -1
00089             resp = -1;
00090             break;
00091         default :
00092             resp =  0;                                 // else return   0
00093             break;
00094     }
00095 #if DEBUGMODE
00096     pc.printf("   Answer received : %d\n",resp);
00097 #endif
00098 
00099     return resp;
00100 }
00101 
00102 //**************************************************************************
00103 void TFT_4DGL :: reset() {  // Reset Screen
00104 
00105     _rst = 0;               // put RESET pin to low
00106     wait_ms(TEMPO);         // wait a few milliseconds for command reception
00107     _rst = 1;               // put RESET back to high
00108     wait(3);                // wait 3s for screen to restart
00109 
00110     freeBUFFER();           // clean buffer from possible garbage
00111 }
00112 
00113 //**************************************************************************
00114 void TFT_4DGL :: autobaud() { // send AutoBaud command (9600)
00115     char command[1] = "";
00116     command[0] = AUTOBAUD;
00117     writeCOMMAND(command, 1);
00118 }
00119 
00120 //**************************************************************************
00121 void TFT_4DGL :: cls() {  // clear screen
00122     char command[1] = "";
00123     command[0] = CLS;
00124     writeCOMMAND(command, 1);
00125 }
00126 
00127 //**************************************************************************
00128 void TFT_4DGL :: version() {  // get API version
00129     char command[2] = "";
00130     command[0] = VERSION;
00131     command[1] = OFF;
00132     readVERSION(command, 2);
00133 }
00134 
00135 //**************************************************************************
00136 void TFT_4DGL :: baudrate(int speed) {  // set screen baud rate
00137     char command[2]= "";
00138     command[0] = BAUDRATE;
00139     switch (speed) {
00140         case  110 :
00141             command[1] = BAUD_110;
00142             break;
00143         case  300 :
00144             command[1] = BAUD_300;
00145             break;
00146         case  600 :
00147             command[1] = BAUD_600;
00148             break;
00149         case 1200 :
00150             command[1] = BAUD_1200;
00151             break;
00152         case 2400 :
00153             command[1] = BAUD_2400;
00154             break;
00155         case 4800 :
00156             command[1] = BAUD_4800;
00157             break;
00158         case 9600 :
00159             command[1] = BAUD_9600;
00160             break;
00161         case 14400 :
00162             command[1] = BAUD_14400;
00163             break;
00164         case 19200 :
00165             command[1] = BAUD_19200;
00166             break;
00167         case 31250 :
00168             command[1] = BAUD_31250;
00169             break;
00170         case 38400 :
00171             command[1] = BAUD_38400;
00172             break;
00173         case 56000 :
00174             command[1] = BAUD_56000;
00175             break;
00176         case 57600 :
00177             command[1] = BAUD_57600;
00178             break;
00179         case 115200 :
00180             command[1] = BAUD_115200;
00181             break;
00182         case 128000 :
00183             command[1] = BAUD_128000;
00184             break;
00185         case 256000 :
00186             command[1] = BAUD_256000;
00187             break;
00188         default   :
00189             command[1] = BAUD_9600;
00190             speed = 9600;
00191             break;
00192     }
00193 
00194     int i, resp = 0;
00195 
00196     freeBUFFER();
00197 
00198     for (i = 0; i <2; i++) writeBYTE(command[i]);      // send command to serial port
00199     _cmd.baud(speed);                                  // set mbed to same speed
00200 
00201     while (!_cmd.readable()) wait_ms(TEMPO);           // wait for screen answer
00202 
00203     if (_cmd.readable()) resp = _cmd.getc();           // read response if any
00204     switch (resp) {
00205         case ACK :                                     // if OK return   1
00206             resp =  1;
00207             break;
00208         case NAK :                                     // if NOK return -1
00209             resp = -1;
00210             break;
00211         default :
00212             resp =  0;                                 // else return   0
00213             break;
00214     }
00215 }
00216 
00217 //******************************************************************************************************
00218 int TFT_4DGL :: readVERSION(char *command, int number) { // read screen info and populate data
00219 
00220     int i, temp = 0, resp = 0;
00221     char response[5] = "";
00222 
00223     freeBUFFER();
00224 
00225     for (i = 0; i < number; i++) writeBYTE(command[i]);    // send all chars to serial port
00226 
00227     while (!_cmd.readable()) wait_ms(TEMPO);               // wait for screen answer
00228 
00229     while (_cmd.readable()) {
00230         temp = _cmd.getc();
00231         response[resp++] = (char)temp;
00232     }
00233     switch (resp) {
00234         case 5 :                                           // if OK populate data and return 1
00235             type      = response[0];
00236             revision  = response[1];
00237             firmware  = response[2];
00238             reserved1 = response[3];
00239             reserved2 = response[4];
00240             resp      = 1;
00241             break;
00242         default :
00243             resp =  0;                                     // else return 0
00244             break;
00245     }
00246     return resp;
00247 }
00248 
00249 //****************************************************************************************************
00250 void TFT_4DGL :: background_color(int color) {            // set screen background color
00251     char command[3]= "";                                  // input color is in 24bits like 0xRRGGBB
00252 
00253     command[0] = BCKGDCOLOR;
00254 
00255     int red5   = (color >> (16 + 3)) & 0x1F;              // get red on 5 bits
00256     int green6 = (color >> (8 + 2))  & 0x3F;              // get green on 6 bits
00257     int blue5  = (color >> (0 + 3))  & 0x1F;              // get blue on 5 bits
00258 
00259     command[1] = ((red5 << 3)   + (green6 >> 3)) & 0xFF;  // first part of 16 bits color
00260     command[2] = ((green6 << 5) + (blue5 >>  0)) & 0xFF;  // second part of 16 bits color
00261 
00262     writeCOMMAND(command, 3);
00263 }
00264 
00265 //****************************************************************************************************
00266 void TFT_4DGL :: display_control(char mode, char value) {   // set screen mode to value
00267     char command[3]= "";
00268 
00269     command[0] = DISPCONTROL;
00270     command[1] = mode;
00271     command[2] = value;
00272 
00273     if (mode ==  ORIENTATION) {
00274         switch (value) {
00275             case LANDSCAPE :
00276                 current_orientation = IS_LANDSCAPE;
00277                 break;
00278             case LANDSCAPE_R :
00279                 current_orientation = IS_LANDSCAPE;
00280                 break;
00281             case PORTRAIT :
00282                 current_orientation = IS_PORTRAIT;
00283                 break;
00284             case PORTRAIT_R :
00285                 current_orientation = IS_PORTRAIT;
00286                 break;
00287         }
00288     }
00289     writeCOMMAND(command, 3);
00290     set_font(current_font);
00291 }
00292 
00293 //****************************************************************************************************
00294 void TFT_4DGL :: set_volume(char value) {   // set sound volume to value
00295     char command[2]= "";
00296 
00297     command[0] = SETVOLUME;
00298     command[1] = value;
00299 
00300     writeCOMMAND(command, 2);
00301 }
00302 
00303 
00304 //******************************************************************************************************
00305 void TFT_4DGL :: getTOUCH(char *command, int number, int *x, int *y) { // read screen info and populate data
00306 
00307 #if DEBUGMODE
00308     pc.printf("\n");
00309     pc.printf("New COMMAND : 0x%02X\n", command[0]);
00310 #endif
00311     int i, temp = 0, resp = 0;
00312     char response[5] = "";
00313 
00314     freeBUFFER();
00315 
00316     for (i = 0; i < number; i++) writeBYTE(command[i]);    // send all chars to serial port
00317 
00318     while (!_cmd.readable()) wait_ms(TEMPO);               // wait for screen answer
00319 
00320     while (_cmd.readable()) {
00321         temp = _cmd.getc();
00322         response[resp++] = (char)temp;
00323     }
00324 
00325 #if DEBUGMODE
00326     pc.printf("   Answer received %d : 0x%02X 0x%02X 0x%02X 0x%02X\n", resp, response[0], response[1], response[2], response[3]);
00327 #endif
00328 
00329     switch (resp) {
00330         case 4 :                                                              // if OK populate data
00331             *x = ((response[0]<<8)+ response[1]) * (response[0] != 0xFF);
00332             *y = ((response[2]<<8)+ response[3]) * (response[2] != 0xFF);
00333             break;
00334         default :
00335             *x = -1;
00336             *y = -1;
00337             break;
00338     }
00339 
00340 #if DEBUGMODE
00341     pc.printf("   X,Y : %03d,%03d\n", *x, *y);
00342 #endif
00343 }
00344 
00345 //******************************************************************************************************
00346 int TFT_4DGL :: getSTATUS(char *command, int number) { // read screen info and populate data
00347 
00348 #if DEBUGMODE
00349     pc.printf("\n");
00350     pc.printf("New COMMAND : 0x%02X\n", command[0]);
00351 #endif
00352 
00353     int i, temp = 0, resp = 0;
00354     char response[5] = "";
00355 
00356     freeBUFFER();
00357 
00358     for (i = 0; i < number; i++) writeBYTE(command[i]);    // send all chars to serial port
00359 
00360     while (!_cmd.readable()) wait_ms(TEMPO);    // wait for screen answer
00361 
00362     while (_cmd.readable()) {
00363         temp = _cmd.getc();
00364         response[resp++] = (char)temp;
00365     }
00366     switch (resp) {
00367         case 4 :
00368             resp = (int)response[1];         // if OK populate data
00369             break;
00370         default :
00371             resp =  -1;                      // else return   0
00372             break;
00373     }
00374     
00375 #if DEBUGMODE
00376     pc.printf("   Answer received : %d\n", resp);
00377 #endif
00378 
00379     return resp;
00380 }