package ycan.com.tw;
import java.io.IOException;
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.PinState; import com.pi4j.io.gpio.RaspiPin; import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent; import com.pi4j.io.gpio.event.GpioPinListenerDigital;
public class ClosePi { public static void main(String[] args) throws IOException {
final GpioController gpio = GpioFactory.getInstance(); //設定GPIO_07為Button final GpioPinDigitalInput closeButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_07, PinPullResistance.PULL_DOWN); Thread th = new Thread(new Runnable() {
@Override public void run() {
while (true) {
// if (closeButton.isLow()) { // D1 on ground? // System.out.println("in Low----"); // // } if (closeButton.isHigh()) { // D1 on ground? System.out.println("in Hi----"); //關機 Runtime runtime = Runtime.getRuntime(); try { Process proc = runtime.exec("shutdown -h now"); } catch (IOException e) { e.printStackTrace(); } System.exit(0); }
}
} }); th.setPriority(Thread.MAX_PRIORITY); th.start(); System.out.println("Thread start");
for (;;) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
} }
|