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