2016-06-07 46 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);