/* * LCD2797X.java * * Copyright © 2005, Ryan C. Payne. All Rights Reserved */ package stamp.peripheral.display.lcd.serial; import stamp.core.CPU; import stamp.core.Uart; /** * This class is for the Parallax 27976/27977 Serial LCD Module * * Revision History: Ver 1.0 - 07/24/05: Initial release * * @author Ryan C. Payne (ryan-payne@comcast.net) * @version 1.0 July 24, 2005 */ public class LCD2797X { private Uart lcdUart; // uart control object /** * Display custom character 0 */ private static final int CMD_DISPLAY_CUSTOM_CHAR_0 = 0x00; /** * Display custom character 1 */ private static final int CMD_DISPLAY_CUSTOM_CHAR_1 = 0x01; /** * Display custom character 2 */ private static final int CMD_DISPLAY_CUSTOM_CHAR_2 = 0x02; /** * Display custom character 3 */ private static final int CMD_DISPLAY_CUSTOM_CHAR_3 = 0x03; /** * Display custom character 4 */ private static final int CMD_DISPLAY_CUSTOM_CHAR_4 = 0x04; /** * Display custom character 5 */ private static final int CMD_DISPLAY_CUSTOM_CHAR_5 = 0x05; /** * Display custom character 6 */ private static final int CMD_DISPLAY_CUSTOM_CHAR_6 = 0x06; /** * Display custom character 7 */ private static final int CMD_DISPLAY_CUSTOM_CHAR_7 = 0x07; /** * Backspace/Left - The cursor is moved one position to the left. The * command doesn't erase the character. */ private static final int CMD_BACKSPACE = 0x08; /** * Right - The cursor is moved one position to the right. The command * doesn't erase the character. */ private static final int CMD_MOVE_RIGHT = 0x09; /** * Line Feed - The cursor is moved down one line. If on like 0, it goes to * line 1. If on line 1, it wraps around to line 0. The horizontal position * remains the same. */ private static final int CMD_LINE_FEED = 0x0A; /** * Form Feed - The cursor is moved to position 0 on line 0 and the entire * display is cleared. Users must pause 5mS after this command. */ private static final int CMD_FORM_FEED = 0x0C; /** * Carriage Return - If on line 0, the cursor is moved to position 0 on line * 1. If on line 1, it wraps around to position 0 on line 0. */ private static final int CMD_CARRIAGE_RETURN = 0x0D; /** * Turn backlight on (only on model 27977) */ private static final int CMD_BACKLIGHT_ON = 0x11; /** * Turn back light off (Default) */ private static final int CMD_BACKLIGHT_OFF = 0x12; /** * Turn the display off */ private static final int CMD_DISPLAY_OFF = 0x15; /** * Turn the display on, with the cursor off and no blink */ private static final int CMD_DISPLAY_ON_CURSOR_OFF_BLINK_OFF = 0x16; /** * Turn the display on, with cursor off and character blink */ private static final int CMD_DISPLAY_ON_CURSOR_OFF_BLINK_ON = 0x17; /** * Turn the display on, with cursor on and no blink (Default) */ private static final int CMD_DISPLAY_ON_CURSOR_ON_BLINK_OFF = 0x18; /** * Turn the display on, with cursor on and character blink */ private static final int CMD_DISPLAY_ON_CURSOR_ON_BLINK_ON = 0x19; /** * Move cursor to line 0, position 0 */ private static final int CMD_MOVE_LINE_0_POSITION_0 = 0x80; /** * Move cursor to line 0, position 1 */ private static final int CMD_MOVE_LINE_0_POSITION_1 = 0x81; /** * Move cursor to line 0, position 2 */ private static final int CMD_MOVE_LINE_0_POSITION_2 = 0x82; /** * Move cursor to line 0, position 3 */ private static final int CMD_MOVE_LINE_0_POSITION_3 = 0x83; /** * Move cursor to line 0, position 4 */ private static final int CMD_MOVE_LINE_0_POSITION_4 = 0x84; /** * Move cursor to line 0, position 5 */ private static final int CMD_MOVE_LINE_0_POSITION_5 = 0x85; /** * Move cursor to line 0, position 6 */ private static final int CMD_MOVE_LINE_0_POSITION_6 = 0x86; /** * Move cursor to line 0, position 7 */ private static final int CMD_MOVE_LINE_0_POSITION_7 = 0x87; /** * Move cursor to line 0, position 8 */ private static final int CMD_MOVE_LINE_0_POSITION_8 = 0x88; /** * Move cursor to line 0, position 9 */ private static final int CMD_MOVE_LINE_0_POSITION_9 = 0x89; /** * Move cursor to line 0, position 10 */ private static final int CMD_MOVE_LINE_0_POSITION_10 = 0x8A; /** * Move cursor to line 0, position 11 */ private static final int CMD_MOVE_LINE_0_POSITION_11 = 0x8B; /** * Move cursor to line 0, position 12 */ private static final int CMD_MOVE_LINE_0_POSITION_12 = 0x8C; /** * Move cursor to line 0, position 13 */ private static final int CMD_MOVE_LINE_0_POSITION_13 = 0x8D; /** * Move cursor to line 0, position 14 */ private static final int CMD_MOVE_LINE_0_POSITION_14 = 0x8E; /** * Move cursor to line 0, position 15 */ private static final int CMD_MOVE_LINE_0_POSITION_15 = 0x8F; /** * Move cursor to line 1, position 0 */ private static final int CMD_MOVE_LINE_1_POSITION_0 = 0x94; /** * Move cursor to line 1, position 1 */ private static final int CMD_MOVE_LINE_1_POSITION_1 = 0x95; /** * Move cursor to line 1, position 2 */ private static final int CMD_MOVE_LINE_1_POSITION_2 = 0x96; /** * Move cursor to line 1, position 3 */ private static final int CMD_MOVE_LINE_1_POSITION_3 = 0x97; /** * Move cursor to line 1, position 4 */ private static final int CMD_MOVE_LINE_1_POSITION_4 = 0x98; /** * Move cursor to line 1, position 5 */ private static final int CMD_MOVE_LINE_1_POSITION_5 = 0x99; /** * Move cursor to line 1, position 6 */ private static final int CMD_MOVE_LINE_1_POSITION_6 = 0x9A; /** * Move cursor to line 1, position 7 */ private static final int CMD_MOVE_LINE_1_POSITION_7 = 0x9B; /** * Move cursor to line 1, position 8 */ private static final int CMD_MOVE_LINE_1_POSITION_8 = 0x9C; /** * Move cursor to line 1, position 9 */ private static final int CMD_MOVE_LINE_1_POSITION_9 = 0x9D; /** * Move cursor to line 1, position 10 */ private static final int CMD_MOVE_LINE_1_POSITION_10 = 0x9E; /** * Move cursor to line 1, position 11 */ private static final int CMD_MOVE_LINE_1_POSITION_11 = 0x9F; /** * Move cursor to line 1, position 12 */ private static final int CMD_MOVE_LINE_1_POSITION_12 = 0xA0; /** * Move cursor to line 1, position 13 */ private static final int CMD_MOVE_LINE_1_POSITION_13 = 0xA1; /** * Move cursor to line 1, position 14 */ private static final int CMD_MOVE_LINE_1_POSITION_14 = 0xA2; /** * Move cursor to line 1, position 15 */ private static final int CMD_MOVE_LINE_1_POSITION_15 = 0xA3; /** * Define custom character 0. This command must be followed by eight data * bytes */ private static final int CMD_DEFINE_CUSTOM_CHAR_0 = 0xF8; /** * Define custom character 1. This command must be followed by eight data * bytes */ private static final int CMD_DEFINE_CUSTOM_CHAR_1 = 0xF9; /** * Define custom character 2. This command must be followed by eight data * bytes */ private static final int CMD_DEFINE_CUSTOM_CHAR_2 = 0xFA; /** * Define custom character 3. This command must be followed by eight data * bytes */ private static final int CMD_DEFINE_CUSTOM_CHAR_3 = 0xFB; /** * Define custom character 4. This command must be followed by eight data * bytes */ private static final int CMD_DEFINE_CUSTOM_CHAR_4 = 0xFC; /** * Define custom character 5. This command must be followed by eight data * bytes */ private static final int CMD_DEFINE_CUSTOM_CHAR_5 = 0xFD; /** * Define custom character 6. This command must be followed by eight data * bytes */ private static final int CMD_DEFINE_CUSTOM_CHAR_6 = 0xFE; /** * Define custom character 7. This command must be followed by eight data * bytes */ private static final int CMD_DEFINE_CUSTOM_CHAR_7 = 0xFF; /** * Creates a new serial LCD display object * * @param lcdUart TX uart object setup for LCD control */ public LCD2797X(Uart lcdUart) { this.lcdUart = lcdUart; // Pause for 100 ms for start up delay CPU.delay(1000); } /** * Clears the display and the cursor is moved to position 0 on line 0 */ public void clearDisplay() { this.sendCommand(LCD2797X.CMD_FORM_FEED); // Pause the required 5 ms per data sheet CPU.delay(50); } /** * Turns the display off */ public void displayOff() { this.sendCommand(LCD2797X.CMD_DISPLAY_OFF); } /** * Turns the display on using the 2797X's default cursor settings */ public void displayOn() { this.sendCommand(LCD2797X.CMD_DISPLAY_ON_CURSOR_ON_BLINK_OFF); } /** * Turns the display on * * @param cursor When true the cursor is displayed * @param blink When true the current character blinks */ public void displayOn(boolean cursor, boolean blink) { if (cursor == false && blink == false) { this.sendCommand(LCD2797X.CMD_DISPLAY_ON_CURSOR_OFF_BLINK_OFF); } else if (cursor == true && blink == true) { this.sendCommand(LCD2797X.CMD_DISPLAY_ON_CURSOR_ON_BLINK_ON); } else if (cursor == true) { this.sendCommand(LCD2797X.CMD_DISPLAY_ON_CURSOR_ON_BLINK_OFF); } else { this.sendCommand(LCD2797X.CMD_DISPLAY_ON_CURSOR_OFF_BLINK_ON); } } /** * Turns the backlight on (27977 only) */ public void backlightOn() { this.setBacklight(true); } /** * Turns the backlight off */ public void backlightOff() { this.setBacklight(false); } /** * Sets the state of the backlight * * @param isOn true to turn the backlight on * false to turn the backlight off */ public void setBacklight(boolean isOn) { if (isOn) { this.sendCommand(LCD2797X.CMD_BACKLIGHT_ON); } else { this.sendCommand(LCD2797X.CMD_BACKLIGHT_OFF); } } /** * Writes character on LCD at current cursor position * * @param c Character to write on LCD */ public void write(char c) { this.sendCommand(c); } /** * Writes string on LCD at current cursor position * * @param s String to write on LCD */ public void write(String s) { for (int i = 0; i < s.length(); i++) { this.write(s.charAt(i)); } } /** * Writes string buffer on LCD at current cursor position * * @param sb StringBuffer to write on LCD */ public void write(StringBuffer sb) { for (int i = 0; i < sb.length(); i++) { this.write(sb.charAt(i)); } } /** * Sets the cursor to the home position: line 0, position 0 */ public void home() { this.sendCommand(LCD2797X.CMD_MOVE_LINE_0_POSITION_0); } /** * Moves the cursor one position to the right. Contents of the position are * not erased. */ public void moveRight() { this.sendCommand(LCD2797X.CMD_MOVE_RIGHT); } /** * Moves the cursor one position to the left. Contents of the position are * not erased. */ public void moveLeft() { this.sendCommand(LCD2797X.CMD_BACKSPACE); } /** * Sends a carriage return to the LCD * * If on line 0, the cursor is moved to position 0 on line 1. If on line 1, * it wraps around to position 0 on line 0. */ public void carriageReturn() { this.sendCommand(LCD2797X.CMD_CARRIAGE_RETURN); } /** * Sends a line feed to the LCD * * If on line 0, it goes to line 1. If on line 1, it wraps around to line 0. * The horizontal position remains the same. */ public void lineFeed() { this.sendCommand(LCD2797X.CMD_LINE_FEED); } /** * Sets the cursor to a specified position. If an invalid line or position * is specified, the cursor is set to the home position * * @param line 0 or 1 * @param position 0 through 15 */ public void setPosition(int line, int position) { // Check for out of range values. If anything is fishy, send them home! if (line < 0 || line > 1 || position < 0 || position > 15) { this.home(); return; } int location = LCD2797X.CMD_MOVE_LINE_0_POSITION_0 + (line * 20) + position; this.sendCommand(location); } /** * Displays the specified custom character * * @param charIndex The index custom character to be displayed; 0 - 8 */ public void displayCustomChar(int charIndex) { // If the index is out of range, don't do anything if (charIndex >= 0 || charIndex <= 7) { this.sendCommand(LCD2797X.CMD_DISPLAY_CUSTOM_CHAR_0 + charIndex); } } /** * Defines a custom character * * @param charIndex The index of the custom character to be defined; 0 - 8 * @param bytes The array of bytes that define the custom character; must be * an array of eight bytes */ public void defineCustomChar(int charIndex, byte[] bytes) { // If the index is out of range, don't do anything if (charIndex >= 0 || charIndex <= 7) { // If the byte array does not contain eight bytes, don't do anything if (bytes.length == 8) { this.sendCommand(LCD2797X.CMD_DEFINE_CUSTOM_CHAR_0 + charIndex); for (int i = 0; i < 8; i++) { this.sendCommand(bytes[i]); } } } } /** * Sends a command to the LCD display * * @param command the command to send */ private void sendCommand(int command) { this.lcdUart.sendByte(command); } }// end class: LCD2797X