Pi4j风速计 - 每个间隔计数器

时间:2016-06-07 10:27:02

标签: java raspberry-pi pi4j

我正在尝试使用数字风速计与pi4j一起使用我的树莓PI。

我的想法是添加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();
        }

    }


}

}

它只是调用Anemometer,这是

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循环...任何想法?

1 个答案:

答案 0 :(得分:1)

我不熟悉这个API,但很明显你没有给听众足够的时间来解雇。这是GpioControllerImpl.unprovisionPin的代码:

public void unprovisionPin(GpioPin... pin) {
    if (pin == null || pin.length == 0) {
        throw new IllegalArgumentException("Missing pin argument.");
    }
    for (int index = (pin.length-1); index >= 0; index--) {
        GpioPin p  = pin[index];

        // ensure the requested pin has been provisioned
        if (!pins.contains(p)) {
            throw new GpioPinNotProvisionedException(p.getPin());
        }
        // remove all listeners and triggers
        if (p instanceof GpioPinInput) {
            ((GpioPinInput)p).removeAllListeners();
            ((GpioPinInput)p).removeAllTriggers();
        }

        // remove this pin instance from the managed collection
        pins.remove(p);
    }        
}

请注意removeAllListeners来电。基本上,您添加了侦听器,然后立即在代码中删除它。 等待200毫秒后尝试拨打gpio.unprovisionPin(input);

try {
    Thread.sleep(200);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
gpio.unprovisionPin(input);