n-Angle graphic demo - modified for 4DGL - original code Eric Wieser - THANKS

Dependencies:   mbed

Committer:
JLS
Date:
Sun Jan 23 19:37:03 2011 +0000
Revision:
0:f2f097ec3172

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JLS 0:f2f097ec3172 1 //
JLS 0:f2f097ec3172 2 // TFT_4DGL is a class to drive 4D Systems TFT touch screens
JLS 0:f2f097ec3172 3 //
JLS 0:f2f097ec3172 4 // Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr>
JLS 0:f2f097ec3172 5 //
JLS 0:f2f097ec3172 6 // TFT_4DGL is free software: you can redistribute it and/or modify
JLS 0:f2f097ec3172 7 // it under the terms of the GNU General Public License as published by
JLS 0:f2f097ec3172 8 // the Free Software Foundation, either version 3 of the License, or
JLS 0:f2f097ec3172 9 // (at your option) any later version.
JLS 0:f2f097ec3172 10 //
JLS 0:f2f097ec3172 11 // TFT_4DGL is distributed in the hope that it will be useful,
JLS 0:f2f097ec3172 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
JLS 0:f2f097ec3172 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
JLS 0:f2f097ec3172 14 // GNU General Public License for more details.
JLS 0:f2f097ec3172 15 //
JLS 0:f2f097ec3172 16 // You should have received a copy of the GNU General Public License
JLS 0:f2f097ec3172 17 // along with TFT_4DGL. If not, see <http://www.gnu.org/licenses/>.
JLS 0:f2f097ec3172 18
JLS 0:f2f097ec3172 19 #include "mbed.h"
JLS 0:f2f097ec3172 20 #include "TFT_4DGL.h"
JLS 0:f2f097ec3172 21
JLS 0:f2f097ec3172 22 //****************************************************************************************************
JLS 0:f2f097ec3172 23 void TFT_4DGL :: set_font(char mode) { // set font size
JLS 0:f2f097ec3172 24 char command[2]= "";
JLS 0:f2f097ec3172 25
JLS 0:f2f097ec3172 26 int w, h, fx = 8, fy = 8;
JLS 0:f2f097ec3172 27
JLS 0:f2f097ec3172 28 command[0] = SETFONT;
JLS 0:f2f097ec3172 29 command[1] = mode;
JLS 0:f2f097ec3172 30
JLS 0:f2f097ec3172 31 current_font = mode;
JLS 0:f2f097ec3172 32
JLS 0:f2f097ec3172 33 if (current_orientation == IS_PORTRAIT) {
JLS 0:f2f097ec3172 34 w = SIZE_X;
JLS 0:f2f097ec3172 35 h = SIZE_Y;
JLS 0:f2f097ec3172 36 } else {
JLS 0:f2f097ec3172 37 w = SIZE_Y;
JLS 0:f2f097ec3172 38 h = SIZE_X;
JLS 0:f2f097ec3172 39 }
JLS 0:f2f097ec3172 40
JLS 0:f2f097ec3172 41 switch (mode) {
JLS 0:f2f097ec3172 42 case FONT_5X7 :
JLS 0:f2f097ec3172 43 fx = 6;
JLS 0:f2f097ec3172 44 fy = 8;
JLS 0:f2f097ec3172 45 break;
JLS 0:f2f097ec3172 46 case FONT_8X8 :
JLS 0:f2f097ec3172 47 fx = 8;
JLS 0:f2f097ec3172 48 fy = 8;
JLS 0:f2f097ec3172 49 break;
JLS 0:f2f097ec3172 50 case FONT_8X12 :
JLS 0:f2f097ec3172 51 fx = 8;
JLS 0:f2f097ec3172 52 fy = 12;
JLS 0:f2f097ec3172 53 break;
JLS 0:f2f097ec3172 54 case FONT_12X16 :
JLS 0:f2f097ec3172 55 fx = 12;
JLS 0:f2f097ec3172 56 fy = 16;
JLS 0:f2f097ec3172 57 break;
JLS 0:f2f097ec3172 58 }
JLS 0:f2f097ec3172 59
JLS 0:f2f097ec3172 60 max_col = w / fx;
JLS 0:f2f097ec3172 61 max_row = h / fy;
JLS 0:f2f097ec3172 62
JLS 0:f2f097ec3172 63 writeCOMMAND(command, 2);
JLS 0:f2f097ec3172 64 }
JLS 0:f2f097ec3172 65
JLS 0:f2f097ec3172 66 //****************************************************************************************************
JLS 0:f2f097ec3172 67 void TFT_4DGL :: text_mode(char mode) { // set text mode
JLS 0:f2f097ec3172 68 char command[2]= "";
JLS 0:f2f097ec3172 69
JLS 0:f2f097ec3172 70 command[0] = TEXTMODE;
JLS 0:f2f097ec3172 71 command[1] = mode;
JLS 0:f2f097ec3172 72
JLS 0:f2f097ec3172 73 writeCOMMAND(command, 2);
JLS 0:f2f097ec3172 74 }
JLS 0:f2f097ec3172 75
JLS 0:f2f097ec3172 76 //****************************************************************************************************
JLS 0:f2f097ec3172 77 void TFT_4DGL :: text_char(char c, char col, char row, int color) { // draw a text char
JLS 0:f2f097ec3172 78 char command[6]= "";
JLS 0:f2f097ec3172 79
JLS 0:f2f097ec3172 80 command[0] = TEXTCHAR;
JLS 0:f2f097ec3172 81
JLS 0:f2f097ec3172 82 command[1] = c;
JLS 0:f2f097ec3172 83 command[2] = col;
JLS 0:f2f097ec3172 84 command[3] = row;
JLS 0:f2f097ec3172 85
JLS 0:f2f097ec3172 86 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
JLS 0:f2f097ec3172 87 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
JLS 0:f2f097ec3172 88 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
JLS 0:f2f097ec3172 89
JLS 0:f2f097ec3172 90 command[4] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
JLS 0:f2f097ec3172 91 command[5] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
JLS 0:f2f097ec3172 92
JLS 0:f2f097ec3172 93 writeCOMMAND(command, 8);
JLS 0:f2f097ec3172 94 }
JLS 0:f2f097ec3172 95
JLS 0:f2f097ec3172 96 //****************************************************************************************************
JLS 0:f2f097ec3172 97 void TFT_4DGL :: graphic_char(char c, int x, int y, int color, char width, char height) { // draw a graphic char
JLS 0:f2f097ec3172 98 char command[10]= "";
JLS 0:f2f097ec3172 99
JLS 0:f2f097ec3172 100 command[0] = GRAPHCHAR;
JLS 0:f2f097ec3172 101
JLS 0:f2f097ec3172 102 command[1] = c;
JLS 0:f2f097ec3172 103
JLS 0:f2f097ec3172 104 command[2] = (x >> 8) & 0xFF;
JLS 0:f2f097ec3172 105 command[3] = x & 0xFF;
JLS 0:f2f097ec3172 106
JLS 0:f2f097ec3172 107 command[4] = (y >> 8) & 0xFF;
JLS 0:f2f097ec3172 108 command[5] = y & 0xFF;
JLS 0:f2f097ec3172 109
JLS 0:f2f097ec3172 110 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
JLS 0:f2f097ec3172 111 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
JLS 0:f2f097ec3172 112 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
JLS 0:f2f097ec3172 113
JLS 0:f2f097ec3172 114 command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
JLS 0:f2f097ec3172 115 command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
JLS 0:f2f097ec3172 116
JLS 0:f2f097ec3172 117 command[8] = width;
JLS 0:f2f097ec3172 118
JLS 0:f2f097ec3172 119 command[9] = height;
JLS 0:f2f097ec3172 120
JLS 0:f2f097ec3172 121 writeCOMMAND(command, 10);
JLS 0:f2f097ec3172 122 }
JLS 0:f2f097ec3172 123
JLS 0:f2f097ec3172 124 //****************************************************************************************************
JLS 0:f2f097ec3172 125 void TFT_4DGL :: text_string(char *s, char col, char row, char font, int color) { // draw a text string
JLS 0:f2f097ec3172 126
JLS 0:f2f097ec3172 127 char command[1000]= "";
JLS 0:f2f097ec3172 128 int size = strlen(s);
JLS 0:f2f097ec3172 129 int i = 0;
JLS 0:f2f097ec3172 130
JLS 0:f2f097ec3172 131 command[0] = TEXTSTRING;
JLS 0:f2f097ec3172 132
JLS 0:f2f097ec3172 133 command[1] = col;
JLS 0:f2f097ec3172 134 command[2] = row;
JLS 0:f2f097ec3172 135
JLS 0:f2f097ec3172 136 command[3] = font;
JLS 0:f2f097ec3172 137
JLS 0:f2f097ec3172 138 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
JLS 0:f2f097ec3172 139 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
JLS 0:f2f097ec3172 140 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
JLS 0:f2f097ec3172 141
JLS 0:f2f097ec3172 142 command[4] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
JLS 0:f2f097ec3172 143 command[5] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
JLS 0:f2f097ec3172 144
JLS 0:f2f097ec3172 145 for (i=0; i<size; i++) command[6+i] = s[i];
JLS 0:f2f097ec3172 146
JLS 0:f2f097ec3172 147 command[6+size] = 0;
JLS 0:f2f097ec3172 148
JLS 0:f2f097ec3172 149 writeCOMMAND(command, 7 + size);
JLS 0:f2f097ec3172 150 }
JLS 0:f2f097ec3172 151
JLS 0:f2f097ec3172 152 //****************************************************************************************************
JLS 0:f2f097ec3172 153 void TFT_4DGL :: graphic_string(char *s, int x, int y, char font, int color, char width, char height) { // draw a text string
JLS 0:f2f097ec3172 154
JLS 0:f2f097ec3172 155 char command[1000]= "";
JLS 0:f2f097ec3172 156 int size = strlen(s);
JLS 0:f2f097ec3172 157 int i = 0;
JLS 0:f2f097ec3172 158
JLS 0:f2f097ec3172 159 command[0] = GRAPHSTRING;
JLS 0:f2f097ec3172 160
JLS 0:f2f097ec3172 161 command[1] = (x >> 8) & 0xFF;
JLS 0:f2f097ec3172 162 command[2] = x & 0xFF;
JLS 0:f2f097ec3172 163
JLS 0:f2f097ec3172 164 command[3] = (y >> 8) & 0xFF;
JLS 0:f2f097ec3172 165 command[4] = y & 0xFF;
JLS 0:f2f097ec3172 166
JLS 0:f2f097ec3172 167 command[5] = font;
JLS 0:f2f097ec3172 168
JLS 0:f2f097ec3172 169 int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits
JLS 0:f2f097ec3172 170 int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits
JLS 0:f2f097ec3172 171 int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits
JLS 0:f2f097ec3172 172
JLS 0:f2f097ec3172 173 command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
JLS 0:f2f097ec3172 174 command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
JLS 0:f2f097ec3172 175
JLS 0:f2f097ec3172 176 command[8] = width;
JLS 0:f2f097ec3172 177
JLS 0:f2f097ec3172 178 command[9] = height;
JLS 0:f2f097ec3172 179
JLS 0:f2f097ec3172 180 for (i=0; i<size; i++) command[10+i] = s[i];
JLS 0:f2f097ec3172 181
JLS 0:f2f097ec3172 182 command[10+size] = 0;
JLS 0:f2f097ec3172 183
JLS 0:f2f097ec3172 184 writeCOMMAND(command, 11 + size);
JLS 0:f2f097ec3172 185 }
JLS 0:f2f097ec3172 186
JLS 0:f2f097ec3172 187 //****************************************************************************************************
JLS 0:f2f097ec3172 188 void TFT_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
JLS 0:f2f097ec3172 189
JLS 0:f2f097ec3172 190 char command[1000]= "";
JLS 0:f2f097ec3172 191 int size = strlen(s);
JLS 0:f2f097ec3172 192 int i = 0, red5, green6, blue5;
JLS 0:f2f097ec3172 193
JLS 0:f2f097ec3172 194 command[0] = TEXTBUTTON;
JLS 0:f2f097ec3172 195
JLS 0:f2f097ec3172 196 command[1] = mode;
JLS 0:f2f097ec3172 197
JLS 0:f2f097ec3172 198 command[2] = (x >> 8) & 0xFF;
JLS 0:f2f097ec3172 199 command[3] = x & 0xFF;
JLS 0:f2f097ec3172 200
JLS 0:f2f097ec3172 201 command[4] = (y >> 8) & 0xFF;
JLS 0:f2f097ec3172 202 command[5] = y & 0xFF;
JLS 0:f2f097ec3172 203
JLS 0:f2f097ec3172 204 red5 = (button_color >> (16 + 3)) & 0x1F; // get red on 5 bits
JLS 0:f2f097ec3172 205 green6 = (button_color >> (8 + 2)) & 0x3F; // get green on 6 bits
JLS 0:f2f097ec3172 206 blue5 = (button_color >> (0 + 3)) & 0x1F; // get blue on 5 bits
JLS 0:f2f097ec3172 207
JLS 0:f2f097ec3172 208 command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
JLS 0:f2f097ec3172 209 command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
JLS 0:f2f097ec3172 210
JLS 0:f2f097ec3172 211 command[8] = font;
JLS 0:f2f097ec3172 212
JLS 0:f2f097ec3172 213 red5 = (text_color >> (16 + 3)) & 0x1F; // get red on 5 bits
JLS 0:f2f097ec3172 214 green6 = (text_color >> (8 + 2)) & 0x3F; // get green on 6 bits
JLS 0:f2f097ec3172 215 blue5 = (text_color >> (0 + 3)) & 0x1F; // get blue on 5 bits
JLS 0:f2f097ec3172 216
JLS 0:f2f097ec3172 217 command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color
JLS 0:f2f097ec3172 218 command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color
JLS 0:f2f097ec3172 219
JLS 0:f2f097ec3172 220 command[11] = width;
JLS 0:f2f097ec3172 221
JLS 0:f2f097ec3172 222 command[12] = height;
JLS 0:f2f097ec3172 223
JLS 0:f2f097ec3172 224 for (i=0; i<size; i++) command[13+i] = s[i];
JLS 0:f2f097ec3172 225
JLS 0:f2f097ec3172 226 command[13+size] = 0;
JLS 0:f2f097ec3172 227
JLS 0:f2f097ec3172 228 writeCOMMAND(command, 14 + size);
JLS 0:f2f097ec3172 229 }
JLS 0:f2f097ec3172 230
JLS 0:f2f097ec3172 231 //****************************************************************************************************
JLS 0:f2f097ec3172 232 void TFT_4DGL :: locate(char col, char row) { // place text curssor at col, row
JLS 0:f2f097ec3172 233 current_col = col;
JLS 0:f2f097ec3172 234 current_row = row;
JLS 0:f2f097ec3172 235 }
JLS 0:f2f097ec3172 236
JLS 0:f2f097ec3172 237 //****************************************************************************************************
JLS 0:f2f097ec3172 238 void TFT_4DGL :: color(int color) { // set text color
JLS 0:f2f097ec3172 239 current_color = color;
JLS 0:f2f097ec3172 240 }
JLS 0:f2f097ec3172 241
JLS 0:f2f097ec3172 242 //****************************************************************************************************
JLS 0:f2f097ec3172 243 void TFT_4DGL :: putc(char c) { // place char at current cursor position
JLS 0:f2f097ec3172 244
JLS 0:f2f097ec3172 245 text_char(c, current_col++, current_row, current_color);
JLS 0:f2f097ec3172 246
JLS 0:f2f097ec3172 247 if (current_col == max_col) {
JLS 0:f2f097ec3172 248 current_col = 0;
JLS 0:f2f097ec3172 249 current_row++;
JLS 0:f2f097ec3172 250 }
JLS 0:f2f097ec3172 251 if (current_row == max_row) {
JLS 0:f2f097ec3172 252 current_row = 0;
JLS 0:f2f097ec3172 253 }
JLS 0:f2f097ec3172 254 }
JLS 0:f2f097ec3172 255
JLS 0:f2f097ec3172 256 //****************************************************************************************************
JLS 0:f2f097ec3172 257 void TFT_4DGL :: puts(char *s) { // place string at current cursor position
JLS 0:f2f097ec3172 258
JLS 0:f2f097ec3172 259 text_string(s, current_col, current_row, current_font, current_color);
JLS 0:f2f097ec3172 260
JLS 0:f2f097ec3172 261 current_col += strlen(s);
JLS 0:f2f097ec3172 262
JLS 0:f2f097ec3172 263 if (current_col >= max_col) {
JLS 0:f2f097ec3172 264 current_row += current_col / max_col;
JLS 0:f2f097ec3172 265 current_col %= max_col;
JLS 0:f2f097ec3172 266 }
JLS 0:f2f097ec3172 267 if (current_row >= max_row) {
JLS 0:f2f097ec3172 268 current_row %= max_row;
JLS 0:f2f097ec3172 269 }
JLS 0:f2f097ec3172 270 }