Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of 4DGL-uLCD-SE by
uLCD_4DGL_main.cpp
00001 // 00002 // uLCD_4DGL is a class to drive 4D Systems uLCD 144 G2 00003 // 00004 // Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr> 00005 // Modifed for Goldelox processor <2013> Jim Hamblen 00006 // 00007 // uLCD_4DGL is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // uLCD_4DGL is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with uLCD_4DGL. If not, see <http://www.gnu.org/licenses/>. 00019 00020 #include "mbed.h" 00021 #include "uLCD_4DGL.h" 00022 00023 #define ARRAY_SIZE(X) sizeof(X)/sizeof(X[0]) 00024 00025 //Serial pc(USBTX,USBRX); 00026 00027 00028 //****************************************************************************************************** 00029 uLCD_4DGL :: uLCD_4DGL(PinName tx, PinName rx, PinName rst) : _cmd(tx, rx), 00030 _rst(rst) 00031 #if DEBUGMODE 00032 ,pc(USBTX, USBRX) 00033 #endif // DEBUGMODE 00034 { 00035 // Constructor 00036 _cmd.baud(9600); 00037 #if DEBUGMODE 00038 pc.baud(115200); 00039 00040 pc.printf("\n\n\n"); 00041 pc.printf("*********************\n"); 00042 pc.printf("uLCD_4DGL CONSTRUCTOR\n"); 00043 pc.printf("*********************\n"); 00044 #endif 00045 00046 _rst = 1; // put RESET pin to high to start TFT screen 00047 reset(); 00048 cls(); // clear screen 00049 current_col = 0; // initial cursor col 00050 current_row = 0; // initial cursor row 00051 current_color = WHITE; // initial text color 00052 current_orientation = IS_PORTRAIT; // initial screen orientation 00053 current_hf = 1; 00054 current_wf = 1; 00055 set_font(FONT_7X8); // initial font 00056 // text_mode(OPAQUE); // initial texr mode 00057 } 00058 00059 //****************************************************************************************************** 00060 void uLCD_4DGL :: writeBYTE(char c) // send a BYTE command to screen 00061 { 00062 00063 _cmd.putc(c); 00064 wait_us(500); //mbed is too fast for LCD at high baud rates in some long commands 00065 00066 #if DEBUGMODE 00067 pc.printf(" Char sent : 0x%02X\n",c); 00068 #endif 00069 00070 } 00071 00072 //****************************************************************************************************** 00073 void uLCD_4DGL :: writeBYTEfast(char c) // send a BYTE command to screen 00074 { 00075 00076 _cmd.putc(c); 00077 //wait_ms(0.0); //mbed is too fast for LCD at high baud rates - but not in short commands 00078 00079 #if DEBUGMODE 00080 pc.printf(" Char sent : 0x%02X\n",c); 00081 #endif 00082 00083 } 00084 //****************************************************************************************************** 00085 void uLCD_4DGL :: freeBUFFER(void) // Clear serial buffer before writing command 00086 { 00087 00088 while (_cmd.readable()) _cmd.getc(); // clear buffer garbage 00089 } 00090 00091 //****************************************************************************************************** 00092 int uLCD_4DGL :: writeCOMMAND(char *command, int number) // send several BYTES making a command and return an answer 00093 { 00094 00095 #if DEBUGMODE 00096 pc.printf("\n"); 00097 pc.printf("New COMMAND : 0x%02X\n", command[0]); 00098 #endif 00099 int i, resp = 0; 00100 freeBUFFER(); 00101 writeBYTE(0xFF); 00102 for (i = 0; i < number; i++) { 00103 if (i<16) 00104 writeBYTEfast(command[i]); // send command to serial port 00105 else 00106 writeBYTE(command[i]); // send command to serial port but slower 00107 } 00108 while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer 00109 if (_cmd.readable()) resp = _cmd.getc(); // read response if any 00110 switch (resp) { 00111 case ACK : // if OK return 1 00112 resp = 1; 00113 break; 00114 case NAK : // if NOK return -1 00115 resp = -1; 00116 break; 00117 default : 00118 resp = 0; // else return 0 00119 break; 00120 } 00121 #if DEBUGMODE 00122 pc.printf(" Answer received : %d\n",resp); 00123 #endif 00124 00125 return resp; 00126 } 00127 00128 //************************************************************************** 00129 void uLCD_4DGL :: reset() // Reset Screen 00130 { 00131 wait_ms(5); 00132 _rst = 0; // put RESET pin to low 00133 wait_ms(5); // wait a few milliseconds for command reception 00134 _rst = 1; // put RESET back to high 00135 wait(3); // wait 3s for screen to restart 00136 00137 freeBUFFER(); // clean buffer from possible garbage 00138 } 00139 //****************************************************************************************************** 00140 int uLCD_4DGL :: writeCOMMANDnull(char *command, int number) // send several BYTES making a command and return an answer 00141 { 00142 00143 #if DEBUGMODE 00144 pc.printf("\n"); 00145 pc.printf("New COMMAND : 0x%02X\n", command[0]); 00146 #endif 00147 int i, resp = 0; 00148 freeBUFFER(); 00149 writeBYTE(0x00); //command has a null prefix byte 00150 for (i = 0; i < number; i++) { 00151 if (i<16) //don't overflow LCD UART buffer 00152 writeBYTEfast(command[i]); // send command to serial port 00153 else 00154 writeBYTE(command[i]); // send command to serial port with delay 00155 } 00156 while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer 00157 if (_cmd.readable()) resp = _cmd.getc(); // read response if any 00158 switch (resp) { 00159 case ACK : // if OK return 1 00160 resp = 1; 00161 break; 00162 case NAK : // if NOK return -1 00163 resp = -1; 00164 break; 00165 default : 00166 resp = 0; // else return 0 00167 break; 00168 } 00169 #if DEBUGMODE 00170 pc.printf(" Answer received : %d\n",resp); 00171 #endif 00172 00173 return resp; 00174 } 00175 00176 //************************************************************************** 00177 void uLCD_4DGL :: cls() // clear screen 00178 { 00179 char command[1] = ""; 00180 00181 command[0] = CLS; 00182 writeCOMMAND(command, 1); 00183 current_row=0; 00184 current_col=0; 00185 current_hf = 1; 00186 current_wf = 1; 00187 set_font(FONT_7X8); // initial font 00188 } 00189 00190 //************************************************************************** 00191 int uLCD_4DGL :: version() // get API version 00192 { 00193 00194 char command[2] = ""; 00195 command[0] = '\x00'; 00196 command[1] = VERSION; 00197 return readVERSION(command, 2); 00198 } 00199 00200 //************************************************************************** 00201 void uLCD_4DGL :: baudrate(int speed) // set screen baud rate 00202 { 00203 char command[3]= ""; 00204 writeBYTE(0x00); 00205 command[0] = BAUDRATE; 00206 command[1] = 0; 00207 int newbaud = BAUD_9600; 00208 switch (speed) { 00209 case 110 : 00210 newbaud = BAUD_110; 00211 break; 00212 case 300 : 00213 newbaud = BAUD_300; 00214 break; 00215 case 600 : 00216 newbaud = BAUD_600; 00217 break; 00218 case 1200 : 00219 newbaud = BAUD_1200; 00220 break; 00221 case 2400 : 00222 newbaud = BAUD_2400; 00223 break; 00224 case 4800 : 00225 newbaud = BAUD_4800; 00226 break; 00227 case 9600 : 00228 newbaud = BAUD_9600; 00229 break; 00230 case 14400 : 00231 newbaud = BAUD_14400; 00232 break; 00233 case 19200 : 00234 newbaud = BAUD_19200; 00235 break; 00236 case 31250 : 00237 newbaud = BAUD_31250; 00238 break; 00239 case 38400 : 00240 newbaud = BAUD_38400; 00241 break; 00242 case 56000 : 00243 newbaud = BAUD_56000; 00244 break; 00245 case 57600 : 00246 newbaud = BAUD_57600; 00247 break; 00248 case 115200 : 00249 newbaud = BAUD_115200; 00250 break; 00251 case 128000 : 00252 newbaud = BAUD_128000; 00253 break; 00254 case 256000 : 00255 newbaud = BAUD_256000; 00256 break; 00257 case 300000 : 00258 newbaud = BAUD_300000; 00259 speed = 272727; 00260 break; 00261 case 375000 : 00262 newbaud = BAUD_375000; 00263 speed = 333333; 00264 break; 00265 case 500000 : 00266 newbaud = BAUD_500000; 00267 speed = 428571; 00268 break; 00269 case 600000 : 00270 newbaud = BAUD_600000; 00271 break; 00272 case 750000 : //rates over 600000 are not documented, but seem to work 00273 newbaud = BAUD_750000; 00274 break; 00275 case 1000000 : 00276 newbaud = BAUD_1000000; 00277 break; 00278 case 1500000 : 00279 newbaud = BAUD_1500000; 00280 break; 00281 case 3000000 : 00282 newbaud = BAUD_3000000; 00283 break; 00284 default : 00285 newbaud = BAUD_9600; 00286 speed = 9600; 00287 break; 00288 } 00289 00290 int i, resp = 0; 00291 00292 freeBUFFER(); 00293 command[1] = char(newbaud >>8); 00294 command[2] = char(newbaud % 256); 00295 wait_ms(1); 00296 for (i = 0; i <3; i++) writeBYTEfast(command[i]); // send command to serial port 00297 for (i = 0; i<10; i++) wait_ms(1); 00298 //dont change baud until all characters get sent out 00299 _cmd.baud(speed); // set mbed to same speed 00300 i=0; 00301 while ((!_cmd.readable()) && (i<25000)) { 00302 wait_ms(TEMPO); // wait for screen answer - comes 100ms after change 00303 i++; //timeout if ack character missed by baud change 00304 } 00305 if (_cmd.readable()) resp = _cmd.getc(); // read response if any 00306 switch (resp) { 00307 case ACK : // if OK return 1 00308 resp = 1; 00309 break; 00310 case NAK : // if NOK return -1 00311 resp = -1; 00312 break; 00313 default : 00314 resp = 0; // else return 0 00315 break; 00316 } 00317 } 00318 00319 //****************************************************************************************************** 00320 int uLCD_4DGL :: readVERSION(char *command, int number) // read screen info and populate data 00321 { 00322 00323 int i, temp = 0, resp = 0; 00324 char response[5] = ""; 00325 00326 freeBUFFER(); 00327 00328 for (i = 0; i < number; i++) writeBYTE(command[i]); // send all chars to serial port 00329 00330 while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer 00331 00332 while (_cmd.readable() && resp < ARRAY_SIZE(response)) { 00333 temp = _cmd.getc(); 00334 response[resp++] = (char)temp; 00335 } 00336 switch (resp) { 00337 case 2 : // if OK populate data and return 1 00338 revision = response[0]<<8 + response[1]; 00339 resp = 1; 00340 break; 00341 default : 00342 resp = 0; // else return 0 00343 break; 00344 } 00345 return resp; 00346 } 00347 00348 //**************************************************************************************************** 00349 void uLCD_4DGL :: background_color(int color) // set screen background color 00350 { 00351 char command[3]= ""; // input color is in 24bits like 0xRRGGBB 00352 00353 command[0] = BCKGDCOLOR; 00354 00355 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00356 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00357 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00358 00359 command[1] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00360 command[2] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00361 00362 writeCOMMAND(command, 3); 00363 } 00364 00365 //**************************************************************************************************** 00366 void uLCD_4DGL :: textbackground_color(int color) // set screen background color 00367 { 00368 char command[3]= ""; // input color is in 24bits like 0xRRGGBB 00369 00370 command[0] = TXTBCKGDCOLOR; 00371 00372 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00373 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00374 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00375 00376 command[1] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00377 command[2] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00378 00379 writeCOMMAND(command, 3); 00380 } 00381 00382 //**************************************************************************************************** 00383 void uLCD_4DGL :: display_control(char mode) // set screen mode to value 00384 { 00385 char command[3]= ""; 00386 00387 command[0] = DISPCONTROL; 00388 command[1] = 0; 00389 command[2] = mode; 00390 00391 if (mode == ORIENTATION) { 00392 switch (mode) { 00393 case LANDSCAPE : 00394 current_orientation = IS_LANDSCAPE; 00395 break; 00396 case LANDSCAPE_R : 00397 current_orientation = IS_LANDSCAPE; 00398 break; 00399 case PORTRAIT : 00400 current_orientation = IS_PORTRAIT; 00401 break; 00402 case PORTRAIT_R : 00403 current_orientation = IS_PORTRAIT; 00404 break; 00405 } 00406 } 00407 writeCOMMAND(command, 3); 00408 set_font(current_font); 00409 } 00410 //**************************************************************************************************** 00411 void uLCD_4DGL :: display_power(char mode) // set screen mode to value 00412 { 00413 char command[3]= ""; 00414 00415 command[0] = DISPPOWER; 00416 command[1] = 0; 00417 command[2] = mode; 00418 writeCOMMAND(command, 3); 00419 } 00420 //**************************************************************************************************** 00421 void uLCD_4DGL :: set_volume(char value) // set sound volume to value 00422 { 00423 char command[2]= ""; 00424 00425 command[0] = SETVOLUME; 00426 command[1] = value; 00427 00428 writeCOMMAND(command, 2); 00429 } 00430 00431 00432 //****************************************************************************************************** 00433 int uLCD_4DGL :: getSTATUS(char *command, int number) // read screen info and populate data 00434 { 00435 00436 #if DEBUGMODE 00437 pc.printf("\n"); 00438 pc.printf("New COMMAND : 0x%02X\n", command[0]); 00439 #endif 00440 00441 int i, temp = 0, resp = 0; 00442 char response[5] = ""; 00443 00444 freeBUFFER(); 00445 00446 for (i = 0; i < number; i++) writeBYTE(command[i]); // send all chars to serial port 00447 00448 while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer 00449 00450 while (_cmd.readable() && resp < ARRAY_SIZE(response)) { 00451 temp = _cmd.getc(); 00452 response[resp++] = (char)temp; 00453 } 00454 switch (resp) { 00455 case 4 : 00456 resp = (int)response[1]; // if OK populate data 00457 break; 00458 default : 00459 resp = -1; // else return 0 00460 break; 00461 } 00462 00463 #if DEBUGMODE 00464 pc.printf(" Answer received : %d\n", resp); 00465 #endif 00466 00467 return resp; 00468 } 00469
Generated on Mon Jul 18 2022 15:35:45 by
1.7.2
