PiG 4 - Lcd and Java
The plan was to deliver you the Golo version of the previous post code to allow you to write on the LCD.
But it looks like I’ve done too much Java those months and not enough Golo. I’m currently stucked on a technical issue.
I’ll find a way but in the meantime and to keep the new goal to publish more often, I’ll share with you the Java version.
First, you have of course to follow the instruction on the second PiG article
Then, I used two command lines.
First, I compiled the source:
javac -source 7 -target 7 -classpath .:classes:/opt/pi4j/lib/'*' -d . LcdExample.java
Then I ran it:
sudo java -classpath .:classes:/opt/pi4j/lib/'*' LcdExample
You need root permissions to access the GPIO pins.
And finally, the Java code you need:
Download Here!
/*
* adapted from https://github.com/Pi4J/pi4j/blob/master/pi4j-example/src/main/java/LcdExample.java
* which is
* Copyright (C) 2012 - 2013 Pi4J
* Licensed under the Apache License, Version 2.0 (the "License")
*
* Modified version by Thierry Chantier
* Copyright (C) 2013 PiG
* Licensed under the Apache License, Version 2.0 (the "License")
*
* compile with: javac -source 7 -target 7 -classpath .:classes:/opt/pi4j/lib/'*' -d . LcdExample.java
* run with: sudo java -classpath .:classes:/opt/pi4j/lib/'*' LcdExample
*/
import java.text.SimpleDateFormat;
import java.util.Date;
import com.pi4j.component.lcd.LCDTextAlignment;
import com.pi4j.component.lcd.impl.GpioLcdDisplay;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
public class LcdExample {
public final static int LCD_ROWS = 2;
public final static int LCD_ROW_1 = 0;
public final static int LCD_ROW_2 = 1;
public final static int LCD_COLUMNS = 16;
public final static int LCD_BITS = 4;
public static void main(String args[]) throws InterruptedException {
System.out.println("<--Pi4J--> GPIO 4 bit LCD example program");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// initialize LCD
final GpioLcdDisplay lcd = new GpioLcdDisplay(LCD_ROWS, // number of row supported by LCD
LCD_COLUMNS, // number of columns supported by LCD
RaspiPin.GPIO_11, // LCD RS pin
RaspiPin.GPIO_10, // LCD strobe pin
RaspiPin.GPIO_06, // LCD data bit 1
RaspiPin.GPIO_05, // LCD data bit 2
RaspiPin.GPIO_04, // LCD data bit 3
RaspiPin.GPIO_01); // LCD data bit 4
// clear LCD
lcd.clear();
Thread.sleep(1000);
// write line 1 to LCD
lcd.write(LCD_ROW_1, "Pi4J is great!");
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
// gpio.shutdown(); <--- implement this method call if you wish to terminate the Pi4J GPIO controller
}
}
I’m going to practice basics of Golo again, my brain is not on full steam those days ;)
See you soon!