Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Raspberry Pi Sense HAT-related drivers #24

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hts221/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
131 changes: 131 additions & 0 deletions hts221/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
HTS221 driver for Android Things
================================

This driver supports STMicroelectronics [HTS221][product_hts221] capacitive digital sensor for
relative humidity and temperature.

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 `hts221` 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-hts221:<version>'
}
```

### Sample usage

```java
import com.google.android.things.contrib.driver.hts221.Hts221;

// Access the environmental sensor:

Hts221 mHts221;

try {
mHts221 = new Hts221(i2cBusName);
} catch (IOException e) {
// Couldn't configure the device...
}

// Read the current humidity:

try {
float humidity = mHts221.readHumidity();
} catch (IOException e) {
// Error reading humidity
}

// Read the current temperature:

try {
float temperature = mHts221.readTemperature();
} catch (IOException e) {
// Error reading temperature
}

// Close the environmental sensor when finished:

try {
mHts221.close();
} catch (IOException e) {
// Error closing sensor
}
```

If you need to read sensor values continuously, you can register the HTS221 with the system and
listen for sensor values using the [Sensor APIs][sensors]:

```java
SensorManager mSensorManager = getSystemService(Context.SENSOR_SERVICE);
SensorEventListener mHumidityListener = ...;
SensorEventListener mTemperatureListener = ...;
Hts221SensorDriver mSensorDriver;

mSensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
@Override
public void onDynamicSensorConnected(Sensor sensor) {
if (sensor.getType() == Sensor.TYPE_RELATIVE_HUMIDITY) {
mSensorManager.registerListener(mHumidityListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
} else if (sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
mSensorManager.registerListener(mTemperatureListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
});

try {
mSensorDriver = new Hts221SensorDriver(i2cBusName);
mSensorDriver.registerHumiditySensor();
mSensorDriver.registerTemperatureSensor();
} catch (IOException e) {
// Error configuring sensor
}

// Unregister and close the driver when finished:

mSensorManager.unregisterListener(mHumidityListener);
mSensorManager.unregisterListener(mTemperatureListener);
mSensorDriver.unregisterHumiditySensor();
mSensorDriver.unregisterTemperatureSensor();
try {
mSensorDriver.close();
} catch (IOException e) {
// Error closing sensor
}
```

License
-------

Copyright 2016 Macro Yau

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_hts221]: http://www.st.com/en/mems-and-sensors/hts221.html
[jcenter]: https://bintray.com/google/androidthings/contrib-driver-hts221/_latestVersion
[sensors]: https://developer.android.com/guide/topics/sensors/sensors_overview.html
38 changes: 38 additions & 0 deletions hts221/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 {
provided 'com.google.android.things:androidthings:0.2-devpreview'
compile 'com.android.support:support-annotations:24.2.0'

testCompile project(':testingutils')
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
}
97 changes: 97 additions & 0 deletions hts221/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
}
}
}
}
22 changes: 22 additions & 0 deletions hts221/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.hts221">
<application>
<uses-library android:name="com.google.android.things"/>
</application>
</manifest>
Loading