From 9066f46df606ccfcc3ef79045204fc94d90680da Mon Sep 17 00:00:00 2001 From: plattysoft Date: Mon, 18 Sep 2017 20:12:21 +0100 Subject: [PATCH 1/6] added invidual brightness setting per led --- .../things/contrib/driver/apa102/Apa102.java | 37 ++++++++++++++++--- .../contrib/driver/apa102/Apa102Test.java | 14 +++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java index 0b24cb1..295d80b 100644 --- a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java +++ b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java @@ -67,7 +67,16 @@ public enum Direction { private Mode mLedMode; // RGB LED strip settings that have sensible defaults. - private int mLedBrightness = MAX_BRIGHTNESS >> 1; // default to half + private int mLedBrightnessGlobal = MAX_BRIGHTNESS/2; + private int mLedBrightness[] = new int[] { + MAX_BRIGHTNESS >> 1, + MAX_BRIGHTNESS >> 1, + MAX_BRIGHTNESS >> 1, + MAX_BRIGHTNESS >> 1, + MAX_BRIGHTNESS >> 1, + MAX_BRIGHTNESS >> 1, + MAX_BRIGHTNESS >> 1 + }; // default to half // Direction of the led strip; private Direction mDirection; @@ -162,14 +171,32 @@ public void setBrightness(int ledBrightness) { throw new IllegalArgumentException("Brightness needs to be between 0 and " + MAX_BRIGHTNESS); } - mLedBrightness = ledBrightness; + mLedBrightnessGlobal = ledBrightness; } /** - * Get the current brightness level + * Sets the brightness for all LEDs in the strip. + * @param ledBrightness The brightness of the LED strip, between 0 and {@link #MAX_BRIGHTNESS}. + */ + public void setBrightness(int[] ledBrightness) { + mLedBrightnessGlobal = 0; + for (int i=0; i MAX_BRIGHTNESS) { + throw new IllegalArgumentException("Brightness needs to be between 0 and " + + MAX_BRIGHTNESS); + } + mLedBrightness[i] = ledBrightness[i]; + if (mLedBrightnessGlobal < ledBrightness[i]) { + mLedBrightnessGlobal = ledBrightness[i]; + } + } + } + + /** + * Get the current brightness maximum level */ public int getBrightness() { - return mLedBrightness; + return mLedBrightnessGlobal; } /** @@ -212,9 +239,9 @@ public void write(int[] colors) throws IOException { pos += APA_START_FRAME_PACKET_LENGTH; // Compute the packets to send. - byte brightness = (byte) (0xE0 | mLedBrightness); // Less brightness possible final Direction currentDirection = mDirection; // Avoids reading changes of mDirection during loop for (int i = 0; i < colors.length; i++) { + byte brightness = (byte) (0xE0 | mLedBrightness[i]); // Less brightness possible int di = currentDirection == Direction.NORMAL ? i : colors.length - i - 1; copyApaColorData(brightness, colors[di], mLedMode, mLedData, pos); pos += APA_COLOR_PACKET_LENGTH; diff --git a/apa102/src/test/java/com/google/android/things/contrib/driver/apa102/Apa102Test.java b/apa102/src/test/java/com/google/android/things/contrib/driver/apa102/Apa102Test.java index 03014ec..633ba30 100644 --- a/apa102/src/test/java/com/google/android/things/contrib/driver/apa102/Apa102Test.java +++ b/apa102/src/test/java/com/google/android/things/contrib/driver/apa102/Apa102Test.java @@ -88,6 +88,20 @@ public void setBrightness_throwsIfTooLarge() throws IOException { leds.setBrightness(Apa102.MAX_BRIGHTNESS + 1); } + @Test + public void setBrightnessArray_throwsIfTooSmall() throws IOException { + Apa102 leds = new Apa102(mSpiDevice, Apa102.Mode.BGR, Apa102.Direction.NORMAL); + mExpectedException.expect(IllegalArgumentException.class); + leds.setBrightness(new int[] {-1}); + } + + @Test + public void setBrightnessArray_throwsIfTooLarge() throws IOException { + Apa102 leds = new Apa102(mSpiDevice, Apa102.Mode.BGR, Apa102.Direction.NORMAL); + mExpectedException.expect(IllegalArgumentException.class); + leds.setBrightness(new int[] {Apa102.MAX_BRIGHTNESS + 1}); + } + @Test public void setDirection() throws IOException { Apa102 leds = new Apa102(mSpiDevice, Apa102.Mode.BGR, Apa102.Direction.NORMAL); From 903b118baed0282260ca052195221b92811146b7 Mon Sep 17 00:00:00 2001 From: plattysoft Date: Mon, 18 Sep 2017 20:14:24 +0100 Subject: [PATCH 2/6] added invidual brightness setting per led --- .../google/android/things/contrib/driver/apa102/Apa102.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java index 295d80b..21eb7df 100644 --- a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java +++ b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java @@ -172,6 +172,9 @@ public void setBrightness(int ledBrightness) { + MAX_BRIGHTNESS); } mLedBrightnessGlobal = ledBrightness; + for (int i=0; i Date: Tue, 19 Sep 2017 10:41:34 +0100 Subject: [PATCH 3/6] separating the individual and global brightness concepts --- .../things/contrib/driver/apa102/Apa102.java | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java index 21eb7df..95f9001 100644 --- a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java +++ b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java @@ -67,16 +67,8 @@ public enum Direction { private Mode mLedMode; // RGB LED strip settings that have sensible defaults. - private int mLedBrightnessGlobal = MAX_BRIGHTNESS/2; - private int mLedBrightness[] = new int[] { - MAX_BRIGHTNESS >> 1, - MAX_BRIGHTNESS >> 1, - MAX_BRIGHTNESS >> 1, - MAX_BRIGHTNESS >> 1, - MAX_BRIGHTNESS >> 1, - MAX_BRIGHTNESS >> 1, - MAX_BRIGHTNESS >> 1 - }; // default to half + private int mLedBrightnessGlobal = MAX_BRIGHTNESS >> 1; // Default to half + private int mLedBrightness[]; // Direction of the led strip; private Direction mDirection; @@ -172,36 +164,35 @@ public void setBrightness(int ledBrightness) { + MAX_BRIGHTNESS); } mLedBrightnessGlobal = ledBrightness; - for (int i=0; i MAX_BRIGHTNESS) { throw new IllegalArgumentException("Brightness needs to be between 0 and " + MAX_BRIGHTNESS); } mLedBrightness[i] = ledBrightness[i]; - if (mLedBrightnessGlobal < ledBrightness[i]) { - mLedBrightnessGlobal = ledBrightness[i]; - } } } /** - * Get the current brightness maximum level + * Get the current brightness */ public int getBrightness() { return mLedBrightnessGlobal; } + public int[] getIndividualBrightness() { + return mLedBrightness; + } + /** * Sets the direction of the LED strip. * @param direction The direction of the LED strip, corresponding to {@link Direction}. @@ -243,8 +234,11 @@ public void write(int[] colors) throws IOException { // Compute the packets to send. final Direction currentDirection = mDirection; // Avoids reading changes of mDirection during loop + byte brightness = (byte) (0xE0 | mLedBrightnessGlobal); // Default initialization for (int i = 0; i < colors.length; i++) { - byte brightness = (byte) (0xE0 | mLedBrightness[i]); // Less brightness possible + if (mLedBrightness != null) { + brightness = (byte) (0xE0 | mLedBrightness[i]); // Less brightness possible + } int di = currentDirection == Direction.NORMAL ? i : colors.length - i - 1; copyApaColorData(brightness, colors[di], mLedMode, mLedData, pos); pos += APA_COLOR_PACKET_LENGTH; From 35b243c01f43b353e7853814cc6f403c59d35540 Mon Sep 17 00:00:00 2001 From: Raul Portales Date: Tue, 19 Sep 2017 10:52:50 +0100 Subject: [PATCH 4/6] updating method name in tests --- .../android/things/contrib/driver/apa102/Apa102Test.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apa102/src/test/java/com/google/android/things/contrib/driver/apa102/Apa102Test.java b/apa102/src/test/java/com/google/android/things/contrib/driver/apa102/Apa102Test.java index 633ba30..1cc7c68 100644 --- a/apa102/src/test/java/com/google/android/things/contrib/driver/apa102/Apa102Test.java +++ b/apa102/src/test/java/com/google/android/things/contrib/driver/apa102/Apa102Test.java @@ -92,14 +92,14 @@ public void setBrightness_throwsIfTooLarge() throws IOException { public void setBrightnessArray_throwsIfTooSmall() throws IOException { Apa102 leds = new Apa102(mSpiDevice, Apa102.Mode.BGR, Apa102.Direction.NORMAL); mExpectedException.expect(IllegalArgumentException.class); - leds.setBrightness(new int[] {-1}); + leds.setIndividualBrightness(new int[] {-1}); } @Test public void setBrightnessArray_throwsIfTooLarge() throws IOException { Apa102 leds = new Apa102(mSpiDevice, Apa102.Mode.BGR, Apa102.Direction.NORMAL); mExpectedException.expect(IllegalArgumentException.class); - leds.setBrightness(new int[] {Apa102.MAX_BRIGHTNESS + 1}); + leds.setIndividualBrightness(new int[] {Apa102.MAX_BRIGHTNESS + 1}); } @Test From 0dc369527e30830e99d716f8d0a0454bc17c885a Mon Sep 17 00:00:00 2001 From: Raul Portales Date: Tue, 19 Sep 2017 15:18:15 +0100 Subject: [PATCH 5/6] fixed code style --- .../google/android/things/contrib/driver/apa102/Apa102.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java index 95f9001..fb774b2 100644 --- a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java +++ b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java @@ -17,6 +17,7 @@ package com.google.android.things.contrib.driver.apa102; import android.graphics.Color; +import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import com.google.android.things.pio.PeripheralManagerService; @@ -68,7 +69,7 @@ public enum Direction { // RGB LED strip settings that have sensible defaults. private int mLedBrightnessGlobal = MAX_BRIGHTNESS >> 1; // Default to half - private int mLedBrightness[]; + private int[] mLedBrightness; // Direction of the led strip; private Direction mDirection; @@ -189,7 +190,7 @@ public int getBrightness() { return mLedBrightnessGlobal; } - public int[] getIndividualBrightness() { + public @Nullable int[] getIndividualBrightness() { return mLedBrightness; } From 96fc125e6d6fcb56b700a69cd52a2257470df70d Mon Sep 17 00:00:00 2001 From: plattysoft Date: Tue, 19 Sep 2017 20:22:27 +0100 Subject: [PATCH 6/6] added check of array sizes --- .../google/android/things/contrib/driver/apa102/Apa102.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java index fb774b2..98d89ca 100644 --- a/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java +++ b/apa102/src/main/java/com/google/android/things/contrib/driver/apa102/Apa102.java @@ -218,6 +218,9 @@ public void write(int[] colors) throws IOException { if (mDevice == null) { throw new IllegalStateException("SPI device not open"); } + if (mLedBrightness != null && colors.length != mLedBrightness.length) { + throw new IllegalStateException("colors and brightness arrays must be of the same length"); + } final int size = APA_START_FRAME_PACKET_LENGTH + APA_COLOR_PACKET_LENGTH * colors.length