![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Allow user to connect multiple screen.
4DGL-uLCD-SE/uLCD_4DGL_main.cpp@0:052d0f82433e, 2015-03-11 (annotated)
- Committer:
- Ratchapong
- Date:
- Wed Mar 11 05:00:37 2015 +0000
- Revision:
- 0:052d0f82433e
Working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ratchapong | 0:052d0f82433e | 1 | // |
Ratchapong | 0:052d0f82433e | 2 | // uLCD_4DGL is a class to drive 4D Systems uLCD 144 G2 |
Ratchapong | 0:052d0f82433e | 3 | // |
Ratchapong | 0:052d0f82433e | 4 | // Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr> |
Ratchapong | 0:052d0f82433e | 5 | // Modifed for Goldelox processor <2013> Jim Hamblen |
Ratchapong | 0:052d0f82433e | 6 | // |
Ratchapong | 0:052d0f82433e | 7 | // uLCD_4DGL is free software: you can redistribute it and/or modify |
Ratchapong | 0:052d0f82433e | 8 | // it under the terms of the GNU General Public License as published by |
Ratchapong | 0:052d0f82433e | 9 | // the Free Software Foundation, either version 3 of the License, or |
Ratchapong | 0:052d0f82433e | 10 | // (at your option) any later version. |
Ratchapong | 0:052d0f82433e | 11 | // |
Ratchapong | 0:052d0f82433e | 12 | // uLCD_4DGL is distributed in the hope that it will be useful, |
Ratchapong | 0:052d0f82433e | 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
Ratchapong | 0:052d0f82433e | 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Ratchapong | 0:052d0f82433e | 15 | // GNU General Public License for more details. |
Ratchapong | 0:052d0f82433e | 16 | // |
Ratchapong | 0:052d0f82433e | 17 | // You should have received a copy of the GNU General Public License |
Ratchapong | 0:052d0f82433e | 18 | // along with uLCD_4DGL. If not, see <http://www.gnu.org/licenses/>. |
Ratchapong | 0:052d0f82433e | 19 | |
Ratchapong | 0:052d0f82433e | 20 | #include "mbed.h" |
Ratchapong | 0:052d0f82433e | 21 | #include "uLCD_4DGL.h" |
Ratchapong | 0:052d0f82433e | 22 | |
Ratchapong | 0:052d0f82433e | 23 | #define ARRAY_SIZE(X) sizeof(X)/sizeof(X[0]) |
Ratchapong | 0:052d0f82433e | 24 | |
Ratchapong | 0:052d0f82433e | 25 | //Serial pc(USBTX,USBRX); |
Ratchapong | 0:052d0f82433e | 26 | |
Ratchapong | 0:052d0f82433e | 27 | |
Ratchapong | 0:052d0f82433e | 28 | //****************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 29 | uLCD_4DGL :: uLCD_4DGL(PinName tx, PinName rx, PinName rst) : _cmd(tx, rx), |
Ratchapong | 0:052d0f82433e | 30 | _rst(rst) |
Ratchapong | 0:052d0f82433e | 31 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 32 | ,pc(USBTX, USBRX) |
Ratchapong | 0:052d0f82433e | 33 | #endif // DEBUGMODE |
Ratchapong | 0:052d0f82433e | 34 | { |
Ratchapong | 0:052d0f82433e | 35 | // Constructor |
Ratchapong | 0:052d0f82433e | 36 | _cmd.baud(9600); |
Ratchapong | 0:052d0f82433e | 37 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 38 | pc.baud(115200); |
Ratchapong | 0:052d0f82433e | 39 | |
Ratchapong | 0:052d0f82433e | 40 | pc.printf("\n\n\n"); |
Ratchapong | 0:052d0f82433e | 41 | pc.printf("*********************\n"); |
Ratchapong | 0:052d0f82433e | 42 | pc.printf("uLCD_4DGL CONSTRUCTOR\n"); |
Ratchapong | 0:052d0f82433e | 43 | pc.printf("*********************\n"); |
Ratchapong | 0:052d0f82433e | 44 | #endif |
Ratchapong | 0:052d0f82433e | 45 | |
Ratchapong | 0:052d0f82433e | 46 | _rst = 1; // put RESET pin to high to start TFT screen |
Ratchapong | 0:052d0f82433e | 47 | reset(); |
Ratchapong | 0:052d0f82433e | 48 | cls(); // clear screen |
Ratchapong | 0:052d0f82433e | 49 | current_col = 0; // initial cursor col |
Ratchapong | 0:052d0f82433e | 50 | current_row = 0; // initial cursor row |
Ratchapong | 0:052d0f82433e | 51 | current_color = WHITE; // initial text color |
Ratchapong | 0:052d0f82433e | 52 | current_orientation = IS_PORTRAIT; // initial screen orientation |
Ratchapong | 0:052d0f82433e | 53 | current_hf = 1; |
Ratchapong | 0:052d0f82433e | 54 | current_wf = 1; |
Ratchapong | 0:052d0f82433e | 55 | set_font(FONT_7X8); // initial font |
Ratchapong | 0:052d0f82433e | 56 | // text_mode(OPAQUE); // initial texr mode |
Ratchapong | 0:052d0f82433e | 57 | } |
Ratchapong | 0:052d0f82433e | 58 | |
Ratchapong | 0:052d0f82433e | 59 | //****************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 60 | void uLCD_4DGL :: writeBYTE(char c) // send a BYTE command to screen |
Ratchapong | 0:052d0f82433e | 61 | { |
Ratchapong | 0:052d0f82433e | 62 | |
Ratchapong | 0:052d0f82433e | 63 | _cmd.putc(c); |
Ratchapong | 0:052d0f82433e | 64 | wait_us(500); //mbed is too fast for LCD at high baud rates in some long commands |
Ratchapong | 0:052d0f82433e | 65 | |
Ratchapong | 0:052d0f82433e | 66 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 67 | pc.printf(" Char sent : 0x%02X\n",c); |
Ratchapong | 0:052d0f82433e | 68 | #endif |
Ratchapong | 0:052d0f82433e | 69 | |
Ratchapong | 0:052d0f82433e | 70 | } |
Ratchapong | 0:052d0f82433e | 71 | |
Ratchapong | 0:052d0f82433e | 72 | //****************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 73 | void uLCD_4DGL :: writeBYTEfast(char c) // send a BYTE command to screen |
Ratchapong | 0:052d0f82433e | 74 | { |
Ratchapong | 0:052d0f82433e | 75 | |
Ratchapong | 0:052d0f82433e | 76 | _cmd.putc(c); |
Ratchapong | 0:052d0f82433e | 77 | //wait_ms(0.0); //mbed is too fast for LCD at high baud rates - but not in short commands |
Ratchapong | 0:052d0f82433e | 78 | |
Ratchapong | 0:052d0f82433e | 79 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 80 | pc.printf(" Char sent : 0x%02X\n",c); |
Ratchapong | 0:052d0f82433e | 81 | #endif |
Ratchapong | 0:052d0f82433e | 82 | |
Ratchapong | 0:052d0f82433e | 83 | } |
Ratchapong | 0:052d0f82433e | 84 | //****************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 85 | void uLCD_4DGL :: freeBUFFER(void) // Clear serial buffer before writing command |
Ratchapong | 0:052d0f82433e | 86 | { |
Ratchapong | 0:052d0f82433e | 87 | |
Ratchapong | 0:052d0f82433e | 88 | while (_cmd.readable()) _cmd.getc(); // clear buffer garbage |
Ratchapong | 0:052d0f82433e | 89 | } |
Ratchapong | 0:052d0f82433e | 90 | |
Ratchapong | 0:052d0f82433e | 91 | //****************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 92 | int uLCD_4DGL :: writeCOMMAND(char *command, int number) // send several BYTES making a command and return an answer |
Ratchapong | 0:052d0f82433e | 93 | { |
Ratchapong | 0:052d0f82433e | 94 | |
Ratchapong | 0:052d0f82433e | 95 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 96 | pc.printf("\n"); |
Ratchapong | 0:052d0f82433e | 97 | pc.printf("New COMMAND : 0x%02X\n", command[0]); |
Ratchapong | 0:052d0f82433e | 98 | #endif |
Ratchapong | 0:052d0f82433e | 99 | int i, resp = 0; |
Ratchapong | 0:052d0f82433e | 100 | freeBUFFER(); |
Ratchapong | 0:052d0f82433e | 101 | writeBYTE(0xFF); |
Ratchapong | 0:052d0f82433e | 102 | for (i = 0; i < number; i++) { |
Ratchapong | 0:052d0f82433e | 103 | if (i<16) |
Ratchapong | 0:052d0f82433e | 104 | writeBYTEfast(command[i]); // send command to serial port |
Ratchapong | 0:052d0f82433e | 105 | else |
Ratchapong | 0:052d0f82433e | 106 | writeBYTE(command[i]); // send command to serial port but slower |
Ratchapong | 0:052d0f82433e | 107 | } |
Ratchapong | 0:052d0f82433e | 108 | while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer |
Ratchapong | 0:052d0f82433e | 109 | if (_cmd.readable()) resp = _cmd.getc(); // read response if any |
Ratchapong | 0:052d0f82433e | 110 | switch (resp) { |
Ratchapong | 0:052d0f82433e | 111 | case ACK : // if OK return 1 |
Ratchapong | 0:052d0f82433e | 112 | resp = 1; |
Ratchapong | 0:052d0f82433e | 113 | break; |
Ratchapong | 0:052d0f82433e | 114 | case NAK : // if NOK return -1 |
Ratchapong | 0:052d0f82433e | 115 | resp = -1; |
Ratchapong | 0:052d0f82433e | 116 | break; |
Ratchapong | 0:052d0f82433e | 117 | default : |
Ratchapong | 0:052d0f82433e | 118 | resp = 0; // else return 0 |
Ratchapong | 0:052d0f82433e | 119 | break; |
Ratchapong | 0:052d0f82433e | 120 | } |
Ratchapong | 0:052d0f82433e | 121 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 122 | pc.printf(" Answer received : %d\n",resp); |
Ratchapong | 0:052d0f82433e | 123 | #endif |
Ratchapong | 0:052d0f82433e | 124 | |
Ratchapong | 0:052d0f82433e | 125 | return resp; |
Ratchapong | 0:052d0f82433e | 126 | } |
Ratchapong | 0:052d0f82433e | 127 | |
Ratchapong | 0:052d0f82433e | 128 | //************************************************************************** |
Ratchapong | 0:052d0f82433e | 129 | void uLCD_4DGL :: reset() // Reset Screen |
Ratchapong | 0:052d0f82433e | 130 | { |
Ratchapong | 0:052d0f82433e | 131 | wait_ms(5); |
Ratchapong | 0:052d0f82433e | 132 | _rst = 0; // put RESET pin to low |
Ratchapong | 0:052d0f82433e | 133 | wait_ms(5); // wait a few milliseconds for command reception |
Ratchapong | 0:052d0f82433e | 134 | _rst = 1; // put RESET back to high |
Ratchapong | 0:052d0f82433e | 135 | wait(3); // wait 3s for screen to restart |
Ratchapong | 0:052d0f82433e | 136 | |
Ratchapong | 0:052d0f82433e | 137 | freeBUFFER(); // clean buffer from possible garbage |
Ratchapong | 0:052d0f82433e | 138 | } |
Ratchapong | 0:052d0f82433e | 139 | //****************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 140 | int uLCD_4DGL :: writeCOMMANDnull(char *command, int number) // send several BYTES making a command and return an answer |
Ratchapong | 0:052d0f82433e | 141 | { |
Ratchapong | 0:052d0f82433e | 142 | |
Ratchapong | 0:052d0f82433e | 143 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 144 | pc.printf("\n"); |
Ratchapong | 0:052d0f82433e | 145 | pc.printf("New COMMAND : 0x%02X\n", command[0]); |
Ratchapong | 0:052d0f82433e | 146 | #endif |
Ratchapong | 0:052d0f82433e | 147 | int i, resp = 0; |
Ratchapong | 0:052d0f82433e | 148 | freeBUFFER(); |
Ratchapong | 0:052d0f82433e | 149 | writeBYTE(0x00); //command has a null prefix byte |
Ratchapong | 0:052d0f82433e | 150 | for (i = 0; i < number; i++) { |
Ratchapong | 0:052d0f82433e | 151 | if (i<16) //don't overflow LCD UART buffer |
Ratchapong | 0:052d0f82433e | 152 | writeBYTEfast(command[i]); // send command to serial port |
Ratchapong | 0:052d0f82433e | 153 | else |
Ratchapong | 0:052d0f82433e | 154 | writeBYTE(command[i]); // send command to serial port with delay |
Ratchapong | 0:052d0f82433e | 155 | } |
Ratchapong | 0:052d0f82433e | 156 | while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer |
Ratchapong | 0:052d0f82433e | 157 | if (_cmd.readable()) resp = _cmd.getc(); // read response if any |
Ratchapong | 0:052d0f82433e | 158 | switch (resp) { |
Ratchapong | 0:052d0f82433e | 159 | case ACK : // if OK return 1 |
Ratchapong | 0:052d0f82433e | 160 | resp = 1; |
Ratchapong | 0:052d0f82433e | 161 | break; |
Ratchapong | 0:052d0f82433e | 162 | case NAK : // if NOK return -1 |
Ratchapong | 0:052d0f82433e | 163 | resp = -1; |
Ratchapong | 0:052d0f82433e | 164 | break; |
Ratchapong | 0:052d0f82433e | 165 | default : |
Ratchapong | 0:052d0f82433e | 166 | resp = 0; // else return 0 |
Ratchapong | 0:052d0f82433e | 167 | break; |
Ratchapong | 0:052d0f82433e | 168 | } |
Ratchapong | 0:052d0f82433e | 169 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 170 | pc.printf(" Answer received : %d\n",resp); |
Ratchapong | 0:052d0f82433e | 171 | #endif |
Ratchapong | 0:052d0f82433e | 172 | |
Ratchapong | 0:052d0f82433e | 173 | return resp; |
Ratchapong | 0:052d0f82433e | 174 | } |
Ratchapong | 0:052d0f82433e | 175 | |
Ratchapong | 0:052d0f82433e | 176 | //************************************************************************** |
Ratchapong | 0:052d0f82433e | 177 | void uLCD_4DGL :: cls() // clear screen |
Ratchapong | 0:052d0f82433e | 178 | { |
Ratchapong | 0:052d0f82433e | 179 | char command[1] = ""; |
Ratchapong | 0:052d0f82433e | 180 | |
Ratchapong | 0:052d0f82433e | 181 | command[0] = CLS; |
Ratchapong | 0:052d0f82433e | 182 | writeCOMMAND(command, 1); |
Ratchapong | 0:052d0f82433e | 183 | current_row=0; |
Ratchapong | 0:052d0f82433e | 184 | current_col=0; |
Ratchapong | 0:052d0f82433e | 185 | current_hf = 1; |
Ratchapong | 0:052d0f82433e | 186 | current_wf = 1; |
Ratchapong | 0:052d0f82433e | 187 | set_font(FONT_7X8); // initial font |
Ratchapong | 0:052d0f82433e | 188 | } |
Ratchapong | 0:052d0f82433e | 189 | |
Ratchapong | 0:052d0f82433e | 190 | //************************************************************************** |
Ratchapong | 0:052d0f82433e | 191 | int uLCD_4DGL :: version() // get API version |
Ratchapong | 0:052d0f82433e | 192 | { |
Ratchapong | 0:052d0f82433e | 193 | |
Ratchapong | 0:052d0f82433e | 194 | char command[2] = ""; |
Ratchapong | 0:052d0f82433e | 195 | command[0] = '\x00'; |
Ratchapong | 0:052d0f82433e | 196 | command[1] = VERSION; |
Ratchapong | 0:052d0f82433e | 197 | return readVERSION(command, 2); |
Ratchapong | 0:052d0f82433e | 198 | } |
Ratchapong | 0:052d0f82433e | 199 | |
Ratchapong | 0:052d0f82433e | 200 | //************************************************************************** |
Ratchapong | 0:052d0f82433e | 201 | void uLCD_4DGL :: baudrate(int speed) // set screen baud rate |
Ratchapong | 0:052d0f82433e | 202 | { |
Ratchapong | 0:052d0f82433e | 203 | char command[3]= ""; |
Ratchapong | 0:052d0f82433e | 204 | writeBYTE(0x00); |
Ratchapong | 0:052d0f82433e | 205 | command[0] = BAUDRATE; |
Ratchapong | 0:052d0f82433e | 206 | command[1] = 0; |
Ratchapong | 0:052d0f82433e | 207 | int newbaud = BAUD_9600; |
Ratchapong | 0:052d0f82433e | 208 | switch (speed) { |
Ratchapong | 0:052d0f82433e | 209 | case 110 : |
Ratchapong | 0:052d0f82433e | 210 | newbaud = BAUD_110; |
Ratchapong | 0:052d0f82433e | 211 | break; |
Ratchapong | 0:052d0f82433e | 212 | case 300 : |
Ratchapong | 0:052d0f82433e | 213 | newbaud = BAUD_300; |
Ratchapong | 0:052d0f82433e | 214 | break; |
Ratchapong | 0:052d0f82433e | 215 | case 600 : |
Ratchapong | 0:052d0f82433e | 216 | newbaud = BAUD_600; |
Ratchapong | 0:052d0f82433e | 217 | break; |
Ratchapong | 0:052d0f82433e | 218 | case 1200 : |
Ratchapong | 0:052d0f82433e | 219 | newbaud = BAUD_1200; |
Ratchapong | 0:052d0f82433e | 220 | break; |
Ratchapong | 0:052d0f82433e | 221 | case 2400 : |
Ratchapong | 0:052d0f82433e | 222 | newbaud = BAUD_2400; |
Ratchapong | 0:052d0f82433e | 223 | break; |
Ratchapong | 0:052d0f82433e | 224 | case 4800 : |
Ratchapong | 0:052d0f82433e | 225 | newbaud = BAUD_4800; |
Ratchapong | 0:052d0f82433e | 226 | break; |
Ratchapong | 0:052d0f82433e | 227 | case 9600 : |
Ratchapong | 0:052d0f82433e | 228 | newbaud = BAUD_9600; |
Ratchapong | 0:052d0f82433e | 229 | break; |
Ratchapong | 0:052d0f82433e | 230 | case 14400 : |
Ratchapong | 0:052d0f82433e | 231 | newbaud = BAUD_14400; |
Ratchapong | 0:052d0f82433e | 232 | break; |
Ratchapong | 0:052d0f82433e | 233 | case 19200 : |
Ratchapong | 0:052d0f82433e | 234 | newbaud = BAUD_19200; |
Ratchapong | 0:052d0f82433e | 235 | break; |
Ratchapong | 0:052d0f82433e | 236 | case 31250 : |
Ratchapong | 0:052d0f82433e | 237 | newbaud = BAUD_31250; |
Ratchapong | 0:052d0f82433e | 238 | break; |
Ratchapong | 0:052d0f82433e | 239 | case 38400 : |
Ratchapong | 0:052d0f82433e | 240 | newbaud = BAUD_38400; |
Ratchapong | 0:052d0f82433e | 241 | break; |
Ratchapong | 0:052d0f82433e | 242 | case 56000 : |
Ratchapong | 0:052d0f82433e | 243 | newbaud = BAUD_56000; |
Ratchapong | 0:052d0f82433e | 244 | break; |
Ratchapong | 0:052d0f82433e | 245 | case 57600 : |
Ratchapong | 0:052d0f82433e | 246 | newbaud = BAUD_57600; |
Ratchapong | 0:052d0f82433e | 247 | break; |
Ratchapong | 0:052d0f82433e | 248 | case 115200 : |
Ratchapong | 0:052d0f82433e | 249 | newbaud = BAUD_115200; |
Ratchapong | 0:052d0f82433e | 250 | break; |
Ratchapong | 0:052d0f82433e | 251 | case 128000 : |
Ratchapong | 0:052d0f82433e | 252 | newbaud = BAUD_128000; |
Ratchapong | 0:052d0f82433e | 253 | break; |
Ratchapong | 0:052d0f82433e | 254 | case 256000 : |
Ratchapong | 0:052d0f82433e | 255 | newbaud = BAUD_256000; |
Ratchapong | 0:052d0f82433e | 256 | break; |
Ratchapong | 0:052d0f82433e | 257 | case 300000 : |
Ratchapong | 0:052d0f82433e | 258 | newbaud = BAUD_300000; |
Ratchapong | 0:052d0f82433e | 259 | speed = 272727; |
Ratchapong | 0:052d0f82433e | 260 | break; |
Ratchapong | 0:052d0f82433e | 261 | case 375000 : |
Ratchapong | 0:052d0f82433e | 262 | newbaud = BAUD_375000; |
Ratchapong | 0:052d0f82433e | 263 | speed = 333333; |
Ratchapong | 0:052d0f82433e | 264 | break; |
Ratchapong | 0:052d0f82433e | 265 | case 500000 : |
Ratchapong | 0:052d0f82433e | 266 | newbaud = BAUD_500000; |
Ratchapong | 0:052d0f82433e | 267 | speed = 428571; |
Ratchapong | 0:052d0f82433e | 268 | break; |
Ratchapong | 0:052d0f82433e | 269 | case 600000 : |
Ratchapong | 0:052d0f82433e | 270 | newbaud = BAUD_600000; |
Ratchapong | 0:052d0f82433e | 271 | break; |
Ratchapong | 0:052d0f82433e | 272 | case 750000 : //rates over 600000 are not documented, but seem to work |
Ratchapong | 0:052d0f82433e | 273 | newbaud = BAUD_750000; |
Ratchapong | 0:052d0f82433e | 274 | break; |
Ratchapong | 0:052d0f82433e | 275 | case 1000000 : |
Ratchapong | 0:052d0f82433e | 276 | newbaud = BAUD_1000000; |
Ratchapong | 0:052d0f82433e | 277 | break; |
Ratchapong | 0:052d0f82433e | 278 | case 1500000 : |
Ratchapong | 0:052d0f82433e | 279 | newbaud = BAUD_1500000; |
Ratchapong | 0:052d0f82433e | 280 | break; |
Ratchapong | 0:052d0f82433e | 281 | case 3000000 : |
Ratchapong | 0:052d0f82433e | 282 | newbaud = BAUD_3000000; |
Ratchapong | 0:052d0f82433e | 283 | break; |
Ratchapong | 0:052d0f82433e | 284 | default : |
Ratchapong | 0:052d0f82433e | 285 | newbaud = BAUD_9600; |
Ratchapong | 0:052d0f82433e | 286 | speed = 9600; |
Ratchapong | 0:052d0f82433e | 287 | break; |
Ratchapong | 0:052d0f82433e | 288 | } |
Ratchapong | 0:052d0f82433e | 289 | |
Ratchapong | 0:052d0f82433e | 290 | int i, resp = 0; |
Ratchapong | 0:052d0f82433e | 291 | |
Ratchapong | 0:052d0f82433e | 292 | freeBUFFER(); |
Ratchapong | 0:052d0f82433e | 293 | command[1] = char(newbaud >>8); |
Ratchapong | 0:052d0f82433e | 294 | command[2] = char(newbaud % 256); |
Ratchapong | 0:052d0f82433e | 295 | wait_ms(1); |
Ratchapong | 0:052d0f82433e | 296 | for (i = 0; i <3; i++) writeBYTEfast(command[i]); // send command to serial port |
Ratchapong | 0:052d0f82433e | 297 | for (i = 0; i<10; i++) wait_ms(1); |
Ratchapong | 0:052d0f82433e | 298 | //dont change baud until all characters get sent out |
Ratchapong | 0:052d0f82433e | 299 | _cmd.baud(speed); // set mbed to same speed |
Ratchapong | 0:052d0f82433e | 300 | i=0; |
Ratchapong | 0:052d0f82433e | 301 | while ((!_cmd.readable()) && (i<25000)) { |
Ratchapong | 0:052d0f82433e | 302 | wait_ms(TEMPO); // wait for screen answer - comes 100ms after change |
Ratchapong | 0:052d0f82433e | 303 | i++; //timeout if ack character missed by baud change |
Ratchapong | 0:052d0f82433e | 304 | } |
Ratchapong | 0:052d0f82433e | 305 | if (_cmd.readable()) resp = _cmd.getc(); // read response if any |
Ratchapong | 0:052d0f82433e | 306 | switch (resp) { |
Ratchapong | 0:052d0f82433e | 307 | case ACK : // if OK return 1 |
Ratchapong | 0:052d0f82433e | 308 | resp = 1; |
Ratchapong | 0:052d0f82433e | 309 | break; |
Ratchapong | 0:052d0f82433e | 310 | case NAK : // if NOK return -1 |
Ratchapong | 0:052d0f82433e | 311 | resp = -1; |
Ratchapong | 0:052d0f82433e | 312 | break; |
Ratchapong | 0:052d0f82433e | 313 | default : |
Ratchapong | 0:052d0f82433e | 314 | resp = 0; // else return 0 |
Ratchapong | 0:052d0f82433e | 315 | break; |
Ratchapong | 0:052d0f82433e | 316 | } |
Ratchapong | 0:052d0f82433e | 317 | } |
Ratchapong | 0:052d0f82433e | 318 | |
Ratchapong | 0:052d0f82433e | 319 | //****************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 320 | int uLCD_4DGL :: readVERSION(char *command, int number) // read screen info and populate data |
Ratchapong | 0:052d0f82433e | 321 | { |
Ratchapong | 0:052d0f82433e | 322 | |
Ratchapong | 0:052d0f82433e | 323 | int i, temp = 0, resp = 0; |
Ratchapong | 0:052d0f82433e | 324 | char response[5] = ""; |
Ratchapong | 0:052d0f82433e | 325 | |
Ratchapong | 0:052d0f82433e | 326 | freeBUFFER(); |
Ratchapong | 0:052d0f82433e | 327 | |
Ratchapong | 0:052d0f82433e | 328 | for (i = 0; i < number; i++) writeBYTE(command[i]); // send all chars to serial port |
Ratchapong | 0:052d0f82433e | 329 | |
Ratchapong | 0:052d0f82433e | 330 | while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer |
Ratchapong | 0:052d0f82433e | 331 | |
Ratchapong | 0:052d0f82433e | 332 | while (_cmd.readable() && resp < ARRAY_SIZE(response)) { |
Ratchapong | 0:052d0f82433e | 333 | temp = _cmd.getc(); |
Ratchapong | 0:052d0f82433e | 334 | response[resp++] = (char)temp; |
Ratchapong | 0:052d0f82433e | 335 | } |
Ratchapong | 0:052d0f82433e | 336 | switch (resp) { |
Ratchapong | 0:052d0f82433e | 337 | case 2 : // if OK populate data and return 1 |
Ratchapong | 0:052d0f82433e | 338 | revision = response[0]<<8 + response[1]; |
Ratchapong | 0:052d0f82433e | 339 | resp = 1; |
Ratchapong | 0:052d0f82433e | 340 | break; |
Ratchapong | 0:052d0f82433e | 341 | default : |
Ratchapong | 0:052d0f82433e | 342 | resp = 0; // else return 0 |
Ratchapong | 0:052d0f82433e | 343 | break; |
Ratchapong | 0:052d0f82433e | 344 | } |
Ratchapong | 0:052d0f82433e | 345 | return resp; |
Ratchapong | 0:052d0f82433e | 346 | } |
Ratchapong | 0:052d0f82433e | 347 | |
Ratchapong | 0:052d0f82433e | 348 | //**************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 349 | void uLCD_4DGL :: background_color(int color) // set screen background color |
Ratchapong | 0:052d0f82433e | 350 | { |
Ratchapong | 0:052d0f82433e | 351 | char command[3]= ""; // input color is in 24bits like 0xRRGGBB |
Ratchapong | 0:052d0f82433e | 352 | |
Ratchapong | 0:052d0f82433e | 353 | command[0] = BCKGDCOLOR; |
Ratchapong | 0:052d0f82433e | 354 | |
Ratchapong | 0:052d0f82433e | 355 | int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits |
Ratchapong | 0:052d0f82433e | 356 | int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits |
Ratchapong | 0:052d0f82433e | 357 | int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits |
Ratchapong | 0:052d0f82433e | 358 | |
Ratchapong | 0:052d0f82433e | 359 | command[1] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color |
Ratchapong | 0:052d0f82433e | 360 | command[2] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color |
Ratchapong | 0:052d0f82433e | 361 | |
Ratchapong | 0:052d0f82433e | 362 | writeCOMMAND(command, 3); |
Ratchapong | 0:052d0f82433e | 363 | } |
Ratchapong | 0:052d0f82433e | 364 | |
Ratchapong | 0:052d0f82433e | 365 | //**************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 366 | void uLCD_4DGL :: textbackground_color(int color) // set screen background color |
Ratchapong | 0:052d0f82433e | 367 | { |
Ratchapong | 0:052d0f82433e | 368 | char command[3]= ""; // input color is in 24bits like 0xRRGGBB |
Ratchapong | 0:052d0f82433e | 369 | |
Ratchapong | 0:052d0f82433e | 370 | command[0] = TXTBCKGDCOLOR; |
Ratchapong | 0:052d0f82433e | 371 | |
Ratchapong | 0:052d0f82433e | 372 | int red5 = (color >> (16 + 3)) & 0x1F; // get red on 5 bits |
Ratchapong | 0:052d0f82433e | 373 | int green6 = (color >> (8 + 2)) & 0x3F; // get green on 6 bits |
Ratchapong | 0:052d0f82433e | 374 | int blue5 = (color >> (0 + 3)) & 0x1F; // get blue on 5 bits |
Ratchapong | 0:052d0f82433e | 375 | |
Ratchapong | 0:052d0f82433e | 376 | command[1] = ((red5 << 3) + (green6 >> 3)) & 0xFF; // first part of 16 bits color |
Ratchapong | 0:052d0f82433e | 377 | command[2] = ((green6 << 5) + (blue5 >> 0)) & 0xFF; // second part of 16 bits color |
Ratchapong | 0:052d0f82433e | 378 | |
Ratchapong | 0:052d0f82433e | 379 | writeCOMMAND(command, 3); |
Ratchapong | 0:052d0f82433e | 380 | } |
Ratchapong | 0:052d0f82433e | 381 | |
Ratchapong | 0:052d0f82433e | 382 | //**************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 383 | void uLCD_4DGL :: display_control(char mode) // set screen mode to value |
Ratchapong | 0:052d0f82433e | 384 | { |
Ratchapong | 0:052d0f82433e | 385 | char command[3]= ""; |
Ratchapong | 0:052d0f82433e | 386 | |
Ratchapong | 0:052d0f82433e | 387 | command[0] = DISPCONTROL; |
Ratchapong | 0:052d0f82433e | 388 | command[1] = 0; |
Ratchapong | 0:052d0f82433e | 389 | command[2] = mode; |
Ratchapong | 0:052d0f82433e | 390 | |
Ratchapong | 0:052d0f82433e | 391 | if (mode == ORIENTATION) { |
Ratchapong | 0:052d0f82433e | 392 | switch (mode) { |
Ratchapong | 0:052d0f82433e | 393 | case LANDSCAPE : |
Ratchapong | 0:052d0f82433e | 394 | current_orientation = IS_LANDSCAPE; |
Ratchapong | 0:052d0f82433e | 395 | break; |
Ratchapong | 0:052d0f82433e | 396 | case LANDSCAPE_R : |
Ratchapong | 0:052d0f82433e | 397 | current_orientation = IS_LANDSCAPE; |
Ratchapong | 0:052d0f82433e | 398 | break; |
Ratchapong | 0:052d0f82433e | 399 | case PORTRAIT : |
Ratchapong | 0:052d0f82433e | 400 | current_orientation = IS_PORTRAIT; |
Ratchapong | 0:052d0f82433e | 401 | break; |
Ratchapong | 0:052d0f82433e | 402 | case PORTRAIT_R : |
Ratchapong | 0:052d0f82433e | 403 | current_orientation = IS_PORTRAIT; |
Ratchapong | 0:052d0f82433e | 404 | break; |
Ratchapong | 0:052d0f82433e | 405 | } |
Ratchapong | 0:052d0f82433e | 406 | } |
Ratchapong | 0:052d0f82433e | 407 | writeCOMMAND(command, 3); |
Ratchapong | 0:052d0f82433e | 408 | set_font(current_font); |
Ratchapong | 0:052d0f82433e | 409 | } |
Ratchapong | 0:052d0f82433e | 410 | //**************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 411 | void uLCD_4DGL :: display_power(char mode) // set screen mode to value |
Ratchapong | 0:052d0f82433e | 412 | { |
Ratchapong | 0:052d0f82433e | 413 | char command[3]= ""; |
Ratchapong | 0:052d0f82433e | 414 | |
Ratchapong | 0:052d0f82433e | 415 | command[0] = DISPPOWER; |
Ratchapong | 0:052d0f82433e | 416 | command[1] = 0; |
Ratchapong | 0:052d0f82433e | 417 | command[2] = mode; |
Ratchapong | 0:052d0f82433e | 418 | writeCOMMAND(command, 3); |
Ratchapong | 0:052d0f82433e | 419 | } |
Ratchapong | 0:052d0f82433e | 420 | //**************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 421 | void uLCD_4DGL :: set_volume(char value) // set sound volume to value |
Ratchapong | 0:052d0f82433e | 422 | { |
Ratchapong | 0:052d0f82433e | 423 | char command[2]= ""; |
Ratchapong | 0:052d0f82433e | 424 | |
Ratchapong | 0:052d0f82433e | 425 | command[0] = SETVOLUME; |
Ratchapong | 0:052d0f82433e | 426 | command[1] = value; |
Ratchapong | 0:052d0f82433e | 427 | |
Ratchapong | 0:052d0f82433e | 428 | writeCOMMAND(command, 2); |
Ratchapong | 0:052d0f82433e | 429 | } |
Ratchapong | 0:052d0f82433e | 430 | |
Ratchapong | 0:052d0f82433e | 431 | |
Ratchapong | 0:052d0f82433e | 432 | //****************************************************************************************************** |
Ratchapong | 0:052d0f82433e | 433 | int uLCD_4DGL :: getSTATUS(char *command, int number) // read screen info and populate data |
Ratchapong | 0:052d0f82433e | 434 | { |
Ratchapong | 0:052d0f82433e | 435 | |
Ratchapong | 0:052d0f82433e | 436 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 437 | pc.printf("\n"); |
Ratchapong | 0:052d0f82433e | 438 | pc.printf("New COMMAND : 0x%02X\n", command[0]); |
Ratchapong | 0:052d0f82433e | 439 | #endif |
Ratchapong | 0:052d0f82433e | 440 | |
Ratchapong | 0:052d0f82433e | 441 | int i, temp = 0, resp = 0; |
Ratchapong | 0:052d0f82433e | 442 | char response[5] = ""; |
Ratchapong | 0:052d0f82433e | 443 | |
Ratchapong | 0:052d0f82433e | 444 | freeBUFFER(); |
Ratchapong | 0:052d0f82433e | 445 | |
Ratchapong | 0:052d0f82433e | 446 | for (i = 0; i < number; i++) writeBYTE(command[i]); // send all chars to serial port |
Ratchapong | 0:052d0f82433e | 447 | |
Ratchapong | 0:052d0f82433e | 448 | while (!_cmd.readable()) wait_ms(TEMPO); // wait for screen answer |
Ratchapong | 0:052d0f82433e | 449 | |
Ratchapong | 0:052d0f82433e | 450 | while (_cmd.readable() && resp < ARRAY_SIZE(response)) { |
Ratchapong | 0:052d0f82433e | 451 | temp = _cmd.getc(); |
Ratchapong | 0:052d0f82433e | 452 | response[resp++] = (char)temp; |
Ratchapong | 0:052d0f82433e | 453 | } |
Ratchapong | 0:052d0f82433e | 454 | switch (resp) { |
Ratchapong | 0:052d0f82433e | 455 | case 4 : |
Ratchapong | 0:052d0f82433e | 456 | resp = (int)response[1]; // if OK populate data |
Ratchapong | 0:052d0f82433e | 457 | break; |
Ratchapong | 0:052d0f82433e | 458 | default : |
Ratchapong | 0:052d0f82433e | 459 | resp = -1; // else return 0 |
Ratchapong | 0:052d0f82433e | 460 | break; |
Ratchapong | 0:052d0f82433e | 461 | } |
Ratchapong | 0:052d0f82433e | 462 | |
Ratchapong | 0:052d0f82433e | 463 | #if DEBUGMODE |
Ratchapong | 0:052d0f82433e | 464 | pc.printf(" Answer received : %d\n", resp); |
Ratchapong | 0:052d0f82433e | 465 | #endif |
Ratchapong | 0:052d0f82433e | 466 | |
Ratchapong | 0:052d0f82433e | 467 | return resp; |
Ratchapong | 0:052d0f82433e | 468 | } |
Ratchapong | 0:052d0f82433e | 469 |