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