Skip to content

Commit

Permalink
Moves the WS2801 driver to a new module and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavi Rigau committed Jan 12, 2017
1 parent 72e4f65 commit 30b9a30
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 174 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ android {

dependencies {
provided 'com.google.android.things:androidthings:0.1-devpreview'
compile project(':ws2801-driver')
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.xavirigau.ledcontroller;

import com.xrigau.driver.ws2801.Ws2801;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
Expand All @@ -14,7 +16,7 @@ public class RainbowLedActivity extends Activity {

private static final String TAG = RainbowLedActivity.class.getSimpleName();
private static final int FRAME_DELAY_MS = 15;
private static final int NUM_LEDS = 5;
private static final int NUM_LEDS = 1;

private final int[] mLedColors = new int[NUM_LEDS];

Expand All @@ -35,7 +37,7 @@ public void onCreate(Bundle savedInstanceState) {

try {
Log.d(TAG, "Initializing LED strip");
mLedstrip = new Ws2801(BoardDefaults.getSPIPort(), Ws2801.Mode.RBG);
mLedstrip = Ws2801.create(BoardDefaults.getSPIPort(), Ws2801.Mode.RBG);
mHandler.post(mAnimateRunnable);
// mLedstrip.write(new int[]{Color.BLACK}); // Uncomment this line and comment the previous one to turn off
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.xavirigau.ledcontroller;

import com.xrigau.driver.ws2801.Ws2801;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
Expand All @@ -18,7 +20,7 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

try {
mLedstrip = new Ws2801(BoardDefaults.getSPIPort(), Ws2801.Mode.RGB);
mLedstrip = Ws2801.create(BoardDefaults.getSPIPort(), Ws2801.Mode.RGB);
mLedstrip.write(new int[]{Color.parseColor("#0face0")});
Log.d(TAG, "Done!");
} catch (IOException e) {
Expand Down
170 changes: 0 additions & 170 deletions app/src/main/java/com/xavirigau/ledcontroller/Ws2801.java

This file was deleted.

2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app'
include ':app', ':ws2801-driver'
29 changes: 29 additions & 0 deletions ws2801-driver/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
provided 'com.google.android.things:androidthings:0.1-devpreview'

testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.10.19"
}
25 changes: 25 additions & 0 deletions ws2801-driver/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /usr/local/Cellar/android-sdk/24.4.1_1/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
11 changes: 11 additions & 0 deletions ws2801-driver/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<manifest package="com.xrigau.driver.ws2801"

xmlns:android="http://schemas.android.com/apk/res/android">

<application>

<uses-library android:name="com.google.android.things" />

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.xrigau.driver.ws2801;

import com.xrigau.driver.ws2801.Ws2801.Mode;

import android.graphics.Color;

class ColorUnpacker {

// RGB LED strip configuration that must be provided by the caller.
private final Mode ledMode;

ColorUnpacker(Mode ledMode) {
this.ledMode = ledMode;
}

/**
* Returns an WS2801 packet corresponding to the current brightness and given {@link Color}.
*
* @param color The {@link Color} to retrieve the protocol packet for.
* @return WS2801 packet corresponding to the current brightness and given {@link Color}.
*/
byte[] unpack(int color) {
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
return getOrderedRgbBytes(ledMode, (byte) r, (byte) g, (byte) b);
}

static byte[] getOrderedRgbBytes(Mode ledMode, byte r, byte g, byte b) {
switch (ledMode) {
case RBG:
return new byte[]{r, b, g};
case BGR:
return new byte[]{b, g, r};
case BRG:
return new byte[]{b, r, g};
case GRB:
return new byte[]{g, r, b};
case GBR:
return new byte[]{g, b, r};
default:
throw new IllegalArgumentException(ledMode.name() + " is an unknown " + Mode.class.getSimpleName());
}
}

}
Loading

0 comments on commit 30b9a30

Please sign in to comment.