Hello IOIO – Android


Hello IOIO Android

Here’s (finally) my first attempt at programming an Android phone AND interacting with the physical world at the same time… !

As many others, I was quite pleasantly surprised when during Google I/O 2011 they announced the Android Open Accessory standard / development kit…

And given that I was at the end of the autonomous tank project, I decided I HAD to get into this… it was the perfect occasion…

After some searching and looking around, I decided to buy a basic Android phone (Orange San Francisco or ZTE Blade) for around 90Β£ and go with the IOIO board from Sparkfun.

This choice might seem strange, as this is NOT an Android Open Accessory board (they’ve released new firmware that makes it compatible by supporting the ADK protocol !) BUT given the (lack of) availability of those, their size and price I decided it was better to go with this one, that also has the advantage to have been around for slightly longer…

Also it should be really straight forward to update the code from one standard to another.

Actually the MOST IMPORTANT reason of going with this board is that being based on the ADB (USB debugging) protocol it works with any version of Android >= 1.5. Google’s ADK protocol for the open accessory standard has only been implemented in 3.1 for tables and back ported to 2.3.4 for phones… Finding a 2.3.4 phone or upgrading one, is a pain…

So without further due, here’s a short video of my first Hello IOIO program, which simply blinks a LED, but provides 2 seek bars allowing you to change the frequency of the blinking and the brightness.

 

And here is the Java code for the Android:


package ioio.examples.hello;

import ioio.lib.api.DigitalOutput;
import ioio.lib.api.PwmOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.AbstractIOIOActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
* This is the main activity of the HelloIOIO example application.
*/
public class MainActivity extends AbstractIOIOActivity {
private ToggleButton _button;
private SeekBar _seekBarFreq;
private SeekBar _seekBarBright;
private TextView _freq;
private TextView _bright;
private volatile int _delay;
private volatile float _brigthness;

/**
* 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);
_button = (ToggleButton) findViewById(R.id.button);
_seekBarFreq = (SeekBar) findViewById(R.id.seekBarFreq);
_seekBarBright = (SeekBar) findViewById(R.id.seekBarBright);
_freq = (TextView) findViewById(R.id.textViewFreqVal);
_bright = (TextView) findViewById(R.id.textViewBrightVal);

_seekBarFreq.setMax(100);
_seekBarBright.setMax(100);

_seekBarFreq.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
_delay = (2000 * progress) / _seekBarFreq.getMax();
_freq.setText(String.valueOf(_delay));
}
});

_seekBarBright.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
_brigthness = (float)progress / (float)_seekBarBright.getMax();
_bright.setText(String.valueOf(_brigthness));
}
});
}

/**
* 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 PwmOutput _led;

/**
* 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_.openPwmOutput(0, 300);
}

/**
* 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 {
try {
_led.setDutyCycle(1 - _brigthness);
sleep(_delay);
_led.setDutyCycle(1);
sleep(_delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

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

Enjoy, and don’t hesitate to leave comments or questions…

32 Responses to Hello IOIO – Android

  1. JULIEN says:

    Hi Trandi,

    don’t know if you’re french or not… so let’s write in english ! I’m very interested with the ioio but have some questions ! Could you please tell me if the outputs can act like physical push buttons (those with just 2 wires) ? If so, how many pushbuttons can be replaced with one ioio and ?

    Thanks a lot in advance for your help !

    Julien

    • trandi says:

      Salut Julien,

      Yep, better in English so that most other people can benefit from the discussion… πŸ™‚
      Not sure I understand your question, do you want your IOIO to simulate a button *or* to accept the input from a button to control something ?
      What *exactly* do you want to use it for ?
      It normally can do either, but I wouldn’t be able to tell you for how many buttons, it’s been years since I played with a IOIO board…

      Dan

  2. Mounesh says:

    Please let me know about IOIO programming guide or anything

    Thank you in advance
    Mounesh.S.K

  3. javier ruiz says:

    HI Brother, I’m think creater arm robotic for 4 grade liberty with model al5a ,this codes example run with eclipse but found error “AbstractIOIOActivity;”,this reason library no found this code, I’m found This page is kept here as reference for legacy applications. This class is deprecated, and replaced by the ioio.lib.util.android.IOIOActivity class. For more details, see this page.

    you can help me with solution problem with library AbstractIOIOActivity.

    https://github.com/ytai/ioio/wiki/Using-AbstractIOIOActivity-(deprecated)

    thank’s you very muchs.

    by xavier
    j

  4. Sebastian says:

    Hello trandi,do you have more documentation about this ioio?? a book,something like that..or just what is on google ?!

  5. Jerre says:

    Hi,

    I am not experienced at this.
    I get errors at this point :

    _seekBarFreq = (SeekBar) findViewById(R.id.seekBarFreq);
    _seekBarBright = (SeekBar) findViewById(R.id.seekBarBright);
    _freq = (TextView) findViewById(R.id.textViewFreqVal);
    _bright = (TextView) findViewById(R.id.textViewBrightVal);

    it says something like he can’t find seekBarFreq at the R file, and i can’t modify the R file ..

    Can u help me ?

    Grtz

    • trandi says:

      The R.java is automatically generated from the resources files.
      In this case you need to update main.xml (in res/layout) to add these graphical objects.

      This is really basic Android stuff, has nothing to do with IOIO programming.

      Hope this helps,
      Dan

  6. Tobz says:

    Hey Man, Good JOB!!! I tried Copying your source code, but am not getting any result. i ran the HelloIOIO, That works Perfect, but when i tried the code above, i get this errors,

    textViewFreqVal cannot be resolved or is not a field MainActivity.java /HelloIOIO/src/ioio

    Any help!!!! Please Thanks

  7. sebastian says:

    hello trandi,i wonder if u can help me to learn more about this android IOIO,i’m student (last year) and for my license,i must creat something like that . I must use RC servo or something with led,i saw what u do,is amazing :d
    My question is, Can u help me to learn about ?:D
    my english is to bad,but i hope u can understand what i said..
    ciao,replayme :d

    • trandi says:

      Salut,

      Vad ca te-ai conectat din RO, deci presupun ca vorbeshti romaneste πŸ™‚ La ce facultate esti si ce specialitate ?

      I’ll revert back to English now, for the benefit of other eventual visitors on this blog… πŸ™‚

      I’d be happy to help you of course, but don’t get too excited, there’s not much that I can do for you.. you’re the one that needs to invest the time and work !

      Start eventually by giving more details about what exactly you’re studying and what is your EXACT project.
      I can only give you some advice / suggestions on concrete issues or questions…

      Dan

      • sebastian says:

        Salut,ma bucur ca ati fost asa promt la ceea ce am postat.Ma bucur ca ai inteles ce am scris in limba engleza,desi sunt greseli de exprimare.
        Sunt student in anul IV,la facultate de Inginerie Electrica si Stiinta Calculatoarelor,profil TELECOM.(TRANSILVANIA BRASOV )
        Am vorbit cu profesorul de licenta si mi-a propus sa fac pe acesta placa IOIO o aplicatie cu ceva senzori(la alegere) de presiune,temperatura,de procent de alcoolemie in aerul liber sau o aplicatie cu servomotor.Nu trebuie sa fac ceva mult prea amplu,mi-a zis ca pentru licenta este suficient sa fac asa ceva sau sa transmit niste date,eventual ptr master ceva mai amplu.
        Sunt curios la tine ce pret are acesta placa ,pentru ca la noi este in jur de 200ron(50euro) destul de scumpa.Dar imi asum acest costuri.
        Problema este ca la capitolul programare java,c++ stau foarte prost,de aceea incerc sa ti cer tie ajutorul.Mi-au promis si cativa colegi ca ma vor ajuta. Si vroiam sa apelez la tine cand intampin ceva probleme,sa te contactez daca se poate .
        Si exact ceea ce ai creat tu,trebuie sa fac si eu :p .Zi-mi te rog un cost care te-a ajuns pe tine,ptr sa fac o comparatie cu costul care m-ar ajunge pe mine.
        Mersi,O zi buna !
        If u dont understand what i wrote here,i will translate u.

      • trandi says:

        Hi Sebastian,

        I’ll reply in English as mentioned before, simply because all of this blog in in English and I like to keep it consistent for future reference… hope you don’t mind…

        – overhere, I bought my IOIO board from here http://www.coolcomponents.co.uk/catalog/product_info.php?products_id=717&osCsid=j90j5kqfegquksdbumahtmuqg5 for about 30£ (it was more I think when I bought it) or you can get it from sparkfun for 50$ http://www.coolcomponents.co.uk/catalog/product_info.php?products_id=717&osCsid=j90j5kqfegquksdbumahtmuqg5 If you’re in Romania, I would recommend to get it from within the EU to avoid extra import taxes

        – as you can see on my blog, you can also connect it to a Wii Motion plus device or Wii Nunchuck. In case you can find any of these or you already have them, this will allow you to save some money

        – the IOIO is all about Java programming. The whole point of the board is to allow software developpers to interact with the physical world, WITHOUT knowing much about electronics. If you’re doing electronics at university and are not good at programming, maybe this board is NOT for you ? Unless you want to improve your Java skills…

        – I’ll of course try to help you, but again I can only answer PRECISE questions, I won’t be able to do the project or write all or part of the code for you… πŸ™‚

        Dan

      • sebastian says:

        Thanks for everything.Of course,i didnt want to say to do my license or creat the prog code..it is my job and my work to creat something .
        I will talk with the teacher to give me something certainly, i will discuss about the cost … I think it’s quite expensive ,but i hope deserve what i’ll do.

        Thanks again,and certainly i will contact you as so as i have something to do.

        To my shame I do not know very well to express in English.

  8. hieutran says:

    i wonder IOIO kits have any drive for it, or android can get it automatically

    • trandi says:

      If you mean “driver”, then no, there is no need for it.
      The IOIO uses the ADB protocol (debug) that is already an Android standard.
      However, for any Android app to interact with the IOIO, you have to write / modify it, so that it uses the IOIO library.

      Hope this helps,
      Dan

  9. tamlyn says:

    Hi Trandi, did you have any difficulty getting the board to talk to your phone? I just bought a ZTE Blade and when I plug it into the IOIO, the yellow LED comes on but the HelloIOIO example doesn’t let me control it.

    • trandi says:

      Hi Tamlyn,

      NO, absolutely no problem.
      HOWEVER, my phone is running Android 2.3.3, as CyanogenMod. (I bought it like this on eBay, so don’t have more details about why or how it was installed…)

      Have you checked that debugging is enabled from the settings of your phone ? The LED on the board lighting up doesn’t mean that the phone actually sees the board…

      • tamlyn says:

        I just installed the Modaco 2.2 ROM and now it works! Guess the stock Orange ROM is breaking it somehow.

      • trandi says:

        Wow, good to hear ! I guess I was just lucky to have bought my second hand phone with the Cyanomod already installed…

        dan

  10. jingleyeah says:

    Hi,trandi,
    I’m wondering which android 2.3.4 rom you use for ZTE Blade, my current use, cm7.1.0RC1 doesn’t have support for adk, thanks

    • trandi says:

      I’m NOT using 2.3.4, but 2.3.3 from http://www.cyanogenmod.com/.

      Also, I’m NOT using ADK. The IOIO board does not use the ADK protocol, instead it relies on ADB (Android Debug Bridge) and hence supports andy Android version since 1.5.

      Worth noticing that the new firmware (in beta) does support ADK if you want / need (better speed) but it’s by no mean mandatory.

      Hope this helps,
      dan

  11. Pingback: Android IOIO Wii Nunchuck « Robotics / Electronics / Physical Computing

  12. codywalker says:

    can’t wait to get mine. Is it possible to run the app in the background, to monitor and react to events such as “Phone Ring”, or “incoming txt msg”, or “new email”. Etc?

    if so, I think it would be fun to make a multi-color LED notification system for a good challenge.

  13. Pingback: » Android- interface usb

  14. Michael Blix says:

    Nice. I’m glad it’s so quick to get started with and get the traditional ‘blinking LED’ up and running, but also at a more advanced level. Any project goals for the platform in mind?

    • trandi says:

      Yes, I was really pleasantly surprised that in basically a few hours I got the Android SDK installed and had a (not so) basic hello world example working on the IOIO !

      Now, regarding future goals, that’s the hard part… πŸ™‚ Don’t have any interesting ideas yet so I’m open to suggestions…

      dan

      • Michael Blix says:

        It’s great that now you can so easily add a display with touchscreen input and access to other sensors and onboard processing power to a physical app.

        I just ordered a “yo-yo” board so will try some stuff out. Something using the sensors or GPS.

Leave a comment