2016-06-07 51 views
1

我试图用数字风速计与我的覆盆子PI与pi4j一起工作。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; 
} 

} 

它看起来就像从来没有同时进入里面循环...任何想法?

+0

我不是100%确定究竟是什么问题,但我认为intervall设置错误。在进入循环之前立即设置'开始'变量。那么你是否面临同样的问题? – kaetzacoatl

+0

是的,我改变了它,但没有任何反应......我会编辑这些问题,谢谢 – besmart

回答

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电话。所以基本上,你添加了监听器,然后立即在你的代码中删除它。尝试拨打gpio.unprovisionPin(input);等待200ms。

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