Library to support using the 2.8" TFT Touch Shield v1.0 - made by Seeed Studio - for Arduino This shield uses either the SPFD5408A or ST7781R display chip.
tft.cpp@0:93976d4026d3, 2015-02-05 (annotated)
- Committer:
- tj4shee
- Date:
- Thu Feb 05 19:39:58 2015 +0000
- Revision:
- 0:93976d4026d3
Initial version of library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tj4shee | 0:93976d4026d3 | 1 | /* |
tj4shee | 0:93976d4026d3 | 2 | SPFD5408A or ST7781R TFT Library. |
tj4shee | 0:93976d4026d3 | 3 | |
tj4shee | 0:93976d4026d3 | 4 | 2015 Copyright (c) Bluegrass Digital Inc. |
tj4shee | 0:93976d4026d3 | 5 | |
tj4shee | 0:93976d4026d3 | 6 | Authors: TJ Forshee (with initializtion code from TFT vendor) |
tj4shee | 0:93976d4026d3 | 7 | |
tj4shee | 0:93976d4026d3 | 8 | This library is free software; you can redistribute it and/or |
tj4shee | 0:93976d4026d3 | 9 | modify it under the terms of the GNU Lesser General Public |
tj4shee | 0:93976d4026d3 | 10 | License as published by the Free Software Foundation; either |
tj4shee | 0:93976d4026d3 | 11 | version 2.1 of the License, or (at your option) any later version. |
tj4shee | 0:93976d4026d3 | 12 | |
tj4shee | 0:93976d4026d3 | 13 | This library is distributed in the hope that it will be useful, |
tj4shee | 0:93976d4026d3 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
tj4shee | 0:93976d4026d3 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
tj4shee | 0:93976d4026d3 | 16 | Lesser General Public License for more details. |
tj4shee | 0:93976d4026d3 | 17 | |
tj4shee | 0:93976d4026d3 | 18 | You should have received a copy of the GNU Lesser General Public |
tj4shee | 0:93976d4026d3 | 19 | License along with this library; if not, write to the Free Software |
tj4shee | 0:93976d4026d3 | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
tj4shee | 0:93976d4026d3 | 21 | |
tj4shee | 0:93976d4026d3 | 22 | */ |
tj4shee | 0:93976d4026d3 | 23 | |
tj4shee | 0:93976d4026d3 | 24 | #include "tft.h" |
tj4shee | 0:93976d4026d3 | 25 | |
tj4shee | 0:93976d4026d3 | 26 | BusInOut lcd_data(D2, D3, D4, D5, D6, D7, D8, D9); |
tj4shee | 0:93976d4026d3 | 27 | DigitalOut CS_BIT(D10); |
tj4shee | 0:93976d4026d3 | 28 | DigitalOut RS_BIT(D11); |
tj4shee | 0:93976d4026d3 | 29 | DigitalOut WR_BIT(D12); |
tj4shee | 0:93976d4026d3 | 30 | DigitalOut RD_BIT(D13); |
tj4shee | 0:93976d4026d3 | 31 | |
tj4shee | 0:93976d4026d3 | 32 | void TFT::pushData(unsigned char data) |
tj4shee | 0:93976d4026d3 | 33 | { |
tj4shee | 0:93976d4026d3 | 34 | //lcd_data.output(); |
tj4shee | 0:93976d4026d3 | 35 | lcd_data.write(data); |
tj4shee | 0:93976d4026d3 | 36 | } |
tj4shee | 0:93976d4026d3 | 37 | |
tj4shee | 0:93976d4026d3 | 38 | unsigned char TFT::getData(void) |
tj4shee | 0:93976d4026d3 | 39 | { |
tj4shee | 0:93976d4026d3 | 40 | //unsigned char data=0; |
tj4shee | 0:93976d4026d3 | 41 | //delay(1); |
tj4shee | 0:93976d4026d3 | 42 | // data |= ((PIND&0xfc)>>2); |
tj4shee | 0:93976d4026d3 | 43 | // data |= ((PINB&0x03)<<6); |
tj4shee | 0:93976d4026d3 | 44 | wait(0.001); |
tj4shee | 0:93976d4026d3 | 45 | //lcd_data.input(); // ??????????? will this clear the inputs ? |
tj4shee | 0:93976d4026d3 | 46 | return lcd_data.read(); |
tj4shee | 0:93976d4026d3 | 47 | } |
tj4shee | 0:93976d4026d3 | 48 | |
tj4shee | 0:93976d4026d3 | 49 | void TFT::sendCommand(unsigned int index) |
tj4shee | 0:93976d4026d3 | 50 | { |
tj4shee | 0:93976d4026d3 | 51 | CS_LOW(); |
tj4shee | 0:93976d4026d3 | 52 | RS_LOW(); |
tj4shee | 0:93976d4026d3 | 53 | RD_HIGH(); |
tj4shee | 0:93976d4026d3 | 54 | WR_HIGH(); |
tj4shee | 0:93976d4026d3 | 55 | |
tj4shee | 0:93976d4026d3 | 56 | WR_LOW(); |
tj4shee | 0:93976d4026d3 | 57 | pushData(0); |
tj4shee | 0:93976d4026d3 | 58 | WR_HIGH(); |
tj4shee | 0:93976d4026d3 | 59 | |
tj4shee | 0:93976d4026d3 | 60 | WR_LOW(); |
tj4shee | 0:93976d4026d3 | 61 | pushData(index&0xff); |
tj4shee | 0:93976d4026d3 | 62 | WR_HIGH(); |
tj4shee | 0:93976d4026d3 | 63 | |
tj4shee | 0:93976d4026d3 | 64 | CS_HIGH(); |
tj4shee | 0:93976d4026d3 | 65 | } |
tj4shee | 0:93976d4026d3 | 66 | |
tj4shee | 0:93976d4026d3 | 67 | void TFT::sendData(unsigned int data) |
tj4shee | 0:93976d4026d3 | 68 | { |
tj4shee | 0:93976d4026d3 | 69 | CS_LOW(); |
tj4shee | 0:93976d4026d3 | 70 | RS_HIGH(); |
tj4shee | 0:93976d4026d3 | 71 | RD_HIGH(); |
tj4shee | 0:93976d4026d3 | 72 | |
tj4shee | 0:93976d4026d3 | 73 | WR_LOW(); |
tj4shee | 0:93976d4026d3 | 74 | pushData((data&0xff00)>>8); |
tj4shee | 0:93976d4026d3 | 75 | WR_HIGH(); |
tj4shee | 0:93976d4026d3 | 76 | |
tj4shee | 0:93976d4026d3 | 77 | WR_LOW(); |
tj4shee | 0:93976d4026d3 | 78 | pushData(data&0xff); |
tj4shee | 0:93976d4026d3 | 79 | WR_HIGH(); |
tj4shee | 0:93976d4026d3 | 80 | |
tj4shee | 0:93976d4026d3 | 81 | CS_HIGH(); |
tj4shee | 0:93976d4026d3 | 82 | } |
tj4shee | 0:93976d4026d3 | 83 | |
tj4shee | 0:93976d4026d3 | 84 | unsigned int TFT::readRegister(unsigned int index) |
tj4shee | 0:93976d4026d3 | 85 | { |
tj4shee | 0:93976d4026d3 | 86 | unsigned int data=0; |
tj4shee | 0:93976d4026d3 | 87 | |
tj4shee | 0:93976d4026d3 | 88 | CS_LOW(); |
tj4shee | 0:93976d4026d3 | 89 | RS_LOW(); |
tj4shee | 0:93976d4026d3 | 90 | RD_HIGH(); |
tj4shee | 0:93976d4026d3 | 91 | |
tj4shee | 0:93976d4026d3 | 92 | all_pin_output(); |
tj4shee | 0:93976d4026d3 | 93 | |
tj4shee | 0:93976d4026d3 | 94 | WR_LOW(); |
tj4shee | 0:93976d4026d3 | 95 | pushData(0); |
tj4shee | 0:93976d4026d3 | 96 | WR_HIGH(); |
tj4shee | 0:93976d4026d3 | 97 | |
tj4shee | 0:93976d4026d3 | 98 | WR_LOW(); |
tj4shee | 0:93976d4026d3 | 99 | pushData(index); |
tj4shee | 0:93976d4026d3 | 100 | WR_HIGH(); |
tj4shee | 0:93976d4026d3 | 101 | |
tj4shee | 0:93976d4026d3 | 102 | all_pin_input(); |
tj4shee | 0:93976d4026d3 | 103 | all_pin_low(); |
tj4shee | 0:93976d4026d3 | 104 | RS_HIGH(); |
tj4shee | 0:93976d4026d3 | 105 | |
tj4shee | 0:93976d4026d3 | 106 | RD_LOW(); |
tj4shee | 0:93976d4026d3 | 107 | data |= getData()<<8; |
tj4shee | 0:93976d4026d3 | 108 | RD_HIGH(); |
tj4shee | 0:93976d4026d3 | 109 | |
tj4shee | 0:93976d4026d3 | 110 | RD_LOW(); |
tj4shee | 0:93976d4026d3 | 111 | data |= getData(); |
tj4shee | 0:93976d4026d3 | 112 | RD_HIGH(); |
tj4shee | 0:93976d4026d3 | 113 | |
tj4shee | 0:93976d4026d3 | 114 | CS_HIGH(); |
tj4shee | 0:93976d4026d3 | 115 | all_pin_output(); |
tj4shee | 0:93976d4026d3 | 116 | return data; |
tj4shee | 0:93976d4026d3 | 117 | } |
tj4shee | 0:93976d4026d3 | 118 | |
tj4shee | 0:93976d4026d3 | 119 | void TFT::init (void) |
tj4shee | 0:93976d4026d3 | 120 | { |
tj4shee | 0:93976d4026d3 | 121 | //CS_OUTPUT; |
tj4shee | 0:93976d4026d3 | 122 | CS_HIGH(); |
tj4shee | 0:93976d4026d3 | 123 | //RD_OUTPUT; |
tj4shee | 0:93976d4026d3 | 124 | //WR_OUTPUT; |
tj4shee | 0:93976d4026d3 | 125 | //RS_OUTPUT; |
tj4shee | 0:93976d4026d3 | 126 | |
tj4shee | 0:93976d4026d3 | 127 | Tft.all_pin_output(); |
tj4shee | 0:93976d4026d3 | 128 | Tft.all_pin_low(); |
tj4shee | 0:93976d4026d3 | 129 | |
tj4shee | 0:93976d4026d3 | 130 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 131 | IC_CODE = readRegister(0x0); |
tj4shee | 0:93976d4026d3 | 132 | |
tj4shee | 0:93976d4026d3 | 133 | if(IC_CODE == 0x5408) |
tj4shee | 0:93976d4026d3 | 134 | { |
tj4shee | 0:93976d4026d3 | 135 | sendCommand(0x0000); |
tj4shee | 0:93976d4026d3 | 136 | sendData(0x0001); |
tj4shee | 0:93976d4026d3 | 137 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 138 | |
tj4shee | 0:93976d4026d3 | 139 | sendCommand(0x0001); |
tj4shee | 0:93976d4026d3 | 140 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 141 | sendCommand(0x0002); |
tj4shee | 0:93976d4026d3 | 142 | sendData(0x0700); |
tj4shee | 0:93976d4026d3 | 143 | sendCommand(0x0003); |
tj4shee | 0:93976d4026d3 | 144 | sendData(0x1030); |
tj4shee | 0:93976d4026d3 | 145 | sendCommand(0x0004); |
tj4shee | 0:93976d4026d3 | 146 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 147 | sendCommand(0x0008); |
tj4shee | 0:93976d4026d3 | 148 | sendData(0x0207); |
tj4shee | 0:93976d4026d3 | 149 | sendCommand(0x0009); |
tj4shee | 0:93976d4026d3 | 150 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 151 | sendCommand(0x000A); |
tj4shee | 0:93976d4026d3 | 152 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 153 | sendCommand(0x000C); |
tj4shee | 0:93976d4026d3 | 154 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 155 | sendCommand(0x000D); |
tj4shee | 0:93976d4026d3 | 156 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 157 | sendCommand(0x000F); |
tj4shee | 0:93976d4026d3 | 158 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 159 | //power on sequence VGHVGL |
tj4shee | 0:93976d4026d3 | 160 | sendCommand(0x0010); |
tj4shee | 0:93976d4026d3 | 161 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 162 | sendCommand(0x0011); |
tj4shee | 0:93976d4026d3 | 163 | sendData(0x0007); |
tj4shee | 0:93976d4026d3 | 164 | sendCommand(0x0012); |
tj4shee | 0:93976d4026d3 | 165 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 166 | sendCommand(0x0013); |
tj4shee | 0:93976d4026d3 | 167 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 168 | //vgh |
tj4shee | 0:93976d4026d3 | 169 | sendCommand(0x0010); |
tj4shee | 0:93976d4026d3 | 170 | sendData(0x1290); |
tj4shee | 0:93976d4026d3 | 171 | sendCommand(0x0011); |
tj4shee | 0:93976d4026d3 | 172 | sendData(0x0227); |
tj4shee | 0:93976d4026d3 | 173 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 174 | //vregiout |
tj4shee | 0:93976d4026d3 | 175 | sendCommand(0x0012); |
tj4shee | 0:93976d4026d3 | 176 | sendData(0x001d); //0x001b |
tj4shee | 0:93976d4026d3 | 177 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 178 | //vom amplitude |
tj4shee | 0:93976d4026d3 | 179 | sendCommand(0x0013); |
tj4shee | 0:93976d4026d3 | 180 | sendData(0x1500); |
tj4shee | 0:93976d4026d3 | 181 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 182 | //vom H |
tj4shee | 0:93976d4026d3 | 183 | sendCommand(0x0029); |
tj4shee | 0:93976d4026d3 | 184 | sendData(0x0018); |
tj4shee | 0:93976d4026d3 | 185 | sendCommand(0x002B); |
tj4shee | 0:93976d4026d3 | 186 | sendData(0x000D); |
tj4shee | 0:93976d4026d3 | 187 | |
tj4shee | 0:93976d4026d3 | 188 | //gamma |
tj4shee | 0:93976d4026d3 | 189 | sendCommand(0x0030); |
tj4shee | 0:93976d4026d3 | 190 | sendData(0x0004); |
tj4shee | 0:93976d4026d3 | 191 | sendCommand(0x0031); |
tj4shee | 0:93976d4026d3 | 192 | sendData(0x0307); |
tj4shee | 0:93976d4026d3 | 193 | sendCommand(0x0032); |
tj4shee | 0:93976d4026d3 | 194 | sendData(0x0002);// 0006 |
tj4shee | 0:93976d4026d3 | 195 | sendCommand(0x0035); |
tj4shee | 0:93976d4026d3 | 196 | sendData(0x0206); |
tj4shee | 0:93976d4026d3 | 197 | sendCommand(0x0036); |
tj4shee | 0:93976d4026d3 | 198 | sendData(0x0408); |
tj4shee | 0:93976d4026d3 | 199 | sendCommand(0x0037); |
tj4shee | 0:93976d4026d3 | 200 | sendData(0x0507); |
tj4shee | 0:93976d4026d3 | 201 | sendCommand(0x0038); |
tj4shee | 0:93976d4026d3 | 202 | sendData(0x0204);//0200 |
tj4shee | 0:93976d4026d3 | 203 | sendCommand(0x0039); |
tj4shee | 0:93976d4026d3 | 204 | sendData(0x0707); |
tj4shee | 0:93976d4026d3 | 205 | sendCommand(0x003C); |
tj4shee | 0:93976d4026d3 | 206 | sendData(0x0405);// 0504 |
tj4shee | 0:93976d4026d3 | 207 | sendCommand(0x003D); |
tj4shee | 0:93976d4026d3 | 208 | sendData(0x0F02); |
tj4shee | 0:93976d4026d3 | 209 | //ram |
tj4shee | 0:93976d4026d3 | 210 | sendCommand(0x0050); |
tj4shee | 0:93976d4026d3 | 211 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 212 | sendCommand(0x0051); |
tj4shee | 0:93976d4026d3 | 213 | sendData(0x00EF); |
tj4shee | 0:93976d4026d3 | 214 | sendCommand(0x0052); |
tj4shee | 0:93976d4026d3 | 215 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 216 | sendCommand(0x0053); |
tj4shee | 0:93976d4026d3 | 217 | sendData(0x013F); |
tj4shee | 0:93976d4026d3 | 218 | sendCommand(0x0060); |
tj4shee | 0:93976d4026d3 | 219 | sendData(0xA700); |
tj4shee | 0:93976d4026d3 | 220 | sendCommand(0x0061); |
tj4shee | 0:93976d4026d3 | 221 | sendData(0x0001); |
tj4shee | 0:93976d4026d3 | 222 | sendCommand(0x006A); |
tj4shee | 0:93976d4026d3 | 223 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 224 | // |
tj4shee | 0:93976d4026d3 | 225 | sendCommand(0x0080); |
tj4shee | 0:93976d4026d3 | 226 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 227 | sendCommand(0x0081); |
tj4shee | 0:93976d4026d3 | 228 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 229 | sendCommand(0x0082); |
tj4shee | 0:93976d4026d3 | 230 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 231 | sendCommand(0x0083); |
tj4shee | 0:93976d4026d3 | 232 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 233 | sendCommand(0x0084); |
tj4shee | 0:93976d4026d3 | 234 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 235 | sendCommand(0x0085); |
tj4shee | 0:93976d4026d3 | 236 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 237 | // |
tj4shee | 0:93976d4026d3 | 238 | sendCommand(0x0090); |
tj4shee | 0:93976d4026d3 | 239 | sendData(0x0010); |
tj4shee | 0:93976d4026d3 | 240 | sendCommand(0x0092); |
tj4shee | 0:93976d4026d3 | 241 | sendData(0x0600); |
tj4shee | 0:93976d4026d3 | 242 | sendCommand(0x0093); |
tj4shee | 0:93976d4026d3 | 243 | sendData(0x0003); |
tj4shee | 0:93976d4026d3 | 244 | sendCommand(0x0095); |
tj4shee | 0:93976d4026d3 | 245 | sendData(0x0110); |
tj4shee | 0:93976d4026d3 | 246 | sendCommand(0x0097); |
tj4shee | 0:93976d4026d3 | 247 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 248 | sendCommand(0x0098); |
tj4shee | 0:93976d4026d3 | 249 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 250 | sendCommand(0x0007); |
tj4shee | 0:93976d4026d3 | 251 | sendData(0x0133); |
tj4shee | 0:93976d4026d3 | 252 | |
tj4shee | 0:93976d4026d3 | 253 | sendCommand(0x0022);//Start to write to display RAM |
tj4shee | 0:93976d4026d3 | 254 | |
tj4shee | 0:93976d4026d3 | 255 | } |
tj4shee | 0:93976d4026d3 | 256 | else |
tj4shee | 0:93976d4026d3 | 257 | { |
tj4shee | 0:93976d4026d3 | 258 | |
tj4shee | 0:93976d4026d3 | 259 | sendCommand(0x0001); |
tj4shee | 0:93976d4026d3 | 260 | sendData(0x0100); |
tj4shee | 0:93976d4026d3 | 261 | sendCommand(0x0002); |
tj4shee | 0:93976d4026d3 | 262 | sendData(0x0700); |
tj4shee | 0:93976d4026d3 | 263 | sendCommand(0x0003); |
tj4shee | 0:93976d4026d3 | 264 | sendData(0x1030); |
tj4shee | 0:93976d4026d3 | 265 | sendCommand(0x0004); |
tj4shee | 0:93976d4026d3 | 266 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 267 | sendCommand(0x0008); |
tj4shee | 0:93976d4026d3 | 268 | sendData(0x0302); |
tj4shee | 0:93976d4026d3 | 269 | |
tj4shee | 0:93976d4026d3 | 270 | sendCommand(0x000A); |
tj4shee | 0:93976d4026d3 | 271 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 272 | sendCommand(0x000C); |
tj4shee | 0:93976d4026d3 | 273 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 274 | sendCommand(0x000D); |
tj4shee | 0:93976d4026d3 | 275 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 276 | sendCommand(0x000F); |
tj4shee | 0:93976d4026d3 | 277 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 278 | |
tj4shee | 0:93976d4026d3 | 279 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 280 | |
tj4shee | 0:93976d4026d3 | 281 | sendCommand(0x0030); |
tj4shee | 0:93976d4026d3 | 282 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 283 | sendCommand(0x0031); |
tj4shee | 0:93976d4026d3 | 284 | sendData(0x0405); |
tj4shee | 0:93976d4026d3 | 285 | sendCommand(0x0032); |
tj4shee | 0:93976d4026d3 | 286 | sendData(0x0203); |
tj4shee | 0:93976d4026d3 | 287 | sendCommand(0x0035); |
tj4shee | 0:93976d4026d3 | 288 | sendData(0x0004); |
tj4shee | 0:93976d4026d3 | 289 | sendCommand(0x0036); |
tj4shee | 0:93976d4026d3 | 290 | sendData(0x0B07); |
tj4shee | 0:93976d4026d3 | 291 | sendCommand(0x0037); |
tj4shee | 0:93976d4026d3 | 292 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 293 | sendCommand(0x0038); |
tj4shee | 0:93976d4026d3 | 294 | sendData(0x0405); |
tj4shee | 0:93976d4026d3 | 295 | sendCommand(0x0039); |
tj4shee | 0:93976d4026d3 | 296 | sendData(0x0203); |
tj4shee | 0:93976d4026d3 | 297 | sendCommand(0x003c); |
tj4shee | 0:93976d4026d3 | 298 | sendData(0x0004); |
tj4shee | 0:93976d4026d3 | 299 | sendCommand(0x003d); |
tj4shee | 0:93976d4026d3 | 300 | sendData(0x0B07); |
tj4shee | 0:93976d4026d3 | 301 | sendCommand(0x0020); |
tj4shee | 0:93976d4026d3 | 302 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 303 | sendCommand(0x0021); |
tj4shee | 0:93976d4026d3 | 304 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 305 | sendCommand(0x0050); |
tj4shee | 0:93976d4026d3 | 306 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 307 | sendCommand(0x0051); |
tj4shee | 0:93976d4026d3 | 308 | sendData(0x00ef); |
tj4shee | 0:93976d4026d3 | 309 | sendCommand(0x0052); |
tj4shee | 0:93976d4026d3 | 310 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 311 | sendCommand(0x0053); |
tj4shee | 0:93976d4026d3 | 312 | sendData(0x013f); |
tj4shee | 0:93976d4026d3 | 313 | |
tj4shee | 0:93976d4026d3 | 314 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 315 | |
tj4shee | 0:93976d4026d3 | 316 | sendCommand(0x0060); |
tj4shee | 0:93976d4026d3 | 317 | sendData(0xa700); |
tj4shee | 0:93976d4026d3 | 318 | sendCommand(0x0061); |
tj4shee | 0:93976d4026d3 | 319 | sendData(0x0001); |
tj4shee | 0:93976d4026d3 | 320 | sendCommand(0x0090); |
tj4shee | 0:93976d4026d3 | 321 | sendData(0x003A); |
tj4shee | 0:93976d4026d3 | 322 | sendCommand(0x0095); |
tj4shee | 0:93976d4026d3 | 323 | sendData(0x021E); |
tj4shee | 0:93976d4026d3 | 324 | sendCommand(0x0080); |
tj4shee | 0:93976d4026d3 | 325 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 326 | sendCommand(0x0081); |
tj4shee | 0:93976d4026d3 | 327 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 328 | sendCommand(0x0082); |
tj4shee | 0:93976d4026d3 | 329 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 330 | sendCommand(0x0083); |
tj4shee | 0:93976d4026d3 | 331 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 332 | sendCommand(0x0084); |
tj4shee | 0:93976d4026d3 | 333 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 334 | sendCommand(0x0085); |
tj4shee | 0:93976d4026d3 | 335 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 336 | sendCommand(0x00FF); |
tj4shee | 0:93976d4026d3 | 337 | sendData(0x0001); |
tj4shee | 0:93976d4026d3 | 338 | sendCommand(0x00B0); |
tj4shee | 0:93976d4026d3 | 339 | sendData(0x140D); |
tj4shee | 0:93976d4026d3 | 340 | sendCommand(0x00FF); |
tj4shee | 0:93976d4026d3 | 341 | sendData(0x0000); |
tj4shee | 0:93976d4026d3 | 342 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 343 | sendCommand(0x0007); |
tj4shee | 0:93976d4026d3 | 344 | sendData(0x0133); |
tj4shee | 0:93976d4026d3 | 345 | wait(0.05); |
tj4shee | 0:93976d4026d3 | 346 | //exit standby |
tj4shee | 0:93976d4026d3 | 347 | sendCommand(0x0010); |
tj4shee | 0:93976d4026d3 | 348 | sendData(0x14E0); |
tj4shee | 0:93976d4026d3 | 349 | wait(0.1); |
tj4shee | 0:93976d4026d3 | 350 | sendCommand(0x0007); |
tj4shee | 0:93976d4026d3 | 351 | sendData(0x0133); |
tj4shee | 0:93976d4026d3 | 352 | sendCommand(0x0022); |
tj4shee | 0:93976d4026d3 | 353 | } |
tj4shee | 0:93976d4026d3 | 354 | |
tj4shee | 0:93976d4026d3 | 355 | //paint screen black |
tj4shee | 0:93976d4026d3 | 356 | for(unsigned char i=0;i<2;i++) |
tj4shee | 0:93976d4026d3 | 357 | { |
tj4shee | 0:93976d4026d3 | 358 | for(unsigned int f=0;f<38400;f++) |
tj4shee | 0:93976d4026d3 | 359 | { |
tj4shee | 0:93976d4026d3 | 360 | sendData(BLACK); |
tj4shee | 0:93976d4026d3 | 361 | } |
tj4shee | 0:93976d4026d3 | 362 | } |
tj4shee | 0:93976d4026d3 | 363 | } |
tj4shee | 0:93976d4026d3 | 364 | |
tj4shee | 0:93976d4026d3 | 365 | void TFT::setOrientation(unsigned int HV)//horizontal or vertical |
tj4shee | 0:93976d4026d3 | 366 | { |
tj4shee | 0:93976d4026d3 | 367 | sendCommand(0x03); |
tj4shee | 0:93976d4026d3 | 368 | if(HV==1)//vertical |
tj4shee | 0:93976d4026d3 | 369 | { |
tj4shee | 0:93976d4026d3 | 370 | if(IC_CODE == 0x5408) { sendData(0x1038); } |
tj4shee | 0:93976d4026d3 | 371 | else { sendData(0x5038); } |
tj4shee | 0:93976d4026d3 | 372 | } |
tj4shee | 0:93976d4026d3 | 373 | else//horizontal |
tj4shee | 0:93976d4026d3 | 374 | { |
tj4shee | 0:93976d4026d3 | 375 | if(IC_CODE == 0x5408) { sendData(0x1030); } |
tj4shee | 0:93976d4026d3 | 376 | else { sendData(0x5030); } |
tj4shee | 0:93976d4026d3 | 377 | } |
tj4shee | 0:93976d4026d3 | 378 | sendCommand(0x0022); //Start to write to display RAM |
tj4shee | 0:93976d4026d3 | 379 | } |
tj4shee | 0:93976d4026d3 | 380 | |
tj4shee | 0:93976d4026d3 | 381 | unsigned int TFT::constrain(unsigned int val2chk, unsigned int lowval, unsigned int highval) |
tj4shee | 0:93976d4026d3 | 382 | { |
tj4shee | 0:93976d4026d3 | 383 | if (val2chk < lowval) |
tj4shee | 0:93976d4026d3 | 384 | return lowval; |
tj4shee | 0:93976d4026d3 | 385 | else if (val2chk > highval) |
tj4shee | 0:93976d4026d3 | 386 | return highval; |
tj4shee | 0:93976d4026d3 | 387 | else |
tj4shee | 0:93976d4026d3 | 388 | return val2chk; |
tj4shee | 0:93976d4026d3 | 389 | } |
tj4shee | 0:93976d4026d3 | 390 | |
tj4shee | 0:93976d4026d3 | 391 | void TFT::setXY(unsigned int poX, unsigned int poY) |
tj4shee | 0:93976d4026d3 | 392 | { |
tj4shee | 0:93976d4026d3 | 393 | poX = constrain(poX,MIN_X,MAX_X); //Limits the pixel range to 0 - 239 |
tj4shee | 0:93976d4026d3 | 394 | poY = constrain(poY,MIN_Y,MAX_Y); //Limits the pixel range to 0 - 319 |
tj4shee | 0:93976d4026d3 | 395 | /* Writing to GRAM beyond the range gives unpredictable results. The above code |
tj4shee | 0:93976d4026d3 | 396 | is to limit this. This might also slow down the writing to TFT. You can |
tj4shee | 0:93976d4026d3 | 397 | remove the above two lines of code, if you think your application code |
tj4shee | 0:93976d4026d3 | 398 | does not write beyond this range. This would speed up TFT writing. */ |
tj4shee | 0:93976d4026d3 | 399 | |
tj4shee | 0:93976d4026d3 | 400 | sendCommand(0x0020);//X |
tj4shee | 0:93976d4026d3 | 401 | sendData(poX); |
tj4shee | 0:93976d4026d3 | 402 | sendCommand(0x0021);//Y |
tj4shee | 0:93976d4026d3 | 403 | sendData(poY); |
tj4shee | 0:93976d4026d3 | 404 | sendCommand(0x0022);//Start to write to display RAM |
tj4shee | 0:93976d4026d3 | 405 | } |
tj4shee | 0:93976d4026d3 | 406 | |
tj4shee | 0:93976d4026d3 | 407 | void TFT::setPixel(unsigned int poX, unsigned int poY,unsigned int color) |
tj4shee | 0:93976d4026d3 | 408 | { |
tj4shee | 0:93976d4026d3 | 409 | setXY(poX,poY); |
tj4shee | 0:93976d4026d3 | 410 | sendData(color); |
tj4shee | 0:93976d4026d3 | 411 | } |
tj4shee | 0:93976d4026d3 | 412 | |
tj4shee | 0:93976d4026d3 | 413 | void TFT::drawCircle(int poX, int poY, int r,unsigned int color) |
tj4shee | 0:93976d4026d3 | 414 | { |
tj4shee | 0:93976d4026d3 | 415 | int x = -r, y = 0, err = 2-2*r, e2; |
tj4shee | 0:93976d4026d3 | 416 | do |
tj4shee | 0:93976d4026d3 | 417 | { |
tj4shee | 0:93976d4026d3 | 418 | setPixel(poX-x, poY+y,color); |
tj4shee | 0:93976d4026d3 | 419 | setPixel(poX+x, poY+y,color); |
tj4shee | 0:93976d4026d3 | 420 | setPixel(poX+x, poY-y,color); |
tj4shee | 0:93976d4026d3 | 421 | setPixel(poX-x, poY-y,color); |
tj4shee | 0:93976d4026d3 | 422 | e2 = err; |
tj4shee | 0:93976d4026d3 | 423 | if (e2 <= y) |
tj4shee | 0:93976d4026d3 | 424 | { |
tj4shee | 0:93976d4026d3 | 425 | err += ++y*2+1; |
tj4shee | 0:93976d4026d3 | 426 | if (-x == y && e2 <= x) e2 = 0; |
tj4shee | 0:93976d4026d3 | 427 | } |
tj4shee | 0:93976d4026d3 | 428 | if (e2 > x) err += ++x*2+1; |
tj4shee | 0:93976d4026d3 | 429 | } |
tj4shee | 0:93976d4026d3 | 430 | while (x <= 0); |
tj4shee | 0:93976d4026d3 | 431 | } |
tj4shee | 0:93976d4026d3 | 432 | |
tj4shee | 0:93976d4026d3 | 433 | void TFT::fillCircle(int poX, int poY, int r,unsigned int color) |
tj4shee | 0:93976d4026d3 | 434 | { |
tj4shee | 0:93976d4026d3 | 435 | int x = -r, y = 0, err = 2-2*r, e2; |
tj4shee | 0:93976d4026d3 | 436 | do |
tj4shee | 0:93976d4026d3 | 437 | { |
tj4shee | 0:93976d4026d3 | 438 | drawVerticalLine(poX-x,poY-y,2*y,color); |
tj4shee | 0:93976d4026d3 | 439 | drawVerticalLine(poX+x,poY-y,2*y,color); |
tj4shee | 0:93976d4026d3 | 440 | |
tj4shee | 0:93976d4026d3 | 441 | e2 = err; |
tj4shee | 0:93976d4026d3 | 442 | if (e2 <= y) |
tj4shee | 0:93976d4026d3 | 443 | { |
tj4shee | 0:93976d4026d3 | 444 | err += ++y*2+1; |
tj4shee | 0:93976d4026d3 | 445 | if (-x == y && e2 <= x) e2 = 0; |
tj4shee | 0:93976d4026d3 | 446 | } |
tj4shee | 0:93976d4026d3 | 447 | if (e2 > x) err += ++x*2+1; |
tj4shee | 0:93976d4026d3 | 448 | } |
tj4shee | 0:93976d4026d3 | 449 | while (x <= 0); |
tj4shee | 0:93976d4026d3 | 450 | } |
tj4shee | 0:93976d4026d3 | 451 | |
tj4shee | 0:93976d4026d3 | 452 | void TFT::drawLine(unsigned int x0,unsigned int y0,unsigned int x1,unsigned int y1,unsigned int color) |
tj4shee | 0:93976d4026d3 | 453 | { |
tj4shee | 0:93976d4026d3 | 454 | int dx = x1-x0; |
tj4shee | 0:93976d4026d3 | 455 | dx = abs(dx); |
tj4shee | 0:93976d4026d3 | 456 | int sx = x0<x1 ? 1 : -1; |
tj4shee | 0:93976d4026d3 | 457 | int dy = y1-y0; |
tj4shee | 0:93976d4026d3 | 458 | dy = -abs(dy); |
tj4shee | 0:93976d4026d3 | 459 | int sy = y0<y1 ? 1 : -1; |
tj4shee | 0:93976d4026d3 | 460 | int err = dx+dy, e2; /* error value e_xy */ |
tj4shee | 0:93976d4026d3 | 461 | for (;;) |
tj4shee | 0:93976d4026d3 | 462 | { |
tj4shee | 0:93976d4026d3 | 463 | setPixel(x0,y0,color); |
tj4shee | 0:93976d4026d3 | 464 | e2 = 2*err; |
tj4shee | 0:93976d4026d3 | 465 | if (e2 >= dy) |
tj4shee | 0:93976d4026d3 | 466 | { /* e_xy+e_x > 0 */ |
tj4shee | 0:93976d4026d3 | 467 | if (x0 == x1) break; |
tj4shee | 0:93976d4026d3 | 468 | err += dy; |
tj4shee | 0:93976d4026d3 | 469 | x0 += sx; |
tj4shee | 0:93976d4026d3 | 470 | } |
tj4shee | 0:93976d4026d3 | 471 | if (e2 <= dx) |
tj4shee | 0:93976d4026d3 | 472 | { /* e_xy+e_y < 0 */ |
tj4shee | 0:93976d4026d3 | 473 | if (y0 == y1) break; |
tj4shee | 0:93976d4026d3 | 474 | err += dx; |
tj4shee | 0:93976d4026d3 | 475 | y0 += sy; |
tj4shee | 0:93976d4026d3 | 476 | } |
tj4shee | 0:93976d4026d3 | 477 | } |
tj4shee | 0:93976d4026d3 | 478 | } |
tj4shee | 0:93976d4026d3 | 479 | |
tj4shee | 0:93976d4026d3 | 480 | void TFT::drawVerticalLine(unsigned int poX, unsigned int poY,unsigned int length,unsigned int color) |
tj4shee | 0:93976d4026d3 | 481 | { |
tj4shee | 0:93976d4026d3 | 482 | poX = constrain(poX,MIN_X,MAX_X); //Limits the pixel range to 0 - 239 |
tj4shee | 0:93976d4026d3 | 483 | poY = constrain(poY,MIN_Y,MAX_Y); //Limits the pixel range to 0 - 319 |
tj4shee | 0:93976d4026d3 | 484 | |
tj4shee | 0:93976d4026d3 | 485 | setXY(poX,poY); |
tj4shee | 0:93976d4026d3 | 486 | setOrientation(1); |
tj4shee | 0:93976d4026d3 | 487 | if(length+poY>MAX_Y) |
tj4shee | 0:93976d4026d3 | 488 | { |
tj4shee | 0:93976d4026d3 | 489 | length=MAX_Y-poY; |
tj4shee | 0:93976d4026d3 | 490 | } |
tj4shee | 0:93976d4026d3 | 491 | |
tj4shee | 0:93976d4026d3 | 492 | for(unsigned int i=0;i<length;i++) |
tj4shee | 0:93976d4026d3 | 493 | { |
tj4shee | 0:93976d4026d3 | 494 | sendData(color); |
tj4shee | 0:93976d4026d3 | 495 | } |
tj4shee | 0:93976d4026d3 | 496 | } |
tj4shee | 0:93976d4026d3 | 497 | |
tj4shee | 0:93976d4026d3 | 498 | void TFT::drawHorizontalLine(unsigned int poX, unsigned int poY,unsigned int length,unsigned int color) |
tj4shee | 0:93976d4026d3 | 499 | { |
tj4shee | 0:93976d4026d3 | 500 | poX = constrain(poX,MIN_X,MAX_X); //Limits the pixel range to 0 - 239 |
tj4shee | 0:93976d4026d3 | 501 | poY = constrain(poY,MIN_Y,MAX_Y); //Limits the pixel range to 0 - 319 |
tj4shee | 0:93976d4026d3 | 502 | |
tj4shee | 0:93976d4026d3 | 503 | setXY(poX,poY); |
tj4shee | 0:93976d4026d3 | 504 | setOrientation(0); |
tj4shee | 0:93976d4026d3 | 505 | if(length+poX>MAX_X) |
tj4shee | 0:93976d4026d3 | 506 | { |
tj4shee | 0:93976d4026d3 | 507 | length=MAX_X-poX; |
tj4shee | 0:93976d4026d3 | 508 | } |
tj4shee | 0:93976d4026d3 | 509 | for(unsigned int i=0;i<length;i++) |
tj4shee | 0:93976d4026d3 | 510 | { |
tj4shee | 0:93976d4026d3 | 511 | sendData(color); |
tj4shee | 0:93976d4026d3 | 512 | } |
tj4shee | 0:93976d4026d3 | 513 | } |
tj4shee | 0:93976d4026d3 | 514 | |
tj4shee | 0:93976d4026d3 | 515 | void TFT::drawRectangle(unsigned int poX, unsigned int poY, unsigned int length,unsigned int width,unsigned int color) |
tj4shee | 0:93976d4026d3 | 516 | { |
tj4shee | 0:93976d4026d3 | 517 | drawHorizontalLine(poX, poY, length, color); |
tj4shee | 0:93976d4026d3 | 518 | drawHorizontalLine(poX, poY+width, length, color); |
tj4shee | 0:93976d4026d3 | 519 | |
tj4shee | 0:93976d4026d3 | 520 | drawVerticalLine(poX, poY, width,color); |
tj4shee | 0:93976d4026d3 | 521 | drawVerticalLine(poX + length, poY, width,color); |
tj4shee | 0:93976d4026d3 | 522 | } |
tj4shee | 0:93976d4026d3 | 523 | |
tj4shee | 0:93976d4026d3 | 524 | void TFT::fillRectangle(unsigned int poX, unsigned int poY, unsigned int length, unsigned int width, unsigned int color) |
tj4shee | 0:93976d4026d3 | 525 | { |
tj4shee | 0:93976d4026d3 | 526 | for(unsigned int i=0;i<width;i++) |
tj4shee | 0:93976d4026d3 | 527 | { |
tj4shee | 0:93976d4026d3 | 528 | drawHorizontalLine(poX, poY+i, length, color); |
tj4shee | 0:93976d4026d3 | 529 | } |
tj4shee | 0:93976d4026d3 | 530 | } |
tj4shee | 0:93976d4026d3 | 531 | |
tj4shee | 0:93976d4026d3 | 532 | void TFT::drawChar(unsigned char ascii,unsigned int poX, unsigned int poY,unsigned int size, unsigned int fgcolor) |
tj4shee | 0:93976d4026d3 | 533 | { |
tj4shee | 0:93976d4026d3 | 534 | setXY(poX,poY); |
tj4shee | 0:93976d4026d3 | 535 | setOrientation(1); |
tj4shee | 0:93976d4026d3 | 536 | if((ascii < 0x20)||(ascii > 0x7e))//Unsupported char. |
tj4shee | 0:93976d4026d3 | 537 | { |
tj4shee | 0:93976d4026d3 | 538 | ascii = '?'; |
tj4shee | 0:93976d4026d3 | 539 | } |
tj4shee | 0:93976d4026d3 | 540 | for(unsigned char i=0;i<8;i++) |
tj4shee | 0:93976d4026d3 | 541 | { |
tj4shee | 0:93976d4026d3 | 542 | unsigned char temp = simpleFont[ascii-0x20][i]; |
tj4shee | 0:93976d4026d3 | 543 | for(unsigned char f=0;f<8;f++) |
tj4shee | 0:93976d4026d3 | 544 | { |
tj4shee | 0:93976d4026d3 | 545 | if((temp>>f)&0x01) |
tj4shee | 0:93976d4026d3 | 546 | { |
tj4shee | 0:93976d4026d3 | 547 | fillRectangle(poX+i*size, poY+f*size, size, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 548 | } |
tj4shee | 0:93976d4026d3 | 549 | |
tj4shee | 0:93976d4026d3 | 550 | } |
tj4shee | 0:93976d4026d3 | 551 | } |
tj4shee | 0:93976d4026d3 | 552 | } |
tj4shee | 0:93976d4026d3 | 553 | |
tj4shee | 0:93976d4026d3 | 554 | void TFT::drawString(char *string,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor) |
tj4shee | 0:93976d4026d3 | 555 | { |
tj4shee | 0:93976d4026d3 | 556 | while(*string) |
tj4shee | 0:93976d4026d3 | 557 | { |
tj4shee | 0:93976d4026d3 | 558 | drawChar(*string, poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 559 | *string++; |
tj4shee | 0:93976d4026d3 | 560 | |
tj4shee | 0:93976d4026d3 | 561 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 562 | { |
tj4shee | 0:93976d4026d3 | 563 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 564 | } |
tj4shee | 0:93976d4026d3 | 565 | } |
tj4shee | 0:93976d4026d3 | 566 | } |
tj4shee | 0:93976d4026d3 | 567 | |
tj4shee | 0:93976d4026d3 | 568 | unsigned char TFT::drawNumber(long long_num,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor) |
tj4shee | 0:93976d4026d3 | 569 | { |
tj4shee | 0:93976d4026d3 | 570 | unsigned char char_buffer[10]=""; |
tj4shee | 0:93976d4026d3 | 571 | unsigned char i = 0; |
tj4shee | 0:93976d4026d3 | 572 | unsigned char f = 0; |
tj4shee | 0:93976d4026d3 | 573 | |
tj4shee | 0:93976d4026d3 | 574 | if (long_num < 0) |
tj4shee | 0:93976d4026d3 | 575 | { |
tj4shee | 0:93976d4026d3 | 576 | f=1; |
tj4shee | 0:93976d4026d3 | 577 | drawChar('-',poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 578 | long_num = -long_num; |
tj4shee | 0:93976d4026d3 | 579 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 580 | { |
tj4shee | 0:93976d4026d3 | 581 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 582 | } |
tj4shee | 0:93976d4026d3 | 583 | } |
tj4shee | 0:93976d4026d3 | 584 | else if (long_num == 0) |
tj4shee | 0:93976d4026d3 | 585 | { |
tj4shee | 0:93976d4026d3 | 586 | f=1; |
tj4shee | 0:93976d4026d3 | 587 | drawChar('0',poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 588 | if (poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 589 | { |
tj4shee | 0:93976d4026d3 | 590 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 591 | } |
tj4shee | 0:93976d4026d3 | 592 | return f; |
tj4shee | 0:93976d4026d3 | 593 | } |
tj4shee | 0:93976d4026d3 | 594 | |
tj4shee | 0:93976d4026d3 | 595 | while (long_num > 0) |
tj4shee | 0:93976d4026d3 | 596 | { |
tj4shee | 0:93976d4026d3 | 597 | char_buffer[i++] = long_num % 10; |
tj4shee | 0:93976d4026d3 | 598 | long_num /= 10; |
tj4shee | 0:93976d4026d3 | 599 | } |
tj4shee | 0:93976d4026d3 | 600 | |
tj4shee | 0:93976d4026d3 | 601 | f=f+i; |
tj4shee | 0:93976d4026d3 | 602 | for(; i > 0; i--) |
tj4shee | 0:93976d4026d3 | 603 | { |
tj4shee | 0:93976d4026d3 | 604 | drawChar('0'+ char_buffer[i - 1],poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 605 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 606 | { |
tj4shee | 0:93976d4026d3 | 607 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 608 | } |
tj4shee | 0:93976d4026d3 | 609 | } |
tj4shee | 0:93976d4026d3 | 610 | return f; |
tj4shee | 0:93976d4026d3 | 611 | } |
tj4shee | 0:93976d4026d3 | 612 | |
tj4shee | 0:93976d4026d3 | 613 | unsigned char TFT::drawFloat(float floatNumber,unsigned char decimal,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor) |
tj4shee | 0:93976d4026d3 | 614 | { |
tj4shee | 0:93976d4026d3 | 615 | unsigned int temp=0; |
tj4shee | 0:93976d4026d3 | 616 | float decy=0.0; |
tj4shee | 0:93976d4026d3 | 617 | float rounding = 0.5; |
tj4shee | 0:93976d4026d3 | 618 | unsigned char f=0; |
tj4shee | 0:93976d4026d3 | 619 | if(floatNumber<0.0) |
tj4shee | 0:93976d4026d3 | 620 | { |
tj4shee | 0:93976d4026d3 | 621 | drawChar('-',poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 622 | floatNumber = -floatNumber; |
tj4shee | 0:93976d4026d3 | 623 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 624 | { |
tj4shee | 0:93976d4026d3 | 625 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 626 | } |
tj4shee | 0:93976d4026d3 | 627 | f =1; |
tj4shee | 0:93976d4026d3 | 628 | } |
tj4shee | 0:93976d4026d3 | 629 | for (unsigned char i=0; i<decimal; ++i) |
tj4shee | 0:93976d4026d3 | 630 | { |
tj4shee | 0:93976d4026d3 | 631 | rounding /= 10.0; |
tj4shee | 0:93976d4026d3 | 632 | } |
tj4shee | 0:93976d4026d3 | 633 | floatNumber += rounding; |
tj4shee | 0:93976d4026d3 | 634 | |
tj4shee | 0:93976d4026d3 | 635 | temp = (unsigned int)floatNumber; |
tj4shee | 0:93976d4026d3 | 636 | unsigned char howlong=drawNumber(temp,poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 637 | f += howlong; |
tj4shee | 0:93976d4026d3 | 638 | if((poX+8*size*howlong) < MAX_X) |
tj4shee | 0:93976d4026d3 | 639 | { |
tj4shee | 0:93976d4026d3 | 640 | poX+=8*size*howlong; // Move cursor right |
tj4shee | 0:93976d4026d3 | 641 | } |
tj4shee | 0:93976d4026d3 | 642 | |
tj4shee | 0:93976d4026d3 | 643 | if(decimal>0) |
tj4shee | 0:93976d4026d3 | 644 | { |
tj4shee | 0:93976d4026d3 | 645 | drawChar('.',poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 646 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 647 | { |
tj4shee | 0:93976d4026d3 | 648 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 649 | } |
tj4shee | 0:93976d4026d3 | 650 | f +=1; |
tj4shee | 0:93976d4026d3 | 651 | } |
tj4shee | 0:93976d4026d3 | 652 | decy = floatNumber-temp;//decimal part, |
tj4shee | 0:93976d4026d3 | 653 | for(unsigned char i=0;i<decimal;i++)//4 |
tj4shee | 0:93976d4026d3 | 654 | { |
tj4shee | 0:93976d4026d3 | 655 | decy *=10;// for the next decimal |
tj4shee | 0:93976d4026d3 | 656 | temp = decy;//get the decimal |
tj4shee | 0:93976d4026d3 | 657 | drawNumber(temp,poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 658 | floatNumber = -floatNumber; |
tj4shee | 0:93976d4026d3 | 659 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 660 | { |
tj4shee | 0:93976d4026d3 | 661 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 662 | } |
tj4shee | 0:93976d4026d3 | 663 | decy -= temp; |
tj4shee | 0:93976d4026d3 | 664 | } |
tj4shee | 0:93976d4026d3 | 665 | f +=decimal; |
tj4shee | 0:93976d4026d3 | 666 | return f; |
tj4shee | 0:93976d4026d3 | 667 | } |
tj4shee | 0:93976d4026d3 | 668 | |
tj4shee | 0:93976d4026d3 | 669 | unsigned char TFT::drawFloat(float floatNumber,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor) |
tj4shee | 0:93976d4026d3 | 670 | { |
tj4shee | 0:93976d4026d3 | 671 | unsigned char decimal=2; |
tj4shee | 0:93976d4026d3 | 672 | unsigned int temp=0; |
tj4shee | 0:93976d4026d3 | 673 | float decy=0.0; |
tj4shee | 0:93976d4026d3 | 674 | float rounding = 0.5; |
tj4shee | 0:93976d4026d3 | 675 | unsigned char f=0; |
tj4shee | 0:93976d4026d3 | 676 | if(floatNumber<0.0) |
tj4shee | 0:93976d4026d3 | 677 | { |
tj4shee | 0:93976d4026d3 | 678 | drawChar('-',poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 679 | floatNumber = -floatNumber; |
tj4shee | 0:93976d4026d3 | 680 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 681 | { |
tj4shee | 0:93976d4026d3 | 682 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 683 | } |
tj4shee | 0:93976d4026d3 | 684 | f =1; |
tj4shee | 0:93976d4026d3 | 685 | } |
tj4shee | 0:93976d4026d3 | 686 | for (unsigned char i=0; i<decimal; ++i) |
tj4shee | 0:93976d4026d3 | 687 | { |
tj4shee | 0:93976d4026d3 | 688 | rounding /= 10.0; |
tj4shee | 0:93976d4026d3 | 689 | } |
tj4shee | 0:93976d4026d3 | 690 | floatNumber += rounding; |
tj4shee | 0:93976d4026d3 | 691 | |
tj4shee | 0:93976d4026d3 | 692 | temp = (unsigned int)floatNumber; |
tj4shee | 0:93976d4026d3 | 693 | unsigned char howlong=drawNumber(temp,poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 694 | f += howlong; |
tj4shee | 0:93976d4026d3 | 695 | if((poX+8*size*howlong) < MAX_X) |
tj4shee | 0:93976d4026d3 | 696 | { |
tj4shee | 0:93976d4026d3 | 697 | poX+=8*size*howlong; // Move cursor right |
tj4shee | 0:93976d4026d3 | 698 | } |
tj4shee | 0:93976d4026d3 | 699 | |
tj4shee | 0:93976d4026d3 | 700 | if(decimal>0) |
tj4shee | 0:93976d4026d3 | 701 | { |
tj4shee | 0:93976d4026d3 | 702 | drawChar('.',poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 703 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 704 | { |
tj4shee | 0:93976d4026d3 | 705 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 706 | } |
tj4shee | 0:93976d4026d3 | 707 | f +=1; |
tj4shee | 0:93976d4026d3 | 708 | } |
tj4shee | 0:93976d4026d3 | 709 | decy = floatNumber-temp;//decimal part, |
tj4shee | 0:93976d4026d3 | 710 | for(unsigned char i=0;i<decimal;i++)//4 |
tj4shee | 0:93976d4026d3 | 711 | { |
tj4shee | 0:93976d4026d3 | 712 | decy *=10;// for the next decimal |
tj4shee | 0:93976d4026d3 | 713 | temp = decy;//get the decimal |
tj4shee | 0:93976d4026d3 | 714 | drawNumber(temp,poX, poY, size, fgcolor); |
tj4shee | 0:93976d4026d3 | 715 | floatNumber = -floatNumber; |
tj4shee | 0:93976d4026d3 | 716 | if(poX < MAX_X) |
tj4shee | 0:93976d4026d3 | 717 | { |
tj4shee | 0:93976d4026d3 | 718 | poX+=8*size; // Move cursor right |
tj4shee | 0:93976d4026d3 | 719 | } |
tj4shee | 0:93976d4026d3 | 720 | decy -= temp; |
tj4shee | 0:93976d4026d3 | 721 | } |
tj4shee | 0:93976d4026d3 | 722 | f +=decimal; |
tj4shee | 0:93976d4026d3 | 723 | return f; |
tj4shee | 0:93976d4026d3 | 724 | } |
tj4shee | 0:93976d4026d3 | 725 | |
tj4shee | 0:93976d4026d3 | 726 | void TFT::all_pin_input(void) |
tj4shee | 0:93976d4026d3 | 727 | { |
tj4shee | 0:93976d4026d3 | 728 | //DDRD &=~ 0xfc; // 0b 1111 1100 |
tj4shee | 0:93976d4026d3 | 729 | //DDRB &=~ 0x03; // 0b 0000 0011 |
tj4shee | 0:93976d4026d3 | 730 | lcd_data.input(); |
tj4shee | 0:93976d4026d3 | 731 | } |
tj4shee | 0:93976d4026d3 | 732 | |
tj4shee | 0:93976d4026d3 | 733 | void TFT::all_pin_output(void) |
tj4shee | 0:93976d4026d3 | 734 | { |
tj4shee | 0:93976d4026d3 | 735 | //DDRD |= 0xfc; |
tj4shee | 0:93976d4026d3 | 736 | //DDRB |= 0x03; |
tj4shee | 0:93976d4026d3 | 737 | lcd_data.output(); |
tj4shee | 0:93976d4026d3 | 738 | } |
tj4shee | 0:93976d4026d3 | 739 | |
tj4shee | 0:93976d4026d3 | 740 | void TFT::all_pin_low(void) |
tj4shee | 0:93976d4026d3 | 741 | { |
tj4shee | 0:93976d4026d3 | 742 | //PORTD &=~ 0xfc; |
tj4shee | 0:93976d4026d3 | 743 | //PORTB &=~ 0x03; |
tj4shee | 0:93976d4026d3 | 744 | lcd_data.write(0); |
tj4shee | 0:93976d4026d3 | 745 | } |
tj4shee | 0:93976d4026d3 | 746 | |
tj4shee | 0:93976d4026d3 | 747 | void TFT::CS_HIGH(void) |
tj4shee | 0:93976d4026d3 | 748 | { |
tj4shee | 0:93976d4026d3 | 749 | CS_BIT.write(1); |
tj4shee | 0:93976d4026d3 | 750 | } |
tj4shee | 0:93976d4026d3 | 751 | |
tj4shee | 0:93976d4026d3 | 752 | void TFT::CS_LOW(void) |
tj4shee | 0:93976d4026d3 | 753 | { |
tj4shee | 0:93976d4026d3 | 754 | CS_BIT.write(0); |
tj4shee | 0:93976d4026d3 | 755 | } |
tj4shee | 0:93976d4026d3 | 756 | |
tj4shee | 0:93976d4026d3 | 757 | void TFT::RS_HIGH(void) |
tj4shee | 0:93976d4026d3 | 758 | { |
tj4shee | 0:93976d4026d3 | 759 | RS_BIT.write(1); |
tj4shee | 0:93976d4026d3 | 760 | } |
tj4shee | 0:93976d4026d3 | 761 | |
tj4shee | 0:93976d4026d3 | 762 | void TFT::RS_LOW(void) |
tj4shee | 0:93976d4026d3 | 763 | { |
tj4shee | 0:93976d4026d3 | 764 | RS_BIT.write(0); |
tj4shee | 0:93976d4026d3 | 765 | } |
tj4shee | 0:93976d4026d3 | 766 | |
tj4shee | 0:93976d4026d3 | 767 | void TFT::WR_HIGH(void) |
tj4shee | 0:93976d4026d3 | 768 | { |
tj4shee | 0:93976d4026d3 | 769 | WR_BIT.write(1); |
tj4shee | 0:93976d4026d3 | 770 | } |
tj4shee | 0:93976d4026d3 | 771 | |
tj4shee | 0:93976d4026d3 | 772 | void TFT::WR_LOW(void) |
tj4shee | 0:93976d4026d3 | 773 | { |
tj4shee | 0:93976d4026d3 | 774 | WR_BIT.write(0); |
tj4shee | 0:93976d4026d3 | 775 | } |
tj4shee | 0:93976d4026d3 | 776 | |
tj4shee | 0:93976d4026d3 | 777 | void TFT::WR_RISING(void) |
tj4shee | 0:93976d4026d3 | 778 | { |
tj4shee | 0:93976d4026d3 | 779 | WR_HIGH(); |
tj4shee | 0:93976d4026d3 | 780 | WR_LOW(); |
tj4shee | 0:93976d4026d3 | 781 | } |
tj4shee | 0:93976d4026d3 | 782 | |
tj4shee | 0:93976d4026d3 | 783 | void TFT::RD_HIGH(void) |
tj4shee | 0:93976d4026d3 | 784 | { |
tj4shee | 0:93976d4026d3 | 785 | RD_BIT.write(1); |
tj4shee | 0:93976d4026d3 | 786 | } |
tj4shee | 0:93976d4026d3 | 787 | |
tj4shee | 0:93976d4026d3 | 788 | void TFT::RD_LOW(void) |
tj4shee | 0:93976d4026d3 | 789 | { |
tj4shee | 0:93976d4026d3 | 790 | RD_BIT.write(0); |
tj4shee | 0:93976d4026d3 | 791 | } |
tj4shee | 0:93976d4026d3 | 792 | |
tj4shee | 0:93976d4026d3 | 793 | void TFT::RD_RISING(void) |
tj4shee | 0:93976d4026d3 | 794 | { |
tj4shee | 0:93976d4026d3 | 795 | RD_HIGH(); RD_LOW(); |
tj4shee | 0:93976d4026d3 | 796 | } |
tj4shee | 0:93976d4026d3 | 797 | |
tj4shee | 0:93976d4026d3 | 798 | TFT Tft=TFT(); |