Getting Started With Google's Android Things
Getting Started With Google's Android Things
Written by Harry Fairhead   
Article Index
Getting Started With Google's Android Things
GIPO Performance

Simple GPIO

From here all you have to do is make use of the new classes and methods provided by the Things support library. There are classes to interface to the GPIO lines including PWM, I2C, SPI and Serial bus. Using them is just a matter or reading the documentation and dealing with the hardware side of the connection. 

As an illustration of how things work let's see how fast we can toggle a GPIO line. This will give a rough idea of how fast things can be done in Things - the results might surprise you.

To use the GPIO you have to know what each pin is called and use an appropriate String identifier. There doesn't seem to be any attempt to standardize names between platforms as there is in, say, mBed. To find out what pins a device supports you either have to look in the documenation or run a small query program:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

 PeripheralManagerService manager =
                      new PeripheralManagerService();
 List<String> portList = manager.getGpioList();
 if (portList.isEmpty()) {
  Log.i(TAG, "No GPIO port available on
                               this device.");
 } else {
  Log.i(TAG, "List of available ports: " + portList);
 }

 

To make use of the PeripheralManagerService you need to add:

import com.google.android.
              things.pio.PeripheralManagerService;

The documentation gives the Pi's GPIO pin outs as:

 

gpiodiagram

The list of pin names returned by the code corresponds to all of the BCM pins, which means you cannot choose to use the I2C or SPI pins as GPIO lines. 

To flash an LED connected to BCM4 (i.e. pin 7) you would use:

 private Gpio mGpio;
 try {
  mGpio = manager.openGpio("BCM4");
  mGpio.setDirection(
             Gpio.DIRECTION_OUT_INITIALLY_LOW);
  mGpio.setActiveType(Gpio.ACTIVE_HIGH);
  for(int i=0;i<1000;i++){   

    mGpio.setValue(true);
    SystemClock.sleep(1000);
    mGpio.setValue(false);
    SystemClock.sleep(1000);

  }
 } catch (IOException e) {
  e.printStackTrace();
 }

 

Notice that the GPIO line is selected using a string identifier "BCM4" and there are configuration methods to set it to output and active high or low. The actual work is done using the setValue method. The SystemClock.sleep suspends the thread for the specified number of milliseconds.

If you try the program with suitable hardware you should see an LED flash slowly - this is Blinky in action.

blink1

 blink2

 

How Fast?

A question that always has to be asked when using a new platform is how fast can you go? Putting this another way, how much of the device's processing power is actually available to you?

The Pi 3 is a fast quad-core device and it runs Linux fairly fast. In fact, it is among the fastest IoT devices I use so I was very hopeful of good results. 

First a modified Blinky program toggled the GPIO line as fast as possible:

for(int i=0;i<10000;i++){
 mGpio.setValue(true);
 mGpio.setValue(false);
}

Connecting a logic analyser to the pin revealed the following timings:

logic1

The fastest pulses seen are around 0.23ms and there are lots of large (up to 8ms) interruptions in the pulse train.

Compare this to the same program implemented in C, which can be found in Raspberry Pi IoT in C running on the Pi 3. This has a smallest pulse of around 45ns. It too has some irregularities but not as bad as in the case of Android Things. 

That is, the Java program is running a staggering 5,000 times slower than the same program in C. 

With such a slow control over the GPIO lines, there are a great many things that you could not use Android Things for. You most certainly couldn't use it for anything that needed even a reasonably fast response time. If you think that 0.23 ms is fast enough, then consider flashing say ten LEDs on and off. A Things controller inside a robot vacuum cleaner stands a good chance of falling down a staircase by the time the program manages to scan a clutch of sensors. There may be ways to speed things up, but this all depends on how well the asynchronous event notifications have been implemented. Even so it is clear that if you are planning to do anything even slightly demanding you are going to have to worry exactly how it is implemented.

At the moment the verdict has to be that Android Things takes one of the most powerful IoT devices available and brings it to its knees.

User Drivers A Solution?

This also means that the ability to write user drivers is also crippled. It is fine if you simply want to read and write a new I2C or SPI device because the low level interfacing which needs speed is taken care of by the hardware and system software. What it doesn't help with is connecting any custom or unsupported bus device. For example, where is the 1-wire bus and what about the DHT22? At the moment there seems to be no way to use them.

A possible solution is to use C functions within the user driver, but there seems to be no guidance on doing this. I will report back if it is possible. 

The UI 

To finish on a happier note - the real strength of Android Things is the ease with which you can create a UI. There is almost no need to explain if you already know how to create an Android UI - if you don't then obtain a copy of Android Programming: Starting With An App by Mike James which is a comprehensive introduction to creating an Android UI.

To demonstrate how easy it is let's just add a button to our current example.

First select the res directory, right click on it and select New, Android Resource file. Fill in the details in the dialog box that appears:

resource

Call the file mainlayout and set its resource type to Layout. You can leave the root element as LinearLayout, but in most cases you would want to choose something more sophisticated.

When you close the dialog box the resource is created. The chances are you will see an error message when the Designer tries to open. Don't panic - just refresh the editor and it should work. Use the Designer to add a Button to the layout . You could add an event handler if you want to but more of this another time. 

Now all you have to do is add the line:

setContentView(R.layout.mainlayout);

in onCreate immediately after 

super.onCreate(savedInstanceState);

This loads, inflates and displays the resource file to display the UI.

Now when you run the program you will see a single button, assuming you have a monitor. If you also have a mouse, or if the display is a supported touch-sensitive screen, then you can also click the button.

For the IoT having a complete Android UI toolkit is a luxury indeed. 

Watch this space for more developments.

Listing

The complete program including a suitable destructor is:


package com.example.androidthings.myproject;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import com.google.android.things.pio.Gpio;
import com.google.android.
      things.pio.PeripheralManagerService;
import java.io.IOException;
import java.util.List;

public class MainActivity extends Activity {
 private static final String TAG =
                    MainActivity.class.getSimpleName();
 private Gpio mGpio;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.mainlayout);
  PeripheralManagerService manager =
                        new PeripheralManagerService();
  List<String> portList = manager.getGpioList();
  if (portList.isEmpty()) {
   Log.i(TAG, "No GPIO port available
                             on this device.");
  } else {
   Log.i(TAG, "List of available ports: " + portList);
  }
  Log.d(TAG, "onCreate");
  try {
   mGpio = manager.openGpio("BCM4");     
   mGpio.setDirection(
              Gpio.DIRECTION_OUT_INITIALLY_LOW);
   mGpio.setActiveType(Gpio.ACTIVE_HIGH);
   for(int i=0;i<10000;i++){
    mGpio.setValue(true);
    mGpio.setValue(false);
   }
  } catch (IOException e) {
    e.printStackTrace();
 }
}

@Override protected void onDestroy() {   super.onDestroy();
 if (mGpio != null) {
   try {
    mGpio.close();
    mGpio = null;
   } catch (IOException e) {
    Log.w(TAG, "Unable to close GPIO", e);
   }
 }
 Log.d(TAG, "onDestroy");
 }
}

The XML file for the simple layout is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
      "http://schemas.android.com/apk/res/android"
 android:orientation="vertical"  android:layout_width="match_parent"
 android:layout_height="match_parent">
<Button
 android:text="Button"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/button" />
</LinearLayout>

 

pi3top

More Information

Android Things

 

Related Articles

Android Things - Google's IoT

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin.

 

 

 
 
 

blog comments powered by Disqus

 

<ASIN:1871962471> 

 



Last Updated ( Monday, 19 December 2016 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
Banner