2012-05-13 21 views
1

背景:フォトダイオードからの出力を測定するために使用しているIOIOがあり、これがデジタル出力に変換されます。私は、信号が1と0の間で変化する周波数を見つける必要があります。誰かが私に周波数を測定するために使用できるいくつかのコードを教えましたが、私はそれを私の既存のアプリケーションにどのように統合するのか分かりません。変数を更新するUIスレッドが周波数を計算する他のスレッドからの戻り値を待っているためスレッドが実行されるときにスレッドの値が取得されるだけなので、実装した方法は機能しません。では、周波数スレッドにダイオードのリアルタイム値を与える方法と、周波数を計算した後に表示するUIスレッドに戻す方法はありますか?ここ外光センサーからの入力頻度

は私のUIスレッド(FrequencyApp.java)である:

public class FrequencyThread extends Thread { 
public float run(int diode){ 
// Find frequency to the nearest hz (+/- 10%) 
// It's assumed that some other process is responsible for updating the "diode" 
// variable. "diode" must be declared volatile. 
long duration = 1000; // 1 second 
final int interval = 100; // sampling interval = .01 second 
int oldState = diode; 
int count = 0; 
final long startTime = System.currentTimeMillis(); 
final long endtime = startTime + duration; 
while (System.currentTimeMillis() < endtime) { 
    // count all transitions, both leading and trailing 
    if (diode != oldState) { 
    ++count; 
    oldState = diode; 
    } 

    Thread.sleep(interval); 
} catch (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
} 
// find the actual duration 
duration = System.currentTimeMillis() - startTime; 
// Compute frequency. The 0.5 term is because we were counting both leading and 
// trailing edges. 
float frequency = (float) (0.5 * count/(duration/1000)); 
return frequency; 
} 

}

:ここ

public class FrequencyApp extends IOIOActivity { 
private TextView textView_; 
private TextView textView2_; 
private TextView textView3_; 
private ToggleButton toggleButton_; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    textView_ = (TextView)findViewById(R.id.TextView); 
    textView2_ = (TextView)findViewById(R.id.TextView2); 
    textView3_ = (TextView)findViewById(R.id.FrequencyLabel); 
    toggleButton_ = (ToggleButton)findViewById(R.id.ToggleButton); 

    enableUi(false); 
} 

class Looper extends BaseIOIOLooper { 
    private AnalogInput input_; 
    private DigitalOutput led_; 
    volatile int diode; 
    private long frequency; 


    @Override 
    public void setup() throws ConnectionLostException { 
     try { 
      input_ = ioio_.openAnalogInput(31); 
      led_ = ioio_.openDigitalOutput(IOIO.LED_PIN, true); 
      enableUi(true); 
     } catch (ConnectionLostException e) { 
      enableUi(false); 
      throw e; 
     } 
    } 

    @Override 
    public void loop() throws ConnectionLostException { 
     try { 
      led_.write(!toggleButton_.isChecked()); 


      float reading = input_.getVoltage(); 

      if(reading > 1){ 
       diode = 1; 
      } else { 
       diode = 0; 
      } 
      if(toggleButton_.isChecked()){ 
       FrequencyThread frequencyTaskThread = new FrequencyThread(); 
       frequencyTaskThread.setPriority(Thread.NORM_PRIORITY-1); //Make the background thread low priority. This way it will not affect the UI performance 
       frequencyTaskThread.start(); 
       frequency = (long) frequencyTaskThread.run(diode); 
       frequencyTaskThread.stop(); 
      } 
      setText(Float.toString(reading), Long.toString(diode), Long.toString(frequency)); 
      Thread.sleep(100); 
     } catch (InterruptedException e) { 
      ioio_.disconnect(); 
     } catch (ConnectionLostException e) { 
      enableUi(false); 
      throw e; 
     } 
    } 
} 

@Override 
protected IOIOLooper createIOIOLooper() { 
    return new Looper(); 
} 

private void enableUi(final boolean enable) { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      toggleButton_.setEnabled(enable); 
     } 
    }); 
} 

private void setText(final String str,final String str2,final String str3) { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      textView_.setText(str); 
      textView2_.setText(str2); 
      textView3_.setText(str3); 
     } 
    }); 
} 

}

周波数(FrequencyThread.javaを算出するためのスレッドであります

+0

あなたは試していないキャッチブロックを持っていて、あなたの角括弧はFrequencyThread内のあらゆる種類のものです... – FoamyGuy

答えて

0

最も簡単に設定する方法は、おそらくFrequencyThreadからメインスレッドにメッセージを送信します。スレッド/ハンドラを抽象化し、全体の状況を扱いやすくするためにAsyncTaskを使用する方がおそらく好ましいでしょう。 IOIOLooperをAsyncTaskに入れる必要があるかもしれませんが、そのボードやそのJava APIに関する経験はありません。

また、setText()メソッドでは、 "runOnUiThread"またはRunnableは必要ありません。とにかくメインスレッドから呼び出されているようだ。

あなたがしたいことは、Handler handleMessage()をオーバーライドしてsetText()を呼び出すことです。その後、FrequencyThreadの内部で、データやメッセージ(つまりエラー)を返すhandler.sendMessage()を呼び出します。

私が例を挙げて投稿したコードを使用してみましたが、正直にそれに続く問題があります。