This is a library for 4DGL screens from 4D Systems in Autralia (http://www.4dsystems.com.au). Most graphic, touch and text functions are supported

Dependents:   snake

Fork of 4DGL by Stephane ROCHON

Committer:
Kerpower
Date:
Tue Sep 07 12:07:29 2010 +0000
Revision:
0:1102d253f057
Child:
7:38db58dc6f4d

        

Who changed what in which revision?

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