local copy of rgplcd shield

Dependents:  

Fork of AdaFruit_RGBLCDShield by Justin Howard

Committer:
vtraveller
Date:
Sat Aug 30 16:29:58 2014 +0000
Revision:
2:01f8491ca9a5
Parent:
1:24ab601221e2
Child:
3:bc884631d057
Updated with support for MCP9080 Adafruit sensor.

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 2:01f8491ca9a5 95 Adafruit_RGBLCDShield::Adafruit_RGBLCDShield
vtraveller 2:01f8491ca9a5 96 (
vtraveller 2:01f8491ca9a5 97 MCP23017 & inMCP,
vtraveller 2:01f8491ca9a5 98 uint8_t inRs,
vtraveller 2:01f8491ca9a5 99 uint8_t inRw,
vtraveller 2:01f8491ca9a5 100 uint8_t inEn,
vtraveller 2:01f8491ca9a5 101 uint8_t inD4,
vtraveller 2:01f8491ca9a5 102 uint8_t inD5,
vtraveller 2:01f8491ca9a5 103 uint8_t inD6,
vtraveller 2:01f8491ca9a5 104 uint8_t inD7
vtraveller 2:01f8491ca9a5 105 )
vtraveller 2:01f8491ca9a5 106 : Serial(SERIAL_TX, SERIAL_RX)
vtraveller 2:01f8491ca9a5 107 , _i2c(inMCP)
vtraveller 2:01f8491ca9a5 108 {
vtraveller 2:01f8491ca9a5 109 _i2cAddr = 0;
vtraveller 2:01f8491ca9a5 110
vtraveller 2:01f8491ca9a5 111 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
vtraveller 2:01f8491ca9a5 112
vtraveller 2:01f8491ca9a5 113 // the I/O expander pinout
vtraveller 2:01f8491ca9a5 114 _rs_pin = inRs;
vtraveller 2:01f8491ca9a5 115 _rw_pin = inRw;
vtraveller 2:01f8491ca9a5 116 _enable_pin = inEn;
vtraveller 2:01f8491ca9a5 117 _data_pins[0] = inD4; // really d4
vtraveller 2:01f8491ca9a5 118 _data_pins[1] = inD5; // really d5
vtraveller 2:01f8491ca9a5 119 _data_pins[2] = inD6; // really d6
vtraveller 2:01f8491ca9a5 120 _data_pins[3] = inD7; // really d7
vtraveller 2:01f8491ca9a5 121
vtraveller 2:01f8491ca9a5 122 // we can't begin() yet :(
vtraveller 2:01f8491ca9a5 123 }
vtraveller 2:01f8491ca9a5 124
vtraveller 0:45264ce231f9 125 void Adafruit_RGBLCDShield::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
vtraveller 0:45264ce231f9 126 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
vtraveller 0:45264ce231f9 127 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
vtraveller 0:45264ce231f9 128 {
vtraveller 0:45264ce231f9 129 _rs_pin = rs;
vtraveller 0:45264ce231f9 130 _rw_pin = rw;
vtraveller 0:45264ce231f9 131 _enable_pin = enable;
vtraveller 0:45264ce231f9 132
vtraveller 0:45264ce231f9 133 _data_pins[0] = d0;
vtraveller 0:45264ce231f9 134 _data_pins[1] = d1;
vtraveller 0:45264ce231f9 135 _data_pins[2] = d2;
vtraveller 0:45264ce231f9 136 _data_pins[3] = d3;
vtraveller 0:45264ce231f9 137 _data_pins[4] = d4;
vtraveller 0:45264ce231f9 138 _data_pins[5] = d5;
vtraveller 0:45264ce231f9 139 _data_pins[6] = d6;
vtraveller 0:45264ce231f9 140 _data_pins[7] = d7;
vtraveller 0:45264ce231f9 141
vtraveller 0:45264ce231f9 142 _i2cAddr = 255;
vtraveller 0:45264ce231f9 143
vtraveller 0:45264ce231f9 144 _pinMode(_rs_pin, OUTPUT);
vtraveller 0:45264ce231f9 145 // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
vtraveller 0:45264ce231f9 146 if (_rw_pin != 255) {
vtraveller 0:45264ce231f9 147 _pinMode(_rw_pin, OUTPUT);
vtraveller 0:45264ce231f9 148 }
vtraveller 0:45264ce231f9 149 _pinMode(_enable_pin, OUTPUT);
vtraveller 0:45264ce231f9 150
vtraveller 0:45264ce231f9 151
vtraveller 0:45264ce231f9 152 if (fourbitmode)
vtraveller 0:45264ce231f9 153 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
vtraveller 0:45264ce231f9 154 else
vtraveller 0:45264ce231f9 155 _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
vtraveller 0:45264ce231f9 156
vtraveller 0:45264ce231f9 157 begin(16, 1);
vtraveller 0:45264ce231f9 158 }
vtraveller 0:45264ce231f9 159
vtraveller 0:45264ce231f9 160 void Adafruit_RGBLCDShield::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
vtraveller 0:45264ce231f9 161 // check if i2c
vtraveller 0:45264ce231f9 162 if (_i2cAddr != 255) {
vtraveller 0:45264ce231f9 163 //_i2c.begin(_i2cAddr);
vtraveller 0:45264ce231f9 164 //WIRE.begin();
vtraveller 0:45264ce231f9 165
vtraveller 0:45264ce231f9 166 _i2c.reset();
vtraveller 0:45264ce231f9 167
vtraveller 0:45264ce231f9 168 _i2c.pinMode(8, OUTPUT);
vtraveller 0:45264ce231f9 169 _i2c.pinMode(6, OUTPUT);
vtraveller 0:45264ce231f9 170 _i2c.pinMode(7, OUTPUT);
vtraveller 0:45264ce231f9 171
vtraveller 0:45264ce231f9 172 setBacklight(0x7);
vtraveller 0:45264ce231f9 173
vtraveller 0:45264ce231f9 174 if (_rw_pin)
vtraveller 0:45264ce231f9 175 _i2c.pinMode(_rw_pin, OUTPUT);
vtraveller 0:45264ce231f9 176
vtraveller 0:45264ce231f9 177 _i2c.pinMode(_rs_pin, OUTPUT);
vtraveller 0:45264ce231f9 178 _i2c.pinMode(_enable_pin, OUTPUT);
vtraveller 0:45264ce231f9 179 for (uint8_t i=0; i<4; i++)
vtraveller 0:45264ce231f9 180 _i2c.pinMode(_data_pins[i], OUTPUT);
vtraveller 0:45264ce231f9 181
vtraveller 0:45264ce231f9 182 unsigned short nPullups = 0;
vtraveller 0:45264ce231f9 183 for (uint8_t i=0; i<5; i++) {
vtraveller 0:45264ce231f9 184 _i2c.pinMode(_button_pins[i], INPUT);
vtraveller 0:45264ce231f9 185 nPullups |= (1 << _button_pins[i]);
vtraveller 0:45264ce231f9 186 //_i2c.pullUp(_button_pins[i], 1);
vtraveller 0:45264ce231f9 187 }
vtraveller 0:45264ce231f9 188
vtraveller 0:45264ce231f9 189 _i2c.internalPullupMask(nPullups);
vtraveller 0:45264ce231f9 190 }
vtraveller 0:45264ce231f9 191
vtraveller 0:45264ce231f9 192 if (lines > 1) {
vtraveller 0:45264ce231f9 193 _displayfunction |= LCD_2LINE;
vtraveller 0:45264ce231f9 194 }
vtraveller 0:45264ce231f9 195 _numlines = lines;
vtraveller 0:45264ce231f9 196 _currline = 0;
vtraveller 0:45264ce231f9 197
vtraveller 0:45264ce231f9 198 // for some 1 line displays you can select a 10 pixel high font
vtraveller 0:45264ce231f9 199 if ((dotsize != 0) && (lines == 1)) {
vtraveller 0:45264ce231f9 200 _displayfunction |= LCD_5x10DOTS;
vtraveller 0:45264ce231f9 201 }
vtraveller 0:45264ce231f9 202
vtraveller 0:45264ce231f9 203 // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
vtraveller 0:45264ce231f9 204 // according to datasheet, we need at least 40ms after power rises above 2.7V
vtraveller 0:45264ce231f9 205 // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
vtraveller 0:45264ce231f9 206 delayMicroseconds(50000);
vtraveller 0:45264ce231f9 207 // Now we pull both RS and R/W low to begin commands
vtraveller 0:45264ce231f9 208 _digitalWrite(_rs_pin, LOW);
vtraveller 0:45264ce231f9 209 _digitalWrite(_enable_pin, LOW);
vtraveller 0:45264ce231f9 210 if (_rw_pin != 255) {
vtraveller 0:45264ce231f9 211 _digitalWrite(_rw_pin, LOW);
vtraveller 0:45264ce231f9 212 }
vtraveller 0:45264ce231f9 213
vtraveller 0:45264ce231f9 214 //put the LCD into 4 bit or 8 bit mode
vtraveller 0:45264ce231f9 215 if (! (_displayfunction & LCD_8BITMODE)) {
vtraveller 0:45264ce231f9 216 // this is according to the hitachi HD44780 datasheet
vtraveller 0:45264ce231f9 217 // figure 24, pg 46
vtraveller 0:45264ce231f9 218
vtraveller 0:45264ce231f9 219 // we start in 8bit mode, try to set 4 bit mode
vtraveller 0:45264ce231f9 220 write4bits(0x03);
vtraveller 0:45264ce231f9 221 delayMicroseconds(4500); // wait min 4.1ms
vtraveller 0:45264ce231f9 222
vtraveller 0:45264ce231f9 223 // second try
vtraveller 0:45264ce231f9 224 write4bits(0x03);
vtraveller 0:45264ce231f9 225 delayMicroseconds(4500); // wait min 4.1ms
vtraveller 0:45264ce231f9 226
vtraveller 0:45264ce231f9 227 // third go!
vtraveller 0:45264ce231f9 228 write4bits(0x03);
vtraveller 0:45264ce231f9 229 delayMicroseconds(150);
vtraveller 0:45264ce231f9 230
vtraveller 0:45264ce231f9 231 // finally, set to 8-bit interface
vtraveller 0:45264ce231f9 232 write4bits(0x02);
vtraveller 0:45264ce231f9 233 } else {
vtraveller 0:45264ce231f9 234 // this is according to the hitachi HD44780 datasheet
vtraveller 0:45264ce231f9 235 // page 45 figure 23
vtraveller 0:45264ce231f9 236
vtraveller 0:45264ce231f9 237 // Send function set command sequence
vtraveller 0:45264ce231f9 238 command(LCD_FUNCTIONSET | _displayfunction);
vtraveller 0:45264ce231f9 239 delayMicroseconds(4500); // wait more than 4.1ms
vtraveller 0:45264ce231f9 240
vtraveller 0:45264ce231f9 241 // second try
vtraveller 0:45264ce231f9 242 command(LCD_FUNCTIONSET | _displayfunction);
vtraveller 0:45264ce231f9 243 delayMicroseconds(150);
vtraveller 0:45264ce231f9 244
vtraveller 0:45264ce231f9 245 // third go
vtraveller 0:45264ce231f9 246 command(LCD_FUNCTIONSET | _displayfunction);
vtraveller 0:45264ce231f9 247 }
vtraveller 0:45264ce231f9 248
vtraveller 0:45264ce231f9 249 // finally, set # lines, font size, etc.
vtraveller 0:45264ce231f9 250 command(LCD_FUNCTIONSET | _displayfunction);
vtraveller 0:45264ce231f9 251
vtraveller 0:45264ce231f9 252 // turn the display on with no cursor or blinking default
vtraveller 0:45264ce231f9 253 _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
vtraveller 0:45264ce231f9 254 display();
vtraveller 0:45264ce231f9 255
vtraveller 0:45264ce231f9 256 // clear it off
vtraveller 0:45264ce231f9 257 clear();
vtraveller 0:45264ce231f9 258
vtraveller 0:45264ce231f9 259 // Initialize to default text direction (for romance languages)
vtraveller 0:45264ce231f9 260 _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
vtraveller 0:45264ce231f9 261 // set the entry mode
vtraveller 0:45264ce231f9 262 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 263 }
vtraveller 0:45264ce231f9 264
vtraveller 0:45264ce231f9 265 /********** high level commands, for the user! */
vtraveller 0:45264ce231f9 266 void Adafruit_RGBLCDShield::clear()
vtraveller 0:45264ce231f9 267 {
vtraveller 0:45264ce231f9 268 command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
vtraveller 0:45264ce231f9 269 delayMicroseconds(2000); // this command takes a long time!
vtraveller 0:45264ce231f9 270 }
vtraveller 0:45264ce231f9 271
vtraveller 0:45264ce231f9 272 void Adafruit_RGBLCDShield::home()
vtraveller 0:45264ce231f9 273 {
vtraveller 0:45264ce231f9 274 command(LCD_RETURNHOME); // set cursor position to zero
vtraveller 0:45264ce231f9 275 delayMicroseconds(2000); // this command takes a long time!
vtraveller 0:45264ce231f9 276 }
vtraveller 0:45264ce231f9 277
vtraveller 1:24ab601221e2 278 uint8_t Adafruit_RGBLCDShield::lines()
vtraveller 1:24ab601221e2 279 {
vtraveller 1:24ab601221e2 280 return _numlines;
vtraveller 1:24ab601221e2 281 }
vtraveller 1:24ab601221e2 282
vtraveller 0:45264ce231f9 283 void Adafruit_RGBLCDShield::setCursor(uint8_t col, uint8_t row)
vtraveller 0:45264ce231f9 284 {
vtraveller 0:45264ce231f9 285 int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
vtraveller 0:45264ce231f9 286 if ( row > _numlines ) {
vtraveller 0:45264ce231f9 287 row = _numlines-1; // we count rows starting w/0
vtraveller 0:45264ce231f9 288 }
vtraveller 0:45264ce231f9 289
vtraveller 0:45264ce231f9 290 command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
vtraveller 0:45264ce231f9 291 }
vtraveller 0:45264ce231f9 292
vtraveller 0:45264ce231f9 293 // Turn the display on/off (quickly)
vtraveller 0:45264ce231f9 294 void Adafruit_RGBLCDShield::noDisplay() {
vtraveller 0:45264ce231f9 295 _displaycontrol &= ~LCD_DISPLAYON;
vtraveller 0:45264ce231f9 296 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 297 }
vtraveller 0:45264ce231f9 298 void Adafruit_RGBLCDShield::display() {
vtraveller 0:45264ce231f9 299 _displaycontrol |= LCD_DISPLAYON;
vtraveller 0:45264ce231f9 300 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 301 }
vtraveller 0:45264ce231f9 302
vtraveller 0:45264ce231f9 303 // Turns the underline cursor on/off
vtraveller 0:45264ce231f9 304 void Adafruit_RGBLCDShield::noCursor() {
vtraveller 0:45264ce231f9 305 _displaycontrol &= ~LCD_CURSORON;
vtraveller 0:45264ce231f9 306 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 307 }
vtraveller 0:45264ce231f9 308 void Adafruit_RGBLCDShield::cursor() {
vtraveller 0:45264ce231f9 309 _displaycontrol |= LCD_CURSORON;
vtraveller 0:45264ce231f9 310 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 311 }
vtraveller 0:45264ce231f9 312
vtraveller 0:45264ce231f9 313 // Turn on and off the blinking cursor
vtraveller 0:45264ce231f9 314 void Adafruit_RGBLCDShield::noBlink() {
vtraveller 0:45264ce231f9 315 _displaycontrol &= ~LCD_BLINKON;
vtraveller 0:45264ce231f9 316 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 317 }
vtraveller 0:45264ce231f9 318 void Adafruit_RGBLCDShield::blink() {
vtraveller 0:45264ce231f9 319 _displaycontrol |= LCD_BLINKON;
vtraveller 0:45264ce231f9 320 command(LCD_DISPLAYCONTROL | _displaycontrol);
vtraveller 0:45264ce231f9 321 }
vtraveller 0:45264ce231f9 322
vtraveller 0:45264ce231f9 323 // These commands scroll the display without changing the RAM
vtraveller 0:45264ce231f9 324 void Adafruit_RGBLCDShield::scrollDisplayLeft(void) {
vtraveller 0:45264ce231f9 325 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
vtraveller 0:45264ce231f9 326 }
vtraveller 0:45264ce231f9 327 void Adafruit_RGBLCDShield::scrollDisplayRight(void) {
vtraveller 0:45264ce231f9 328 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
vtraveller 0:45264ce231f9 329 }
vtraveller 0:45264ce231f9 330
vtraveller 0:45264ce231f9 331 // This is for text that flows Left to Right
vtraveller 0:45264ce231f9 332 void Adafruit_RGBLCDShield::leftToRight(void) {
vtraveller 0:45264ce231f9 333 _displaymode |= LCD_ENTRYLEFT;
vtraveller 0:45264ce231f9 334 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 335 }
vtraveller 0:45264ce231f9 336
vtraveller 0:45264ce231f9 337 // This is for text that flows Right to Left
vtraveller 0:45264ce231f9 338 void Adafruit_RGBLCDShield::rightToLeft(void) {
vtraveller 0:45264ce231f9 339 _displaymode &= ~LCD_ENTRYLEFT;
vtraveller 0:45264ce231f9 340 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 341 }
vtraveller 0:45264ce231f9 342
vtraveller 0:45264ce231f9 343 // This will 'right justify' text from the cursor
vtraveller 0:45264ce231f9 344 void Adafruit_RGBLCDShield::autoscroll(void) {
vtraveller 0:45264ce231f9 345 _displaymode |= LCD_ENTRYSHIFTINCREMENT;
vtraveller 0:45264ce231f9 346 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 347 }
vtraveller 0:45264ce231f9 348
vtraveller 0:45264ce231f9 349 // This will 'left justify' text from the cursor
vtraveller 0:45264ce231f9 350 void Adafruit_RGBLCDShield::noAutoscroll(void) {
vtraveller 0:45264ce231f9 351 _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
vtraveller 0:45264ce231f9 352 command(LCD_ENTRYMODESET | _displaymode);
vtraveller 0:45264ce231f9 353 }
vtraveller 0:45264ce231f9 354
vtraveller 0:45264ce231f9 355 // Allows us to fill the first 8 CGRAM locations
vtraveller 0:45264ce231f9 356 // with custom characters
vtraveller 0:45264ce231f9 357 void Adafruit_RGBLCDShield::createChar(uint8_t location, uint8_t charmap[]) {
vtraveller 0:45264ce231f9 358 location &= 0x7; // we only have 8 locations 0-7
vtraveller 0:45264ce231f9 359 command(LCD_SETCGRAMADDR | (location << 3));
vtraveller 0:45264ce231f9 360 for (int i=0; i<8; i++) {
vtraveller 0:45264ce231f9 361 _putc(charmap[i]);
vtraveller 0:45264ce231f9 362 }
vtraveller 0:45264ce231f9 363 command(LCD_SETDDRAMADDR); // unfortunately resets the location to 0,0
vtraveller 0:45264ce231f9 364 }
vtraveller 0:45264ce231f9 365
vtraveller 0:45264ce231f9 366 /*********** mid level commands, for sending data/cmds */
vtraveller 0:45264ce231f9 367
vtraveller 0:45264ce231f9 368 inline void Adafruit_RGBLCDShield::command(uint8_t value) {
vtraveller 0:45264ce231f9 369 send(value, LOW);
vtraveller 0:45264ce231f9 370 }
vtraveller 0:45264ce231f9 371
vtraveller 0:45264ce231f9 372 #if ARDUINO >= 100
vtraveller 0:45264ce231f9 373 inline size_t Adafruit_RGBLCDShield::write(uint8_t value) {
vtraveller 0:45264ce231f9 374 send(value, HIGH);
vtraveller 0:45264ce231f9 375 return 1;
vtraveller 0:45264ce231f9 376 }
vtraveller 0:45264ce231f9 377 #else
vtraveller 0:45264ce231f9 378 //inline void Adafruit_RGBLCDShield::write(uint8_t value) {
vtraveller 0:45264ce231f9 379 int Adafruit_RGBLCDShield::_putc(int value) {
vtraveller 0:45264ce231f9 380 send(value, HIGH);
vtraveller 0:45264ce231f9 381 return 1;
vtraveller 0:45264ce231f9 382 }
vtraveller 0:45264ce231f9 383 #endif
vtraveller 0:45264ce231f9 384
vtraveller 0:45264ce231f9 385 /************ low level data pushing commands **********/
vtraveller 0:45264ce231f9 386
vtraveller 0:45264ce231f9 387 // little wrapper for i/o writes
vtraveller 0:45264ce231f9 388 void Adafruit_RGBLCDShield::_digitalWrite(uint8_t p, uint8_t d) {
vtraveller 0:45264ce231f9 389 if (_i2cAddr != 255) {
vtraveller 0:45264ce231f9 390 // an i2c command
vtraveller 0:45264ce231f9 391 _i2c.digitalWrite(p, d);
vtraveller 0:45264ce231f9 392 } else {
vtraveller 0:45264ce231f9 393 // straightup IO
vtraveller 0:45264ce231f9 394 // MBED TURNED OFF
vtraveller 0:45264ce231f9 395 //digitalWrite(p, d);
vtraveller 0:45264ce231f9 396 }
vtraveller 0:45264ce231f9 397 }
vtraveller 0:45264ce231f9 398
vtraveller 0:45264ce231f9 399 // Allows to set the backlight, if the LCD backpack is used
vtraveller 0:45264ce231f9 400 void Adafruit_RGBLCDShield::setBacklight(uint8_t status) {
vtraveller 0:45264ce231f9 401 // check if i2c or SPI
vtraveller 0:45264ce231f9 402 _i2c.digitalWrite(8, ~(status >> 2) & 0x1);
vtraveller 0:45264ce231f9 403 _i2c.digitalWrite(7, ~(status >> 1) & 0x1);
vtraveller 0:45264ce231f9 404 _i2c.digitalWrite(6, ~status & 0x1);
vtraveller 0:45264ce231f9 405 }
vtraveller 0:45264ce231f9 406
vtraveller 0:45264ce231f9 407 // little wrapper for i/o directions
vtraveller 0:45264ce231f9 408 void Adafruit_RGBLCDShield::_pinMode(uint8_t p, uint8_t d) {
vtraveller 0:45264ce231f9 409 if (_i2cAddr != 255) {
vtraveller 0:45264ce231f9 410 // an i2c command
vtraveller 0:45264ce231f9 411 _i2c.pinMode(p, d);
vtraveller 0:45264ce231f9 412 } else {
vtraveller 0:45264ce231f9 413 // straightup IO
vtraveller 0:45264ce231f9 414 // MBED TURNED OFF
vtraveller 0:45264ce231f9 415 //pinMode(p, d);
vtraveller 0:45264ce231f9 416 }
vtraveller 0:45264ce231f9 417 }
vtraveller 0:45264ce231f9 418
vtraveller 0:45264ce231f9 419 // write either command or data, with automatic 4/8-bit selection
vtraveller 0:45264ce231f9 420 void Adafruit_RGBLCDShield::send(uint8_t value, uint8_t mode) {
vtraveller 0:45264ce231f9 421 _digitalWrite(_rs_pin, mode);
vtraveller 0:45264ce231f9 422
vtraveller 0:45264ce231f9 423 // if there is a RW pin indicated, set it low to Write
vtraveller 0:45264ce231f9 424 if (_rw_pin != 255) {
vtraveller 0:45264ce231f9 425 _digitalWrite(_rw_pin, LOW);
vtraveller 0:45264ce231f9 426 }
vtraveller 0:45264ce231f9 427
vtraveller 0:45264ce231f9 428 if (_displayfunction & LCD_8BITMODE) {
vtraveller 0:45264ce231f9 429 write8bits(value);
vtraveller 0:45264ce231f9 430 } else {
vtraveller 0:45264ce231f9 431 write4bits(value>>4);
vtraveller 0:45264ce231f9 432 write4bits(value);
vtraveller 0:45264ce231f9 433 }
vtraveller 0:45264ce231f9 434 }
vtraveller 0:45264ce231f9 435
vtraveller 0:45264ce231f9 436 void Adafruit_RGBLCDShield::pulseEnable(void) {
vtraveller 0:45264ce231f9 437 _digitalWrite(_enable_pin, LOW);
vtraveller 0:45264ce231f9 438 delayMicroseconds(1);
vtraveller 0:45264ce231f9 439 _digitalWrite(_enable_pin, HIGH);
vtraveller 0:45264ce231f9 440 delayMicroseconds(1); // enable pulse must be >450ns
vtraveller 0:45264ce231f9 441 _digitalWrite(_enable_pin, LOW);
vtraveller 0:45264ce231f9 442 delayMicroseconds(100); // commands need > 37us to settle
vtraveller 0:45264ce231f9 443 }
vtraveller 0:45264ce231f9 444
vtraveller 0:45264ce231f9 445 void Adafruit_RGBLCDShield::write4bits(uint8_t value) {
vtraveller 0:45264ce231f9 446 if (_i2cAddr != 255) {
vtraveller 0:45264ce231f9 447 uint16_t out = 0;
vtraveller 0:45264ce231f9 448
vtraveller 0:45264ce231f9 449 //out = _i2c.readGPIOAB();
vtraveller 0:45264ce231f9 450 out = _i2c.digitalWordRead();
vtraveller 0:45264ce231f9 451
vtraveller 0:45264ce231f9 452 // speed up for i2c since its sluggish
vtraveller 0:45264ce231f9 453 for (int i = 0; i < 4; i++) {
vtraveller 0:45264ce231f9 454 out &= ~(1 << _data_pins[i]);
vtraveller 0:45264ce231f9 455 out |= ((value >> i) & 0x1) << _data_pins[i];
vtraveller 0:45264ce231f9 456 }
vtraveller 0:45264ce231f9 457
vtraveller 0:45264ce231f9 458 // make sure enable is low
vtraveller 0:45264ce231f9 459 out &= ~(1 << _enable_pin);
vtraveller 0:45264ce231f9 460
vtraveller 0:45264ce231f9 461 //_i2c.writeGPIOAB(out);
vtraveller 0:45264ce231f9 462 _i2c.digitalWordWrite(out);
vtraveller 0:45264ce231f9 463
vtraveller 0:45264ce231f9 464 // pulse enable
vtraveller 0:45264ce231f9 465 delayMicroseconds(1);
vtraveller 0:45264ce231f9 466 out |= (1 << _enable_pin);
vtraveller 0:45264ce231f9 467 //_i2c.writeGPIOAB(out);
vtraveller 0:45264ce231f9 468 _i2c.digitalWordWrite(out);
vtraveller 0:45264ce231f9 469
vtraveller 0:45264ce231f9 470 delayMicroseconds(1);
vtraveller 0:45264ce231f9 471 out &= ~(1 << _enable_pin);
vtraveller 0:45264ce231f9 472
vtraveller 0:45264ce231f9 473 //_i2c.writeGPIOAB(out);
vtraveller 0:45264ce231f9 474 _i2c.digitalWordWrite(out);
vtraveller 0:45264ce231f9 475
vtraveller 0:45264ce231f9 476 delayMicroseconds(100);
vtraveller 0:45264ce231f9 477
vtraveller 0:45264ce231f9 478 } else {
vtraveller 0:45264ce231f9 479 for (int i = 0; i < 4; i++) {
vtraveller 0:45264ce231f9 480 _pinMode(_data_pins[i], OUTPUT);
vtraveller 0:45264ce231f9 481 _digitalWrite(_data_pins[i], (value >> i) & 0x01);
vtraveller 0:45264ce231f9 482 }
vtraveller 0:45264ce231f9 483 pulseEnable();
vtraveller 0:45264ce231f9 484 }
vtraveller 0:45264ce231f9 485 }
vtraveller 0:45264ce231f9 486
vtraveller 0:45264ce231f9 487 void Adafruit_RGBLCDShield::write8bits(uint8_t value) {
vtraveller 0:45264ce231f9 488 for (int i = 0; i < 8; i++) {
vtraveller 0:45264ce231f9 489 _pinMode(_data_pins[i], OUTPUT);
vtraveller 0:45264ce231f9 490 _digitalWrite(_data_pins[i], (value >> i) & 0x01);
vtraveller 0:45264ce231f9 491 }
vtraveller 0:45264ce231f9 492
vtraveller 0:45264ce231f9 493 pulseEnable();
vtraveller 0:45264ce231f9 494 }
vtraveller 0:45264ce231f9 495
vtraveller 0:45264ce231f9 496 uint8_t Adafruit_RGBLCDShield::readButtons(void) {
vtraveller 0:45264ce231f9 497 uint8_t reply = 0x1F;
vtraveller 0:45264ce231f9 498
vtraveller 0:45264ce231f9 499 for (uint8_t i=0; i<5; i++) {
vtraveller 0:45264ce231f9 500 reply &= ~((_i2c.digitalRead(_button_pins[i])) << i);
vtraveller 0:45264ce231f9 501 }
vtraveller 0:45264ce231f9 502 return reply;
vtraveller 0:45264ce231f9 503 }