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.
TouchScreen.cpp
00001 /** 00002 * @file 00003 * @version 00004 * @author Ashley.Mills@vodafone.com 00005 * @brief Touchscreen control. 00006 */ 00007 00008 #include "TouchScreen.h" 00009 00010 #define WAIT_TIMEOUT_MS 1000 00011 00012 TouchScreen::TouchScreen(PinName tx, PinName rx, PinName resetPin, const char *name) : Serial(tx,rx,name) { 00013 baud(9600); 00014 _resetPin = new DigitalOut(resetPin); 00015 _resetPin->write(1); 00016 reset(); 00017 _initOK = false; 00018 putc('U'); 00019 _initOK = gack(); 00020 _fillColor = createRGBClassic(21,21,21); 00021 _strokeColor = createRGBClassic(221,221,221); 00022 } 00023 00024 TouchScreen::TouchScreen(PinName tx, PinName rx, PinName resetPin, eBaudRate baudRate, const char *name) : Serial (tx,rx,name) { 00025 baud(9600); 00026 _resetPin = new DigitalOut(resetPin); 00027 _resetPin->write(1); 00028 reset(); 00029 _initOK = false; 00030 putc('U'); 00031 if((_initOK=gack())) { 00032 setBaudRate(baudRate); 00033 } 00034 _fillColor = createRGBClassic(21,21,21); 00035 _strokeColor = createRGBClassic(221,221,221); 00036 } 00037 00038 void TouchScreen::reset() { 00039 _resetPin->write(0); 00040 Thread::wait(2); 00041 _resetPin->write(1); 00042 Thread::wait(3000); 00043 } 00044 00045 void TouchScreen::clearScreen() { 00046 putc('E'); 00047 gack(); 00048 } 00049 00050 bool TouchScreen::setBaudRate(eBaudRate baudRate) { 00051 long newRate = 9600; 00052 uint8_t rateCommand = (uint8_t)0x06; 00053 putc('Q'); 00054 switch(baudRate) { 00055 case BAUD_9600: 00056 rateCommand = (uint8_t)0x06; 00057 newRate = 9600; 00058 break; 00059 00060 case BAUD_14400: 00061 rateCommand = (uint8_t)0x07; 00062 newRate = 14400; 00063 break; 00064 00065 case BAUD_19200: 00066 rateCommand = (uint8_t)0x08; 00067 newRate = 19200; 00068 break; 00069 00070 case BAUD_57600: 00071 rateCommand = (uint8_t)0x0C; 00072 newRate = 57600; 00073 break; 00074 00075 case BAUD_115200: 00076 rateCommand = (uint8_t)0x0D; 00077 newRate = 115200; 00078 break; 00079 00080 case BAUD_256000: 00081 rateCommand = (uint8_t)0x0F; 00082 newRate = 256000; 00083 break; 00084 } 00085 putc((uint8_t)rateCommand); 00086 // doesn't seem to want to acknowledge at either the old baud rate or the new baud rate 00087 dropBytes(1); 00088 baud(newRate); 00089 return true; 00090 } 00091 00092 void TouchScreen::setPenSolid() { 00093 putc('p'); 00094 putc((uint8_t)0x0); 00095 gack(); 00096 } 00097 00098 void TouchScreen::setPenWireframe() { 00099 putc('p'); 00100 putc((uint8_t)0x1); 00101 gack(); 00102 } 00103 00104 void TouchScreen::setBackgroundColor(uint16_t color) { 00105 putc('K'); 00106 putc(highByte(color)); 00107 putc(lowByte(color)); 00108 gack(); 00109 } 00110 00111 bool TouchScreen::waitUntilReadable() { 00112 long timeOut = WAIT_TIMEOUT_MS; 00113 Timer timer; 00114 timer.start(); 00115 while(1) { 00116 if(readable()) 00117 return true; 00118 if(timer.read_ms()>timeOut) 00119 return false; 00120 } 00121 } 00122 00123 bool TouchScreen::gack() { 00124 if(waitUntilReadable()) { 00125 uint8_t b = getc(); 00126 return (b==0x06); 00127 } 00128 return false; 00129 } 00130 00131 void TouchScreen::drawPixel(uint16_t xPos, uint16_t yPos, uint16_t color) { 00132 putc('P'); 00133 putc(highByte(xPos)); // x pos 00134 putc(lowByte(xPos)); 00135 00136 putc(highByte(yPos)); // y pos 00137 putc(lowByte(yPos)); 00138 00139 putc(highByte(color)); // color 00140 putc(lowByte(color)); 00141 gack(); 00142 } 00143 00144 void TouchScreen::drawCircle(uint16_t xPos, uint16_t yPos, uint16_t radius, uint16_t color) { 00145 putc('C'); 00146 putc(highByte(xPos)); // x pos 00147 putc(lowByte(xPos)); 00148 00149 putc(highByte(yPos)); // y pos 00150 putc(lowByte(yPos)); 00151 00152 putc(highByte(radius)); // radius 00153 putc(lowByte(radius)); 00154 00155 putc(highByte(color)); // color 00156 putc(lowByte(color)); 00157 gack(); 00158 } 00159 00160 uint16_t TouchScreen::createRGBClassic(int r, int g, int b) { 00161 return createRGBF(r/255.0f,g/255.0f,b/255.0f); 00162 } 00163 00164 uint16_t TouchScreen::createRGB(uint8_t r, uint8_t g, uint8_t b) { 00165 if(r>31) r = 31; 00166 if(g>63) g = 63; 00167 if(b>31) b = 31; 00168 uint16_t o = 0; 00169 o |= (r<<0xb); 00170 o |= (g<<0x5); 00171 o |= b; 00172 return o; 00173 } 00174 00175 uint16_t TouchScreen::createRGBF(float r, float g, float b) { 00176 return createRGB((uint8_t)(r*31.0f),(uint8_t)(g*63.0f),(uint8_t)(b*31.0f)); 00177 } 00178 00179 void TouchScreen::enableTouch() { 00180 putc('Y'); 00181 putc((uint8_t)0x5); 00182 putc((uint8_t)0x0); 00183 gack(); 00184 } 00185 00186 void TouchScreen::disableTouch() { 00187 putc('Y'); 00188 putc((uint8_t)0x5); 00189 putc((uint8_t)0x1); 00190 gack(); 00191 } 00192 00193 void TouchScreen::setOrientation(eScreenOrientation o) { 00194 putc('Y'); 00195 putc((uint8_t)0x4); 00196 putc((uint8_t)o); 00197 gack(); 00198 } 00199 00200 void TouchScreen::getVersion() { 00201 putc('V'); 00202 putc((uint8_t)0x1); 00203 dropBytes((uint16_t)5); 00204 } 00205 00206 void TouchScreen::dropBytes(uint16_t numBytes) { 00207 while(numBytes--) { 00208 dropByte(); 00209 } 00210 } 00211 00212 void TouchScreen::drawImage(char *name, uint16_t x, uint16_t y) { 00213 putc('@'); 00214 putc('m'); 00215 printf(name); 00216 putc((uint8_t)0); 00217 putc(highByte((uint16_t)x)); 00218 putc(lowByte((uint16_t)x)); 00219 putc(highByte((uint16_t)y)); 00220 putc(lowByte((uint16_t)y)); 00221 putc((uint8_t)0x00); 00222 putc((uint8_t)0x00); 00223 putc((uint8_t)0x00); 00224 putc((uint8_t)0x00); 00225 gack(); 00226 } 00227 00228 void TouchScreen::dropByte() { 00229 if(waitUntilReadable()) { 00230 getc(); 00231 } 00232 } 00233 00234 void TouchScreen::dropByteAndWaitUntilReadable() { 00235 if(waitUntilReadable()) { 00236 getc(); 00237 } 00238 waitUntilReadable(); 00239 } 00240 00241 eTouchStatus TouchScreen::getTouchStatus() { 00242 putc('o'); 00243 putc((uint8_t)0x4); 00244 dropByteAndWaitUntilReadable(); 00245 uint8_t touchStatus = getc(); 00246 dropByte(); 00247 dropByte(); 00248 return (eTouchStatus)touchStatus; 00249 } 00250 00251 TouchPoint TouchScreen::getLastTouch() { 00252 TouchPoint p; 00253 00254 putc('o'); 00255 putc((uint8_t)0x05); 00256 waitUntilReadable(); 00257 uint8_t v = getc(); 00258 p.x = (v<<0x8); 00259 waitUntilReadable(); 00260 p.x += getc(); 00261 waitUntilReadable(); 00262 v = getc(); 00263 p.y = (v<<0x8); 00264 waitUntilReadable(); 00265 p.y += getc(); 00266 return p; 00267 } 00268 00269 void TouchScreen::setBacklight(bool state) { 00270 putc('Y'); 00271 putc((uint8_t)0x00); 00272 if(state) { 00273 putc((uint8_t)0x01); 00274 } else { 00275 putc((uint8_t)0x00); 00276 } 00277 gack(); 00278 } 00279 00280 void TouchScreen::setContrast(uint8_t value) { 00281 if(value>0x0F) 00282 value = 0x0F; 00283 putc('Y'); 00284 putc((uint8_t)0x02); 00285 putc(value); 00286 gack(); 00287 } 00288 00289 void TouchScreen::drawGraphicsFormatString(uint16_t x, uint16_t y, eInternalFont font, uint16_t textColor, uint8_t charWidth, uint8_t charHeight, char* text) { 00290 putc('S'); 00291 putc((uint8_t)highByte(x)); 00292 putc((uint8_t)lowByte(x)); 00293 putc((uint8_t)highByte(y)); 00294 putc((uint8_t)lowByte(y)); 00295 putc((uint8_t)font); 00296 putc((uint8_t)highByte(textColor)); 00297 putc((uint8_t)lowByte(textColor)); 00298 putc((uint8_t)charWidth); 00299 putc((uint8_t)charHeight); 00300 if(strlen(text)==256) { // change to own implementation of strnlen 00301 text[256] = 0; 00302 } 00303 printf(text); 00304 putc((uint8_t)0x00); 00305 gack(); 00306 } 00307 00308 00309 void TouchScreen::drawTextFormatString(uint8_t row, uint8_t column, eInternalFont font, uint16_t textColor, char* text) { 00310 putc('s'); 00311 putc((uint8_t)column); 00312 putc((uint8_t)row); 00313 putc((uint8_t)font); 00314 putc((uint8_t)highByte(textColor)); 00315 putc((uint8_t)lowByte(textColor)); 00316 if(strlen(text)==256) { // change to own implementation of strnlen 00317 text[256] = 0; 00318 } 00319 printf(text); 00320 putc((uint8_t)0x00); 00321 gack(); 00322 } 00323 00324 void TouchScreen::drawRectangle(uint16_t topLeftX, uint16_t topLeftY, uint16_t botRightX, uint16_t botRightY, uint16_t color) { 00325 putc('r'); 00326 putc(highByte(topLeftX)); 00327 putc(lowByte (topLeftX)); 00328 putc(highByte(topLeftY)); 00329 putc(lowByte (topLeftY)); 00330 putc(highByte(botRightX)); 00331 putc(lowByte (botRightX)); 00332 putc(highByte(botRightY)); 00333 putc(lowByte (botRightY)); 00334 putc(highByte(color)); 00335 putc(lowByte (color)); 00336 gack(); 00337 } 00338 00339 void TouchScreen::drawRectangleWH(uint16_t topLeftX, uint16_t topLeftY, uint16_t width, uint16_t height, uint16_t color) { 00340 uint16_t botRightX = topLeftX + width; 00341 uint16_t botRightY = topLeftY + height; 00342 putc('r'); 00343 putc(highByte(topLeftX)); 00344 putc(lowByte (topLeftX)); 00345 putc(highByte(topLeftY)); 00346 putc(lowByte (topLeftY)); 00347 putc(highByte(botRightX)); 00348 putc(lowByte (botRightX)); 00349 putc(highByte(botRightY)); 00350 putc(lowByte (botRightY)); 00351 putc(highByte(color)); 00352 putc(lowByte (color)); 00353 gack(); 00354 } 00355 00356 void TouchScreen::drawRectangleWH(uint16_t topLeftX, uint16_t topLeftY, uint16_t width, uint16_t height) { 00357 setPenSolid(); 00358 drawRectangleWH(topLeftX,topLeftY,width,height,_fillColor); 00359 setPenWireframe(); 00360 drawRectangleWH(topLeftX,topLeftY,width,height,_strokeColor); 00361 setPenSolid(); 00362 } 00363 00364 00365 void TouchScreen::drawLine(uint16_t topLeftX, uint16_t topLeftY, uint16_t botRightX, uint16_t botRightY, uint16_t color) { 00366 putc('L'); 00367 putc(highByte(topLeftX)); 00368 putc(lowByte (topLeftX)); 00369 putc(highByte(topLeftY)); 00370 putc(lowByte (topLeftY)); 00371 putc(highByte(botRightX)); 00372 putc(lowByte (botRightX)); 00373 putc(highByte(botRightY)); 00374 putc(lowByte (botRightY)); 00375 putc(highByte(color)); 00376 putc(lowByte (color)); 00377 gack(); 00378 } 00379 00380 void TouchScreen::drawChar(char c, uint16_t x, uint16_t y, uint16_t color, uint8_t width, uint8_t height) { 00381 putc('t'); 00382 putc(c); 00383 putc(highByte(x)); 00384 putc(lowByte (x)); 00385 putc(highByte(y)); 00386 putc(lowByte (y)); 00387 putc(highByte(color)); 00388 putc(lowByte (color)); 00389 putc(width); 00390 putc(height); 00391 gack(); 00392 } 00393 00394 00395 void TouchScreen::setFont(uint8_t fontIndex) { 00396 putc('F'); 00397 putc(fontIndex); 00398 gack(); 00399 } 00400 00401 void TouchScreen::drawText(char *s, uint16_t x, uint16_t y) { 00402 drawGraphicsFormatString( 00403 x,y, 00404 INTERNAL_FONT_8x8, 00405 _strokeColor, 00406 1,1, 00407 s 00408 ); 00409 } 00410 00411 void TouchScreen::setFillColor(uint16_t c) { 00412 _fillColor = c; 00413 } 00414 00415 void TouchScreen::setStrokeColor(uint16_t c) { 00416 _strokeColor = c; 00417 }
Generated on Wed Jul 13 2022 00:44:04 by
1.7.2