Class Lcd


  • public class Lcd
    extends Object

    Part of wiringPi is a library to allow access to parallel interface LCD displays (Those that use the popular Hitachi HD44780U or compatible controllers)

    The library is simple to use in your own programs, however wiring the displays up may be challenging, so do take care. It is possible to wire up more than one display! In 8-bit mode, the first display needs 10 GPIO pins and each additional display needs just one more pin, so with a maximum of 17 GPIO pins, that's 8 displays. If you move to using a 4-bit interface (trivial in the code), then it's 4 more displays and 12 LCDs! However I suspect the rest of the wiring might be somewhat challenging. Wiring is described at the end of the this page.

    The LCD display can be either a 5V display or a 3,3v display, however if we are using a 5V display then we must make absolutely sure the display can never write data back to the Raspberry Pi, otherwise it will present 5V on the Pi's GPIO pins which will not be good. At best you'll destroy the pin drivers, at worst you'll destroy your Pi.

    So make sure you always connect the R/W pin on the display to ground to force the display to be read-only to the host.

    Before using the Pi4J library, you need to ensure that the Java VM in configured with access to the following system libraries:

    • pi4j
    • wiringPi
    This library depends on the wiringPi native system library. (developed by Gordon Henderson @ http://wiringpi.com/)

    Author:
    Robert Savage (http://www.savagehomeautomation.com)
    See Also:
    https://pi4j.com/, http://wiringpi.com/dev-lib/lcd-library/
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void lcdCharDef​(int handle, int index, byte[] data)
      This allows you to re-define one of the 8 user-definable chanracters in the display.
      static void lcdClear​(int handle)
      Clears the LCD screen.
      static void lcdCursor​(int handle, int state)
      Turns the LCD cursor ON (1) / OFF (0)
      static void lcdCursorBlink​(int handle, int state)
      Turns the LCD cursor blinking behavior ON (1) / OFF (0)
      static void lcdDisplay​(int handle, int state)
      Turns the LCD display ON (1) / OFF (0)
      static void lcdHome​(int handle)
      Set the cursor to the home position.
      static int lcdInit​(int rows, int cols, int bits, int rs, int strb, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
      First, you need to initialize wiringPi in the way you want to.
      static void lcdPosition​(int handle, int x, int y)
      Set the position of the cursor for subsequent text entry.
      static void lcdPutchar​(int handle, byte data)
      Write a single character of data to the LCD display.
      static void lcdPuts​(int handle, String data)
      Write string of data to the LCD display.
      static void lcdPuts​(int handle, String data, String... args)
      Write formatted string of data to the LCD display.
    • Method Detail

      • lcdInit

        public static int lcdInit​(int rows,
                                  int cols,
                                  int bits,
                                  int rs,
                                  int strb,
                                  int d0,
                                  int d1,
                                  int d2,
                                  int d3,
                                  int d4,
                                  int d5,
                                  int d6,
                                  int d7)

        First, you need to initialize wiringPi in the way you want to. The LCD library will call pinMode functions, but these are ignored if you have already set the modes using the gpio program and want to use the wiringPiSetupSys() mechanism.

         int lcdInit(int rows, int cols, int bits, int rs, int strb, int d0, int d1, int d2, int d3, int d4,
                 int d5, int d6, int d7);
         

        This is the main initialization function and must be called before you use any other LCD functions.

        Rows and cols are the rows and columns on the display (e.g. 2, 16 or 4,20). Bits is the number of bits wide on the interface (4 or 8). The rs and strb represent the pin numbers of the displays RS pin and Strobe (E) pin. The parameters d0 through d7 are the pin numbers of the 8 data pins connected from the Pi to the display. Only the first 4 are used if you are running the display in 4-bit mode.

        The pin numbers will be either wiringPi pin numbers of GPIO pin numbers depending on which wiringPiSetup function you used.

        The return value is the handle to be used for all subsequent calls to the lcd library when dealing with that LCD, or -1 to indicate a fault. (Usually incorrect parameters)

        Parameters:
        rows - number of rows
        cols - number of columns
        bits - number of bits wide on the interface (4 or 8)
        rs - pin number of the RS pin
        strb - pin number of the strobe (E) pin
        d0 - pin number for driving bit 1
        d1 - pin number for driving bit 2
        d2 - pin number for driving bit 3
        d3 - pin number for driving bit 4
        d4 - pin number for driving bit 5 (only used in 8-bit mode)
        d5 - pin number for driving bit 6 (only used in 8-bit mode)
        d6 - pin number for driving bit 7 (only used in 8-bit mode)
        d7 - pin number for driving bit 8 (only used in 8-bit mode)
        Returns:
        The return value is the ‘handle’ to be used for all subsequent calls to the lcd library when dealing with that LCD, or -1 to indicate a fault. (Usually incorrect parameters)
        See Also:
        http://wiringpi.com/dev-lib/lcd-library
      • lcdCursorBlink

        public static void lcdCursorBlink​(int handle,
                                          int state)

        Turns the LCD cursor blinking behavior ON (1) / OFF (0)

        Parameters:
        handle - file handle
        See Also:
        http://wiringpi.com/dev-lib/lcd-library/
      • lcdPosition

        public static void lcdPosition​(int handle,
                                       int x,
                                       int y)

        Set the position of the cursor for subsequent text entry.

        Parameters:
        handle - file handle
        x - column position staring at 0 (left)
        y - row position starting at 0 (top)
        See Also:
        http://wiringpi.com/dev-lib/lcd-library/
      • lcdCharDef

        public static void lcdCharDef​(int handle,
                                      int index,
                                      byte[] data)

        This allows you to re-define one of the 8 user-definable chanracters in the display. The data array is 8 bytes which represent the character from the top-line to the bottom line. Note that the characters are actually 5×8, so only the lower 5 bits are used. The index is from 0 to 7 and you can subsequently print the character defined using the lcdPutchar() call.

        Parameters:
        handle - file handle
        index - index value from 0 to 7
        data - 8 bytes which represent the character from the top-line to the bottom line
        See Also:
        http://wiringpi.com/dev-lib/lcd-library/
      • lcdPutchar

        public static void lcdPutchar​(int handle,
                                      byte data)

        Write a single character of data to the LCD display.

        Parameters:
        handle - file handle
        data - character data to write
        See Also:
        http://wiringpi.com/dev-lib/lcd-library/
      • lcdPuts

        public static void lcdPuts​(int handle,
                                   String data)

        Write string of data to the LCD display.

        (ATTENTION: the 'data' argument can only be a maximum of 512 characters.)

        Parameters:
        handle - file handle
        data - string data to write
        See Also:
        http://wiringpi.com/dev-lib/lcd-library/
      • lcdPuts

        public static void lcdPuts​(int handle,
                                   String data,
                                   String... args)

        Write formatted string of data to the LCD display.

        (ATTENTION: the 'data' argument can only be a maximum of 512 characters.)

        Parameters:
        handle - file handle
        data - format string to write
        args - string arguments to use in formatted string
        See Also:
        http://wiringpi.com/dev-lib/lcd-library/