local copy of rgplcd shield

Dependents:  

Fork of AdaFruit_RGBLCDShield by Justin Howard

Committer:
vtraveller
Date:
Sun Aug 03 15:03:47 2014 +0000
Revision:
0:45264ce231f9
Child:
1:24ab601221e2
Moved AdaFruit_RGBShield to a library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vtraveller 0:45264ce231f9 1 /***************************************************
vtraveller 0:45264ce231f9 2 This is a library for the Adafruit RGB 16x2 LCD Shield
vtraveller 0:45264ce231f9 3 Pick one up at the Adafruit shop!
vtraveller 0:45264ce231f9 4 ---------> http://http://www.adafruit.com/products/714
vtraveller 0:45264ce231f9 5
vtraveller 0:45264ce231f9 6 The shield uses I2C to communicate, 2 pins are required to
vtraveller 0:45264ce231f9 7 interface
vtraveller 0:45264ce231f9 8 Adafruit invests time and resources providing this open source code,
vtraveller 0:45264ce231f9 9 please support Adafruit and open-source hardware by purchasing
vtraveller 0:45264ce231f9 10 products from Adafruit!
vtraveller 0:45264ce231f9 11
vtraveller 0:45264ce231f9 12 Written by Limor Fried/Ladyada for Adafruit Industries.
vtraveller 0:45264ce231f9 13 BSD license, all text above must be included in any redistribution
vtraveller 0:45264ce231f9 14 ****************************************************/
vtraveller 0:45264ce231f9 15
vtraveller 0:45264ce231f9 16 #include "Adafruit_RGBLCDShield.h"
vtraveller 0:45264ce231f9 17
vtraveller 0:45264ce231f9 18 #include <stdio.h>
vtraveller 0:45264ce231f9 19 #include <string.h>
vtraveller 0:45264ce231f9 20 #include <inttypes.h>
vtraveller 0:45264ce231f9 21
vtraveller 0:45264ce231f9 22 // New MBED code to handle porting
vtraveller 0:45264ce231f9 23 #define OUTPUT DIR_OUTPUT
vtraveller 0:45264ce231f9 24 #define INPUT DIR_INPUT
vtraveller 0:45264ce231f9 25
vtraveller 0:45264ce231f9 26 #define LOW 0
vtraveller 0:45264ce231f9 27 #define HIGH 1
vtraveller 0:45264ce231f9 28
vtraveller 0:45264ce231f9 29 #define delayMicroseconds(a) wait(a / 1000000)
vtraveller 0:45264ce231f9 30
vtraveller 0:45264ce231f9 31 /* MBED TURNED OFF
vtraveller 0:45264ce231f9 32
vtraveller 0:45264ce231f9 33 Arduino i2c (wire) interface
vtraveller 0:45264ce231f9 34
vtraveller 0:45264ce231f9 35 #include <Wire.h>
vtraveller 0:45264ce231f9 36 #ifdef __AVR__
vtraveller 0:45264ce231f9 37 #define WIRE Wire
vtraveller 0:45264ce231f9 38 #else // Arduino Due
vtraveller 0:45264ce231f9 39 #define WIRE Wire1
vtraveller 0:45264ce231f9 40 #endif
vtraveller 0:45264ce231f9 41
vtraveller 0:45264ce231f9 42 #if ARDUINO >= 100
vtraveller 0:45264ce231f9 43 #include "Arduino.h"
vtraveller 0:45264ce231f9 44 #else
vtraveller 0:45264ce231f9 45 #include "WProgram.h"
vtraveller 0:45264ce231f9 46 #endif
vtraveller 0:45264ce231f9 47 */
vtraveller 0:45264ce231f9 48
vtraveller 0:45264ce231f9 49 // When the display powers up, it is configured as follows:
vtraveller 0:45264ce231f9 50 //
vtraveller 0:45264ce231f9 51 // 1. Display clear
vtraveller 0:45264ce231f9 52 // 2. Function set:
vtraveller 0:45264ce231f9 53 // DL = 1; 8-bit interface data
vtraveller 0:45264ce231f9 54 // N = 0; 1-line display
vtraveller 0:45264ce231f9 55 // F = 0; 5x8 dot character font
vtraveller 0:45264ce231f9 56 // 3. Display on/off control:
vtraveller 0:45264ce231f9 57 // D = 0; Display off
vtraveller 0:45264ce231f9 58 // C = 0; Cursor off
vtraveller 0:45264ce231f9 59 // B = 0; Blinking off
vtraveller 0:45264ce231f9 60 // 4. Entry mode set:
vtraveller 0:45264ce231f9 61 // I/D = 1; Increment by 1
vtraveller 0:45264ce231f9 62 // S = 0; No shift
vtraveller 0:45264ce231f9 63 //
vtraveller 0:45264ce231f9 64 // Note, however, that resetting the Arduino doesn't reset the LCD, so we
vtraveller 0:45264ce231f9 65 // can't assume that its in that state when a sketch starts (and the
vtraveller 0:45264ce231f9 66 // RGBLCDShield constructor is called).
vtraveller 0:45264ce231f9 67
vtraveller 0:45264ce231f9 68 // MBED - wired in serial
vtraveller 0:45264ce231f9 69 // MBED - wired in _i2c
vtraveller 0:45264ce231f9 70 Adafruit_RGBLCDShield::Adafruit_RGBLCDShield(MCP23017 & inMCP)
vtraveller 0:45264ce231f9 71 : Serial(SERIAL_TX, SERIAL_RX)
vtraveller 0:45264ce231f9 72 , _i2c(inMCP)
vtraveller 0:45264ce231f9 73 {
vtraveller 0:45264ce231f9 74 _i2cAddr = 0;
vtraveller 0:45264ce231f9 75
vtraveller 0:45264ce231f9 76 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
vtraveller 0:45264ce231f9 77
vtraveller 0:45264ce231f9 78 // the I/O expander pinout
vtraveller 0:45264ce231f9 79 _rs_pin = 15;
vtraveller 0:45264ce231f9 80 _rw_pin = 14;
vtraveller 0:45264ce231f9 81 _enable_pin = 13;
vtraveller 0:45264ce231f9 82 _data_pins[0] = 12; // really d4
vtraveller 0:45264ce231f9 83 _data_pins[1] = 11; // really d5
vtraveller 0:45264ce231f9 84 _data_pins[2] = 10; // really d6
vtraveller 0:45264ce231f9 85 _data_pins[3] = 9; // really d7
vtraveller 0:45264ce231f9 86
vtraveller 0:45264ce231f9 87 _button_pins[0] = 0;
vtraveller 0:45264ce231f9 88 _button_pins[1] = 1;
vtraveller 0:45264ce231f9 89 _button_pins[2] = 2;
vtraveller 0:45264ce231f9 90 _button_pins[3] = 3;
vtraveller 0:45264ce231f9 91 _button_pins[4] = 4;
vtraveller 0:45264ce231f9 92 // we can't begin() yet :(
vtraveller 0:45264ce231f9 93 }
vtraveller 0:45264ce231f9 94
vtraveller 0:45264ce231f9 95 void Adafruit_RGBLCDShield::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
vtraveller 0:45264ce231f9 96 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
vtraveller 0:45264ce231f9 97 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
vtraveller 0:45264ce231f9 98 {
vtraveller 0:45264ce231f9 99 _rs_pin = rs;
vtraveller 0:45264ce231f9 100 _rw_pin = rw;
vtraveller 0:45264ce231f9 101 _enable_pin = enable;
vtraveller 0:45264ce231f9 102
vtraveller 0:45264ce231f9 103 _data_pins[0] = d0;
vtraveller 0:45264ce231f9 104 _data_pins[1] = d1;
vtraveller 0:45264ce231f9 105 _data_pins[2] = d2;
vtraveller 0:45264ce231f9 106 _data_pins[3] = d3;
vtraveller 0:45264ce231f9 107 _data_pins[4] = d4;
vtraveller 0:45264ce231f9 108 _data_pins[5] = d5;
vtraveller 0:45264ce231f9 109 _data_pins[6] = d6;
vtraveller 0:45264ce231f9 110 _data_pins[7] = d7;
vtraveller 0:45264ce231f9 111
vtraveller 0:45264ce231f9 112 _i2cAddr = 255;
vtraveller 0:45264ce231f9 113
vtraveller 0:45264ce231f9 114 _pinMode(_rs_pin, OUTPUT);
vtraveller 0:45264ce231f9 115 // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
vtraveller 0:45264ce231f9 116 if (_rw_pin != 255) {
vtraveller 0:45264ce231f9 117 _pinMode(_rw_pin, OUTPUT);
vtraveller 0:45264ce231f9 118 }
vtraveller 0:45264ce231f9 119 _pinMode(_enable_pin, OUTPUT);
vtraveller 0:45264ce231f9 120
vtraveller 0:45264ce231f9 121
vtraveller 0:45264ce231f9 122 if (fourbitmode)
vtraveller 0:45264ce231f9 123 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
vtraveller 0:45264ce231f9 124 else
vtraveller 0:45264ce231f9 125 _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
vtraveller 0:45264ce231f9 126
vtraveller 0:45264ce231f9 127 begin(16, 1);
vtraveller 0:45264ce231f9 128 }
vtraveller 0:45264ce231f9 129
vtraveller 0:45264ce231f9 130 void Adafruit_RGBLCDShield::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
vtraveller 0:45264ce231f9 131 // check if i2c
vtraveller 0:45264ce231f9 132 if (_i2cAddr != 255) {
vtraveller 0:45264ce231f9 133 //_i2c.begin(_i2cAddr);
vtraveller 0:45264ce231f9 134 //WIRE.begin();
vtraveller 0:45264ce231f9 135
vtraveller 0:45264ce231f9 136 _i2c.reset();
vtraveller 0:45264ce231f9 137
vtraveller 0:45264ce231f9 138 _i2c.pinMode(8, OUTPUT);
vtraveller 0:45264ce231f9 139 _i2c.pinMode(6, OUTPUT);
vtraveller 0:45264ce231f9 140 _i2c.pinMode(7, OUTPUT);
vtraveller 0:45264ce231f9 141
vtraveller 0:45264ce231f9 142 setBacklight(0x7);
vtraveller 0:45264ce231f9 143
vtraveller 0:45264ce231f9 144 if (_rw_pin)
vtraveller 0:45264ce231f9 145 _i2c.pinMode(_rw_pin, OUTPUT);
vtraveller 0:45264ce231f9 146
vtraveller 0:45264ce231f9 147 _i2c.pinMode(_rs_pin, OUTPUT);
vtraveller 0:45264ce231f9 148 _i2c.pinMode(_enable_pin, OUTPUT);
vtraveller 0:45264ce231f9 149 for (uint8_t i=0; i<4; i++)
vtraveller 0:45264ce231f9 150 _i2c.pinMode(_data_pins[i], OUTPUT);
vtraveller 0:45264ce231f9 151
vtraveller 0:45264ce231f9 152 unsigned short nPullups = 0;
vtraveller 0:45264ce231f9 153 for (uint8_t i=0; i<5; i++) {
vtraveller 0:45264ce231f9 154 _i2c.pinMode(_button_pins[i], INPUT);
vtraveller 0:45264ce231f9 155 nPullups |= (1 << _button_pins[i]);
vtraveller 0:45264ce231f9 156 //_i2c.pullUp(_button_pins[i], 1);
vtraveller 0:45264ce231f9 157 }
vtraveller 0:45264ce231f9 158
vtraveller 0:45264ce231f9 159 _i2c.internalPullupMask(nPullups);
vtraveller 0:45264ce231f9 160 }
vtraveller 0:45264ce231f9 161
vtraveller 0:45264ce231f9 162 if (lines > 1) {
vtraveller 0:45264ce231f9 163 _displayfunction |= LCD_2LINE;
vtraveller 0:45264ce231f9 164 }
vtraveller 0:45264ce231f9 165 _numlines = lines;
vtraveller 0:45264ce231f9 166 _currline = 0;
vtraveller 0:45264ce231f9 167
vtraveller 0:45264ce231f9 168 // for some 1 line displays you can select a 10 pixel high font
vtraveller 0:45264ce231f9 169 if ((dotsize != 0) && (lines == 1)) {
vtraveller 0:45264ce231f9 170 _displayfunction |= LCD_5x10DOTS;
vtraveller 0:45264ce231f9 171 }
vtraveller 0:45264ce231f9 172
vtraveller 0:45264ce231f9 173 // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
vtraveller 0:45264ce231f9 174 // according to datasheet, we need at least 40ms after power rises above 2.7V
vtraveller 0:45264ce231f9 175 // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
vtraveller 0:45264ce231f9 176 delayMicroseconds(50000);
vtraveller 0:45264ce231f9 177 // Now we pull both RS and R/W low to begin commands
vtraveller 0:45264ce231f9 178 _digitalWrite(_rs_pin, LOW);
vtraveller 0:45264ce231f9 179 _digitalWrite(_enable_pin, LOW);
vtraveller 0:45264ce231f9 180 if (_rw_pin != 255) {
vtraveller 0:45264ce231f9 181 _digitalWrite(_rw_pin, LOW);
vtraveller 0:45264ce231f9 182 }
vtraveller 0:45264ce231f9 183
vtraveller 0:45264ce231f9 184 //put the LCD into 4 bit or 8 bit mode
vtraveller 0:45264ce231f9 185 if (! (_displayfunction & LCD_8BITMODE)) {
vtraveller 0:45264ce231f9 186 // this is according to the hitachi HD44780 datasheet
vtraveller 0:45264ce231f9 187 // figure 24, pg 46
vtraveller 0:45264ce231f9 188
vtraveller 0:45264ce231f9 189 // we start in 8bit mode, try to set 4 bit mode
vtraveller 0:45264ce231f9 190 write4bits(0x03);
vtraveller 0:45264ce231f9 191 delayMicroseconds(4500); // wait min 4.1ms
vtraveller 0:45264ce231f9 192
vtraveller 0:45264ce231f9 193 // second try
vtraveller 0:45264ce231f9 194 write4bits(0x03);
vtraveller 0:45264ce231f9 195 delayMicroseconds(4500); // wait min 4.1ms
vtraveller 0:45264ce231f9 196
vtraveller 0:45264ce231f9 197 // third go!
vtraveller 0:45264ce231f9 198 write4bits(0x03);
vtraveller 0:45264ce231f9 199 delayMicroseconds(150);
vtraveller 0:45264ce231f9 200
vtraveller 0:45264ce231f9 201 // finally, set to 8-bit interface
vtraveller 0:45264ce231f9 202 write4bits(0x02);
vtraveller 0:45264ce231f9 203 } else {
vtraveller 0:45264ce231f9 204 // this is according to the hitachi HD44780 datasheet
vtraveller 0:45264ce231f9 205 // page 45 figure 23
vtraveller 0:45264ce231f9 206
vtraveller 0:45264ce231f9 207 // Send function set command sequence
vtraveller 0:45264ce231f9 208 command(LCD_FUNCTIONSET | _displayfunction);
vtraveller 0:45264ce231f9 209 delayMicroseconds(4500); // wait more than 4.1ms
vtraveller 0:45264ce231f9 210
vtraveller 0:45264ce231f9 211 // second try
vtraveller 0:45264ce231f9 212 command(LCD_FUNCTIONSET | _displayfunction);
vtraveller 0:45264ce231f9 213 delayMicroseconds(150);
vtraveller 0:45264ce231f9 214
vtraveller 0:45264ce231f9 215 // third go
vtraveller 0:45264ce231f9 216 command(LCD_FUNCTIONSET | _displayfunction);
vtraveller 0:45264ce231f9 217 }
vtraveller 0:45264ce231f9 218
vtraveller 0:45264ce231f9 219 // finally, set # lines, font size, etc.
vtraveller 0:45264ce231f9 220 command(LCD_FUNCTIONSET | _displayfunction);
vtraveller 0:45264ce231f9 221
vtraveller 0:45264ce231f9 222 // turn the display on with no cursor or blinking default
vtraveller 0:45264ce231f9 223 _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
vtraveller 0:45264ce231f9 224 display();
vtraveller 0:45264ce231f9 225
vtraveller 0:45264ce231f9 226 // clear it off
vtraveller 0:45264ce231f9 227 clear();
vtraveller 0:45264ce231f9 228
vtraveller 0:45264ce231f9 229 // Initialize to default text direction (for romance languages)
vtraveller 0:45264ce231f9 230 _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
vtraveller 0:45264ce231f9 231 // set the entry mode
vtraveller 0:45264ce231f9 232 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 233 }
vtraveller 0:45264ce231f9 234
vtraveller 0:45264ce231f9 235 /********** high level commands, for the user! */
vtraveller 0:45264ce231f9 236 void Adafruit_RGBLCDShield::clear()
vtraveller 0:45264ce231f9 237 {
vtraveller 0:45264ce231f9 238 command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
vtraveller 0:45264ce231f9 239 delayMicroseconds(2000); // this command takes a long time!
vtraveller 0:45264ce231f9 240 }
vtraveller 0:45264ce231f9 241
vtraveller 0:45264ce231f9 242 void Adafruit_RGBLCDShield::home()
vtraveller 0:45264ce231f9 243 {
vtraveller 0:45264ce231f9 244 command(LCD_RETURNHOME); // set cursor position to zero
vtraveller 0:45264ce231f9 245 delayMicroseconds(2000); // this command takes a long time!
vtraveller 0:45264ce231f9 246 }
vtraveller 0:45264ce231f9 247
vtraveller 0:45264ce231f9 248 void Adafruit_RGBLCDShield::setCursor(uint8_t col, uint8_t row)
vtraveller 0:45264ce231f9 249 {
vtraveller 0:45264ce231f9 250 int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
vtraveller 0:45264ce231f9 251 if ( row > _numlines ) {
vtraveller 0:45264ce231f9 252 row = _numlines-1; // we count rows starting w/0
vtraveller 0:45264ce231f9 253 }
vtraveller 0:45264ce231f9 254
vtraveller 0:45264ce231f9 255 command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
vtraveller 0:45264ce231f9 256 }
vtraveller 0:45264ce231f9 257
vtraveller 0:45264ce231f9 258 // Turn the display on/off (quickly)
vtraveller 0:45264ce231f9 259 void Adafruit_RGBLCDShield::noDisplay() {
vtraveller 0:45264ce231f9 260 _displaycontrol &= ~LCD_DISPLAYON;
vtraveller 0:45264ce231f9 261 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 262 }
vtraveller 0:45264ce231f9 263 void Adafruit_RGBLCDShield::display() {
vtraveller 0:45264ce231f9 264 _displaycontrol |= LCD_DISPLAYON;
vtraveller 0:45264ce231f9 265 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 266 }
vtraveller 0:45264ce231f9 267
vtraveller 0:45264ce231f9 268 // Turns the underline cursor on/off
vtraveller 0:45264ce231f9 269 void Adafruit_RGBLCDShield::noCursor() {
vtraveller 0:45264ce231f9 270 _displaycontrol &= ~LCD_CURSORON;
vtraveller 0:45264ce231f9 271 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 272 }
vtraveller 0:45264ce231f9 273 void Adafruit_RGBLCDShield::cursor() {
vtraveller 0:45264ce231f9 274 _displaycontrol |= LCD_CURSORON;
vtraveller 0:45264ce231f9 275 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 276 }
vtraveller 0:45264ce231f9 277
vtraveller 0:45264ce231f9 278 // Turn on and off the blinking cursor
vtraveller 0:45264ce231f9 279 void Adafruit_RGBLCDShield::noBlink() {
vtraveller 0:45264ce231f9 280 _displaycontrol &= ~LCD_BLINKON;
vtraveller 0:45264ce231f9 281 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 282 }
vtraveller 0:45264ce231f9 283 void Adafruit_RGBLCDShield::blink() {
vtraveller 0:45264ce231f9 284 _displaycontrol |= LCD_BLINKON;
vtraveller 0:45264ce231f9 285 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 286 }
vtraveller 0:45264ce231f9 287
vtraveller 0:45264ce231f9 288 // These commands scroll the display without changing the RAM
vtraveller 0:45264ce231f9 289 void Adafruit_RGBLCDShield::scrollDisplayLeft(void) {
vtraveller 0:45264ce231f9 290 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
vtraveller 0:45264ce231f9 291 }
vtraveller 0:45264ce231f9 292 void Adafruit_RGBLCDShield::scrollDisplayRight(void) {
vtraveller 0:45264ce231f9 293 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
vtraveller 0:45264ce231f9 294 }
vtraveller 0:45264ce231f9 295
vtraveller 0:45264ce231f9 296 // This is for text that flows Left to Right
vtraveller 0:45264ce231f9 297 void Adafruit_RGBLCDShield::leftToRight(void) {
vtraveller 0:45264ce231f9 298 _displaymode |= LCD_ENTRYLEFT;
vtraveller 0:45264ce231f9 299 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 300 }
vtraveller 0:45264ce231f9 301
vtraveller 0:45264ce231f9 302 // This is for text that flows Right to Left
vtraveller 0:45264ce231f9 303 void Adafruit_RGBLCDShield::rightToLeft(void) {
vtraveller 0:45264ce231f9 304 _displaymode &= ~LCD_ENTRYLEFT;
vtraveller 0:45264ce231f9 305 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 306 }
vtraveller 0:45264ce231f9 307
vtraveller 0:45264ce231f9 308 // This will 'right justify' text from the cursor
vtraveller 0:45264ce231f9 309 void Adafruit_RGBLCDShield::autoscroll(void) {
vtraveller 0:45264ce231f9 310 _displaymode |= LCD_ENTRYSHIFTINCREMENT;
vtraveller 0:45264ce231f9 311 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 312 }
vtraveller 0:45264ce231f9 313
vtraveller 0:45264ce231f9 314 // This will 'left justify' text from the cursor
vtraveller 0:45264ce231f9 315 void Adafruit_RGBLCDShield::noAutoscroll(void) {
vtraveller 0:45264ce231f9 316 _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
vtraveller 0:45264ce231f9 317 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 318 }
vtraveller 0:45264ce231f9 319
vtraveller 0:45264ce231f9 320 // Allows us to fill the first 8 CGRAM locations
vtraveller 0:45264ce231f9 321 // with custom characters
vtraveller 0:45264ce231f9 322 void Adafruit_RGBLCDShield::createChar(uint8_t location, uint8_t charmap[]) {
vtraveller 0:45264ce231f9 323 location &= 0x7; // we only have 8 locations 0-7
vtraveller 0:45264ce231f9 324 command(LCD_SETCGRAMADDR | (location << 3));
vtraveller 0:45264ce231f9 325 for (int i=0; i<8; i++) {
vtraveller 0:45264ce231f9 326 _putc(charmap[i]);
vtraveller 0:45264ce231f9 327 }
vtraveller 0:45264ce231f9 328 command(LCD_SETDDRAMADDR); // unfortunately resets the location to 0,0
vtraveller 0:45264ce231f9 329 }
vtraveller 0:45264ce231f9 330
vtraveller 0:45264ce231f9 331 /*********** mid level commands, for sending data/cmds */
vtraveller 0:45264ce231f9 332
vtraveller 0:45264ce231f9 333 inline void Adafruit_RGBLCDShield::command(uint8_t value) {
vtraveller 0:45264ce231f9 334 send(value, LOW);
vtraveller 0:45264ce231f9 335 }
vtraveller 0:45264ce231f9 336
vtraveller 0:45264ce231f9 337 #if ARDUINO >= 100
vtraveller 0:45264ce231f9 338 inline size_t Adafruit_RGBLCDShield::write(uint8_t value) {
vtraveller 0:45264ce231f9 339 send(value, HIGH);
vtraveller 0:45264ce231f9 340 return 1;
vtraveller 0:45264ce231f9 341 }
vtraveller 0:45264ce231f9 342 #else
vtraveller 0:45264ce231f9 343 //inline void Adafruit_RGBLCDShield::write(uint8_t value) {
vtraveller 0:45264ce231f9 344 int Adafruit_RGBLCDShield::_putc(int value) {
vtraveller 0:45264ce231f9 345 send(value, HIGH);
vtraveller 0:45264ce231f9 346 return 1;
vtraveller 0:45264ce231f9 347 }
vtraveller 0:45264ce231f9 348 #endif
vtraveller 0:45264ce231f9 349
vtraveller 0:45264ce231f9 350 /************ low level data pushing commands **********/
vtraveller 0:45264ce231f9 351
vtraveller 0:45264ce231f9 352 // little wrapper for i/o writes
vtraveller 0:45264ce231f9 353 void Adafruit_RGBLCDShield::_digitalWrite(uint8_t p, uint8_t d) {
vtraveller 0:45264ce231f9 354 if (_i2cAddr != 255) {
vtraveller 0:45264ce231f9 355 // an i2c command
vtraveller 0:45264ce231f9 356 _i2c.digitalWrite(p, d);
vtraveller 0:45264ce231f9 357 } else {
vtraveller 0:45264ce231f9 358 // straightup IO
vtraveller 0:45264ce231f9 359 // MBED TURNED OFF
vtraveller 0:45264ce231f9 360 //digitalWrite(p, d);
vtraveller 0:45264ce231f9 361 }
vtraveller 0:45264ce231f9 362 }
vtraveller 0:45264ce231f9 363
vtraveller 0:45264ce231f9 364 // Allows to set the backlight, if the LCD backpack is used
vtraveller 0:45264ce231f9 365 void Adafruit_RGBLCDShield::setBacklight(uint8_t status) {
vtraveller 0:45264ce231f9 366 // check if i2c or SPI
vtraveller 0:45264ce231f9 367 _i2c.digitalWrite(8, ~(status >> 2) & 0x1);
vtraveller 0:45264ce231f9 368 _i2c.digitalWrite(7, ~(status >> 1) & 0x1);
vtraveller 0:45264ce231f9 369 _i2c.digitalWrite(6, ~status & 0x1);
vtraveller 0:45264ce231f9 370 }
vtraveller 0:45264ce231f9 371
vtraveller 0:45264ce231f9 372 // little wrapper for i/o directions
vtraveller 0:45264ce231f9 373 void Adafruit_RGBLCDShield::_pinMode(uint8_t p, uint8_t d) {
vtraveller 0:45264ce231f9 374 if (_i2cAddr != 255) {
vtraveller 0:45264ce231f9 375 // an i2c command
vtraveller 0:45264ce231f9 376 _i2c.pinMode(p, d);
vtraveller 0:45264ce231f9 377 } else {
vtraveller 0:45264ce231f9 378 // straightup IO
vtraveller 0:45264ce231f9 379 // MBED TURNED OFF
vtraveller 0:45264ce231f9 380 //pinMode(p, d);
vtraveller 0:45264ce231f9 381 }
vtraveller 0:45264ce231f9 382 }
vtraveller 0:45264ce231f9 383
vtraveller 0:45264ce231f9 384 // write either command or data, with automatic 4/8-bit selection
vtraveller 0:45264ce231f9 385 void Adafruit_RGBLCDShield::send(uint8_t value, uint8_t mode) {
vtraveller 0:45264ce231f9 386 _digitalWrite(_rs_pin, mode);
vtraveller 0:45264ce231f9 387
vtraveller 0:45264ce231f9 388 // if there is a RW pin indicated, set it low to Write
vtraveller 0:45264ce231f9 389 if (_rw_pin != 255) {
vtraveller 0:45264ce231f9 390 _digitalWrite(_rw_pin, LOW);
vtraveller 0:45264ce231f9 391 }
vtraveller 0:45264ce231f9 392
vtraveller 0:45264ce231f9 393 if (_displayfunction & LCD_8BITMODE) {
vtraveller 0:45264ce231f9 394 write8bits(value);
vtraveller 0:45264ce231f9 395 } else {
vtraveller 0:45264ce231f9 396 write4bits(value>>4);
vtraveller 0:45264ce231f9 397 write4bits(value);
vtraveller 0:45264ce231f9 398 }
vtraveller 0:45264ce231f9 399 }
vtraveller 0:45264ce231f9 400
vtraveller 0:45264ce231f9 401 void Adafruit_RGBLCDShield::pulseEnable(void) {
vtraveller 0:45264ce231f9 402 _digitalWrite(_enable_pin, LOW);
vtraveller 0:45264ce231f9 403 delayMicroseconds(1);
vtraveller 0:45264ce231f9 404 _digitalWrite(_enable_pin, HIGH);
vtraveller 0:45264ce231f9 405 delayMicroseconds(1); // enable pulse must be >450ns
vtraveller 0:45264ce231f9 406 _digitalWrite(_enable_pin, LOW);
vtraveller 0:45264ce231f9 407 delayMicroseconds(100); // commands need > 37us to settle
vtraveller 0:45264ce231f9 408 }
vtraveller 0:45264ce231f9 409
vtraveller 0:45264ce231f9 410 void Adafruit_RGBLCDShield::write4bits(uint8_t value) {
vtraveller 0:45264ce231f9 411 if (_i2cAddr != 255) {
vtraveller 0:45264ce231f9 412 uint16_t out = 0;
vtraveller 0:45264ce231f9 413
vtraveller 0:45264ce231f9 414 //out = _i2c.readGPIOAB();
vtraveller 0:45264ce231f9 415 out = _i2c.digitalWordRead();
vtraveller 0:45264ce231f9 416
vtraveller 0:45264ce231f9 417 // speed up for i2c since its sluggish
vtraveller 0:45264ce231f9 418 for (int i = 0; i < 4; i++) {
vtraveller 0:45264ce231f9 419 out &= ~(1 << _data_pins[i]);
vtraveller 0:45264ce231f9 420 out |= ((value >> i) & 0x1) << _data_pins[i];
vtraveller 0:45264ce231f9 421 }
vtraveller 0:45264ce231f9 422
vtraveller 0:45264ce231f9 423 // make sure enable is low
vtraveller 0:45264ce231f9 424 out &= ~(1 << _enable_pin);
vtraveller 0:45264ce231f9 425
vtraveller 0:45264ce231f9 426 //_i2c.writeGPIOAB(out);
vtraveller 0:45264ce231f9 427 _i2c.digitalWordWrite(out);
vtraveller 0:45264ce231f9 428
vtraveller 0:45264ce231f9 429 // pulse enable
vtraveller 0:45264ce231f9 430 delayMicroseconds(1);
vtraveller 0:45264ce231f9 431 out |= (1 << _enable_pin);
vtraveller 0:45264ce231f9 432 //_i2c.writeGPIOAB(out);
vtraveller 0:45264ce231f9 433 _i2c.digitalWordWrite(out);
vtraveller 0:45264ce231f9 434
vtraveller 0:45264ce231f9 435 delayMicroseconds(1);
vtraveller 0:45264ce231f9 436 out &= ~(1 << _enable_pin);
vtraveller 0:45264ce231f9 437
vtraveller 0:45264ce231f9 438 //_i2c.writeGPIOAB(out);
vtraveller 0:45264ce231f9 439 _i2c.digitalWordWrite(out);
vtraveller 0:45264ce231f9 440
vtraveller 0:45264ce231f9 441 delayMicroseconds(100);
vtraveller 0:45264ce231f9 442
vtraveller 0:45264ce231f9 443 } else {
vtraveller 0:45264ce231f9 444 for (int i = 0; i < 4; i++) {
vtraveller 0:45264ce231f9 445 _pinMode(_data_pins[i], OUTPUT);
vtraveller 0:45264ce231f9 446 _digitalWrite(_data_pins[i], (value >> i) & 0x01);
vtraveller 0:45264ce231f9 447 }
vtraveller 0:45264ce231f9 448 pulseEnable();
vtraveller 0:45264ce231f9 449 }
vtraveller 0:45264ce231f9 450 }
vtraveller 0:45264ce231f9 451
vtraveller 0:45264ce231f9 452 void Adafruit_RGBLCDShield::write8bits(uint8_t value) {
vtraveller 0:45264ce231f9 453 for (int i = 0; i < 8; i++) {
vtraveller 0:45264ce231f9 454 _pinMode(_data_pins[i], OUTPUT);
vtraveller 0:45264ce231f9 455 _digitalWrite(_data_pins[i], (value >> i) & 0x01);
vtraveller 0:45264ce231f9 456 }
vtraveller 0:45264ce231f9 457
vtraveller 0:45264ce231f9 458 pulseEnable();
vtraveller 0:45264ce231f9 459 }
vtraveller 0:45264ce231f9 460
vtraveller 0:45264ce231f9 461 uint8_t Adafruit_RGBLCDShield::readButtons(void) {
vtraveller 0:45264ce231f9 462 uint8_t reply = 0x1F;
vtraveller 0:45264ce231f9 463
vtraveller 0:45264ce231f9 464 for (uint8_t i=0; i<5; i++) {
vtraveller 0:45264ce231f9 465 reply &= ~((_i2c.digitalRead(_button_pins[i])) << i);
vtraveller 0:45264ce231f9 466 }
vtraveller 0:45264ce231f9 467 return reply;
vtraveller 0:45264ce231f9 468 }