Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tuxitheone 0:ecaf3e593122 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
Tuxitheone 0:ecaf3e593122 2 * Copyright (c) 2007-2010, sford, http://mbed.org
Tuxitheone 0:ecaf3e593122 3 * 2013, v01: WH, Added LCD types, fixed LCD address issues, added Cursor and UDCs
Tuxitheone 0:ecaf3e593122 4 * 2013, v02: WH, Added I2C and SPI bus interfaces
Tuxitheone 0:ecaf3e593122 5 * 2013, v03: WH, Added support for LCD40x4 which uses 2 controllers
Tuxitheone 0:ecaf3e593122 6 * 2013, v04: WH, Added support for Display On/Off, improved 4bit bootprocess
Tuxitheone 0:ecaf3e593122 7 * 2013, v05: WH, Added support for 8x2B, added some UDCs
Tuxitheone 0:ecaf3e593122 8 * 2013, v06: WH, Added support for devices that use internal DC/DC converters
Tuxitheone 0:ecaf3e593122 9 * 2013, v07: WH, Added support for backlight and include portdefinitions for LCD2004 Module from DFROBOT
Tuxitheone 0:ecaf3e593122 10 *
Tuxitheone 0:ecaf3e593122 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
Tuxitheone 0:ecaf3e593122 12 * of this software and associated documentation files (the "Software"), to deal
Tuxitheone 0:ecaf3e593122 13 * in the Software without restriction, including without limitation the rights
Tuxitheone 0:ecaf3e593122 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Tuxitheone 0:ecaf3e593122 15 * copies of the Software, and to permit persons to whom the Software is
Tuxitheone 0:ecaf3e593122 16 * furnished to do so, subject to the following conditions:
Tuxitheone 0:ecaf3e593122 17 *
Tuxitheone 0:ecaf3e593122 18 * The above copyright notice and this permission notice shall be included in
Tuxitheone 0:ecaf3e593122 19 * all copies or substantial portions of the Software.
Tuxitheone 0:ecaf3e593122 20 *
Tuxitheone 0:ecaf3e593122 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Tuxitheone 0:ecaf3e593122 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Tuxitheone 0:ecaf3e593122 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Tuxitheone 0:ecaf3e593122 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Tuxitheone 0:ecaf3e593122 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Tuxitheone 0:ecaf3e593122 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Tuxitheone 0:ecaf3e593122 27 * THE SOFTWARE.
Tuxitheone 0:ecaf3e593122 28 */
Tuxitheone 0:ecaf3e593122 29
Tuxitheone 0:ecaf3e593122 30 #include "TextLCD.h"
Tuxitheone 0:ecaf3e593122 31 #include "mbed.h"
Tuxitheone 0:ecaf3e593122 32
Tuxitheone 0:ecaf3e593122 33
Tuxitheone 0:ecaf3e593122 34 /* Create a TextLCD interface for using regular mbed pins
Tuxitheone 0:ecaf3e593122 35 *
Tuxitheone 0:ecaf3e593122 36 * @param rs Instruction/data control line
Tuxitheone 0:ecaf3e593122 37 * @param e Enable line (clock)
Tuxitheone 0:ecaf3e593122 38 * @param d4-d7 Data lines for using as a 4-bit interface
Tuxitheone 0:ecaf3e593122 39 * @param type Sets the panel size/addressing mode (default = LCD16x2)
Tuxitheone 0:ecaf3e593122 40 * @param bl Backlight control line (optional, default = NC)
Tuxitheone 0:ecaf3e593122 41 * @param e2 Enable2 line (clock for second controller, LCD40x4 only)
Tuxitheone 0:ecaf3e593122 42 * @param ctrl LCD controller (default = HD44780)
Tuxitheone 0:ecaf3e593122 43 */
Tuxitheone 0:ecaf3e593122 44 TextLCD::TextLCD(PinName rs, PinName e,
Tuxitheone 0:ecaf3e593122 45 PinName d4, PinName d5, PinName d6, PinName d7,
Tuxitheone 0:ecaf3e593122 46 LCDType type, PinName bl, PinName e2, LCDCtrl ctrl) : _rs(rs), _e(e), _bl(bl), _e2(e2),
Tuxitheone 0:ecaf3e593122 47 _d(d4, d5, d6, d7),
Tuxitheone 0:ecaf3e593122 48 _cs(NC),
Tuxitheone 0:ecaf3e593122 49 _type(type),
Tuxitheone 0:ecaf3e593122 50 _ctrl(ctrl) {
Tuxitheone 0:ecaf3e593122 51
Tuxitheone 0:ecaf3e593122 52 _busType = _PinBus;
Tuxitheone 0:ecaf3e593122 53
Tuxitheone 0:ecaf3e593122 54 _init();
Tuxitheone 0:ecaf3e593122 55
Tuxitheone 0:ecaf3e593122 56 }
Tuxitheone 0:ecaf3e593122 57
Tuxitheone 0:ecaf3e593122 58 /* Create a TextLCD interface using an I2C PC8574 portexpander
Tuxitheone 0:ecaf3e593122 59 *
Tuxitheone 0:ecaf3e593122 60 * @param i2c I2C Bus
Tuxitheone 0:ecaf3e593122 61 * @param deviceAddress I2C slave address (PCF8574)
Tuxitheone 0:ecaf3e593122 62 * @param type Sets the panel size/addressing mode (default = LCD16x2)
Tuxitheone 0:ecaf3e593122 63 * @param ctrl LCD controller (default = HD44780)
Tuxitheone 0:ecaf3e593122 64 */
Tuxitheone 0:ecaf3e593122 65 TextLCD::TextLCD(I2C *i2c, char deviceAddress, LCDType type, LCDCtrl ctrl) :
Tuxitheone 0:ecaf3e593122 66 _rs(NC), _e(NC), _bl(NC), _e2(NC),
Tuxitheone 0:ecaf3e593122 67 _d(NC),
Tuxitheone 0:ecaf3e593122 68 _i2c(i2c),
Tuxitheone 0:ecaf3e593122 69 _cs(NC),
Tuxitheone 0:ecaf3e593122 70 _type(type),
Tuxitheone 0:ecaf3e593122 71 _ctrl(ctrl) {
Tuxitheone 0:ecaf3e593122 72
Tuxitheone 0:ecaf3e593122 73 _slaveAddress = deviceAddress;
Tuxitheone 0:ecaf3e593122 74 _busType = _I2CBus;
Tuxitheone 0:ecaf3e593122 75
Tuxitheone 0:ecaf3e593122 76
Tuxitheone 0:ecaf3e593122 77 // Init the portexpander bus
Tuxitheone 0:ecaf3e593122 78 _lcd_bus = D_LCD_BUS_DEF;
Tuxitheone 0:ecaf3e593122 79
Tuxitheone 0:ecaf3e593122 80 // write the new data to the portexpander
Tuxitheone 0:ecaf3e593122 81 _i2c->write(_slaveAddress, &_lcd_bus, 1);
Tuxitheone 0:ecaf3e593122 82
Tuxitheone 0:ecaf3e593122 83 _init();
Tuxitheone 0:ecaf3e593122 84
Tuxitheone 0:ecaf3e593122 85 }
Tuxitheone 0:ecaf3e593122 86
Tuxitheone 0:ecaf3e593122 87 /* Create a TextLCD interface using an SPI 74595 portexpander
Tuxitheone 0:ecaf3e593122 88 *
Tuxitheone 0:ecaf3e593122 89 * @param spi SPI Bus
Tuxitheone 0:ecaf3e593122 90 * @param cs chip select pin (active low)
Tuxitheone 0:ecaf3e593122 91 * @param type Sets the panel size/addressing mode (default = LCD16x2)
Tuxitheone 0:ecaf3e593122 92 * @param ctrl LCD controller (default = HD44780)
Tuxitheone 0:ecaf3e593122 93 */
Tuxitheone 0:ecaf3e593122 94 TextLCD::TextLCD(SPI *spi, PinName cs, LCDType type, LCDCtrl ctrl) :
Tuxitheone 0:ecaf3e593122 95 _rs(NC), _e(NC), _bl(NC), _e2(NC),
Tuxitheone 0:ecaf3e593122 96 _d(NC),
Tuxitheone 0:ecaf3e593122 97 _spi(spi),
Tuxitheone 0:ecaf3e593122 98 _cs(cs),
Tuxitheone 0:ecaf3e593122 99 _type(type),
Tuxitheone 0:ecaf3e593122 100 _ctrl(ctrl) {
Tuxitheone 0:ecaf3e593122 101
Tuxitheone 0:ecaf3e593122 102 _busType = _SPIBus;
Tuxitheone 0:ecaf3e593122 103
Tuxitheone 0:ecaf3e593122 104 // Setup the spi for 8 bit data, low steady state clock,
Tuxitheone 0:ecaf3e593122 105 // rising edge capture, with a 500KHz or 1MHz clock rate
Tuxitheone 0:ecaf3e593122 106 _spi->format(8,0);
Tuxitheone 0:ecaf3e593122 107 _spi->frequency(500000);
Tuxitheone 0:ecaf3e593122 108 //_spi.frequency(1000000);
Tuxitheone 0:ecaf3e593122 109
Tuxitheone 0:ecaf3e593122 110
Tuxitheone 0:ecaf3e593122 111 // Init the portexpander bus
Tuxitheone 0:ecaf3e593122 112 _lcd_bus = D_LCD_BUS_DEF;
Tuxitheone 0:ecaf3e593122 113
Tuxitheone 0:ecaf3e593122 114 // write the new data to the portexpander
Tuxitheone 0:ecaf3e593122 115 _setCS(false);
Tuxitheone 0:ecaf3e593122 116 _spi->write(_lcd_bus);
Tuxitheone 0:ecaf3e593122 117 _setCS(true);
Tuxitheone 0:ecaf3e593122 118
Tuxitheone 0:ecaf3e593122 119 _init();
Tuxitheone 0:ecaf3e593122 120
Tuxitheone 0:ecaf3e593122 121 }
Tuxitheone 0:ecaf3e593122 122
Tuxitheone 0:ecaf3e593122 123
Tuxitheone 0:ecaf3e593122 124 /* Init the LCD Controller(s)
Tuxitheone 0:ecaf3e593122 125 * Clear display
Tuxitheone 0:ecaf3e593122 126 */
Tuxitheone 0:ecaf3e593122 127 void TextLCD::_init() {
Tuxitheone 0:ecaf3e593122 128
Tuxitheone 0:ecaf3e593122 129 // Select and configure second LCD controller when needed
Tuxitheone 0:ecaf3e593122 130 if(_type==LCD40x4) {
Tuxitheone 0:ecaf3e593122 131 _ctrl_idx=TextLCD::_LCDCtrl_1; // Select 2nd controller
Tuxitheone 0:ecaf3e593122 132
Tuxitheone 0:ecaf3e593122 133 _initCtrl(); // Init 2nd controller
Tuxitheone 0:ecaf3e593122 134
Tuxitheone 0:ecaf3e593122 135 // Secondary LCD controller Clearscreen
Tuxitheone 0:ecaf3e593122 136 _writeCommand(0x01); // cls, and set cursor to 0
Tuxitheone 0:ecaf3e593122 137 wait_ms(10); // The CLS command takes 1.64 ms.
Tuxitheone 0:ecaf3e593122 138 // Since we are not using the Busy flag, Lets be safe and take 10 ms
Tuxitheone 0:ecaf3e593122 139
Tuxitheone 0:ecaf3e593122 140 }
Tuxitheone 0:ecaf3e593122 141
Tuxitheone 0:ecaf3e593122 142 // Select and configure primary LCD controller
Tuxitheone 0:ecaf3e593122 143 _ctrl_idx=TextLCD::_LCDCtrl_0; // Select primary controller
Tuxitheone 0:ecaf3e593122 144
Tuxitheone 0:ecaf3e593122 145 _initCtrl(); // Init primary controller
Tuxitheone 0:ecaf3e593122 146
Tuxitheone 0:ecaf3e593122 147 // Primary LCD controller Clearscreen
Tuxitheone 0:ecaf3e593122 148 _writeCommand(0x01); // cls, and set cursor to 0
Tuxitheone 0:ecaf3e593122 149
Tuxitheone 0:ecaf3e593122 150 wait_ms(10); // The CLS command takes 1.64 ms.
Tuxitheone 0:ecaf3e593122 151 // Since we are not using the Busy flag, Lets be safe and take 10 ms
Tuxitheone 0:ecaf3e593122 152
Tuxitheone 0:ecaf3e593122 153 }
Tuxitheone 0:ecaf3e593122 154
Tuxitheone 0:ecaf3e593122 155 /* Init the LCD controller
Tuxitheone 0:ecaf3e593122 156 * 4-bit mode, number of lines, fonttype, no cursor etc
Tuxitheone 0:ecaf3e593122 157 *
Tuxitheone 0:ecaf3e593122 158 */
Tuxitheone 0:ecaf3e593122 159 void TextLCD::_initCtrl() {
Tuxitheone 0:ecaf3e593122 160
Tuxitheone 0:ecaf3e593122 161 _setRS(false); // command mode
Tuxitheone 0:ecaf3e593122 162
Tuxitheone 0:ecaf3e593122 163 wait_ms(20); // Wait 20ms to ensure powered up
Tuxitheone 0:ecaf3e593122 164
Tuxitheone 0:ecaf3e593122 165 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
Tuxitheone 0:ecaf3e593122 166 for (int i=0; i<3; i++) {
Tuxitheone 0:ecaf3e593122 167 _writeNibble(0x3);
Tuxitheone 0:ecaf3e593122 168 wait_ms(15); // This command takes 1.64ms, so wait for it
Tuxitheone 0:ecaf3e593122 169 }
Tuxitheone 0:ecaf3e593122 170 _writeNibble(0x2); // 4-bit mode
Tuxitheone 0:ecaf3e593122 171 wait_us(40); // most instructions take 40us
Tuxitheone 0:ecaf3e593122 172
Tuxitheone 0:ecaf3e593122 173 // Display is now in 4-bit mode
Tuxitheone 0:ecaf3e593122 174
Tuxitheone 0:ecaf3e593122 175
Tuxitheone 0:ecaf3e593122 176 // Device specific initialisations for DC/DC converter to generate VLCD or VLED
Tuxitheone 0:ecaf3e593122 177 switch (_ctrl) {
Tuxitheone 0:ecaf3e593122 178 case ST7036:
Tuxitheone 0:ecaf3e593122 179 // ST7036 controller: Initialise Voltage booster for VLCD. VDD=5V
Tuxitheone 0:ecaf3e593122 180 // Note: supports 1,2 or 3 lines
Tuxitheone 0:ecaf3e593122 181 _writeByte( 0x29 ); // 4-bit Databus, 2 Lines, Select Instruction table 1
Tuxitheone 0:ecaf3e593122 182 wait_ms(30); // > 26,3ms
Tuxitheone 0:ecaf3e593122 183 _writeByte( 0x14 ); // Bias: 1/5, 2-Lines LCD
Tuxitheone 0:ecaf3e593122 184 wait_ms(30); // > 26,3ms
Tuxitheone 0:ecaf3e593122 185 _writeByte( 0x55 ); // Icon off, Booster on, Set Contrast C5, C4
Tuxitheone 0:ecaf3e593122 186 wait_ms(30); // > 26,3ms
Tuxitheone 0:ecaf3e593122 187 _writeByte( 0x6d ); // Voltagefollower On, Ampl ratio Rab2, Rab1, Rab0
Tuxitheone 0:ecaf3e593122 188 wait_ms(200); // > 200ms!
Tuxitheone 0:ecaf3e593122 189 _writeByte( 0x78 ); // Set Contrast C3, C2, C1, C0
Tuxitheone 0:ecaf3e593122 190 wait_ms(30); // > 26,3ms
Tuxitheone 0:ecaf3e593122 191 _writeByte( 0x28 ); // Return to Instruction table 0
Tuxitheone 0:ecaf3e593122 192 wait_ms(50);
Tuxitheone 0:ecaf3e593122 193
Tuxitheone 0:ecaf3e593122 194 break;
Tuxitheone 0:ecaf3e593122 195
Tuxitheone 0:ecaf3e593122 196 case WS0010:
Tuxitheone 0:ecaf3e593122 197 // WS0010 OLED controller: Initialise DC/DC Voltage converter for LEDs
Tuxitheone 0:ecaf3e593122 198 // Note: supports 1 or 2 lines (and 16x100 graphics)
Tuxitheone 0:ecaf3e593122 199 // supports 4 fonts (English/Japanese (default), Western European-I, English/Russian, Western European-II)
Tuxitheone 0:ecaf3e593122 200
Tuxitheone 0:ecaf3e593122 201 // Cursor/Disp shift set 0001 SC RL 0 0
Tuxitheone 0:ecaf3e593122 202 //
Tuxitheone 0:ecaf3e593122 203 // Mode en Power set 0001 GC PWR 1 1
Tuxitheone 0:ecaf3e593122 204 // GC = 0 (Graph Mode=1, Char Mode=0)
Tuxitheone 0:ecaf3e593122 205 // PWR = (DC/DC On/Off)
Tuxitheone 0:ecaf3e593122 206
Tuxitheone 0:ecaf3e593122 207 //_writeCommand(0x13); // DC/DC off
Tuxitheone 0:ecaf3e593122 208
Tuxitheone 0:ecaf3e593122 209 _writeCommand(0x17); // DC/DC on
Tuxitheone 0:ecaf3e593122 210 wait_ms(10);
Tuxitheone 0:ecaf3e593122 211
Tuxitheone 0:ecaf3e593122 212 break;
Tuxitheone 0:ecaf3e593122 213
Tuxitheone 0:ecaf3e593122 214 default:
Tuxitheone 0:ecaf3e593122 215 // Devices that do not use DC/DC Voltage converters but external VLCD
Tuxitheone 0:ecaf3e593122 216 break;
Tuxitheone 0:ecaf3e593122 217 }
Tuxitheone 0:ecaf3e593122 218
Tuxitheone 0:ecaf3e593122 219 // Initialise Display configuration
Tuxitheone 0:ecaf3e593122 220 switch (_type) {
Tuxitheone 0:ecaf3e593122 221 case LCD8x1:
Tuxitheone 0:ecaf3e593122 222 case LCD8x2B:
Tuxitheone 0:ecaf3e593122 223 //8x1 is a regular 1 line display
Tuxitheone 0:ecaf3e593122 224 //8x2B is a special case of 16x1
Tuxitheone 0:ecaf3e593122 225 _writeCommand(0x20); // Function set 001 DL N F - -
Tuxitheone 0:ecaf3e593122 226 // DL=0 (4 bits bus)
Tuxitheone 0:ecaf3e593122 227 // N=0 (1 line)
Tuxitheone 0:ecaf3e593122 228 // F=0 (5x7 dots font)
Tuxitheone 0:ecaf3e593122 229 break;
Tuxitheone 0:ecaf3e593122 230
Tuxitheone 0:ecaf3e593122 231 case LCD24x4:
Tuxitheone 0:ecaf3e593122 232 // Special mode for KS0078
Tuxitheone 0:ecaf3e593122 233 _writeCommand(0x2A); // Function set 001 DL N RE DH REV
Tuxitheone 0:ecaf3e593122 234 // DL=0 (4 bits bus)
Tuxitheone 0:ecaf3e593122 235 // N=1 (Dont care for KS0078)
Tuxitheone 0:ecaf3e593122 236 // RE=0 (Extended Regs, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 237 // DH=1 (Disp shift, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 238 // REV=0 (Reverse, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 239
Tuxitheone 0:ecaf3e593122 240 _writeCommand(0x2E); // Function set 001 DL N RE DH REV
Tuxitheone 0:ecaf3e593122 241 // DL=0 (4 bits bus)
Tuxitheone 0:ecaf3e593122 242 // N=1 (Dont care for KS0078)
Tuxitheone 0:ecaf3e593122 243 // RE=1 (Ena Extended Regs, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 244 // DH=1 (Disp shift, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 245 // REV=0 (Reverse, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 246
Tuxitheone 0:ecaf3e593122 247 _writeCommand(0x09); // Ext Function set 0000 1 FW BW NW
Tuxitheone 0:ecaf3e593122 248 // FW=0 (5-dot font, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 249 // BW=0 (Cur BW invert disable, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 250 // NW=1 (4 Line, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 251
Tuxitheone 0:ecaf3e593122 252 _writeCommand(0x2A); // Function set 001 DL N RE DH REV
Tuxitheone 0:ecaf3e593122 253 // DL=0 (4 bits bus)
Tuxitheone 0:ecaf3e593122 254 // N=1 (Dont care for KS0078)
Tuxitheone 0:ecaf3e593122 255 // RE=0 (Dis. Extended Regs, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 256 // DH=1 (Disp shift, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 257 // REV=0 (Reverse, special mode for KS0078)
Tuxitheone 0:ecaf3e593122 258 break;
Tuxitheone 0:ecaf3e593122 259
Tuxitheone 0:ecaf3e593122 260 // All other LCD types are initialised as 2 Line displays (including LCD40x4)
Tuxitheone 0:ecaf3e593122 261 default:
Tuxitheone 0:ecaf3e593122 262 _writeCommand(0x28); // Function set 001 DL N F - -
Tuxitheone 0:ecaf3e593122 263 // DL=0 (4 bits bus)
Tuxitheone 0:ecaf3e593122 264 // N=1 (2 lines)
Tuxitheone 0:ecaf3e593122 265 // F=0 (5x7 dots font, only option for 2 line display)
Tuxitheone 0:ecaf3e593122 266 // - (Don't care)
Tuxitheone 0:ecaf3e593122 267
Tuxitheone 0:ecaf3e593122 268 break;
Tuxitheone 0:ecaf3e593122 269 }
Tuxitheone 0:ecaf3e593122 270
Tuxitheone 0:ecaf3e593122 271 _writeCommand(0x06); // Entry Mode 0000 01 CD S
Tuxitheone 0:ecaf3e593122 272 // Cursor Direction and Display Shift
Tuxitheone 0:ecaf3e593122 273 // CD=1 (Cur incr)
Tuxitheone 0:ecaf3e593122 274 // S=0 (No display shift)
Tuxitheone 0:ecaf3e593122 275
Tuxitheone 0:ecaf3e593122 276 // _writeCommand(0x0C); // Display Ctrl 0000 1 D C B
Tuxitheone 0:ecaf3e593122 277 // // Display On, Cursor Off, Blink Off
Tuxitheone 0:ecaf3e593122 278 setCursor(TextLCD::CurOff_BlkOff);
Tuxitheone 0:ecaf3e593122 279 setMode(TextLCD::DispOn);
Tuxitheone 0:ecaf3e593122 280 }
Tuxitheone 0:ecaf3e593122 281
Tuxitheone 0:ecaf3e593122 282
Tuxitheone 0:ecaf3e593122 283 // Clear the screen, Cursor home.
Tuxitheone 0:ecaf3e593122 284 void TextLCD::cls() {
Tuxitheone 0:ecaf3e593122 285
Tuxitheone 0:ecaf3e593122 286 // Select and configure second LCD controller when needed
Tuxitheone 0:ecaf3e593122 287 if(_type==LCD40x4) {
Tuxitheone 0:ecaf3e593122 288 _ctrl_idx=TextLCD::_LCDCtrl_1; // Select 2nd controller
Tuxitheone 0:ecaf3e593122 289
Tuxitheone 0:ecaf3e593122 290 // Second LCD controller Cursor always Off
Tuxitheone 0:ecaf3e593122 291 _setCursorAndDisplayMode(_currentMode, TextLCD::CurOff_BlkOff);
Tuxitheone 0:ecaf3e593122 292
Tuxitheone 0:ecaf3e593122 293 // Second LCD controller Clearscreen
Tuxitheone 0:ecaf3e593122 294 _writeCommand(0x01); // cls, and set cursor to 0
Tuxitheone 0:ecaf3e593122 295
Tuxitheone 0:ecaf3e593122 296 wait_ms(10); // The CLS command takes 1.64 ms.
Tuxitheone 0:ecaf3e593122 297 // Since we are not using the Busy flag, Lets be safe and take 10 ms
Tuxitheone 0:ecaf3e593122 298
Tuxitheone 0:ecaf3e593122 299
Tuxitheone 0:ecaf3e593122 300 _ctrl_idx=TextLCD::_LCDCtrl_0; // Select primary controller
Tuxitheone 0:ecaf3e593122 301 }
Tuxitheone 0:ecaf3e593122 302
Tuxitheone 0:ecaf3e593122 303 // Primary LCD controller Clearscreen
Tuxitheone 0:ecaf3e593122 304 _writeCommand(0x01); // cls, and set cursor to 0
Tuxitheone 0:ecaf3e593122 305
Tuxitheone 0:ecaf3e593122 306 wait_ms(10); // The CLS command takes 1.64 ms.
Tuxitheone 0:ecaf3e593122 307 // Since we are not using the Busy flag, Lets be safe and take 10 ms
Tuxitheone 0:ecaf3e593122 308
Tuxitheone 0:ecaf3e593122 309 // Restore cursormode on primary LCD controller when needed
Tuxitheone 0:ecaf3e593122 310 if(_type==LCD40x4) {
Tuxitheone 0:ecaf3e593122 311 _setCursorAndDisplayMode(_currentMode,_currentCursor);
Tuxitheone 0:ecaf3e593122 312 }
Tuxitheone 0:ecaf3e593122 313
Tuxitheone 0:ecaf3e593122 314 _row=0; // Reset Cursor location
Tuxitheone 0:ecaf3e593122 315 _column=0;
Tuxitheone 0:ecaf3e593122 316 }
Tuxitheone 0:ecaf3e593122 317
Tuxitheone 0:ecaf3e593122 318 // Move cursor to selected row and column
Tuxitheone 0:ecaf3e593122 319 void TextLCD::locate(int column, int row) {
Tuxitheone 0:ecaf3e593122 320
Tuxitheone 0:ecaf3e593122 321 // setAddress() does all the heavy lifting:
Tuxitheone 0:ecaf3e593122 322 // check column and row sanity,
Tuxitheone 0:ecaf3e593122 323 // switch controllers for LCD40x4 if needed
Tuxitheone 0:ecaf3e593122 324 // switch cursor for LCD40x4 if needed
Tuxitheone 0:ecaf3e593122 325 // set the new memory address to show cursor at correct location
Tuxitheone 0:ecaf3e593122 326 setAddress(column, row);
Tuxitheone 0:ecaf3e593122 327
Tuxitheone 0:ecaf3e593122 328 }
Tuxitheone 0:ecaf3e593122 329
Tuxitheone 0:ecaf3e593122 330
Tuxitheone 0:ecaf3e593122 331 // Write a single character (Stream implementation)
Tuxitheone 0:ecaf3e593122 332 int TextLCD::_putc(int value) {
Tuxitheone 0:ecaf3e593122 333 int addr;
Tuxitheone 0:ecaf3e593122 334
Tuxitheone 0:ecaf3e593122 335 if (value == '\n') {
Tuxitheone 0:ecaf3e593122 336 //No character to write
Tuxitheone 0:ecaf3e593122 337
Tuxitheone 0:ecaf3e593122 338 //Update Cursor
Tuxitheone 0:ecaf3e593122 339 _column = 0;
Tuxitheone 0:ecaf3e593122 340 _row++;
Tuxitheone 0:ecaf3e593122 341 if (_row >= rows()) {
Tuxitheone 0:ecaf3e593122 342 _row = 0;
Tuxitheone 0:ecaf3e593122 343 }
Tuxitheone 0:ecaf3e593122 344 }
Tuxitheone 0:ecaf3e593122 345 else {
Tuxitheone 0:ecaf3e593122 346 //Character to write
Tuxitheone 0:ecaf3e593122 347 _writeData(value);
Tuxitheone 0:ecaf3e593122 348
Tuxitheone 0:ecaf3e593122 349 //Update Cursor
Tuxitheone 0:ecaf3e593122 350 _column++;
Tuxitheone 0:ecaf3e593122 351 if (_column >= columns()) {
Tuxitheone 0:ecaf3e593122 352 _column = 0;
Tuxitheone 0:ecaf3e593122 353 _row++;
Tuxitheone 0:ecaf3e593122 354 if (_row >= rows()) {
Tuxitheone 0:ecaf3e593122 355 _row = 0;
Tuxitheone 0:ecaf3e593122 356 }
Tuxitheone 0:ecaf3e593122 357 }
Tuxitheone 0:ecaf3e593122 358 } //else
Tuxitheone 0:ecaf3e593122 359
Tuxitheone 0:ecaf3e593122 360 //Set next memoryaddress, make sure cursor blinks at next location
Tuxitheone 0:ecaf3e593122 361 addr = getAddress(_column, _row);
Tuxitheone 0:ecaf3e593122 362 _writeCommand(0x80 | addr);
Tuxitheone 0:ecaf3e593122 363
Tuxitheone 0:ecaf3e593122 364 return value;
Tuxitheone 0:ecaf3e593122 365 }
Tuxitheone 0:ecaf3e593122 366
Tuxitheone 0:ecaf3e593122 367
Tuxitheone 0:ecaf3e593122 368 // get a single character (Stream implementation)
Tuxitheone 0:ecaf3e593122 369 int TextLCD::_getc() {
Tuxitheone 0:ecaf3e593122 370 return -1;
Tuxitheone 0:ecaf3e593122 371 }
Tuxitheone 0:ecaf3e593122 372
Tuxitheone 0:ecaf3e593122 373 // Set E pin (or E2 pin)
Tuxitheone 0:ecaf3e593122 374 // Used for mbed pins, I2C bus expander or SPI shifregister
Tuxitheone 0:ecaf3e593122 375 void TextLCD::_setEnable(bool value) {
Tuxitheone 0:ecaf3e593122 376
Tuxitheone 0:ecaf3e593122 377 switch(_busType) {
Tuxitheone 0:ecaf3e593122 378 case _PinBus :
Tuxitheone 0:ecaf3e593122 379 if(_ctrl_idx==TextLCD::_LCDCtrl_0) {
Tuxitheone 0:ecaf3e593122 380 if (value)
Tuxitheone 0:ecaf3e593122 381 _e = 1; // Set E bit
Tuxitheone 0:ecaf3e593122 382 else
Tuxitheone 0:ecaf3e593122 383 _e = 0; // Reset E bit
Tuxitheone 0:ecaf3e593122 384 }
Tuxitheone 0:ecaf3e593122 385 else {
Tuxitheone 0:ecaf3e593122 386 if (value)
Tuxitheone 0:ecaf3e593122 387 _e2 = 1; // Set E2 bit
Tuxitheone 0:ecaf3e593122 388 else
Tuxitheone 0:ecaf3e593122 389 _e2 = 0; // Reset E2 bit
Tuxitheone 0:ecaf3e593122 390 }
Tuxitheone 0:ecaf3e593122 391
Tuxitheone 0:ecaf3e593122 392 break;
Tuxitheone 0:ecaf3e593122 393
Tuxitheone 0:ecaf3e593122 394 case _I2CBus :
Tuxitheone 0:ecaf3e593122 395
Tuxitheone 0:ecaf3e593122 396 if(_ctrl_idx==TextLCD::_LCDCtrl_0) {
Tuxitheone 0:ecaf3e593122 397 if (value)
Tuxitheone 0:ecaf3e593122 398 _lcd_bus |= D_LCD_E; // Set E bit
Tuxitheone 0:ecaf3e593122 399 else
Tuxitheone 0:ecaf3e593122 400 _lcd_bus &= ~D_LCD_E; // Reset E bit
Tuxitheone 0:ecaf3e593122 401 }
Tuxitheone 0:ecaf3e593122 402 else {
Tuxitheone 0:ecaf3e593122 403 if (value)
Tuxitheone 0:ecaf3e593122 404 _lcd_bus |= D_LCD_E2; // Set E2 bit
Tuxitheone 0:ecaf3e593122 405 else
Tuxitheone 0:ecaf3e593122 406 _lcd_bus &= ~D_LCD_E2; // Reset E2bit
Tuxitheone 0:ecaf3e593122 407 }
Tuxitheone 0:ecaf3e593122 408
Tuxitheone 0:ecaf3e593122 409 // write the new data to the I2C portexpander
Tuxitheone 0:ecaf3e593122 410 _i2c->write(_slaveAddress, &_lcd_bus, 1);
Tuxitheone 0:ecaf3e593122 411
Tuxitheone 0:ecaf3e593122 412 break;
Tuxitheone 0:ecaf3e593122 413
Tuxitheone 0:ecaf3e593122 414 case _SPIBus :
Tuxitheone 0:ecaf3e593122 415 if(_ctrl_idx==TextLCD::_LCDCtrl_0) {
Tuxitheone 0:ecaf3e593122 416 if (value)
Tuxitheone 0:ecaf3e593122 417 _lcd_bus |= D_LCD_E; // Set E bit
Tuxitheone 0:ecaf3e593122 418 else
Tuxitheone 0:ecaf3e593122 419 _lcd_bus &= ~D_LCD_E; // Reset E bit
Tuxitheone 0:ecaf3e593122 420 }
Tuxitheone 0:ecaf3e593122 421 else {
Tuxitheone 0:ecaf3e593122 422 if (value)
Tuxitheone 0:ecaf3e593122 423 _lcd_bus |= D_LCD_E2; // Set E2 bit
Tuxitheone 0:ecaf3e593122 424 else
Tuxitheone 0:ecaf3e593122 425 _lcd_bus &= ~D_LCD_E2; // Reset E2 bit
Tuxitheone 0:ecaf3e593122 426 }
Tuxitheone 0:ecaf3e593122 427
Tuxitheone 0:ecaf3e593122 428 // write the new data to the SPI portexpander
Tuxitheone 0:ecaf3e593122 429 _setCS(false);
Tuxitheone 0:ecaf3e593122 430 _spi->write(_lcd_bus);
Tuxitheone 0:ecaf3e593122 431 _setCS(true);
Tuxitheone 0:ecaf3e593122 432
Tuxitheone 0:ecaf3e593122 433 break;
Tuxitheone 0:ecaf3e593122 434 }
Tuxitheone 0:ecaf3e593122 435 }
Tuxitheone 0:ecaf3e593122 436
Tuxitheone 0:ecaf3e593122 437 // Set RS pin
Tuxitheone 0:ecaf3e593122 438 // Used for mbed pins, I2C bus expander or SPI shifregister
Tuxitheone 0:ecaf3e593122 439 void TextLCD::_setRS(bool value) {
Tuxitheone 0:ecaf3e593122 440
Tuxitheone 0:ecaf3e593122 441 switch(_busType) {
Tuxitheone 0:ecaf3e593122 442 case _PinBus :
Tuxitheone 0:ecaf3e593122 443 if (value)
Tuxitheone 0:ecaf3e593122 444 _rs = 1; // Set RS bit
Tuxitheone 0:ecaf3e593122 445 else
Tuxitheone 0:ecaf3e593122 446 _rs = 0; // Reset RS bit
Tuxitheone 0:ecaf3e593122 447
Tuxitheone 0:ecaf3e593122 448 break;
Tuxitheone 0:ecaf3e593122 449
Tuxitheone 0:ecaf3e593122 450 case _I2CBus :
Tuxitheone 0:ecaf3e593122 451 if (value)
Tuxitheone 0:ecaf3e593122 452 _lcd_bus |= D_LCD_RS; // Set RS bit
Tuxitheone 0:ecaf3e593122 453 else
Tuxitheone 0:ecaf3e593122 454 _lcd_bus &= ~D_LCD_RS; // Reset RS bit
Tuxitheone 0:ecaf3e593122 455
Tuxitheone 0:ecaf3e593122 456 // write the new data to the I2C portexpander
Tuxitheone 0:ecaf3e593122 457 _i2c->write(_slaveAddress, &_lcd_bus, 1);
Tuxitheone 0:ecaf3e593122 458
Tuxitheone 0:ecaf3e593122 459 break;
Tuxitheone 0:ecaf3e593122 460
Tuxitheone 0:ecaf3e593122 461 case _SPIBus :
Tuxitheone 0:ecaf3e593122 462 if (value)
Tuxitheone 0:ecaf3e593122 463 _lcd_bus |= D_LCD_RS; // Set RS bit
Tuxitheone 0:ecaf3e593122 464 else
Tuxitheone 0:ecaf3e593122 465 _lcd_bus &= ~D_LCD_RS; // Reset RS bit
Tuxitheone 0:ecaf3e593122 466
Tuxitheone 0:ecaf3e593122 467 // write the new data to the SPI portexpander
Tuxitheone 0:ecaf3e593122 468 _setCS(false);
Tuxitheone 0:ecaf3e593122 469 _spi->write(_lcd_bus);
Tuxitheone 0:ecaf3e593122 470 _setCS(true);
Tuxitheone 0:ecaf3e593122 471
Tuxitheone 0:ecaf3e593122 472 break;
Tuxitheone 0:ecaf3e593122 473 }
Tuxitheone 0:ecaf3e593122 474
Tuxitheone 0:ecaf3e593122 475 }
Tuxitheone 0:ecaf3e593122 476
Tuxitheone 0:ecaf3e593122 477 // Set BL pin
Tuxitheone 0:ecaf3e593122 478 // Used for mbed pins, I2C bus expander or SPI shifregister
Tuxitheone 0:ecaf3e593122 479 void TextLCD::_setBL(bool value) {
Tuxitheone 0:ecaf3e593122 480
Tuxitheone 0:ecaf3e593122 481 switch(_busType) {
Tuxitheone 0:ecaf3e593122 482 case _PinBus :
Tuxitheone 0:ecaf3e593122 483 if (value)
Tuxitheone 0:ecaf3e593122 484 _bl = 1; // Set BL bit
Tuxitheone 0:ecaf3e593122 485 else
Tuxitheone 0:ecaf3e593122 486 _bl = 0; // Reset BL bit
Tuxitheone 0:ecaf3e593122 487
Tuxitheone 0:ecaf3e593122 488 break;
Tuxitheone 0:ecaf3e593122 489
Tuxitheone 0:ecaf3e593122 490 case _I2CBus :
Tuxitheone 0:ecaf3e593122 491 if (value)
Tuxitheone 0:ecaf3e593122 492 _lcd_bus |= D_LCD_BL; // Set BL bit
Tuxitheone 0:ecaf3e593122 493 else
Tuxitheone 0:ecaf3e593122 494 _lcd_bus &= ~D_LCD_BL; // Reset BL bit
Tuxitheone 0:ecaf3e593122 495
Tuxitheone 0:ecaf3e593122 496 // write the new data to the I2C portexpander
Tuxitheone 0:ecaf3e593122 497 _i2c->write(_slaveAddress, &_lcd_bus, 1);
Tuxitheone 0:ecaf3e593122 498
Tuxitheone 0:ecaf3e593122 499 break;
Tuxitheone 0:ecaf3e593122 500
Tuxitheone 0:ecaf3e593122 501 case _SPIBus :
Tuxitheone 0:ecaf3e593122 502 if (value)
Tuxitheone 0:ecaf3e593122 503 _lcd_bus |= D_LCD_BL; // Set BL bit
Tuxitheone 0:ecaf3e593122 504 else
Tuxitheone 0:ecaf3e593122 505 _lcd_bus &= ~D_LCD_BL; // Reset BL bit
Tuxitheone 0:ecaf3e593122 506
Tuxitheone 0:ecaf3e593122 507 // write the new data to the SPI portexpander
Tuxitheone 0:ecaf3e593122 508 _setCS(false);
Tuxitheone 0:ecaf3e593122 509 _spi->write(_lcd_bus);
Tuxitheone 0:ecaf3e593122 510 _setCS(true);
Tuxitheone 0:ecaf3e593122 511
Tuxitheone 0:ecaf3e593122 512 break;
Tuxitheone 0:ecaf3e593122 513 }
Tuxitheone 0:ecaf3e593122 514
Tuxitheone 0:ecaf3e593122 515 }
Tuxitheone 0:ecaf3e593122 516
Tuxitheone 0:ecaf3e593122 517
Tuxitheone 0:ecaf3e593122 518
Tuxitheone 0:ecaf3e593122 519 // Place the 4bit data on the databus
Tuxitheone 0:ecaf3e593122 520 // Used for mbed pins, I2C bus expander or SPI shifregister
Tuxitheone 0:ecaf3e593122 521 void TextLCD::_setData(int value) {
Tuxitheone 0:ecaf3e593122 522 int data;
Tuxitheone 0:ecaf3e593122 523
Tuxitheone 0:ecaf3e593122 524 switch(_busType) {
Tuxitheone 0:ecaf3e593122 525 case _PinBus :
Tuxitheone 0:ecaf3e593122 526 _d = value & 0x0F; // Write Databits
Tuxitheone 0:ecaf3e593122 527
Tuxitheone 0:ecaf3e593122 528 break;
Tuxitheone 0:ecaf3e593122 529
Tuxitheone 0:ecaf3e593122 530 case _I2CBus :
Tuxitheone 0:ecaf3e593122 531 data = value & 0x0F;
Tuxitheone 0:ecaf3e593122 532 if (data & 0x01)
Tuxitheone 0:ecaf3e593122 533 _lcd_bus |= D_LCD_D4; // Set Databit
Tuxitheone 0:ecaf3e593122 534 else
Tuxitheone 0:ecaf3e593122 535 _lcd_bus &= ~D_LCD_D4; // Reset Databit
Tuxitheone 0:ecaf3e593122 536
Tuxitheone 0:ecaf3e593122 537 if (data & 0x02)
Tuxitheone 0:ecaf3e593122 538 _lcd_bus |= D_LCD_D5; // Set Databit
Tuxitheone 0:ecaf3e593122 539 else
Tuxitheone 0:ecaf3e593122 540 _lcd_bus &= ~D_LCD_D5; // Reset Databit
Tuxitheone 0:ecaf3e593122 541
Tuxitheone 0:ecaf3e593122 542 if (data & 0x04)
Tuxitheone 0:ecaf3e593122 543 _lcd_bus |= D_LCD_D6; // Set Databit
Tuxitheone 0:ecaf3e593122 544 else
Tuxitheone 0:ecaf3e593122 545 _lcd_bus &= ~D_LCD_D6; // Reset Databit
Tuxitheone 0:ecaf3e593122 546
Tuxitheone 0:ecaf3e593122 547 if (data & 0x08)
Tuxitheone 0:ecaf3e593122 548 _lcd_bus |= D_LCD_D7; // Set Databit
Tuxitheone 0:ecaf3e593122 549 else
Tuxitheone 0:ecaf3e593122 550 _lcd_bus &= ~D_LCD_D7; // Reset Databit
Tuxitheone 0:ecaf3e593122 551
Tuxitheone 0:ecaf3e593122 552 // write the new data to the I2C portexpander
Tuxitheone 0:ecaf3e593122 553 _i2c->write(_slaveAddress, &_lcd_bus, 1);
Tuxitheone 0:ecaf3e593122 554
Tuxitheone 0:ecaf3e593122 555 break;
Tuxitheone 0:ecaf3e593122 556
Tuxitheone 0:ecaf3e593122 557 case _SPIBus :
Tuxitheone 0:ecaf3e593122 558
Tuxitheone 0:ecaf3e593122 559 data = value & 0x0F;
Tuxitheone 0:ecaf3e593122 560 if (data & 0x01)
Tuxitheone 0:ecaf3e593122 561 _lcd_bus |= D_LCD_D4; // Set Databit
Tuxitheone 0:ecaf3e593122 562 else
Tuxitheone 0:ecaf3e593122 563 _lcd_bus &= ~D_LCD_D4; // Reset Databit
Tuxitheone 0:ecaf3e593122 564
Tuxitheone 0:ecaf3e593122 565 if (data & 0x02)
Tuxitheone 0:ecaf3e593122 566 _lcd_bus |= D_LCD_D5; // Set Databit
Tuxitheone 0:ecaf3e593122 567 else
Tuxitheone 0:ecaf3e593122 568 _lcd_bus &= ~D_LCD_D5; // Reset Databit
Tuxitheone 0:ecaf3e593122 569
Tuxitheone 0:ecaf3e593122 570 if (data & 0x04)
Tuxitheone 0:ecaf3e593122 571 _lcd_bus |= D_LCD_D6; // Set Databit
Tuxitheone 0:ecaf3e593122 572 else
Tuxitheone 0:ecaf3e593122 573 _lcd_bus &= ~D_LCD_D6; // Reset Databit
Tuxitheone 0:ecaf3e593122 574
Tuxitheone 0:ecaf3e593122 575 if (data & 0x08)
Tuxitheone 0:ecaf3e593122 576 _lcd_bus |= D_LCD_D7; // Set Databit
Tuxitheone 0:ecaf3e593122 577 else
Tuxitheone 0:ecaf3e593122 578 _lcd_bus &= ~D_LCD_D7; // Reset Databit
Tuxitheone 0:ecaf3e593122 579
Tuxitheone 0:ecaf3e593122 580 // write the new data to the SPI portexpander
Tuxitheone 0:ecaf3e593122 581 _setCS(false);
Tuxitheone 0:ecaf3e593122 582 _spi->write(_lcd_bus);
Tuxitheone 0:ecaf3e593122 583 _setCS(true);
Tuxitheone 0:ecaf3e593122 584
Tuxitheone 0:ecaf3e593122 585 break;
Tuxitheone 0:ecaf3e593122 586 }
Tuxitheone 0:ecaf3e593122 587
Tuxitheone 0:ecaf3e593122 588 }
Tuxitheone 0:ecaf3e593122 589
Tuxitheone 0:ecaf3e593122 590
Tuxitheone 0:ecaf3e593122 591 // Set CS line.
Tuxitheone 0:ecaf3e593122 592 // Only used for SPI bus
Tuxitheone 0:ecaf3e593122 593 void TextLCD::_setCS(bool value) {
Tuxitheone 0:ecaf3e593122 594
Tuxitheone 0:ecaf3e593122 595 if (value) {
Tuxitheone 0:ecaf3e593122 596 _cs = 1; // Set CS pin
Tuxitheone 0:ecaf3e593122 597 }
Tuxitheone 0:ecaf3e593122 598 else
Tuxitheone 0:ecaf3e593122 599 _cs = 0; // Reset CS pin
Tuxitheone 0:ecaf3e593122 600
Tuxitheone 0:ecaf3e593122 601 }
Tuxitheone 0:ecaf3e593122 602
Tuxitheone 0:ecaf3e593122 603
Tuxitheone 0:ecaf3e593122 604 // Write a nibble using the 4-bit interface
Tuxitheone 0:ecaf3e593122 605 // Used for mbed pins, I2C bus expander or SPI shifregister
Tuxitheone 0:ecaf3e593122 606 void TextLCD::_writeNibble(int value) {
Tuxitheone 0:ecaf3e593122 607
Tuxitheone 0:ecaf3e593122 608 // Enable is Low
Tuxitheone 0:ecaf3e593122 609 _setEnable(true);
Tuxitheone 0:ecaf3e593122 610 _setData(value & 0x0F); // Low nibble
Tuxitheone 0:ecaf3e593122 611 wait_us(1); // Data setup time
Tuxitheone 0:ecaf3e593122 612 _setEnable(false);
Tuxitheone 0:ecaf3e593122 613 wait_us(1); // Datahold time
Tuxitheone 0:ecaf3e593122 614
Tuxitheone 0:ecaf3e593122 615 // Enable is Low
Tuxitheone 0:ecaf3e593122 616
Tuxitheone 0:ecaf3e593122 617 }
Tuxitheone 0:ecaf3e593122 618
Tuxitheone 0:ecaf3e593122 619
Tuxitheone 0:ecaf3e593122 620 // Write a byte using the 4-bit interface
Tuxitheone 0:ecaf3e593122 621 // Used for mbed pins, I2C bus expander or SPI shifregister
Tuxitheone 0:ecaf3e593122 622 void TextLCD::_writeByte(int value) {
Tuxitheone 0:ecaf3e593122 623
Tuxitheone 0:ecaf3e593122 624 // Enable is Low
Tuxitheone 0:ecaf3e593122 625 _setEnable(true);
Tuxitheone 0:ecaf3e593122 626 _setData(value >> 4); // High nibble
Tuxitheone 0:ecaf3e593122 627 wait_us(1); // Data setup time
Tuxitheone 0:ecaf3e593122 628 _setEnable(false);
Tuxitheone 0:ecaf3e593122 629 wait_us(1); // Data hold time
Tuxitheone 0:ecaf3e593122 630
Tuxitheone 0:ecaf3e593122 631 _setEnable(true);
Tuxitheone 0:ecaf3e593122 632 _setData(value >> 0); // Low nibble
Tuxitheone 0:ecaf3e593122 633 wait_us(1); // Data setup time
Tuxitheone 0:ecaf3e593122 634 _setEnable(false);
Tuxitheone 0:ecaf3e593122 635 wait_us(1); // Datahold time
Tuxitheone 0:ecaf3e593122 636
Tuxitheone 0:ecaf3e593122 637 // Enable is Low
Tuxitheone 0:ecaf3e593122 638
Tuxitheone 0:ecaf3e593122 639 }
Tuxitheone 0:ecaf3e593122 640
Tuxitheone 0:ecaf3e593122 641 void TextLCD::_writeCommand(int command) {
Tuxitheone 0:ecaf3e593122 642
Tuxitheone 0:ecaf3e593122 643 _setRS(false);
Tuxitheone 0:ecaf3e593122 644 wait_us(1); // Data setup time for RS
Tuxitheone 0:ecaf3e593122 645
Tuxitheone 0:ecaf3e593122 646 _writeByte(command);
Tuxitheone 0:ecaf3e593122 647 wait_us(40); // most instructions take 40us
Tuxitheone 0:ecaf3e593122 648 }
Tuxitheone 0:ecaf3e593122 649
Tuxitheone 0:ecaf3e593122 650 void TextLCD::_writeData(int data) {
Tuxitheone 0:ecaf3e593122 651
Tuxitheone 0:ecaf3e593122 652 _setRS(true);
Tuxitheone 0:ecaf3e593122 653 wait_us(1); // Data setup time for RS
Tuxitheone 0:ecaf3e593122 654
Tuxitheone 0:ecaf3e593122 655 _writeByte(data);
Tuxitheone 0:ecaf3e593122 656 wait_us(40); // data writes take 40us
Tuxitheone 0:ecaf3e593122 657 }
Tuxitheone 0:ecaf3e593122 658
Tuxitheone 0:ecaf3e593122 659
Tuxitheone 0:ecaf3e593122 660 #if (0)
Tuxitheone 0:ecaf3e593122 661 // This is the original _address() method.
Tuxitheone 0:ecaf3e593122 662 // It is confusing since it returns the memoryaddress or-ed with the set memorycommand 0x80.
Tuxitheone 0:ecaf3e593122 663 // Left it in here for compatibility with older code. New applications should use getAddress() instead.
Tuxitheone 0:ecaf3e593122 664 //
Tuxitheone 0:ecaf3e593122 665 int TextLCD::_address(int column, int row) {
Tuxitheone 0:ecaf3e593122 666 switch (_type) {
Tuxitheone 0:ecaf3e593122 667 case LCD20x4:
Tuxitheone 0:ecaf3e593122 668 switch (row) {
Tuxitheone 0:ecaf3e593122 669 case 0:
Tuxitheone 0:ecaf3e593122 670 return 0x80 + column;
Tuxitheone 0:ecaf3e593122 671 case 1:
Tuxitheone 0:ecaf3e593122 672 return 0xc0 + column;
Tuxitheone 0:ecaf3e593122 673 case 2:
Tuxitheone 0:ecaf3e593122 674 return 0x94 + column;
Tuxitheone 0:ecaf3e593122 675 case 3:
Tuxitheone 0:ecaf3e593122 676 return 0xd4 + column;
Tuxitheone 0:ecaf3e593122 677 }
Tuxitheone 0:ecaf3e593122 678 case LCD16x2B:
Tuxitheone 0:ecaf3e593122 679 return 0x80 + (row * 40) + column;
Tuxitheone 0:ecaf3e593122 680 case LCD16x2:
Tuxitheone 0:ecaf3e593122 681 case LCD20x2:
Tuxitheone 0:ecaf3e593122 682 default:
Tuxitheone 0:ecaf3e593122 683 return 0x80 + (row * 0x40) + column;
Tuxitheone 0:ecaf3e593122 684 }
Tuxitheone 0:ecaf3e593122 685 }
Tuxitheone 0:ecaf3e593122 686 #endif
Tuxitheone 0:ecaf3e593122 687
Tuxitheone 0:ecaf3e593122 688
Tuxitheone 0:ecaf3e593122 689 // This replaces the original _address() method.
Tuxitheone 0:ecaf3e593122 690 // Left it in here for compatibility with older code. New applications should use getAddress() instead.
Tuxitheone 0:ecaf3e593122 691 int TextLCD::_address(int column, int row) {
Tuxitheone 0:ecaf3e593122 692 return 0x80 | getAddress(column, row);
Tuxitheone 0:ecaf3e593122 693 }
Tuxitheone 0:ecaf3e593122 694
Tuxitheone 0:ecaf3e593122 695 // This is new method to return the memory address based on row, column and displaytype.
Tuxitheone 0:ecaf3e593122 696 //
Tuxitheone 0:ecaf3e593122 697 int TextLCD::getAddress(int column, int row) {
Tuxitheone 0:ecaf3e593122 698
Tuxitheone 0:ecaf3e593122 699 switch (_type) {
Tuxitheone 0:ecaf3e593122 700 case LCD8x1:
Tuxitheone 0:ecaf3e593122 701 return 0x00 + column;
Tuxitheone 0:ecaf3e593122 702
Tuxitheone 0:ecaf3e593122 703 case LCD8x2B:
Tuxitheone 0:ecaf3e593122 704 // LCD8x2B is a special layout of LCD16x1
Tuxitheone 0:ecaf3e593122 705 if (row==0)
Tuxitheone 0:ecaf3e593122 706 return 0x00 + column;
Tuxitheone 0:ecaf3e593122 707 else
Tuxitheone 0:ecaf3e593122 708 return 0x08 + column;
Tuxitheone 0:ecaf3e593122 709
Tuxitheone 0:ecaf3e593122 710
Tuxitheone 0:ecaf3e593122 711 case LCD16x1:
Tuxitheone 0:ecaf3e593122 712 // LCD16x1 is a special layout of LCD8x2
Tuxitheone 0:ecaf3e593122 713 if (column<8)
Tuxitheone 0:ecaf3e593122 714 return 0x00 + column;
Tuxitheone 0:ecaf3e593122 715 else
Tuxitheone 0:ecaf3e593122 716 return 0x40 + (column - 8);
Tuxitheone 0:ecaf3e593122 717
Tuxitheone 0:ecaf3e593122 718 case LCD12x4:
Tuxitheone 0:ecaf3e593122 719 switch (row) {
Tuxitheone 0:ecaf3e593122 720 case 0:
Tuxitheone 0:ecaf3e593122 721 return 0x00 + column;
Tuxitheone 0:ecaf3e593122 722 case 1:
Tuxitheone 0:ecaf3e593122 723 return 0x40 + column;
Tuxitheone 0:ecaf3e593122 724 case 2:
Tuxitheone 0:ecaf3e593122 725 return 0x0C + column;
Tuxitheone 0:ecaf3e593122 726 case 3:
Tuxitheone 0:ecaf3e593122 727 return 0x4C + column;
Tuxitheone 0:ecaf3e593122 728 }
Tuxitheone 0:ecaf3e593122 729
Tuxitheone 0:ecaf3e593122 730 case LCD16x4:
Tuxitheone 0:ecaf3e593122 731 switch (row) {
Tuxitheone 0:ecaf3e593122 732 case 0:
Tuxitheone 0:ecaf3e593122 733 return 0x00 + column;
Tuxitheone 0:ecaf3e593122 734 case 1:
Tuxitheone 0:ecaf3e593122 735 return 0x40 + column;
Tuxitheone 0:ecaf3e593122 736 case 2:
Tuxitheone 0:ecaf3e593122 737 return 0x10 + column;
Tuxitheone 0:ecaf3e593122 738 case 3:
Tuxitheone 0:ecaf3e593122 739 return 0x50 + column;
Tuxitheone 0:ecaf3e593122 740 }
Tuxitheone 0:ecaf3e593122 741
Tuxitheone 0:ecaf3e593122 742 case LCD20x4:
Tuxitheone 0:ecaf3e593122 743 switch (row) {
Tuxitheone 0:ecaf3e593122 744 case 0:
Tuxitheone 0:ecaf3e593122 745 return 0x00 + column;
Tuxitheone 0:ecaf3e593122 746 case 1:
Tuxitheone 0:ecaf3e593122 747 return 0x40 + column;
Tuxitheone 0:ecaf3e593122 748 case 2:
Tuxitheone 0:ecaf3e593122 749 return 0x14 + column;
Tuxitheone 0:ecaf3e593122 750 case 3:
Tuxitheone 0:ecaf3e593122 751 return 0x54 + column;
Tuxitheone 0:ecaf3e593122 752 }
Tuxitheone 0:ecaf3e593122 753
Tuxitheone 0:ecaf3e593122 754 // Special mode for KS0078
Tuxitheone 0:ecaf3e593122 755 case LCD24x4:
Tuxitheone 0:ecaf3e593122 756 switch (row) {
Tuxitheone 0:ecaf3e593122 757 case 0:
Tuxitheone 0:ecaf3e593122 758 return 0x00 + column;
Tuxitheone 0:ecaf3e593122 759 case 1:
Tuxitheone 0:ecaf3e593122 760 return 0x20 + column;
Tuxitheone 0:ecaf3e593122 761 case 2:
Tuxitheone 0:ecaf3e593122 762 return 0x40 + column;
Tuxitheone 0:ecaf3e593122 763 case 3:
Tuxitheone 0:ecaf3e593122 764 return 0x60 + column;
Tuxitheone 0:ecaf3e593122 765 }
Tuxitheone 0:ecaf3e593122 766
Tuxitheone 0:ecaf3e593122 767 // Not sure about this one, seems wrong.
Tuxitheone 0:ecaf3e593122 768 case LCD16x2B:
Tuxitheone 0:ecaf3e593122 769 return 0x00 + (row * 40) + column;
Tuxitheone 0:ecaf3e593122 770
Tuxitheone 0:ecaf3e593122 771 case LCD8x2:
Tuxitheone 0:ecaf3e593122 772 case LCD12x2:
Tuxitheone 0:ecaf3e593122 773 case LCD16x2:
Tuxitheone 0:ecaf3e593122 774 case LCD20x2:
Tuxitheone 0:ecaf3e593122 775 case LCD24x2:
Tuxitheone 0:ecaf3e593122 776 case LCD40x2:
Tuxitheone 0:ecaf3e593122 777 return 0x00 + (row * 0x40) + column;
Tuxitheone 0:ecaf3e593122 778
Tuxitheone 0:ecaf3e593122 779 case LCD40x4:
Tuxitheone 0:ecaf3e593122 780 // LCD40x4 is a special case since it has 2 controllers
Tuxitheone 0:ecaf3e593122 781 // Each controller is configured as 40x2
Tuxitheone 0:ecaf3e593122 782 if (row<2) {
Tuxitheone 0:ecaf3e593122 783 // Test to see if we need to switch between controllers
Tuxitheone 0:ecaf3e593122 784 if (_ctrl_idx != _LCDCtrl_0) {
Tuxitheone 0:ecaf3e593122 785
Tuxitheone 0:ecaf3e593122 786 // Second LCD controller Cursor Off
Tuxitheone 0:ecaf3e593122 787 _setCursorAndDisplayMode(_currentMode, TextLCD::CurOff_BlkOff);
Tuxitheone 0:ecaf3e593122 788
Tuxitheone 0:ecaf3e593122 789 // Select primary controller
Tuxitheone 0:ecaf3e593122 790 _ctrl_idx = _LCDCtrl_0;
Tuxitheone 0:ecaf3e593122 791
Tuxitheone 0:ecaf3e593122 792 // Restore cursormode on primary LCD controller
Tuxitheone 0:ecaf3e593122 793 _setCursorAndDisplayMode(_currentMode, _currentCursor);
Tuxitheone 0:ecaf3e593122 794 }
Tuxitheone 0:ecaf3e593122 795
Tuxitheone 0:ecaf3e593122 796 return 0x00 + (row * 0x40) + column;
Tuxitheone 0:ecaf3e593122 797 }
Tuxitheone 0:ecaf3e593122 798 else {
Tuxitheone 0:ecaf3e593122 799
Tuxitheone 0:ecaf3e593122 800 // Test to see if we need to switch between controllers
Tuxitheone 0:ecaf3e593122 801 if (_ctrl_idx != _LCDCtrl_1) {
Tuxitheone 0:ecaf3e593122 802 // Primary LCD controller Cursor Off
Tuxitheone 0:ecaf3e593122 803 _setCursorAndDisplayMode(_currentMode, TextLCD::CurOff_BlkOff);
Tuxitheone 0:ecaf3e593122 804
Tuxitheone 0:ecaf3e593122 805 // Select secondary controller
Tuxitheone 0:ecaf3e593122 806 _ctrl_idx = _LCDCtrl_1;
Tuxitheone 0:ecaf3e593122 807
Tuxitheone 0:ecaf3e593122 808 // Restore cursormode on secondary LCD controller
Tuxitheone 0:ecaf3e593122 809 _setCursorAndDisplayMode(_currentMode, _currentCursor);
Tuxitheone 0:ecaf3e593122 810 }
Tuxitheone 0:ecaf3e593122 811
Tuxitheone 0:ecaf3e593122 812 return 0x00 + ((row-2) * 0x40) + column;
Tuxitheone 0:ecaf3e593122 813 }
Tuxitheone 0:ecaf3e593122 814
Tuxitheone 0:ecaf3e593122 815 // Should never get here.
Tuxitheone 0:ecaf3e593122 816 default:
Tuxitheone 0:ecaf3e593122 817 return 0x00;
Tuxitheone 0:ecaf3e593122 818 }
Tuxitheone 0:ecaf3e593122 819 }
Tuxitheone 0:ecaf3e593122 820
Tuxitheone 0:ecaf3e593122 821
Tuxitheone 0:ecaf3e593122 822 // Set row, column and update memoryaddress.
Tuxitheone 0:ecaf3e593122 823 //
Tuxitheone 0:ecaf3e593122 824 void TextLCD::setAddress(int column, int row) {
Tuxitheone 0:ecaf3e593122 825
Tuxitheone 0:ecaf3e593122 826 // Sanity Check column
Tuxitheone 0:ecaf3e593122 827 if (column < 0) {
Tuxitheone 0:ecaf3e593122 828 _column = 0;
Tuxitheone 0:ecaf3e593122 829 }
Tuxitheone 0:ecaf3e593122 830 else if (column >= columns()) {
Tuxitheone 0:ecaf3e593122 831 _column = columns() - 1;
Tuxitheone 0:ecaf3e593122 832 } else _column = column;
Tuxitheone 0:ecaf3e593122 833
Tuxitheone 0:ecaf3e593122 834 // Sanity Check row
Tuxitheone 0:ecaf3e593122 835 if (row < 0) {
Tuxitheone 0:ecaf3e593122 836 _row = 0;
Tuxitheone 0:ecaf3e593122 837 }
Tuxitheone 0:ecaf3e593122 838 else if (row >= rows()) {
Tuxitheone 0:ecaf3e593122 839 _row = rows() - 1;
Tuxitheone 0:ecaf3e593122 840 } else _row = row;
Tuxitheone 0:ecaf3e593122 841
Tuxitheone 0:ecaf3e593122 842
Tuxitheone 0:ecaf3e593122 843 // Compute the memory address
Tuxitheone 0:ecaf3e593122 844 // For LCD40x4: switch controllers if needed
Tuxitheone 0:ecaf3e593122 845 // switch cursor if needed
Tuxitheone 0:ecaf3e593122 846 int addr = getAddress(_column, _row);
Tuxitheone 0:ecaf3e593122 847
Tuxitheone 0:ecaf3e593122 848 _writeCommand(0x80 | addr);
Tuxitheone 0:ecaf3e593122 849 }
Tuxitheone 0:ecaf3e593122 850
Tuxitheone 0:ecaf3e593122 851 int TextLCD::columns() {
Tuxitheone 0:ecaf3e593122 852 switch (_type) {
Tuxitheone 0:ecaf3e593122 853 case LCD8x1:
Tuxitheone 0:ecaf3e593122 854 case LCD8x2:
Tuxitheone 0:ecaf3e593122 855 case LCD8x2B:
Tuxitheone 0:ecaf3e593122 856 return 8;
Tuxitheone 0:ecaf3e593122 857
Tuxitheone 0:ecaf3e593122 858 case LCD12x2:
Tuxitheone 0:ecaf3e593122 859 case LCD12x4:
Tuxitheone 0:ecaf3e593122 860 return 12;
Tuxitheone 0:ecaf3e593122 861
Tuxitheone 0:ecaf3e593122 862 case LCD16x1:
Tuxitheone 0:ecaf3e593122 863 case LCD16x2:
Tuxitheone 0:ecaf3e593122 864 case LCD16x2B:
Tuxitheone 0:ecaf3e593122 865 case LCD16x4:
Tuxitheone 0:ecaf3e593122 866 return 16;
Tuxitheone 0:ecaf3e593122 867
Tuxitheone 0:ecaf3e593122 868 case LCD20x2:
Tuxitheone 0:ecaf3e593122 869 case LCD20x4:
Tuxitheone 0:ecaf3e593122 870 return 20;
Tuxitheone 0:ecaf3e593122 871
Tuxitheone 0:ecaf3e593122 872 case LCD24x2:
Tuxitheone 0:ecaf3e593122 873 case LCD24x4:
Tuxitheone 0:ecaf3e593122 874 return 24;
Tuxitheone 0:ecaf3e593122 875
Tuxitheone 0:ecaf3e593122 876 case LCD40x2:
Tuxitheone 0:ecaf3e593122 877 case LCD40x4:
Tuxitheone 0:ecaf3e593122 878 return 40;
Tuxitheone 0:ecaf3e593122 879
Tuxitheone 0:ecaf3e593122 880 // Should never get here.
Tuxitheone 0:ecaf3e593122 881 default:
Tuxitheone 0:ecaf3e593122 882 return 0;
Tuxitheone 0:ecaf3e593122 883 }
Tuxitheone 0:ecaf3e593122 884 }
Tuxitheone 0:ecaf3e593122 885
Tuxitheone 0:ecaf3e593122 886 int TextLCD::rows() {
Tuxitheone 0:ecaf3e593122 887 switch (_type) {
Tuxitheone 0:ecaf3e593122 888 case LCD8x1:
Tuxitheone 0:ecaf3e593122 889 case LCD16x1:
Tuxitheone 0:ecaf3e593122 890 return 1;
Tuxitheone 0:ecaf3e593122 891
Tuxitheone 0:ecaf3e593122 892 case LCD8x2:
Tuxitheone 0:ecaf3e593122 893 case LCD8x2B:
Tuxitheone 0:ecaf3e593122 894 case LCD12x2:
Tuxitheone 0:ecaf3e593122 895 case LCD16x2:
Tuxitheone 0:ecaf3e593122 896 case LCD16x2B:
Tuxitheone 0:ecaf3e593122 897 case LCD20x2:
Tuxitheone 0:ecaf3e593122 898 case LCD24x2:
Tuxitheone 0:ecaf3e593122 899 case LCD40x2:
Tuxitheone 0:ecaf3e593122 900 return 2;
Tuxitheone 0:ecaf3e593122 901
Tuxitheone 0:ecaf3e593122 902 case LCD12x4:
Tuxitheone 0:ecaf3e593122 903 case LCD16x4:
Tuxitheone 0:ecaf3e593122 904 case LCD20x4:
Tuxitheone 0:ecaf3e593122 905 case LCD24x4:
Tuxitheone 0:ecaf3e593122 906 case LCD40x4:
Tuxitheone 0:ecaf3e593122 907 return 4;
Tuxitheone 0:ecaf3e593122 908
Tuxitheone 0:ecaf3e593122 909 // Should never get here.
Tuxitheone 0:ecaf3e593122 910 default:
Tuxitheone 0:ecaf3e593122 911 return 0;
Tuxitheone 0:ecaf3e593122 912 }
Tuxitheone 0:ecaf3e593122 913 }
Tuxitheone 0:ecaf3e593122 914
Tuxitheone 0:ecaf3e593122 915
Tuxitheone 0:ecaf3e593122 916 // Set the Cursor Mode (Cursor Off & Blink Off, Cursor On & Blink Off, Cursor Off & Blink On, Cursor On & Blink On
Tuxitheone 0:ecaf3e593122 917 void TextLCD::setCursor(TextLCD::LCDCursor cursorMode) {
Tuxitheone 0:ecaf3e593122 918
Tuxitheone 0:ecaf3e593122 919 // Save new cursor mode, needed when 2 controllers are in use or when display is switched off/on
Tuxitheone 0:ecaf3e593122 920 _currentCursor = cursorMode;
Tuxitheone 0:ecaf3e593122 921
Tuxitheone 0:ecaf3e593122 922 // Configure only current LCD controller
Tuxitheone 0:ecaf3e593122 923 _setCursorAndDisplayMode(_currentMode, _currentCursor);
Tuxitheone 0:ecaf3e593122 924
Tuxitheone 0:ecaf3e593122 925 }
Tuxitheone 0:ecaf3e593122 926
Tuxitheone 0:ecaf3e593122 927 // Set the Displaymode (On/Off)
Tuxitheone 0:ecaf3e593122 928 void TextLCD::setMode(TextLCD::LCDMode displayMode) {
Tuxitheone 0:ecaf3e593122 929
Tuxitheone 0:ecaf3e593122 930 // Save new displayMode, needed when 2 controllers are in use or when cursor is changed
Tuxitheone 0:ecaf3e593122 931 _currentMode = displayMode;
Tuxitheone 0:ecaf3e593122 932
Tuxitheone 0:ecaf3e593122 933 // Select and configure second LCD controller when needed
Tuxitheone 0:ecaf3e593122 934 if(_type==LCD40x4) {
Tuxitheone 0:ecaf3e593122 935 if (_ctrl_idx==TextLCD::_LCDCtrl_0) {
Tuxitheone 0:ecaf3e593122 936 // Configure primary LCD controller
Tuxitheone 0:ecaf3e593122 937 _setCursorAndDisplayMode(_currentMode, _currentCursor);
Tuxitheone 0:ecaf3e593122 938
Tuxitheone 0:ecaf3e593122 939 // Select 2nd controller
Tuxitheone 0:ecaf3e593122 940 _ctrl_idx=TextLCD::_LCDCtrl_1;
Tuxitheone 0:ecaf3e593122 941
Tuxitheone 0:ecaf3e593122 942 // Configure secondary LCD controller
Tuxitheone 0:ecaf3e593122 943 _setCursorAndDisplayMode(_currentMode, TextLCD::CurOff_BlkOff);
Tuxitheone 0:ecaf3e593122 944
Tuxitheone 0:ecaf3e593122 945 // Restore current controller
Tuxitheone 0:ecaf3e593122 946 _ctrl_idx=TextLCD::_LCDCtrl_0;
Tuxitheone 0:ecaf3e593122 947 }
Tuxitheone 0:ecaf3e593122 948 else {
Tuxitheone 0:ecaf3e593122 949 // Select primary controller
Tuxitheone 0:ecaf3e593122 950 _ctrl_idx=TextLCD::_LCDCtrl_0;
Tuxitheone 0:ecaf3e593122 951
Tuxitheone 0:ecaf3e593122 952 // Configure primary LCD controller
Tuxitheone 0:ecaf3e593122 953 _setCursorAndDisplayMode(_currentMode, TextLCD::CurOff_BlkOff);
Tuxitheone 0:ecaf3e593122 954
Tuxitheone 0:ecaf3e593122 955 // Restore current controller
Tuxitheone 0:ecaf3e593122 956 _ctrl_idx=TextLCD::_LCDCtrl_1;
Tuxitheone 0:ecaf3e593122 957
Tuxitheone 0:ecaf3e593122 958 // Configure secondary LCD controller
Tuxitheone 0:ecaf3e593122 959 _setCursorAndDisplayMode(_currentMode, _currentCursor);
Tuxitheone 0:ecaf3e593122 960
Tuxitheone 0:ecaf3e593122 961 }
Tuxitheone 0:ecaf3e593122 962 }
Tuxitheone 0:ecaf3e593122 963 else {
Tuxitheone 0:ecaf3e593122 964 // Configure primary LCD controller
Tuxitheone 0:ecaf3e593122 965 _setCursorAndDisplayMode(_currentMode, _currentCursor);
Tuxitheone 0:ecaf3e593122 966 }
Tuxitheone 0:ecaf3e593122 967
Tuxitheone 0:ecaf3e593122 968 }
Tuxitheone 0:ecaf3e593122 969
Tuxitheone 0:ecaf3e593122 970
Tuxitheone 0:ecaf3e593122 971 // Set the Displaymode (On/Off) and Cursortype for current controller
Tuxitheone 0:ecaf3e593122 972 void TextLCD::_setCursorAndDisplayMode(TextLCD::LCDMode displayMode, TextLCD::LCDCursor cursorType) {
Tuxitheone 0:ecaf3e593122 973
Tuxitheone 0:ecaf3e593122 974 // Configure current LCD controller
Tuxitheone 0:ecaf3e593122 975 _writeCommand(0x08 | displayMode | cursorType);
Tuxitheone 0:ecaf3e593122 976 }
Tuxitheone 0:ecaf3e593122 977
Tuxitheone 0:ecaf3e593122 978 // Set the Backlight mode (Off/On)
Tuxitheone 0:ecaf3e593122 979 void TextLCD::setBacklight(TextLCD::LCDBacklight backlightMode) {
Tuxitheone 0:ecaf3e593122 980
Tuxitheone 0:ecaf3e593122 981 if (backlightMode == LightOn) {
Tuxitheone 0:ecaf3e593122 982 _setBL(true);
Tuxitheone 0:ecaf3e593122 983 }
Tuxitheone 0:ecaf3e593122 984 else {
Tuxitheone 0:ecaf3e593122 985 _setBL(false);
Tuxitheone 0:ecaf3e593122 986 }
Tuxitheone 0:ecaf3e593122 987 }
Tuxitheone 0:ecaf3e593122 988
Tuxitheone 0:ecaf3e593122 989
Tuxitheone 0:ecaf3e593122 990 void TextLCD::setUDC(unsigned char c, char *udc_data) {
Tuxitheone 0:ecaf3e593122 991
Tuxitheone 0:ecaf3e593122 992 // Select and configure second LCD controller when needed
Tuxitheone 0:ecaf3e593122 993 if(_type==LCD40x4) {
Tuxitheone 0:ecaf3e593122 994 _LCDCtrl_Idx current_ctrl_idx = _ctrl_idx; // Temp save current controller
Tuxitheone 0:ecaf3e593122 995
Tuxitheone 0:ecaf3e593122 996 // Select primary controller
Tuxitheone 0:ecaf3e593122 997 _ctrl_idx=TextLCD::_LCDCtrl_0;
Tuxitheone 0:ecaf3e593122 998
Tuxitheone 0:ecaf3e593122 999 // Configure primary LCD controller
Tuxitheone 0:ecaf3e593122 1000 _setUDC(c, udc_data);
Tuxitheone 0:ecaf3e593122 1001
Tuxitheone 0:ecaf3e593122 1002 // Select 2nd controller
Tuxitheone 0:ecaf3e593122 1003 _ctrl_idx=TextLCD::_LCDCtrl_1;
Tuxitheone 0:ecaf3e593122 1004
Tuxitheone 0:ecaf3e593122 1005 // Configure secondary LCD controller
Tuxitheone 0:ecaf3e593122 1006 _setUDC(c, udc_data);
Tuxitheone 0:ecaf3e593122 1007
Tuxitheone 0:ecaf3e593122 1008 // Restore current controller
Tuxitheone 0:ecaf3e593122 1009 _ctrl_idx=current_ctrl_idx;
Tuxitheone 0:ecaf3e593122 1010 }
Tuxitheone 0:ecaf3e593122 1011 else {
Tuxitheone 0:ecaf3e593122 1012 // Configure primary LCD controller
Tuxitheone 0:ecaf3e593122 1013 _setUDC(c, udc_data);
Tuxitheone 0:ecaf3e593122 1014 }
Tuxitheone 0:ecaf3e593122 1015
Tuxitheone 0:ecaf3e593122 1016 }
Tuxitheone 0:ecaf3e593122 1017
Tuxitheone 0:ecaf3e593122 1018 void TextLCD::_setUDC(unsigned char c, char *udc_data) {
Tuxitheone 0:ecaf3e593122 1019
Tuxitheone 0:ecaf3e593122 1020 // Select CG RAM for current LCD controller
Tuxitheone 0:ecaf3e593122 1021 _writeCommand(0x40 + ((c & 0x07) << 3)); //Set CG-RAM address,
Tuxitheone 0:ecaf3e593122 1022 //8 sequential locations needed per UDC
Tuxitheone 0:ecaf3e593122 1023 // Store UDC pattern
Tuxitheone 0:ecaf3e593122 1024 for (int i=0; i<8; i++) {
Tuxitheone 0:ecaf3e593122 1025 _writeData(*udc_data++);
Tuxitheone 0:ecaf3e593122 1026 }
Tuxitheone 0:ecaf3e593122 1027
Tuxitheone 0:ecaf3e593122 1028 //Select DD RAM again for current LCD controller
Tuxitheone 0:ecaf3e593122 1029 int addr = getAddress(_column, _row);
Tuxitheone 0:ecaf3e593122 1030 _writeCommand(0x80 | addr);
Tuxitheone 0:ecaf3e593122 1031
Tuxitheone 0:ecaf3e593122 1032 }