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..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 @@ -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; @@ -67,7 +68,8 @@ 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 >> 1; // Default to half + private int[] mLedBrightness; // Direction of the led strip; private Direction mDirection; @@ -162,13 +164,33 @@ public void setBrightness(int ledBrightness) { throw new IllegalArgumentException("Brightness needs to be between 0 and " + MAX_BRIGHTNESS); } - mLedBrightness = ledBrightness; + mLedBrightnessGlobal = ledBrightness; + mLedBrightness = null; } /** - * 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 setIndividualBrightness(int[] ledBrightness) { + mLedBrightness = new int[ledBrightness.length]; + for (int i=0; i MAX_BRIGHTNESS) { + throw new IllegalArgumentException("Brightness needs to be between 0 and " + + MAX_BRIGHTNESS); + } + mLedBrightness[i] = ledBrightness[i]; + } + } + + /** + * Get the current brightness */ public int getBrightness() { + return mLedBrightnessGlobal; + } + + public @Nullable int[] getIndividualBrightness() { return mLedBrightness; } @@ -196,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 @@ -212,9 +237,12 @@ 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 + byte brightness = (byte) (0xE0 | mLedBrightnessGlobal); // Default initialization for (int i = 0; i < colors.length; i++) { + 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; 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..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 @@ -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.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.setIndividualBrightness(new int[] {Apa102.MAX_BRIGHTNESS + 1}); + } + @Test public void setDirection() throws IOException { Apa102 leds = new Apa102(mSpiDevice, Apa102.Mode.BGR, Apa102.Direction.NORMAL);