Skip to content

Commit

Permalink
contrib-drivers: bootstrap sensehat driver
Browse files Browse the repository at this point in the history
Change-Id: Ibba688b1671d7e6ba19a996be7c120f1231bff75
  • Loading branch information
proppy committed Dec 20, 2016
1 parent 23bc0ca commit e1399c1
Show file tree
Hide file tree
Showing 10 changed files with 471 additions and 0 deletions.
1 change: 1 addition & 0 deletions sensehat/.driver-metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TYPE="metadriver for the Sense HAT"
1 change: 1 addition & 0 deletions sensehat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
79 changes: 79 additions & 0 deletions sensehat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Sense Hat driver for Android Things
=====================================

This driver provides easy access to the peripherals available on the Raspberry Pi [Sense Hat][product]:
- 8x8 LED matrix
- TODO: 5 buttons joystick
- TODO: Sensors


NOTE: these drivers are not production-ready. They are offered as sample
implementations of Android Things user space drivers for common peripherals
as part of the Developer Preview release. There is no guarantee
of correctness, completeness or robustness.

How to use the driver
---------------------

### Gradle dependency

To use the `sensehat` driver, simply add the line below to your project's `build.gradle`,
where `<version>` matches the last version of the driver available on [jcenter][jcenter].

```
dependencies {
compile 'com.google.android.things.contrib:driver-sensehat:<version>'
}
```

### Sample usage

```
// import the SenseHat driver
import com.google.android.things.driver.sensehat.SenseHat;
```
```
// Color the LED matrix.
LedMatrix display = SenseHat.openDisplay();
display.draw(Color.MAGENTA);
```
```
// Display a drawable on the LED matrix.
display.draw(context.getDrawable(android.R.drawable.ic_dialog_alert));
```
```
// Display a gradient on the LED matrix.
Bitmap bitmap = Bitmap.createBitmap(SenseHat.DISPLAY_WIDTH, SenseHat.DISPLAY_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setShader(new RadialGradient(4, 4, 4, Color.RED, Color.BLUE, Shader.TileMode.CLAMP));
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
display.draw(bitmap);
```
```
// Close the display when done.
display.close();
```

License
-------

Copyright 2016 Google Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
additional information regarding copyright ownership. The ASF licenses this
file to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.

[product]: https://www.raspberrypi.org/products/sense-hat/
[jcenter]: https://bintray.com/google/androidthings/contrib-driver-sensehat
42 changes: 42 additions & 0 deletions sensehat/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion '24.0.3'

defaultConfig {
minSdkVersion 24
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
}

dependencies {
compile 'com.android.support:support-annotations:24.2.0'
provided 'com.google.android.things:androidthings:0.1-devpreview'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
}

android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
97 changes: 97 additions & 0 deletions sensehat/publish.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* To publish on Bintray:
* - set environmental variables BINTRAY_USER and BINTRAY_API_KEY to proper values
* - from this directory:
* ../gradlew -b publish.gradle bintrayUpload
*
*/

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.1'
}
}

plugins {
id "com.jfrog.bintray" version "1.7"
}

allprojects {
repositories {
jcenter()
}
}

apply from: 'build.gradle'
apply plugin: 'maven-publish'

def packageVersion = '0.1'

task sourceJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}

publishing {
publications {
driverPublish(MavenPublication) {
groupId 'com.google.android.things.contrib'
artifactId "driver-$project.name"
version packageVersion
artifacts = configurations.archives.artifacts
artifact sourceJar
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each {
if(it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null)
{
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}

bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_API_KEY')
publications = ['driverPublish']

publish = true

pkg {
repo = 'androidthings'
name = "contrib-driver-$project.name"
userOrg = 'google'

version {
name = packageVersion
gpg {
sign = true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.things.contrib.driver.sensehat;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class SenseHatDeviceTest {
@Test
public void senseHat_DisplayColor() throws IOException {
// Color the LED matrix.
LedMatrix display = SenseHat.openDisplay();

display.draw(Color.MAGENTA);
// Close the display when done.
display.close();
}

@Test
public void senseHat_DisplayDrawable() throws IOException {
Context context = InstrumentationRegistry.getTargetContext();
// Display a drawable on the LED matrix.
LedMatrix display = SenseHat.openDisplay();
display.draw(context.getDrawable(android.R.drawable.ic_secure));
// Close the display when done.
display.close();
}

@Test
public void senseHat_DisplayGradient() throws IOException {
// Display a gradient on the LED matrix.
LedMatrix display = SenseHat.openDisplay();
Bitmap bitmap = Bitmap.createBitmap(SenseHat.DISPLAY_WIDTH, SenseHat.DISPLAY_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setShader(new RadialGradient(4, 4, 4, Color.RED, Color.BLUE, Shader.TileMode.CLAMP));
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
display.draw(bitmap);
// Close the display when done.
display.close();
}
}
22 changes: 22 additions & 0 deletions sensehat/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.things.contrib.driver.sensehat">
<application>
<uses-library android:name="com.google.android.things"/>
</application>
</manifest>
Loading

0 comments on commit e1399c1

Please sign in to comment.