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

[driver] Add support for bh1750 light sensor #60

Open
wants to merge 2 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 bh1750/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
4 changes: 4 additions & 0 deletions bh1750/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

## [0.1] - 2017-07-17
- initial version
114 changes: 114 additions & 0 deletions bh1750/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
BH1750 driver for Android Things
================================

This driver supports ROHM [BH1750][product_bh1750] ambient light sensor.

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

### Sample usage

```java
import com.google.android.things.contrib.driver.bh1750.Bh1750;

// Access the ambient light sensor:

Bh1750 mBh1750;

try {
mBh1750 = new Bh1750(i2cBusName);
} catch (IOException e) {
// couldn't configure the device...
}

// Read the current light level:

try {
float lightLevel = mBh1750.readLightLevel();
} catch (IOException e) {
// error reading light level
}

// Close the ambient light sensor when finished:

try {
mBh1750.close();
} catch (IOException e) {
// error closing sensor
}
```

If you need to read sensor values continuously, you can register the BH1750 with the system and
listen for sensor values using the [Sensor APIs][sensors]:
```java
SensorManager mSensorManager = getSystemService(Context.SENSOR_SERVICE);
SensorEventListener mListener = ...;
Bh1750SensorDriver mSensorDriver;

mSensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
@Override
public void onDynamicSensorConnected(Sensor sensor) {
if (sensor.getType() == Sensor.TYPE_LIGHT) {
mSensorManager.registerListener(mListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
});

try {
mSensorDriver = new Bh1750SensorDriver(i2cBusName);
mSensorDriver.registerLightSensor();
} catch (IOException e) {
// Error configuring sensor
}

// Unregister and close the driver when finished:

mSensorManager.unregisterListener(mListener);
mSensorDriver.unregisterLightSensor();
try {
mSensorDriver.close();
} catch (IOException e) {
// error closing sensor
}
```

License
-------

Copyright 2017 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_bh1750]: http://cpre.kmutnb.ac.th/esl/learning/bh1750-light-sensor/bh1750fvi-e_datasheet.pdf
[jcenter]: https://bintray.com/google/androidthings/contrib-driver-bh1750/_latestVersion
[sensors]: https://developer.android.com/guide/topics/sensors/sensors_overview.html
38 changes: 38 additions & 0 deletions bh1750/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2017 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.4-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'
}
2 changes: 2 additions & 0 deletions bh1750/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TYPE="ambient light sensor"
ARTIFACT_VERSION=0.1
3 changes: 3 additions & 0 deletions bh1750/publish-settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rootProject.buildFileName = '../publish.gradle'
include ':testingutils'
project(':testingutils').projectDir = new File(settingsDir, '../testingutils')
22 changes: 22 additions & 0 deletions bh1750/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 2017 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.bh1750">
<application>
<uses-library android:name="com.google.android.things"/>
</application>
</manifest>
Loading