With added colors.

Dependents:   Arcade_RedX

Fork of 4DGL-uLCD-SE by jim hamblen

Committer:
4180_1
Date:
Mon Nov 11 01:22:41 2013 +0000
Revision:
1:8b656995301f
Child:
3:9ba47197d94f
ver 1.0 uLCD144-G2 demo

Who changed what in which revision?

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