Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alvarowolfx/bh1750-androidthings

Android Things driver for BH1750 ambient light sensor
https://github.com/alvarowolfx/bh1750-androidthings

Last synced: about 2 months ago
JSON representation

Android Things driver for BH1750 ambient light sensor

Awesome Lists containing this project

README

        

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 `` matches the last version of the driver available on [jcenter][jcenter].

```
dependencies {
compile 'com.alvarowolfx.androidthings:driver-bh1750:'
}
```

### Sample usage

```java
import com.alvarowolfx.androidthings.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
}
```
Schematic
-------
![schematic](https://raw.githubusercontent.com/alvarowolfx/bh1750-androidthings/master/assets/schematic.png)

License
-------

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