uVGA III library
Dependents: CDUFinal CDU_Mbed_21 CDU_Mbed_26 CDU_Mbed_30
Fork of 4DGL by
TFT_4DGL_Text.cpp@3:f34ce48346bc, 2014-07-05 (annotated)
- Committer:
- WillemBraat
- Date:
- Sat Jul 05 16:31:43 2014 +0000
- Revision:
- 3:f34ce48346bc
- Parent:
- 1:ebc67b51c225
Font references have changed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AdamGreen | 0:d6e186427f3e | 1 | // |
AdamGreen | 0:d6e186427f3e | 2 | // TFT_4DGL is a class to drive 4D Systems TFT touch screens |
AdamGreen | 0:d6e186427f3e | 3 | // |
AdamGreen | 0:d6e186427f3e | 4 | |
AdamGreen | 0:d6e186427f3e | 5 | #include "mbed.h" |
AdamGreen | 0:d6e186427f3e | 6 | #include "TFT_4DGL.h" |
AdamGreen | 0:d6e186427f3e | 7 | |
AdamGreen | 0:d6e186427f3e | 8 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 9 | void TFT_4DGL :: set_font(char mode) { // set font size |
AdamGreen | 0:d6e186427f3e | 10 | char command[2]= ""; |
AdamGreen | 0:d6e186427f3e | 11 | |
AdamGreen | 0:d6e186427f3e | 12 | int w, h, fx = 8, fy = 8; |
AdamGreen | 0:d6e186427f3e | 13 | |
AdamGreen | 0:d6e186427f3e | 14 | command[0] = SETFONT; |
AdamGreen | 0:d6e186427f3e | 15 | command[1] = mode; |
AdamGreen | 0:d6e186427f3e | 16 | |
AdamGreen | 0:d6e186427f3e | 17 | current_font = mode; |
AdamGreen | 0:d6e186427f3e | 18 | |
AdamGreen | 0:d6e186427f3e | 19 | if (current_orientation == IS_PORTRAIT) { |
AdamGreen | 0:d6e186427f3e | 20 | w = SIZE_X; |
AdamGreen | 0:d6e186427f3e | 21 | h = SIZE_Y; |
AdamGreen | 0:d6e186427f3e | 22 | } else { |
AdamGreen | 0:d6e186427f3e | 23 | w = SIZE_Y; |
AdamGreen | 0:d6e186427f3e | 24 | h = SIZE_X; |
AdamGreen | 0:d6e186427f3e | 25 | } |
AdamGreen | 0:d6e186427f3e | 26 | |
AdamGreen | 0:d6e186427f3e | 27 | switch (mode) { |
WillemBraat | 3:f34ce48346bc | 28 | case FONT_12X34: |
WillemBraat | 3:f34ce48346bc | 29 | fx = 12; |
WillemBraat | 3:f34ce48346bc | 30 | fy = 34; |
AdamGreen | 0:d6e186427f3e | 31 | break; |
WillemBraat | 1:ebc67b51c225 | 32 | case FONT_24X34 : |
WillemBraat | 1:ebc67b51c225 | 33 | fx = 24; //was 24 |
WillemBraat | 1:ebc67b51c225 | 34 | fy = 34; |
WillemBraat | 1:ebc67b51c225 | 35 | break; |
WillemBraat | 1:ebc67b51c225 | 36 | |
AdamGreen | 0:d6e186427f3e | 37 | } |
AdamGreen | 0:d6e186427f3e | 38 | |
AdamGreen | 0:d6e186427f3e | 39 | max_col = w / fx; |
AdamGreen | 0:d6e186427f3e | 40 | max_row = h / fy; |
AdamGreen | 0:d6e186427f3e | 41 | |
AdamGreen | 0:d6e186427f3e | 42 | writeCOMMAND(command, 2); |
AdamGreen | 0:d6e186427f3e | 43 | } |
AdamGreen | 0:d6e186427f3e | 44 | |
AdamGreen | 0:d6e186427f3e | 45 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 46 | void TFT_4DGL :: text_mode(char mode) { // set text mode |
AdamGreen | 0:d6e186427f3e | 47 | char command[2]= ""; |
AdamGreen | 0:d6e186427f3e | 48 | |
AdamGreen | 0:d6e186427f3e | 49 | command[0] = TEXTMODE; |
AdamGreen | 0:d6e186427f3e | 50 | command[1] = mode; |
AdamGreen | 0:d6e186427f3e | 51 | |
AdamGreen | 0:d6e186427f3e | 52 | writeCOMMAND(command, 2); |
AdamGreen | 0:d6e186427f3e | 53 | } |
AdamGreen | 0:d6e186427f3e | 54 | |
AdamGreen | 0:d6e186427f3e | 55 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 56 | void TFT_4DGL :: text_char(char c, char col, char row, int color) { // draw a text char |
AdamGreen | 0:d6e186427f3e | 57 | char command[6]= ""; |
AdamGreen | 0:d6e186427f3e | 58 | |
AdamGreen | 0:d6e186427f3e | 59 | command[0] = TEXTCHAR; |
AdamGreen | 0:d6e186427f3e | 60 | |
AdamGreen | 0:d6e186427f3e | 61 | command[1] = c; |
AdamGreen | 0:d6e186427f3e | 62 | command[2] = col; |
AdamGreen | 0:d6e186427f3e | 63 | command[3] = row; |
AdamGreen | 0:d6e186427f3e | 64 | |
AdamGreen | 0:d6e186427f3e | 65 | int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits |
AdamGreen | 0:d6e186427f3e | 66 | int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits |
AdamGreen | 0:d6e186427f3e | 67 | int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits |
AdamGreen | 0:d6e186427f3e | 68 | |
AdamGreen | 0:d6e186427f3e | 69 | command[4] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 70 | command[5] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 71 | |
AdamGreen | 0:d6e186427f3e | 72 | writeCOMMAND(command, 8); |
AdamGreen | 0:d6e186427f3e | 73 | } |
AdamGreen | 0:d6e186427f3e | 74 | |
AdamGreen | 0:d6e186427f3e | 75 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 76 | void TFT_4DGL :: graphic_char(char c, int x, int y, int color, char width, char height) { // draw a graphic char |
AdamGreen | 0:d6e186427f3e | 77 | char command[10]= ""; |
AdamGreen | 0:d6e186427f3e | 78 | |
AdamGreen | 0:d6e186427f3e | 79 | command[0] = GRAPHCHAR; |
AdamGreen | 0:d6e186427f3e | 80 | |
AdamGreen | 0:d6e186427f3e | 81 | command[1] = c; |
AdamGreen | 0:d6e186427f3e | 82 | |
AdamGreen | 0:d6e186427f3e | 83 | command[2] = (x >> 8) & 0xFF; |
AdamGreen | 0:d6e186427f3e | 84 | command[3] = x & 0xFF; |
AdamGreen | 0:d6e186427f3e | 85 | |
AdamGreen | 0:d6e186427f3e | 86 | command[4] = (y >> 8) & 0xFF; |
AdamGreen | 0:d6e186427f3e | 87 | command[5] = y & 0xFF; |
AdamGreen | 0:d6e186427f3e | 88 | |
AdamGreen | 0:d6e186427f3e | 89 | int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits |
AdamGreen | 0:d6e186427f3e | 90 | int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits |
AdamGreen | 0:d6e186427f3e | 91 | int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits |
AdamGreen | 0:d6e186427f3e | 92 | |
AdamGreen | 0:d6e186427f3e | 93 | command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 94 | command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 95 | |
AdamGreen | 0:d6e186427f3e | 96 | command[8] = width; |
AdamGreen | 0:d6e186427f3e | 97 | |
AdamGreen | 0:d6e186427f3e | 98 | command[9] = height; |
AdamGreen | 0:d6e186427f3e | 99 | |
AdamGreen | 0:d6e186427f3e | 100 | writeCOMMAND(command, 10); |
AdamGreen | 0:d6e186427f3e | 101 | } |
AdamGreen | 0:d6e186427f3e | 102 | |
AdamGreen | 0:d6e186427f3e | 103 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 104 | void TFT_4DGL :: text_string(char *s, char col, char row, char font, int color) { // draw a text string |
AdamGreen | 0:d6e186427f3e | 105 | |
AdamGreen | 0:d6e186427f3e | 106 | char command[1000]= ""; |
AdamGreen | 0:d6e186427f3e | 107 | int size = strlen(s); |
AdamGreen | 0:d6e186427f3e | 108 | int i = 0; |
AdamGreen | 0:d6e186427f3e | 109 | |
AdamGreen | 0:d6e186427f3e | 110 | command[0] = TEXTSTRING; |
AdamGreen | 0:d6e186427f3e | 111 | |
AdamGreen | 0:d6e186427f3e | 112 | command[1] = col; |
AdamGreen | 0:d6e186427f3e | 113 | command[2] = row; |
AdamGreen | 0:d6e186427f3e | 114 | |
AdamGreen | 0:d6e186427f3e | 115 | command[3] = font; |
AdamGreen | 0:d6e186427f3e | 116 | |
AdamGreen | 0:d6e186427f3e | 117 | int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits |
AdamGreen | 0:d6e186427f3e | 118 | int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits |
AdamGreen | 0:d6e186427f3e | 119 | int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits |
AdamGreen | 0:d6e186427f3e | 120 | |
AdamGreen | 0:d6e186427f3e | 121 | command[4] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 122 | command[5] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 123 | |
AdamGreen | 0:d6e186427f3e | 124 | for (i=0; i<size; i++) command[6+i] = s[i]; |
AdamGreen | 0:d6e186427f3e | 125 | |
AdamGreen | 0:d6e186427f3e | 126 | command[6+size] = 0; |
AdamGreen | 0:d6e186427f3e | 127 | |
AdamGreen | 0:d6e186427f3e | 128 | writeCOMMAND(command, 7 + size); |
AdamGreen | 0:d6e186427f3e | 129 | } |
AdamGreen | 0:d6e186427f3e | 130 | |
AdamGreen | 0:d6e186427f3e | 131 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 132 | void TFT_4DGL :: graphic_string(char *s, int x, int y, char font, int color, char width, char height) { // draw a text string |
AdamGreen | 0:d6e186427f3e | 133 | |
AdamGreen | 0:d6e186427f3e | 134 | char command[1000]= ""; |
AdamGreen | 0:d6e186427f3e | 135 | int size = strlen(s); |
AdamGreen | 0:d6e186427f3e | 136 | int i = 0; |
AdamGreen | 0:d6e186427f3e | 137 | |
AdamGreen | 0:d6e186427f3e | 138 | command[0] = GRAPHSTRING; |
AdamGreen | 0:d6e186427f3e | 139 | |
AdamGreen | 0:d6e186427f3e | 140 | command[1] = (x >> 8) & 0xFF; |
AdamGreen | 0:d6e186427f3e | 141 | command[2] = x & 0xFF; |
AdamGreen | 0:d6e186427f3e | 142 | |
AdamGreen | 0:d6e186427f3e | 143 | command[3] = (y >> 8) & 0xFF; |
AdamGreen | 0:d6e186427f3e | 144 | command[4] = y & 0xFF; |
AdamGreen | 0:d6e186427f3e | 145 | |
AdamGreen | 0:d6e186427f3e | 146 | command[5] = font; |
AdamGreen | 0:d6e186427f3e | 147 | |
AdamGreen | 0:d6e186427f3e | 148 | int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits |
AdamGreen | 0:d6e186427f3e | 149 | int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits |
AdamGreen | 0:d6e186427f3e | 150 | int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits |
AdamGreen | 0:d6e186427f3e | 151 | |
AdamGreen | 0:d6e186427f3e | 152 | command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 153 | command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 154 | |
AdamGreen | 0:d6e186427f3e | 155 | command[8] = width; |
AdamGreen | 0:d6e186427f3e | 156 | |
AdamGreen | 0:d6e186427f3e | 157 | command[9] = height; |
AdamGreen | 0:d6e186427f3e | 158 | |
AdamGreen | 0:d6e186427f3e | 159 | for (i=0; i<size; i++) command[10+i] = s[i]; |
AdamGreen | 0:d6e186427f3e | 160 | |
AdamGreen | 0:d6e186427f3e | 161 | command[10+size] = 0; |
AdamGreen | 0:d6e186427f3e | 162 | |
AdamGreen | 0:d6e186427f3e | 163 | writeCOMMAND(command, 11 + size); |
AdamGreen | 0:d6e186427f3e | 164 | } |
AdamGreen | 0:d6e186427f3e | 165 | |
AdamGreen | 0:d6e186427f3e | 166 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 167 | 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 |
AdamGreen | 0:d6e186427f3e | 168 | |
AdamGreen | 0:d6e186427f3e | 169 | char command[1000]= ""; |
AdamGreen | 0:d6e186427f3e | 170 | int size = strlen(s); |
AdamGreen | 0:d6e186427f3e | 171 | int i = 0, red5, green6, blue5; |
AdamGreen | 0:d6e186427f3e | 172 | |
AdamGreen | 0:d6e186427f3e | 173 | command[0] = TEXTBUTTON; |
AdamGreen | 0:d6e186427f3e | 174 | |
AdamGreen | 0:d6e186427f3e | 175 | command[1] = mode; |
AdamGreen | 0:d6e186427f3e | 176 | |
AdamGreen | 0:d6e186427f3e | 177 | command[2] = (x >> 8) & 0xFF; |
AdamGreen | 0:d6e186427f3e | 178 | command[3] = x & 0xFF; |
AdamGreen | 0:d6e186427f3e | 179 | |
AdamGreen | 0:d6e186427f3e | 180 | command[4] = (y >> 8) & 0xFF; |
AdamGreen | 0:d6e186427f3e | 181 | command[5] = y & 0xFF; |
AdamGreen | 0:d6e186427f3e | 182 | |
AdamGreen | 0:d6e186427f3e | 183 | red5 = (button_color >> (16 + 3)) & 0x1F; // get red on 5 bits |
AdamGreen | 0:d6e186427f3e | 184 | green6 = (button_color >> (8 + 2)) & 0x3F; // get green on 6 bits |
AdamGreen | 0:d6e186427f3e | 185 | blue5 = (button_color >> (0 + 3)) & 0x1F; // get blue on 5 bits |
AdamGreen | 0:d6e186427f3e | 186 | |
AdamGreen | 0:d6e186427f3e | 187 | command[6] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 188 | command[7] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 189 | |
AdamGreen | 0:d6e186427f3e | 190 | command[8] = font; |
AdamGreen | 0:d6e186427f3e | 191 | |
AdamGreen | 0:d6e186427f3e | 192 | red5 = (text_color >> (16 + 3)) & 0x1F; // get red on 5 bits |
AdamGreen | 0:d6e186427f3e | 193 | green6 = (text_color >> (8 + 2)) & 0x3F; // get green on 6 bits |
AdamGreen | 0:d6e186427f3e | 194 | blue5 = (text_color >> (0 + 3)) & 0x1F; // get blue on 5 bits |
AdamGreen | 0:d6e186427f3e | 195 | |
AdamGreen | 0:d6e186427f3e | 196 | command[9] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 197 | command[10] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color |
AdamGreen | 0:d6e186427f3e | 198 | |
AdamGreen | 0:d6e186427f3e | 199 | command[11] = width; |
AdamGreen | 0:d6e186427f3e | 200 | |
AdamGreen | 0:d6e186427f3e | 201 | command[12] = height; |
AdamGreen | 0:d6e186427f3e | 202 | |
AdamGreen | 0:d6e186427f3e | 203 | for (i=0; i<size; i++) command[13+i] = s[i]; |
AdamGreen | 0:d6e186427f3e | 204 | |
AdamGreen | 0:d6e186427f3e | 205 | command[13+size] = 0; |
AdamGreen | 0:d6e186427f3e | 206 | |
AdamGreen | 0:d6e186427f3e | 207 | writeCOMMAND(command, 14 + size); |
AdamGreen | 0:d6e186427f3e | 208 | } |
AdamGreen | 0:d6e186427f3e | 209 | |
AdamGreen | 0:d6e186427f3e | 210 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 211 | void TFT_4DGL :: locate(char col, char row) { // place text curssor at col, row |
AdamGreen | 0:d6e186427f3e | 212 | current_col = col; |
AdamGreen | 0:d6e186427f3e | 213 | current_row = row; |
AdamGreen | 0:d6e186427f3e | 214 | } |
AdamGreen | 0:d6e186427f3e | 215 | |
AdamGreen | 0:d6e186427f3e | 216 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 217 | void TFT_4DGL :: color(int color) { // set text color |
AdamGreen | 0:d6e186427f3e | 218 | current_color = color; |
AdamGreen | 0:d6e186427f3e | 219 | } |
AdamGreen | 0:d6e186427f3e | 220 | |
AdamGreen | 0:d6e186427f3e | 221 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 222 | void TFT_4DGL :: putc(char c) { // place char at current cursor position |
AdamGreen | 0:d6e186427f3e | 223 | |
AdamGreen | 0:d6e186427f3e | 224 | text_char(c, current_col++, current_row, current_color); |
AdamGreen | 0:d6e186427f3e | 225 | |
AdamGreen | 0:d6e186427f3e | 226 | if (current_col == max_col) { |
AdamGreen | 0:d6e186427f3e | 227 | current_col = 0; |
AdamGreen | 0:d6e186427f3e | 228 | current_row++; |
AdamGreen | 0:d6e186427f3e | 229 | } |
AdamGreen | 0:d6e186427f3e | 230 | if (current_row == max_row) { |
AdamGreen | 0:d6e186427f3e | 231 | current_row = 0; |
AdamGreen | 0:d6e186427f3e | 232 | } |
AdamGreen | 0:d6e186427f3e | 233 | } |
AdamGreen | 0:d6e186427f3e | 234 | |
AdamGreen | 0:d6e186427f3e | 235 | //**************************************************************************************************** |
AdamGreen | 0:d6e186427f3e | 236 | void TFT_4DGL :: puts(char *s) { // place string at current cursor position |
AdamGreen | 0:d6e186427f3e | 237 | |
AdamGreen | 0:d6e186427f3e | 238 | text_string(s, current_col, current_row, current_font, current_color); |
AdamGreen | 0:d6e186427f3e | 239 | |
AdamGreen | 0:d6e186427f3e | 240 | current_col += strlen(s); |
AdamGreen | 0:d6e186427f3e | 241 | |
AdamGreen | 0:d6e186427f3e | 242 | if (current_col >= max_col) { |
AdamGreen | 0:d6e186427f3e | 243 | current_row += current_col / max_col; |
AdamGreen | 0:d6e186427f3e | 244 | current_col %= max_col; |
AdamGreen | 0:d6e186427f3e | 245 | } |
AdamGreen | 0:d6e186427f3e | 246 | if (current_row >= max_row) { |
AdamGreen | 0:d6e186427f3e | 247 | current_row %= max_row; |
AdamGreen | 0:d6e186427f3e | 248 | } |
AdamGreen | 0:d6e186427f3e | 249 | } |