Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Created January 5, 2017 04:48
Show Gist options
  • Save patrickhammond/fc20b8394722666afcafd354c3266718 to your computer and use it in GitHub Desktop.
Save patrickhammond/fc20b8394722666afcafd354c3266718 to your computer and use it in GitHub Desktop.
First tinkering with Android Things on a Raspberry Pi. Demo video: https://goo.gl/photos/R6oZmnqS8XBmTNx58
<?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:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"/>
<TextView
android:id="@+id/primes"
style="?android:textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"/>
</LinearLayout>
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.example.androidthings.myproject"
minSdkVersion 24
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
// This is the only really interesting thing
provided 'com.google.android.things:androidthings:0.1-devpreview'
}
package com.example.androidthings.myproject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.PeripheralManagerService;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
// There are lots of better ways of doing this...but it works...
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final String GPIO_NAME = "BCM17";
private TextView primeStatusView;
private Button startView;
private Button stopView;
private Gpio gpio17;
private AsyncTask<Void, Integer, Void> task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
primeStatusView = (TextView) findViewById(R.id.primes);
startView = (Button) findViewById(R.id.start);
stopView = (Button) findViewById(R.id.stop);
PeripheralManagerService manager = new PeripheralManagerService();
try {
gpio17 = manager.openGpio(GPIO_NAME);
gpio17.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH);
gpio17.setActiveType(Gpio.ACTIVE_LOW);
} catch (IOException e) {
Log.w(TAG, "Unable to access GPIO", e);
}
startView.setEnabled(true);
stopView.setEnabled(false);
startView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showPrimes();
}
});
stopView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
primeStatusView.setText("");
try {
gpio17.setValue(false);
} catch (IOException e) {
Log.e(TAG, "Damn it!", e);
}
task.cancel(true);
startView.setEnabled(true);
stopView.setEnabled(false);
}
});
}
private void showPrimes() {
startView.setEnabled(false);
stopView.setEnabled(true);
if (task != null) {
task.cancel(true);
}
task = new AsyncTask<Void, Integer, Void>() {
@Override
protected Void doInBackground(Void... params) {
List<Integer> primes = Arrays.asList(2,3,5,7,11,13,17,19,23,29);
try {
gpio17.setValue(false);
for (Integer prime : primes) {
publishProgress(prime);
for (int i = 0; i < prime; i++) {
Thread.sleep(250);
gpio17.setValue(true);
Thread.sleep(250);
gpio17.setValue(false);
}
gpio17.setValue(false);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
return null;
}
catch (Exception ex) {
Log.e(TAG, "Damn it!", ex);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
primeStatusView.setText(primeStatusView.getText().toString() + " " + values[0].toString().trim());
}
@Override
protected void onPostExecute(Void aVoid) {
startView.setEnabled(true);
stopView.setEnabled(false);
}
}.execute();
}
@Override
protected void onDestroy() {
super.onDestroy();
task.cancel(true);
if (gpio17 != null) {
try {
gpio17.close();
gpio17 = null;
} catch (IOException e) {
Log.w(TAG, "Unable to close GPIO", e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment