-
Notifications
You must be signed in to change notification settings - Fork 174
First version of a driver for WS2801 #78
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| WS2801 LED driver for Android Things | ||
| ==================================== | ||
|
|
||
| This driver supports RGB LED peripherals built on the WS2801 SPI protocol. | ||
|
|
||
| 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 `ws2801` 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-ws2801:<version>' | ||
| } | ||
| ``` | ||
|
|
||
| ### Sample usage | ||
|
|
||
| ```java | ||
| import com.google.android.things.contrib.driver.ws2801.Ws2801; | ||
|
|
||
| // Access the LED strip: | ||
|
|
||
| Ws2801 led_string; | ||
|
|
||
| try { | ||
| led_string = new Ws2801(10, "SPI3.1"); | ||
| } catch (IOException e) { | ||
| // couldn't configure the device... | ||
| } | ||
|
|
||
| // Set colors | ||
|
|
||
| led_string.SetColor(0, Color.GREEN); | ||
| led_string.SetColor(3, Color.RED); | ||
|
|
||
| // Update the led | ||
|
|
||
| try { | ||
| led_string.Show(); | ||
| } catch (IOException e) { | ||
| // error setting LEDs | ||
| } | ||
|
|
||
| // Close the LED strip when finished: | ||
|
|
||
| try { | ||
| led_string.close(); | ||
| } catch (IOException e) { | ||
| // error closing LED strip | ||
| } | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| [jcenter]: https://bintray.com/google/androidthings/contrib-driver-apa102/_latestVersion |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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' | ||
|
|
||
| testCompile project(':testingutils') | ||
| testCompile 'junit:junit:4.12' | ||
| testCompile 'org.mockito:mockito-core:1.10.19' | ||
| } | ||
|
|
||
| sourceCompatibility = "1.7" | ||
| targetCompatibility = "1.7" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| TYPE="WS2801 LED strip" | ||
| ARTIFACT_VERSION=0.1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?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.ws2801"> | ||
| <application> | ||
| <uses-library android:required="false" android:name="com.google.android.things"/> | ||
| </application> | ||
| </manifest> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.google.android.things.contrib.driver.ws2801; | ||
|
|
||
| import android.graphics.Color; | ||
|
|
||
| import com.google.android.things.pio.PeripheralManagerService; | ||
| import com.google.android.things.pio.SpiDevice; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Ws2801 { | ||
| private SpiDevice spi_device; | ||
| private byte[] data; | ||
| private int nb_leds; | ||
|
|
||
| public Ws2801(int nbleds, String spi_bus_name) throws IOException { | ||
| nb_leds = nbleds; | ||
| data = new byte[3*nb_leds]; | ||
| for(int i = 0 ; i < nb_leds ; i++) { | ||
| data[3*i + 0] = (byte)Color.red(Color.BLUE); | ||
| data[3*i + 1] = (byte)Color.green(Color.BLUE); | ||
| data[3*i + 2] = (byte)Color.blue(Color.BLUE); | ||
| } | ||
| PeripheralManagerService service = new PeripheralManagerService(); | ||
| spi_device = service.openSpiDevice(spi_bus_name); | ||
| spi_device.setFrequency(1_000_000); | ||
| spi_device.setMode(SpiDevice.MODE0); | ||
| spi_device.setBitsPerWord(8); | ||
| spi_device.setDelay(1000); | ||
| } | ||
|
|
||
| public void SetColor(int position, int red, int green, int blue) throws IOException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. google code conventions state methods should start with a lowercase letter |
||
| SetColor(position, Color.rgb(red, green, blue)); | ||
| } | ||
|
|
||
| public void SetColor(int position, int color) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about exposing a Even if we end up with distinct drivers for each variant of LED strips, it would be nice if they were sharing some convention. |
||
| if(position > nb_leds) | ||
| return; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be better to throw an exception so a user gets instant feedback that they have done something unexpected |
||
|
|
||
| data[3*position + 0] = (byte)Color.red(color); | ||
| data[3*position + 1] = (byte)Color.green(color); | ||
| data[3*position + 2] = (byte)Color.blue(color); | ||
| } | ||
|
|
||
| public void Show() throws IOException { | ||
| spi_device.write(data, data.length); | ||
| } | ||
|
|
||
| public void Close() throws IOException { | ||
| spi_device.close(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you call this
numberOfLedsit makes the code much more understandable