1

我正在尝试使用数字风速计与我的树莓派 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循环......知道吗?

4

1 回答 1

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);
于 2016-06-07T14:17:04.210 回答