uVGA III library

Dependents:   CDUFinal CDU_Mbed_21 CDU_Mbed_26 CDU_Mbed_30

Fork of 4DGL by Adam Green

Committer:
AdamGreen
Date:
Tue Jul 05 11:45:02 2011 +0000
Revision:
0:d6e186427f3e
Child:
1:ebc67b51c225
Fixed a few minor issues in 4DGL library

Moved pc serial port object used for debug output into 4DGL object itself. Previously it was a global object but its constructor wasnt getting run before the global constructor for the 4DGL object itself which led to an assert in the serial code.

I also fixed a few potential buffer overruns that I saw in the code as well.

Who changed what in which revision?

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