我正在用Java编写Raspberry项目,并使用Pi4j库。 我不会使用轮询,所以例如我使用这个代码:
public class ListenGpioExample {
public static void main(String args[]) throws InterruptedException {
System.out.println("<--Pi4J--> GPIO Listen Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// 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) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
}
});
System.out.println(" ... complete the GPIO #02 circuit and see the listener feedback here in the console.");
// keep program running until user aborts (CTRL-C)
for (;;) {
Thread.sleep(500);
}
// 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
}}
我的问题是: 存在无限循环的事实仍然是一种轮询形式?
因为我不明白是否需要无限循环。
答案 0 :(得分:0)
不,这不是民意调查。此示例使用事件侦听器,该事件侦听器将在GPIO Pin
中发生状态更改时触发正如您可以阅读Pi4J library page:
侦听器实现不基于GPIO硬件中断 州民意调查。
for(;;)
旨在使程序保持活动状态,直到用户停止它为止(正如您在评论中所读到的那样)