for 4180 final project

Fork of 4DGL-uLCD-SE by jim hamblen

Committer:
4180_1
Date:
Mon Nov 11 01:22:41 2013 +0000
Revision:
1:8b656995301f
Child:
2:edae99e4abe7
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 //****************************************************************************************************
4180_1 1:8b656995301f 23 void uLCD_4DGL :: set_font(char mode) // set font size
4180_1 1:8b656995301f 24 {
4180_1 1:8b656995301f 25 char command[3]= "";
4180_1 1:8b656995301f 26
4180_1 1:8b656995301f 27 int w, h, fx = 8, fy = 8;
4180_1 1:8b656995301f 28
4180_1 1:8b656995301f 29 command[0] = SETFONT;
4180_1 1:8b656995301f 30 command[1] = 0;
4180_1 1:8b656995301f 31 command[2] = mode;
4180_1 1:8b656995301f 32
4180_1 1:8b656995301f 33 current_font = mode;
4180_1 1:8b656995301f 34
4180_1 1:8b656995301f 35 if (current_orientation == IS_PORTRAIT) {
4180_1 1:8b656995301f 36 w = SIZE_X;
4180_1 1:8b656995301f 37 h = SIZE_Y;
4180_1 1:8b656995301f 38 } else {
4180_1 1:8b656995301f 39 w = SIZE_Y;
4180_1 1:8b656995301f 40 h = SIZE_X;
4180_1 1:8b656995301f 41 }
4180_1 1:8b656995301f 42
4180_1 1:8b656995301f 43 switch (mode) {
4180_1 1:8b656995301f 44 case FONT_5X7 :
4180_1 1:8b656995301f 45 fx = 6;
4180_1 1:8b656995301f 46 fy = 8;
4180_1 1:8b656995301f 47 break;
4180_1 1:8b656995301f 48 case FONT_8X8 :
4180_1 1:8b656995301f 49 fx = 8;
4180_1 1:8b656995301f 50 fy = 8;
4180_1 1:8b656995301f 51 break;
4180_1 1:8b656995301f 52 case FONT_8X12 :
4180_1 1:8b656995301f 53 fx = 8;
4180_1 1:8b656995301f 54 fy = 12;
4180_1 1:8b656995301f 55 break;
4180_1 1:8b656995301f 56 case FONT_12X16 :
4180_1 1:8b656995301f 57 fx = 12;
4180_1 1:8b656995301f 58 fy = 16;
4180_1 1:8b656995301f 59 break;
4180_1 1:8b656995301f 60 }
4180_1 1:8b656995301f 61
4180_1 1:8b656995301f 62 max_col = w / fx;
4180_1 1:8b656995301f 63 max_row = h / fy;
4180_1 1:8b656995301f 64
4180_1 1:8b656995301f 65 writeCOMMAND(command, 3);
4180_1 1:8b656995301f 66 }
4180_1 1:8b656995301f 67
4180_1 1:8b656995301f 68 //****************************************************************************************************
4180_1 1:8b656995301f 69 void uLCD_4DGL :: text_mode(char mode) // set text mode
4180_1 1:8b656995301f 70 {
4180_1 1:8b656995301f 71 char command[3]= "";
4180_1 1:8b656995301f 72
4180_1 1:8b656995301f 73 command[0] = TEXTMODE;
4180_1 1:8b656995301f 74 command[1] = 0;
4180_1 1:8b656995301f 75 command[2] = mode;
4180_1 1:8b656995301f 76
4180_1 1:8b656995301f 77 writeCOMMAND(command, 3);
4180_1 1:8b656995301f 78 }
4180_1 1:8b656995301f 79
4180_1 1:8b656995301f 80 //****************************************************************************************************
4180_1 1:8b656995301f 81 void uLCD_4DGL :: text_char(char c, char col, char row, int color) // draw a text char
4180_1 1:8b656995301f 82 {
4180_1 1:8b656995301f 83 char command[6]= "";
4180_1 1:8b656995301f 84 command[0] = 0xE4; //move cursor
4180_1 1:8b656995301f 85 command[1] = 0;
4180_1 1:8b656995301f 86 command[2] = row;
4180_1 1:8b656995301f 87 command[3] = 0;
4180_1 1:8b656995301f 88 command[4] = col;
4180_1 1:8b656995301f 89 writeCOMMAND(command, 5);
4180_1 1:8b656995301f 90
4180_1 1:8b656995301f 91 command[0] = 0x7F; //set color
4180_1 1:8b656995301f 92
4180_1 1:8b656995301f 93 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
4180_1 1:8b656995301f 94 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
4180_1 1:8b656995301f 95 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
4180_1 1:8b656995301f 96
4180_1 1:8b656995301f 97 command[1] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
4180_1 1:8b656995301f 98 command[2] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
4180_1 1:8b656995301f 99 writeCOMMAND(command, 3);
4180_1 1:8b656995301f 100
4180_1 1:8b656995301f 101 command[0] = TEXTCHAR; //print char
4180_1 1:8b656995301f 102 command[1] = 0;
4180_1 1:8b656995301f 103 command[2] = c;
4180_1 1:8b656995301f 104 writeCOMMAND(command, 3);
4180_1 1:8b656995301f 105
4180_1 1:8b656995301f 106 }
4180_1 1:8b656995301f 107
4180_1 1:8b656995301f 108 //****************************************************************************************************
4180_1 1:8b656995301f 109 void uLCD_4DGL :: graphic_char(char c, int x, int y, int color, char width, char height) // draw a graphic char
4180_1 1:8b656995301f 110 {
4180_1 1:8b656995301f 111 char command[10]= "";
4180_1 1:8b656995301f 112
4180_1 1:8b656995301f 113 command[0] = GRAPHCHAR;
4180_1 1:8b656995301f 114
4180_1 1:8b656995301f 115 command[1] = c;
4180_1 1:8b656995301f 116
4180_1 1:8b656995301f 117 command[2] = (x >> 8) & 0xFF;
4180_1 1:8b656995301f 118 command[3] = x & 0xFF;
4180_1 1:8b656995301f 119
4180_1 1:8b656995301f 120 command[4] = (y >> 8) & 0xFF;
4180_1 1:8b656995301f 121 command[5] = y & 0xFF;
4180_1 1:8b656995301f 122
4180_1 1:8b656995301f 123 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
4180_1 1:8b656995301f 124 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
4180_1 1:8b656995301f 125 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
4180_1 1:8b656995301f 126
4180_1 1:8b656995301f 127 command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
4180_1 1:8b656995301f 128 command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
4180_1 1:8b656995301f 129
4180_1 1:8b656995301f 130 command[8] = width;
4180_1 1:8b656995301f 131
4180_1 1:8b656995301f 132 command[9] = height;
4180_1 1:8b656995301f 133
4180_1 1:8b656995301f 134 writeCOMMAND(command, 10);
4180_1 1:8b656995301f 135 }
4180_1 1:8b656995301f 136
4180_1 1:8b656995301f 137 //****************************************************************************************************
4180_1 1:8b656995301f 138 void uLCD_4DGL :: text_string(char *s, char col, char row, char font, int color) // draw a text string
4180_1 1:8b656995301f 139 {
4180_1 1:8b656995301f 140
4180_1 1:8b656995301f 141 char command[1000]= "";
4180_1 1:8b656995301f 142 int size = strlen(s);
4180_1 1:8b656995301f 143 int i = 0;
4180_1 1:8b656995301f 144
4180_1 1:8b656995301f 145 set_font(font);
4180_1 1:8b656995301f 146
4180_1 1:8b656995301f 147 command[0] = 0xE4; //move cursor
4180_1 1:8b656995301f 148 command[1] = 0;
4180_1 1:8b656995301f 149 command[2] = row;
4180_1 1:8b656995301f 150 command[3] = 0;
4180_1 1:8b656995301f 151 command[4] = col;
4180_1 1:8b656995301f 152 writeCOMMAND(command, 5);
4180_1 1:8b656995301f 153
4180_1 1:8b656995301f 154 command[0] = 0x7F; //set color
4180_1 1:8b656995301f 155 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
4180_1 1:8b656995301f 156 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
4180_1 1:8b656995301f 157 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
4180_1 1:8b656995301f 158
4180_1 1:8b656995301f 159 command[1] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
4180_1 1:8b656995301f 160 command[2] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
4180_1 1:8b656995301f 161 writeCOMMAND(command, 3);
4180_1 1:8b656995301f 162
4180_1 1:8b656995301f 163 command[0] = TEXTSTRING;
4180_1 1:8b656995301f 164 for (i=0; i<size; i++) command[1+i] = s[i];
4180_1 1:8b656995301f 165 command[1+size] = 0;
4180_1 1:8b656995301f 166 writeCOMMANDnull(command, 2 + size);
4180_1 1:8b656995301f 167 }
4180_1 1:8b656995301f 168
4180_1 1:8b656995301f 169 //****************************************************************************************************
4180_1 1:8b656995301f 170 void uLCD_4DGL :: graphic_string(char *s, int x, int y, char font, int color, char width, char height) // draw a text string
4180_1 1:8b656995301f 171 {
4180_1 1:8b656995301f 172
4180_1 1:8b656995301f 173 char command[1000]= "";
4180_1 1:8b656995301f 174 int size = strlen(s);
4180_1 1:8b656995301f 175 int i = 0;
4180_1 1:8b656995301f 176
4180_1 1:8b656995301f 177 command[0] = GRAPHSTRING;
4180_1 1:8b656995301f 178
4180_1 1:8b656995301f 179 command[1] = (x >> 8) & 0xFF;
4180_1 1:8b656995301f 180 command[2] = x & 0xFF;
4180_1 1:8b656995301f 181
4180_1 1:8b656995301f 182 command[3] = (y >> 8) & 0xFF;
4180_1 1:8b656995301f 183 command[4] = y & 0xFF;
4180_1 1:8b656995301f 184
4180_1 1:8b656995301f 185 command[5] = font;
4180_1 1:8b656995301f 186
4180_1 1:8b656995301f 187 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
4180_1 1:8b656995301f 188 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
4180_1 1:8b656995301f 189 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
4180_1 1:8b656995301f 190
4180_1 1:8b656995301f 191 command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
4180_1 1:8b656995301f 192 command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
4180_1 1:8b656995301f 193
4180_1 1:8b656995301f 194 command[8] = width;
4180_1 1:8b656995301f 195
4180_1 1:8b656995301f 196 command[9] = height;
4180_1 1:8b656995301f 197
4180_1 1:8b656995301f 198 for (i=0; i<size; i++) command[10+i] = s[i];
4180_1 1:8b656995301f 199
4180_1 1:8b656995301f 200 command[10+size] = 0;
4180_1 1:8b656995301f 201
4180_1 1:8b656995301f 202 writeCOMMAND(command, 11 + size);
4180_1 1:8b656995301f 203 }
4180_1 1:8b656995301f 204
4180_1 1:8b656995301f 205 //****************************************************************************************************
4180_1 1:8b656995301f 206 void uLCD_4DGL :: text_button(char *s, char mode, int x, int y, int button_color, char font, int text_color, char width, char height) // draw a text string
4180_1 1:8b656995301f 207 {
4180_1 1:8b656995301f 208
4180_1 1:8b656995301f 209 char command[1000]= "";
4180_1 1:8b656995301f 210 int size = strlen(s);
4180_1 1:8b656995301f 211 int i = 0, red5, green6, blue5;
4180_1 1:8b656995301f 212
4180_1 1:8b656995301f 213 command[0] = TEXTBUTTON;
4180_1 1:8b656995301f 214
4180_1 1:8b656995301f 215 command[1] = mode;
4180_1 1:8b656995301f 216
4180_1 1:8b656995301f 217 command[2] = (x >> 8) & 0xFF;
4180_1 1:8b656995301f 218 command[3] = x & 0xFF;
4180_1 1:8b656995301f 219
4180_1 1:8b656995301f 220 command[4] = (y >> 8) & 0xFF;
4180_1 1:8b656995301f 221 command[5] = y & 0xFF;
4180_1 1:8b656995301f 222
4180_1 1:8b656995301f 223 red5 = (button_color >> (16 + 3)) & 0x1F; // get red on 5 bits
4180_1 1:8b656995301f 224 green6 = (button_color >> (8 + 2)) & 0x3F; // get green on 6 bits
4180_1 1:8b656995301f 225 blue5 = (button_color >> (0 + 3)) & 0x1F; // get blue on 5 bits
4180_1 1:8b656995301f 226
4180_1 1:8b656995301f 227 command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
4180_1 1:8b656995301f 228 command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
4180_1 1:8b656995301f 229
4180_1 1:8b656995301f 230 command[8] = font;
4180_1 1:8b656995301f 231
4180_1 1:8b656995301f 232 red5 = (text_color >> (16 + 3)) & 0x1F; // get red on 5 bits
4180_1 1:8b656995301f 233 green6 = (text_color >> (8 + 2)) & 0x3F; // get green on 6 bits
4180_1 1:8b656995301f 234 blue5 = (text_color >> (0 + 3)) & 0x1F; // get blue on 5 bits
4180_1 1:8b656995301f 235
4180_1 1:8b656995301f 236 command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
4180_1 1:8b656995301f 237 command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
4180_1 1:8b656995301f 238
4180_1 1:8b656995301f 239 command[11] = width;
4180_1 1:8b656995301f 240
4180_1 1:8b656995301f 241 command[12] = height;
4180_1 1:8b656995301f 242
4180_1 1:8b656995301f 243 for (i=0; i<size; i++) command[13+i] = s[i];
4180_1 1:8b656995301f 244
4180_1 1:8b656995301f 245 command[13+size] = 0;
4180_1 1:8b656995301f 246
4180_1 1:8b656995301f 247 writeCOMMAND(command, 14 + size);
4180_1 1:8b656995301f 248 }
4180_1 1:8b656995301f 249
4180_1 1:8b656995301f 250 //****************************************************************************************************
4180_1 1:8b656995301f 251 void uLCD_4DGL :: locate(char col, char row) // place text curssor at col, row
4180_1 1:8b656995301f 252 {
4180_1 1:8b656995301f 253 current_col = col;
4180_1 1:8b656995301f 254 current_row = row;
4180_1 1:8b656995301f 255 }
4180_1 1:8b656995301f 256
4180_1 1:8b656995301f 257 //****************************************************************************************************
4180_1 1:8b656995301f 258 void uLCD_4DGL :: color(int color) // set text color
4180_1 1:8b656995301f 259 {
4180_1 1:8b656995301f 260 current_color = color;
4180_1 1:8b656995301f 261 }
4180_1 1:8b656995301f 262
4180_1 1:8b656995301f 263 //****************************************************************************************************
4180_1 1:8b656995301f 264 void uLCD_4DGL :: putc(char c) // place char at current cursor position
4180_1 1:8b656995301f 265 {
4180_1 1:8b656995301f 266
4180_1 1:8b656995301f 267 text_char(c, current_col++, current_row, current_color);
4180_1 1:8b656995301f 268
4180_1 1:8b656995301f 269 if (current_col == max_col) {
4180_1 1:8b656995301f 270 current_col = 0;
4180_1 1:8b656995301f 271 current_row++;
4180_1 1:8b656995301f 272 }
4180_1 1:8b656995301f 273 if (current_row == max_row) {
4180_1 1:8b656995301f 274 current_row = 0;
4180_1 1:8b656995301f 275 }
4180_1 1:8b656995301f 276 }
4180_1 1:8b656995301f 277
4180_1 1:8b656995301f 278 //****************************************************************************************************
4180_1 1:8b656995301f 279 void uLCD_4DGL :: puts(char *s) // place string at current cursor position
4180_1 1:8b656995301f 280 {
4180_1 1:8b656995301f 281
4180_1 1:8b656995301f 282 text_string(s, current_col, current_row, current_font, current_color);
4180_1 1:8b656995301f 283
4180_1 1:8b656995301f 284 current_col += strlen(s);
4180_1 1:8b656995301f 285
4180_1 1:8b656995301f 286 if (current_col >= max_col) {
4180_1 1:8b656995301f 287 current_row += current_col / max_col;
4180_1 1:8b656995301f 288 current_col %= max_col;
4180_1 1:8b656995301f 289 }
4180_1 1:8b656995301f 290 if (current_row >= max_row) {
4180_1 1:8b656995301f 291 current_row %= max_row;
4180_1 1:8b656995301f 292 }
4180_1 1:8b656995301f 293 }