Getting Started With Google's Android Things - Preview 2
Getting Started With Google's Android Things - Preview 2
Written by Harry Fairhead   
Article Index
Getting Started With Google's Android Things - Preview 2
GIPO Performance
How Fast Is Native?

How Fast Is Native?

To find our how fast we can toggle the GPIO lines, load the blink.cpp file and change its contents to read: 


#include <cmath>
#include <android/log.h>
#include <android_native_app_glue.h>
#include <pio/gpio.h>
#include <pio/peripheral_manager_client.h>
#include "AndroidSystemProperties.h"

void android_main(android_app* app) {
app_dummy(); // prevent native-app-glue to be stripped.
AndroidSystemProperties systemProperties(app->activity);

APeripheralManagerClient* client =
                         APeripheralManagerClient_new();
AGpio* gpio;
APeripheralManagerClient_openGpio(client, "BCM4", &gpio);
 int result = AGpio_setDirection(gpio,
                      AGPIO_DIRECTION_OUT_INITIALLY_LOW);
 while (true) {
  AGpio_setValue(gpio, 0);
  AGpio_setValue(gpio, 1);
 }
}

This will toggle line BCM4 exactly as the Java program given earlier did. 

So what is the speed improvement?

cspeed

The bad news is that the 0.23ms is reduced to around 0.15ms which is an improvement, but not enough. A pulse time of 150us is not fast enough to write drivers that interface to custom protocols. 

So why is it so slow?

As the source code for the Android Things native library hasn't been released it is impossible to be certain. but it seems likely that it isn't using memory mapping to access the GPIO lines. For example, using SYSFS inefficiently yields a pulse time of around 200us. Doing the job correctly without opening and closing the file for each pulse improves this to 5us and using memory mapping you can generate pulses shorter than 1us. 

It seems that the problem isn't that Java slows things down, it is probably the basic GPIO access method.  

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 blinky Java 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 Dev Preview 2 Could Be What We Need.

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:187196251X> 

<ASIN:1871962463>

<ASIN:1871962447>

 



Last Updated ( Sunday, 26 March 2017 )
 
 

   
Banner
Banner
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.