我正在尝试使用数字风速计与我的树莓派 pi4j 一起工作。
我的想法是GpioPinListenerDigital
当一个引脚变高时添加到监视器(意味着 1 个完整的风速计旋转),但我不能让它工作......我想设置一个间隔来观察中断,但没有成功......
这是我的主要课程
public class Main {
public static void main(String[] args) throws InterruptedException {
Anemometer anemometer;
int rotations = 0;
System.out.println("Start");
while(true){
rotations = Anemometer.countPulse();
System.out.println("Rotations "+ rotations);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
它简称为风速计,即
public class Anemometer {
final static int short_interval = 3;
static int final_counter = 0;
public static int countPulse() {
GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalInput input = gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_DOWN);
input.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
long start = System.currentTimeMillis();
long end = 0L;
int counter = 0;
while (end < short_interval * 1000) {
System.out.println("Inizio while");
if (event.getState().isHigh()) {
System.out.println("Pin state: " + event.getState());
counter++;
}
end = (new Date()).getTime() - start;
}
final_counter = counter;
System.out.println("final counter: "+final_counter);
}
});
gpio.unprovisionPin(input);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return final_counter;
}
}
看起来它永远不会进入while循环......知道吗?