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_Graphics.cpp
00001 // 00002 // uLCD_4DGL is a class to drive 4D Systems LCD screens 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 //**************************************************************************************************** 00026 void uLCD_4DGL :: circle(int x, int y , int radius, int color) // draw a circle in (x,y) 00027 { 00028 char command[9]= ""; 00029 00030 command[0] = CIRCLE; 00031 00032 command[1] = (x >> 8) & 0xFF; 00033 command[2] = x & 0xFF; 00034 00035 command[3] = (y >> 8) & 0xFF; 00036 command[4] = y & 0xFF; 00037 00038 command[5] = (radius >> 8) & 0xFF; 00039 command[6] = radius & 0xFF; 00040 00041 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00042 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00043 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00044 00045 command[7] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00046 command[8] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00047 00048 writeCOMMAND(command, 9); 00049 } 00050 //**************************************************************************************************** 00051 void uLCD_4DGL :: filled_circle(int x, int y , int radius, int color) // draw a circle in (x,y) 00052 { 00053 char command[9]= ""; 00054 00055 command[0] = FCIRCLE; 00056 00057 command[1] = (x >> 8) & 0xFF; 00058 command[2] = x & 0xFF; 00059 00060 command[3] = (y >> 8) & 0xFF; 00061 command[4] = y & 0xFF; 00062 00063 command[5] = (radius >> 8) & 0xFF; 00064 command[6] = radius & 0xFF; 00065 00066 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00067 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00068 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00069 00070 command[7] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00071 command[8] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00072 00073 writeCOMMAND(command, 9); 00074 } 00075 00076 //**************************************************************************************************** 00077 void uLCD_4DGL :: triangle(int x1, int y1 , int x2, int y2, int x3, int y3, int color) // draw a traingle 00078 { 00079 char command[15]= ""; 00080 00081 command[0] = TRIANGLE; 00082 00083 command[1] = (x1 >> 8) & 0xFF; 00084 command[2] = x1 & 0xFF; 00085 00086 command[3] = (y1 >> 8) & 0xFF; 00087 command[4] = y1 & 0xFF; 00088 00089 command[5] = (x2 >> 8) & 0xFF; 00090 command[6] = x2 & 0xFF; 00091 00092 command[7] = (y2 >> 8) & 0xFF; 00093 command[8] = y2 & 0xFF; 00094 00095 command[9] = (x3 >> 8) & 0xFF; 00096 command[10] = x3 & 0xFF; 00097 00098 command[11] = (y3 >> 8) & 0xFF; 00099 command[12] = y3 & 0xFF; 00100 00101 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00102 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00103 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00104 00105 command[13] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00106 command[14] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00107 00108 writeCOMMAND(command, 15); 00109 } 00110 00111 //**************************************************************************************************** 00112 void uLCD_4DGL :: line(int x1, int y1 , int x2, int y2, int color) // draw a line 00113 { 00114 char command[11]= ""; 00115 00116 command[0] = LINE; 00117 00118 command[1] = (x1 >> 8) & 0xFF; 00119 command[2] = x1 & 0xFF; 00120 00121 command[3] = (y1 >> 8) & 0xFF; 00122 command[4] = y1 & 0xFF; 00123 00124 command[5] = (x2 >> 8) & 0xFF; 00125 command[6] = x2 & 0xFF; 00126 00127 command[7] = (y2 >> 8) & 0xFF; 00128 command[8] = y2 & 0xFF; 00129 00130 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00131 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00132 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00133 00134 command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00135 command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00136 00137 writeCOMMAND(command, 11); 00138 } 00139 00140 //**************************************************************************************************** 00141 void uLCD_4DGL :: rectangle(int x1, int y1 , int x2, int y2, int color) // draw a rectangle 00142 { 00143 char command[11]= ""; 00144 00145 command[0] = RECTANGLE; 00146 00147 command[1] = (x1 >> 8) & 0xFF; 00148 command[2] = x1 & 0xFF; 00149 00150 command[3] = (y1 >> 8) & 0xFF; 00151 command[4] = y1 & 0xFF; 00152 00153 command[5] = (x2 >> 8) & 0xFF; 00154 command[6] = x2 & 0xFF; 00155 00156 command[7] = (y2 >> 8) & 0xFF; 00157 command[8] = y2 & 0xFF; 00158 00159 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00160 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00161 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00162 00163 command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00164 command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00165 00166 writeCOMMAND(command, 11); 00167 } 00168 00169 //**************************************************************************************************** 00170 void uLCD_4DGL :: filled_rectangle(int x1, int y1 , int x2, int y2, int color) // draw a rectangle 00171 { 00172 char command[11]= ""; 00173 00174 command[0] = FRECTANGLE; 00175 00176 command[1] = (x1 >> 8) & 0xFF; 00177 command[2] = x1 & 0xFF; 00178 00179 command[3] = (y1 >> 8) & 0xFF; 00180 command[4] = y1 & 0xFF; 00181 00182 command[5] = (x2 >> 8) & 0xFF; 00183 command[6] = x2 & 0xFF; 00184 00185 command[7] = (y2 >> 8) & 0xFF; 00186 command[8] = y2 & 0xFF; 00187 00188 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00189 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00190 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00191 00192 command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00193 command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00194 00195 writeCOMMAND(command, 11); 00196 } 00197 00198 00199 00200 //**************************************************************************************************** 00201 void uLCD_4DGL :: pixel(int x, int y, int color) // draw a pixel 00202 { 00203 char command[7]= ""; 00204 00205 command[0] = PIXEL; 00206 00207 command[1] = (x >> 8) & 0xFF; 00208 command[2] = x & 0xFF; 00209 00210 command[3] = (y >> 8) & 0xFF; 00211 command[4] = y & 0xFF; 00212 00213 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits 00214 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits 00215 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits 00216 00217 command[5] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color 00218 command[6] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color 00219 00220 writeCOMMAND(command, 7); 00221 } 00222 //**************************************************************************************************** 00223 void uLCD_4DGL :: BLIT(int x, int y, int w, int h, int *colors) // draw a block of pixels 00224 { 00225 int red5, green6, blue5; 00226 writeBYTEfast('\x00'); 00227 writeBYTEfast(BLITCOM); 00228 writeBYTEfast((x >> 8) & 0xFF); 00229 writeBYTEfast(x & 0xFF); 00230 writeBYTEfast((y >> 8) & 0xFF); 00231 writeBYTEfast(y & 0xFF); 00232 writeBYTEfast((w >> 8) & 0xFF); 00233 writeBYTE(w & 0xFF); 00234 writeBYTE((h >> 8) & 0xFF); 00235 writeBYTE(h & 0xFF); 00236 wait_ms(1); 00237 for (int i=0; i<w*h; i++) { 00238 red5 = (colors[i] >> (16 + 3)) & 0x1F; // get red on 5 bits 00239 green6 = (colors[i] >> (8 + 2)) & 0x3F; // get green on 6 bits 00240 blue5 = (colors[i] >> (0 + 3)) & 0x1F; // get blue on 5 bits 00241 writeBYTEfast(((red5 << 3) + (green6 >> 3)) & 0xFF); // first part of 16 bits color 00242 writeBYTEfast(((green6 << 5) + (blue5 >> 0)) & 0xFF); // second part of 16 bits color 00243 } 00244 int resp=0; 00245 while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer 00246 if (_cmd.readable()) resp = _cmd.getc(); // read response if any 00247 switch (resp) { 00248 case ACK : // if OK return 1 00249 resp = 1; 00250 break; 00251 case NAK : // if NOK return -1 00252 resp = -1; 00253 break; 00254 default : 00255 resp = 0; // else return 0 00256 break; 00257 } 00258 #if DEBUGMODE 00259 pc.printf(" Answer received : %d\n",resp); 00260 #endif 00261 00262 } 00263 //****************************************************************************************************** 00264 int uLCD_4DGL :: read_pixel(int x, int y) // read screen info and populate data 00265 { 00266 00267 char command[6]= ""; 00268 command[0] = 0xFF; 00269 command[1] = READPIXEL; 00270 00271 command[2] = (x >> 8) & 0xFF; 00272 command[3] = x & 0xFF; 00273 00274 command[4] = (y >> 8) & 0xFF; 00275 command[5] = y & 0xFF; 00276 00277 int i, temp = 0, color = 0, resp = 0; 00278 char response[2] = ""; 00279 00280 freeBUFFER(); 00281 00282 for (i = 0; i < 6; i++) { // send all chars to serial port 00283 writeBYTE(command[i]); 00284 } 00285 00286 while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer 00287 00288 while (_cmd.readable() && resp < ARRAY_SIZE(response)) { 00289 temp = _cmd.getc(); 00290 response[resp++] = (char)temp; 00291 } 00292 00293 color = ((response[0] << 8) + response[1]); 00294 00295 return color; // WARNING : this is 16bits color, not 24bits... need to be fixed 00296 } 00297 00298 00299 //**************************************************************************************************** 00300 void uLCD_4DGL :: pen_size(char mode) // set pen to SOLID or WIREFRAME 00301 { 00302 char command[2]= ""; 00303 00304 command[0] = PENSIZE; 00305 command[1] = mode; 00306 writeCOMMAND(command, 2); 00307 } 00308 00309 00310
Generated on Mon Jul 18 2022 15:35:45 by
1.7.2
