官方的一个简单示例
// START SNIPPET: blink-gpio-snippet
/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: Java Examples
* FILENAME : BlinkGpioExample.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: http://www.pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2016 Pi4J
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
*
* #L%
*/
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
/**
* This example code demonstrates how to perform simple
* blinking LED logic of a GPIO pin on the Raspberry Pi
* using the Pi4J library.
*
* @author Robert Savage
*/
public class BlinkGpioExample {
public static void main(String[] args) throws InterruptedException {
System.out.println("<--Pi4J--> GPIO Blink Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// provision gpio pin #01 & #03 as an output pins and blink
final GpioPinDigitalOutput led1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);
final GpioPinDigitalOutput led2 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_03);
// provision gpio pin #02 as an input pin with its internal pull down resistor enabled
final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);
// create and register gpio pin listener
myButton.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// when button is pressed, speed up the blink rate on LED #2
if(event.getState().isHigh()){
led2.blink(200);
}
else{
led2.blink(1000);
}
}
});
// continuously blink the led every 1/2 second for 15 seconds
led1.blink(500, 15000);
// continuously blink the led every 1 second
led2.blink(1000);
System.out.println(" ... the LED will continue blinking until the program is terminated.");
System.out.println(" ... PRESS
// keep program running until user aborts (CTRL-C)
while(true) {
Thread.sleep(500);
}
// stop all GPIO activity/threads
// (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
}
}
//END SNIPPET: blink-gpio-snippet
操作输出
package com.xyes.pi4j.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;
public class EnvTest {
public static void main(String[] args) {
URL url = null;
byte[] buffer = null;
try {
url = new URL("http://www.baidu.com");
InputStream stream = url.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(stream, out);
out.close();
stream.close();
buffer = out.toByteArray();
} catch (Exception e1) {
e1.printStackTrace();
}
final GpioController controller = GpioFactory.getInstance();
final GpioPinDigitalOutput LED = controller.provisionDigitalOutputPin(RaspiPin.GPIO_15);
for (byte b : buffer) {
if (b > 100) {
LED.high();
} else {
LED.low();
}
System.out.println("-->" + b);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
}
操作输入
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinListener;
public class ButtonTest implements GpioPinListener {
private static final GpioController CONTROLLER = GpioFactory.getInstance();
private static final GpioPinDigitalInput BUTTON = CONTROLLER.provisionDigitalInputPin(RaspiPin.GPIO_16);
private static final GpioPinDigitalOutput LED = CONTROLLER.provisionDigitalOutputPin(RaspiPin.GPIO_15);
public static void main(String[] args) {
new ButtonTest().run();
}
public void run() {
while (true) {
if (BUTTON.isHigh()) {
LED.high();
} else {
LED.low();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
监听器方式读取输入
import java.util.Scanner;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
public class GpioListener implements GpioPinListenerDigital {
private static final GpioController CONTROLLER = GpioFactory.getInstance();
private static final GpioPinDigitalInput BUTTON = CONTROLLER.provisionDigitalInputPin(RaspiPin.GPIO_16);
private static final GpioPinDigitalOutput LED = CONTROLLER.provisionDigitalOutputPin(RaspiPin.GPIO_15);
public static void main(String[] args) {
new GpioListener().run();
}
public void run() {
BUTTON.addListener(this);
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
boolean state = event.getState().isHigh();
System.out.println("--->pin:" + event.getPin() + "|state:" + state);
if (state) {
LED.high();
} else {
LED.low();
}
}
}