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