2011-10-28 2 views
0

sparkfun에서 제공 한 IOIO 보드를 사용하고 있습니다. UART 테스트를 시도하는 동안 IOIO 보드의 RX와 TX를 묶어서 전화에서 1 바이트를 보내고 텍스트와 동일한 문자를 Phone에 표시하려고했습니다. 다음 코드를 실행하면 UI가 변경되지 않습니다. 나는 근본적인 것이 빠져 있다고 생각한다. 어떤 제안/아이디어?Android - IOIO 보드, 간단한 UART 코드 문제

package ioio.examples.hello; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import ioio.examples.hello.R; 
import ioio.lib.api.DigitalOutput; 
import ioio.lib.api.IOIO; 
import ioio.lib.api.IOIOFactory; 
import ioio.lib.api.exception.ConnectionLostException; 
import ioio.lib.api.exception.IncompatibilityException; 
import ioio.lib.util.AbstractIOIOActivity; 
import android.os.Bundle; 
import android.widget.ToggleButton; 
import android.widget.TextView; 
import ioio.lib.api.Uart; 

/** 
* This is the main activity of the HelloIOIO example application. 
* 
* It displays a toggle button on the screen, which enables control of the 
* on-board LED. This example shows a very simple usage of the IOIO, by using 
* the {@link AbstractIOIOActivity} class. For a more advanced use case, see the 
* HelloIOIOPower example. 
*/ 

public class MainActivity extends AbstractIOIOActivity { 

private ToggleButton togglebutton; 
private TextView textView; 

/** 
* Called when the activity is first created. Here we normally initialize 
* our GUI. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    textView = (TextView) findViewById(R.id.editText1); 
    togglebutton = (ToggleButton) findViewById(R.id.button); 
} 

/** 
* This is the thread on which all the IOIO activity happens. It will be run 
* every time the application is resumed and aborted when it is paused. The 
* method setup() will be called right after a connection with the IOIO has 
* been established (which might happen several times!). Then, loop() will 
* be called repetitively until the IOIO gets disconnected. 
*/ 
class IOIOThread extends AbstractIOIOActivity.IOIOThread { 
    /** The on-board LED. */ 

    private Uart uart; 
    private InputStream in; 
    private OutputStream out; 
    private byte receivedData[] = new byte[10]; 
    private int offset = 0; 
    private Byte b; 
    protected IOIO ioio_; 

    /** 
    * Called every time a connection with IOIO has been established. 
    * Typically used to open pins. 
    * 
    * @throws ConnectionLostException 
    *    When IOIO connection is lost. 
    * 
    * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup() 
    */ 
    @Override 
    protected void setup() throws ConnectionLostException { 
     // led_ = ioio_.openDigitalOutput(0, true); 
     ioio_ = IOIOFactory.create(); 
     try { 
      ioio_.waitForConnect(); 
     } catch (IncompatibilityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     uart = ioio_.openUart(3, 4, 115200, Uart.Parity.NONE, 
       Uart.StopBits.ONE); 
     in = uart.getInputStream(); 
     out = uart.getOutputStream(); 
    } 

    /** 
    * Called repetitively while the IOIO is connected. 
    * 
    * @throws ConnectionLostException 
    *    When IOIO connection is lost. 
    * 
    * 
    * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop() 
    */ 
    @Override 
    protected void loop() throws ConnectionLostException { 

     offset = 0; 
     while (togglebutton.isChecked()) { 
     try { 
       out.write(65); 
       try { 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        // Ignore 
       } 
      } catch (IOException e) { 
       // TODO ??? 
      } 
      try { 
       in.read(receivedData, 0, 1); 
       try { 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        // Ignore 
       } 
      } catch (IOException e) { 
       // TODO ??? 
      } 

      textView.setText(Byte.toString(receivedData[0])); 
     } 
    } 

} 

/** 
* A method to create our IOIO thread. 
* 
* @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread() 
*/ 
@Override 
protected AbstractIOIOActivity.IOIOThread createIOIOThread() { 
    return new IOIOThread(); 
} 

} 당신이

답변

1

한 명백한 오류는 다음과 같습니다

ioio_ = IOIOFactory.create(); 
try { 
    ioio_.waitForConnect(); 
} catch (IncompatibilityException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

완전히 불필요하고 가능성 문제가있다. AbstractIOIOctivity는 모든 것을 처리합니다. 일부 예 (예 : HelloIOIO 또는 IOIOSimpleApp)를보고 앱이 일반적으로 어떻게 보이는지에 대한 아이디어를 얻으십시오.

그런데 이러한 질문에 대한 적절한 장소는 Google 그룹의 ioio 사용자 목록입니다.

3

UI로 전달되는 모든 항목은 별도의 스레드에서 발생해야합니다. 귀하의 경우 다음과 같이 추가해야합니다 :

 private void changeText(String displayText){ 
     runOnUiThread(new Runnable(){ 
      @Override 
      public void run() { 
       textView.setText(displayText); 

      } 
     }); 
    }