Skip to content

Commit

Permalink
rainbowhat: add README w/ sample usage
Browse files Browse the repository at this point in the history
Bug: 33427585

Change-Id: Iaff83690425e3ede7ef5e578f3509625fa9ad98b
  • Loading branch information
proppy committed Dec 9, 2016
1 parent e90c775 commit ec7380d
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Android Things user-space drivers
Sample peripheral drivers for Android Things.

NOTE: these drivers are not production-ready. They are offered as sample
implementations of Android Things user space drivers for commomn peripherals
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.

Expand Down
182 changes: 182 additions & 0 deletions rainbowhat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
Rainbow Hat driver for Android Things
=====================================

This driver provide easy access to the peripherals available on the [Rainbow Hat for Android Things](https://shop.pimoroni.com/products/rainbow-hat-for-android-things):
- BMP280 temperature sensor (I2C)
- HT16K33 segment display (I2C)
- Capacitive buttons (GPIO)
- LEDs (GPIO)
- APA102 RGB LEDs (SPI)
- Piezo Buzzer (PWM)
- Servo header (PWM)


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 `rainbowhat` driver, simply add the line
below to your project's `build.gradle`, where `<version>` matches the last version of the driver available on [jcenter](https://bintray.com/google/androidthings/contrib-driver-rainbowhat/_latestVersion)

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

### Sample usage


```
// import the RainbowHat driver
import com.google.android.things.driver.rainbowhat.RainbowHat
```

```
// Light up the Red LED.
Gpio led = RainbowHat.openLed(RainbowHat.LED_RED);
led.setValue(true);
// Close the device when done.
led.close();
```

```
// Display a string on the segment display.
AlphanumericDisplay segment = RainbowHat.openDisplay();
segment.setBrightness(Ht16k33.HT16K33_BRIGHTNESS_MAX);
segment.display("ABCD");
segment.setEnabled(true);
// Close the device when done.
segment.close();
```

```
// Play a note on the buzzer.
Speaker buzzer = RainbowHat.openBuzzer();
buzzer.play(440);
// Stop the buzzer.
buzzer.stop();
// Close the device when done.
buzzer.close();
```

```
// Log the current temperature
Bmx280 sensor = RainbowHat.openSensor();
sensor.setTemperatureOversampling(Bmx280.OVERSAMPLING_1X);
Log.d(TAG, "temperature:" + sensor.readTemperature());
// Close the device when done.
sensor.close();
```

```
// Display the temperature on the segment display.
Bmx280 sensor = RainbowHat.openSensor();
sensor.setTemperatureOversampling(Bmx280.OVERSAMPLING_1X);
AlphanumericDisplay segment = RainbowHat.openDisplay();
segment.setBrightness(Ht16k33.HT16K33_BRIGHTNESS_MAX);
segment.display(sensor.readTemperature());
segment.setEnabled(true);
// Close the devices when done.
sensor.close();
segment.close();
```

```
// Light up the rainbow
Apa102 ledstrip = RainbowHat.openLedStrip();
ledstrip.setBrightness(31);
int[] rainbow = new int[RainbowHat.LEDSTRIP_LENGTH];
for (int i = 0; i < rainbow.length; i++) {
rainbow[i] = Color.HSVToColor(255, new float[]{i * 360.f / rainbow.length, 1.0f, 1.0f});
}
ledstrip.write(rainbow);
// Close the device when done.
ledstrip.close();
```

```
// Detect button press.
Button button = RainbowHat.openButton(RainbowHat.BUTTON_A);
button.setOnButtonEventListener(new Button.OnButtonEventListener() {
@Override
public void onButtonEvent(Button button, boolean pressed) {
Log.d(TAG, "button A pressed:" + pressed);
}
});
// Close the device when done.
button.close();
```

```
// Get native Android 'A' key events when button 'A' is pressed.
ButtonInputDriver inputDriver = RainbowHat.createButtonInputDriver(RainbowHat.BUTTON_A, KeyEvent.KEYCODE_A);
inputDriver.register();
// In your Activity.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_A) {
// ...
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_A) {
// ...
}
return super.onKeyUp(keyCode, event);
}
```

```
// Continously report temperature.
final SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
@Override
public void onDynamicSensorConnected(Sensor sensor) {
if (sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
sensorManager.registerListener(
new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Log.i(TAG, "sensor changed: " + event.values[0]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.i(TAG, "accuracy changed: " + accuracy);
}
},
sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
});
```

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.

0 comments on commit ec7380d

Please sign in to comment.