Raspberry Pi 2 Model B Pi4J 示例
創建GPIO控制器實例
final GpioController gpio = GpioFactory.getInstance();引腳編號圖
Provision Pins 提供的針腳
下面的示例演示如何設置輸入 pin。輸入的引腳針腳程序會監視狀態更改或審問時所需的狀態。輸入的引腳用于開關、 傳感器等。
GpioPinDigitalInput button = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, "button",PinPullResistance.PULL_DOWN);下面的示例演示如何設置輸出插針。輸出插針針腳您的程序將控制狀態更改。輸出引腳用于控制繼電器、 發光二極管、 晶體管等。
GpioPinDigitalOutput led =gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "led", PinState.LOW);Control Pin State 控件引腳狀態
// 將狀態顯式設置 pin 對象上 led.setState(PinState.HIGH); // 使用方便的包裝方法將 pin 對象上設置狀態 led.low(); led.high();// 使用切換方法針對象上應用逆狀態 led.toggle();// 脈沖法將 pin 設置為高的狀態為 顯式長度的時間 (毫秒) led.pulse(1000);// 設置此引腳關機狀態 led.setShutdownOptions(true, PinState.LOW);// 打開 gpio pin #01 1 秒鐘,然后又關閉 led.pulse(1000, true);// 通過關閉 GPIO 控制器停止所有 GPIO 活動/線程 gpio.shutdown();提供了一個完整的示例項目展示的 GPIO 引腳控制 here.
Read Pin State 讀引腳狀態
// 獲取與此按鈕關聯的 GPIO 引腳的顯式狀態枚舉 PinState buttonState = button.getState();// 使用方便的包裝方法來審問的按鈕狀態 boolean buttonPressed = button.isHigh();Listen for Pin Changes 監聽引腳變化
// 下面的代碼段演示一個簡單的偵聽器實現 class GpioUsageExampleListener implements GpioPinListenerDigital {@Overridepublic void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {// 在控制臺上顯示 pin 狀態System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());} }// 要設置您的偵聽器,必須用別針注冊監聽器并實例化您的偵聽器 impl。 button.addListener(new GpioUsageExampleListener());// 另一種實現 button.addListener(new GpioPinListenerDigital() {@Overridepublic void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {// 在控制臺上顯示 pin 狀態System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());} });提供了一個完整的示例項目展示偵聽 GPIO 引腳狀態更改 here
Pin Shutdown Pin 關機
led.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF);提供演示 GPIO 引腳關機配置一個完整的示例項目 here
Pin Triggers Pin 觸發器
// 在輸入引腳上創建一個 gpio 同步觸發器 // 當輸入的狀態的更改,也設置 LED 控制 gpio 引腳到相同的狀態 button.addTrigger(new GpioSyncStateTrigger(led));提供了一個完整的示例項目展示 GPIO 引腳觸發器 here
Serial Communication
P14J provides a simplified API to send and receive data using a serial port of the Raspberry's Pi.
The following example project demonstrates implementing serial communication:
Serial Communication Example.
System Information
P14J provides an API to access system and network information from the Raspberry's Pi.
The following example project demonstrates accessing system and network information:
System Information Example.
總結
以上是生活随笔為你收集整理的Raspberry Pi 2 Model B Pi4J 示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 硅谷最有名的帮派:如果你不知道PayPa
- 下一篇: angular之表单