uVGA II for display using vga

Dependents:   4DGL

Committer:
pprasad7
Date:
Thu Oct 11 20:12:02 2012 +0000
Revision:
0:6741e7724181
hangman

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pprasad7 0:6741e7724181 1 //
pprasad7 0:6741e7724181 2 // TFT_4DGL is a class to drive 4D Systems TFT touch screens
pprasad7 0:6741e7724181 3 //
pprasad7 0:6741e7724181 4 // Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr>
pprasad7 0:6741e7724181 5 //
pprasad7 0:6741e7724181 6 // TFT_4DGL is free software: you can redistribute it and/or modify
pprasad7 0:6741e7724181 7 // it under the terms of the GNU General Public License as published by
pprasad7 0:6741e7724181 8 // the Free Software Foundation, either version 3 of the License, or
pprasad7 0:6741e7724181 9 // (at your option) any later version.
pprasad7 0:6741e7724181 10 //
pprasad7 0:6741e7724181 11 // TFT_4DGL is distributed in the hope that it will be useful,
pprasad7 0:6741e7724181 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
pprasad7 0:6741e7724181 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
pprasad7 0:6741e7724181 14 // GNU General Public License for more details.
pprasad7 0:6741e7724181 15 //
pprasad7 0:6741e7724181 16 // You should have received a copy of the GNU General Public License
pprasad7 0:6741e7724181 17 // along with TFT_4DGL. If not, see <http://www.gnu.org/licenses/>.
pprasad7 0:6741e7724181 18
pprasad7 0:6741e7724181 19 #include "mbed.h"
pprasad7 0:6741e7724181 20 #include "TFT_4DGL.h"
pprasad7 0:6741e7724181 21
pprasad7 0:6741e7724181 22 #define ARRAY_SIZE(X) sizeof(X)/sizeof(X[0])
pprasad7 0:6741e7724181 23
pprasad7 0:6741e7724181 24 //****************************************************************************************************
pprasad7 0:6741e7724181 25 void TFT_4DGL :: circle(int x, int y , int radius, int color) { // draw a circle in (x,y)
pprasad7 0:6741e7724181 26 char command[9]= "";
pprasad7 0:6741e7724181 27
pprasad7 0:6741e7724181 28 command[0] = CIRCLE;
pprasad7 0:6741e7724181 29
pprasad7 0:6741e7724181 30 command[1] = (x >> 8) & 0xFF;
pprasad7 0:6741e7724181 31 command[2] = x & 0xFF;
pprasad7 0:6741e7724181 32
pprasad7 0:6741e7724181 33 command[3] = (y >> 8) & 0xFF;
pprasad7 0:6741e7724181 34 command[4] = y & 0xFF;
pprasad7 0:6741e7724181 35
pprasad7 0:6741e7724181 36 command[5] = (radius >> 8) & 0xFF;
pprasad7 0:6741e7724181 37 command[6] = radius & 0xFF;
pprasad7 0:6741e7724181 38
pprasad7 0:6741e7724181 39 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
pprasad7 0:6741e7724181 40 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
pprasad7 0:6741e7724181 41 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
pprasad7 0:6741e7724181 42
pprasad7 0:6741e7724181 43 command[7] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
pprasad7 0:6741e7724181 44 command[8] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
pprasad7 0:6741e7724181 45
pprasad7 0:6741e7724181 46 writeCOMMAND(command, 9);
pprasad7 0:6741e7724181 47 }
pprasad7 0:6741e7724181 48
pprasad7 0:6741e7724181 49 //****************************************************************************************************
pprasad7 0:6741e7724181 50 void TFT_4DGL :: triangle(int x1, int y1 , int x2, int y2, int x3, int y3, int color) { // draw a traingle
pprasad7 0:6741e7724181 51 char command[15]= "";
pprasad7 0:6741e7724181 52
pprasad7 0:6741e7724181 53 command[0] = TRIANGLE;
pprasad7 0:6741e7724181 54
pprasad7 0:6741e7724181 55 command[1] = (x1 >> 8) & 0xFF;
pprasad7 0:6741e7724181 56 command[2] = x1 & 0xFF;
pprasad7 0:6741e7724181 57
pprasad7 0:6741e7724181 58 command[3] = (y1 >> 8) & 0xFF;
pprasad7 0:6741e7724181 59 command[4] = y1 & 0xFF;
pprasad7 0:6741e7724181 60
pprasad7 0:6741e7724181 61 command[5] = (x2 >> 8) & 0xFF;
pprasad7 0:6741e7724181 62 command[6] = x2 & 0xFF;
pprasad7 0:6741e7724181 63
pprasad7 0:6741e7724181 64 command[7] = (y2 >> 8) & 0xFF;
pprasad7 0:6741e7724181 65 command[8] = y2 & 0xFF;
pprasad7 0:6741e7724181 66
pprasad7 0:6741e7724181 67 command[9] = (x3 >> 8) & 0xFF;
pprasad7 0:6741e7724181 68 command[10] = x3 & 0xFF;
pprasad7 0:6741e7724181 69
pprasad7 0:6741e7724181 70 command[11] = (y3 >> 8) & 0xFF;
pprasad7 0:6741e7724181 71 command[12] = y3 & 0xFF;
pprasad7 0:6741e7724181 72
pprasad7 0:6741e7724181 73 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
pprasad7 0:6741e7724181 74 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
pprasad7 0:6741e7724181 75 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
pprasad7 0:6741e7724181 76
pprasad7 0:6741e7724181 77 command[13] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
pprasad7 0:6741e7724181 78 command[14] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
pprasad7 0:6741e7724181 79
pprasad7 0:6741e7724181 80 writeCOMMAND(command, 15);
pprasad7 0:6741e7724181 81 }
pprasad7 0:6741e7724181 82
pprasad7 0:6741e7724181 83 //****************************************************************************************************
pprasad7 0:6741e7724181 84 void TFT_4DGL :: line(int x1, int y1 , int x2, int y2, int color) { // draw a line
pprasad7 0:6741e7724181 85 char command[11]= "";
pprasad7 0:6741e7724181 86
pprasad7 0:6741e7724181 87 command[0] = LINE;
pprasad7 0:6741e7724181 88
pprasad7 0:6741e7724181 89 command[1] = (x1 >> 8) & 0xFF;
pprasad7 0:6741e7724181 90 command[2] = x1 & 0xFF;
pprasad7 0:6741e7724181 91
pprasad7 0:6741e7724181 92 command[3] = (y1 >> 8) & 0xFF;
pprasad7 0:6741e7724181 93 command[4] = y1 & 0xFF;
pprasad7 0:6741e7724181 94
pprasad7 0:6741e7724181 95 command[5] = (x2 >> 8) & 0xFF;
pprasad7 0:6741e7724181 96 command[6] = x2 & 0xFF;
pprasad7 0:6741e7724181 97
pprasad7 0:6741e7724181 98 command[7] = (y2 >> 8) & 0xFF;
pprasad7 0:6741e7724181 99 command[8] = y2 & 0xFF;
pprasad7 0:6741e7724181 100
pprasad7 0:6741e7724181 101 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
pprasad7 0:6741e7724181 102 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
pprasad7 0:6741e7724181 103 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
pprasad7 0:6741e7724181 104
pprasad7 0:6741e7724181 105 command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
pprasad7 0:6741e7724181 106 command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
pprasad7 0:6741e7724181 107
pprasad7 0:6741e7724181 108 writeCOMMAND(command, 11);
pprasad7 0:6741e7724181 109 }
pprasad7 0:6741e7724181 110
pprasad7 0:6741e7724181 111 //****************************************************************************************************
pprasad7 0:6741e7724181 112 void TFT_4DGL :: rectangle(int x1, int y1 , int x2, int y2, int color) { // draw a rectangle
pprasad7 0:6741e7724181 113 char command[11]= "";
pprasad7 0:6741e7724181 114
pprasad7 0:6741e7724181 115 command[0] = RECTANGLE;
pprasad7 0:6741e7724181 116
pprasad7 0:6741e7724181 117 command[1] = (x1 >> 8) & 0xFF;
pprasad7 0:6741e7724181 118 command[2] = x1 & 0xFF;
pprasad7 0:6741e7724181 119
pprasad7 0:6741e7724181 120 command[3] = (y1 >> 8) & 0xFF;
pprasad7 0:6741e7724181 121 command[4] = y1 & 0xFF;
pprasad7 0:6741e7724181 122
pprasad7 0:6741e7724181 123 command[5] = (x2 >> 8) & 0xFF;
pprasad7 0:6741e7724181 124 command[6] = x2 & 0xFF;
pprasad7 0:6741e7724181 125
pprasad7 0:6741e7724181 126 command[7] = (y2 >> 8) & 0xFF;
pprasad7 0:6741e7724181 127 command[8] = y2 & 0xFF;
pprasad7 0:6741e7724181 128
pprasad7 0:6741e7724181 129 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
pprasad7 0:6741e7724181 130 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
pprasad7 0:6741e7724181 131 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
pprasad7 0:6741e7724181 132
pprasad7 0:6741e7724181 133 command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
pprasad7 0:6741e7724181 134 command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
pprasad7 0:6741e7724181 135
pprasad7 0:6741e7724181 136 writeCOMMAND(command, 11);
pprasad7 0:6741e7724181 137 }
pprasad7 0:6741e7724181 138
pprasad7 0:6741e7724181 139 //****************************************************************************************************
pprasad7 0:6741e7724181 140 void TFT_4DGL :: ellipse(int x, int y , int radius_x, int radius_y, int color) { // draw an ellipse
pprasad7 0:6741e7724181 141 char command[11]= "";
pprasad7 0:6741e7724181 142
pprasad7 0:6741e7724181 143 command[0] = ELLIPSE;
pprasad7 0:6741e7724181 144
pprasad7 0:6741e7724181 145 command[1] = (x >> 8) & 0xFF;
pprasad7 0:6741e7724181 146 command[2] = x & 0xFF;
pprasad7 0:6741e7724181 147
pprasad7 0:6741e7724181 148 command[3] = (y >> 8) & 0xFF;
pprasad7 0:6741e7724181 149 command[4] = y & 0xFF;
pprasad7 0:6741e7724181 150
pprasad7 0:6741e7724181 151 command[5] = (radius_x >> 8) & 0xFF;
pprasad7 0:6741e7724181 152 command[6] = radius_x & 0xFF;
pprasad7 0:6741e7724181 153
pprasad7 0:6741e7724181 154 command[7] = (radius_y >> 8) & 0xFF;
pprasad7 0:6741e7724181 155 command[8] = radius_y & 0xFF;
pprasad7 0:6741e7724181 156
pprasad7 0:6741e7724181 157 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
pprasad7 0:6741e7724181 158 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
pprasad7 0:6741e7724181 159 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
pprasad7 0:6741e7724181 160
pprasad7 0:6741e7724181 161 command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
pprasad7 0:6741e7724181 162 command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
pprasad7 0:6741e7724181 163
pprasad7 0:6741e7724181 164 writeCOMMAND(command, 11);
pprasad7 0:6741e7724181 165 }
pprasad7 0:6741e7724181 166
pprasad7 0:6741e7724181 167 //****************************************************************************************************
pprasad7 0:6741e7724181 168 void TFT_4DGL :: pixel(int x, int y, int color) { // draw a pixel
pprasad7 0:6741e7724181 169 char command[7]= "";
pprasad7 0:6741e7724181 170
pprasad7 0:6741e7724181 171 command[0] = PIXEL;
pprasad7 0:6741e7724181 172
pprasad7 0:6741e7724181 173 command[1] = (x >> 8) & 0xFF;
pprasad7 0:6741e7724181 174 command[2] = x & 0xFF;
pprasad7 0:6741e7724181 175
pprasad7 0:6741e7724181 176 command[3] = (y >> 8) & 0xFF;
pprasad7 0:6741e7724181 177 command[4] = y & 0xFF;
pprasad7 0:6741e7724181 178
pprasad7 0:6741e7724181 179 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
pprasad7 0:6741e7724181 180 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
pprasad7 0:6741e7724181 181 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
pprasad7 0:6741e7724181 182
pprasad7 0:6741e7724181 183 command[5] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
pprasad7 0:6741e7724181 184 command[6] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
pprasad7 0:6741e7724181 185
pprasad7 0:6741e7724181 186 writeCOMMAND(command, 7);
pprasad7 0:6741e7724181 187 }
pprasad7 0:6741e7724181 188
pprasad7 0:6741e7724181 189 //******************************************************************************************************
pprasad7 0:6741e7724181 190 int TFT_4DGL :: read_pixel(int x, int y) { // read screen info and populate data
pprasad7 0:6741e7724181 191
pprasad7 0:6741e7724181 192 char command[5]= "";
pprasad7 0:6741e7724181 193
pprasad7 0:6741e7724181 194 command[0] = READPIXEL;
pprasad7 0:6741e7724181 195
pprasad7 0:6741e7724181 196 command[1] = (x >> 8) & 0xFF;
pprasad7 0:6741e7724181 197 command[2] = x & 0xFF;
pprasad7 0:6741e7724181 198
pprasad7 0:6741e7724181 199 command[3] = (y >> 8) & 0xFF;
pprasad7 0:6741e7724181 200 command[4] = y & 0xFF;
pprasad7 0:6741e7724181 201
pprasad7 0:6741e7724181 202 int i, temp = 0, color = 0, resp = 0;
pprasad7 0:6741e7724181 203 char response[2] = "";
pprasad7 0:6741e7724181 204
pprasad7 0:6741e7724181 205 freeBUFFER();
pprasad7 0:6741e7724181 206
pprasad7 0:6741e7724181 207 for (i = 0; i < 5; i++) { // send all chars to serial port
pprasad7 0:6741e7724181 208 writeBYTE(command[i]);
pprasad7 0:6741e7724181 209 }
pprasad7 0:6741e7724181 210
pprasad7 0:6741e7724181 211 while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer
pprasad7 0:6741e7724181 212
pprasad7 0:6741e7724181 213 while (_cmd.readable() && resp < ARRAY_SIZE(response)) {
pprasad7 0:6741e7724181 214 temp = _cmd.getc();
pprasad7 0:6741e7724181 215 response[resp++] = (char)temp;
pprasad7 0:6741e7724181 216 }
pprasad7 0:6741e7724181 217
pprasad7 0:6741e7724181 218 color = ((response[0] << 8) + response[1]);
pprasad7 0:6741e7724181 219
pprasad7 0:6741e7724181 220 return color; // WARNING : this is 16bits color, not 24bits... need to be fixed
pprasad7 0:6741e7724181 221 }
pprasad7 0:6741e7724181 222
pprasad7 0:6741e7724181 223 //******************************************************************************************************
pprasad7 0:6741e7724181 224 void TFT_4DGL :: screen_copy(int xs, int ys , int xd, int yd , int width, int height) {
pprasad7 0:6741e7724181 225
pprasad7 0:6741e7724181 226 char command[13]= "";
pprasad7 0:6741e7724181 227
pprasad7 0:6741e7724181 228 command[0] = SCREENCOPY;
pprasad7 0:6741e7724181 229
pprasad7 0:6741e7724181 230 command[1] = (xs >> 8) & 0xFF;
pprasad7 0:6741e7724181 231 command[2] = xs & 0xFF;
pprasad7 0:6741e7724181 232
pprasad7 0:6741e7724181 233 command[3] = (ys >> 8) & 0xFF;
pprasad7 0:6741e7724181 234 command[4] = ys & 0xFF;
pprasad7 0:6741e7724181 235
pprasad7 0:6741e7724181 236 command[5] = (xd >> 8) & 0xFF;
pprasad7 0:6741e7724181 237 command[6] = xd & 0xFF;
pprasad7 0:6741e7724181 238
pprasad7 0:6741e7724181 239 command[7] = (yd >> 8) & 0xFF;
pprasad7 0:6741e7724181 240 command[8] = yd & 0xFF;
pprasad7 0:6741e7724181 241
pprasad7 0:6741e7724181 242 command[9] = (width >> 8) & 0xFF;
pprasad7 0:6741e7724181 243 command[10] = width & 0xFF;
pprasad7 0:6741e7724181 244
pprasad7 0:6741e7724181 245 command[11] = (height >> 8) & 0xFF;
pprasad7 0:6741e7724181 246 command[12] = height & 0xFF;
pprasad7 0:6741e7724181 247
pprasad7 0:6741e7724181 248 writeCOMMAND(command, 13);
pprasad7 0:6741e7724181 249 }
pprasad7 0:6741e7724181 250
pprasad7 0:6741e7724181 251 //****************************************************************************************************
pprasad7 0:6741e7724181 252 void TFT_4DGL :: pen_size(char mode) { // set pen to SOLID or WIREFRAME
pprasad7 0:6741e7724181 253 char command[2]= "";
pprasad7 0:6741e7724181 254
pprasad7 0:6741e7724181 255 command[0] = PENSIZE;
pprasad7 0:6741e7724181 256 command[1] = mode;
pprasad7 0:6741e7724181 257
pprasad7 0:6741e7724181 258 writeCOMMAND(command, 2);
pprasad7 0:6741e7724181 259 }