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