1

デジタル風速計を使用して、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();
        }

    }


}

}

単純に風速計と呼ばれます。

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呼び出しに注意してください。したがって、本質的には、リスナーを追加してからすぐにコードから削除します。200ms待ってから呼び出してみてください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 に答える