@Test public void write() throws IOException { ColorMock.mockStatic(); Apa102 leds = new Apa102(mSpiDevice, Apa102.Mode.BGR, Apa102.Direction.NORMAL); final int brightness = 15; leds.setBrightness(brightness); final int[] colors = {0xff0000, 0x00ff00, 0x0000ff}; leds.write(colors); int headerSize = 4; int resetframeSize = 4; int endframeSize = 4; Mockito.verify(mSpiDevice).write(Mockito.argThat(BytesMatcher.contains( (byte)(0xE0|brightness), (byte)(colors[0]&0xff), (byte)(colors[0]>>8&0xff), (byte)(colors[0]>>16&0xff), (byte)(0xE0|brightness), (byte)(colors[1]&0xff), (byte)(colors[1]>>8&0xff), (byte)(colors[1]>>16&0xff), (byte)(0xE0|brightness), (byte)(colors[2]&0xff), (byte)(colors[2]>>8&0xff), (byte)(colors[2]>>16&0xff) )), Mockito.eq(headerSize + colors.length*4 + resetframeSize + endframeSize)); }
public void write(int[] colors) throws IOException { if (mDevice == null) { throw new IllegalStateException("SPI device not open"); } final int size = APA_START_FRAME_PACKET_LENGTH + APA_COLOR_PACKET_LENGTH * colors.length + APA_RESET_FRAME_PACKET_LENGTH + APA_END_FRAME_PACKET_LENGTH; int pos = 0; if (mLedData == null || mLedData.length < size) { mLedData = new byte[size]; Arrays.fill(mLedData, 0, APA_START_FRAME_PACKET_LENGTH, APA_START_DATA_BYTE); } pos += APA_START_FRAME_PACKET_LENGTH; byte brightness = (byte) (0xE0 | mLedBrightness); final Direction currentDirection = mDirection; for (int i = 0; i < colors.length; i++) { int di = currentDirection == Direction.NORMAL ? i : colors.length - i - 1; copyApaColorData(brightness, colors[di], mLedMode, mLedData, pos); pos += APA_COLOR_PACKET_LENGTH; } Arrays.fill(mLedData, pos, pos + APA_RESET_FRAME_PACKET_LENGTH, APA_RESET_DATA_BYTE); pos += APA_RESET_FRAME_PACKET_LENGTH; Arrays.fill(mLedData, pos, pos + APA_END_FRAME_PACKET_LENGTH, APA_END_DATA_BYTE); pos += APA_END_FRAME_PACKET_LENGTH; if (pos != size) { throw new IllegalStateException("end position: " + pos + " should match size: " + size); } mDevice.write(mLedData, size); }
Apa102 implements AutoCloseable { public void write(int[] colors) throws IOException { if (mDevice == null) { throw new IllegalStateException("SPI device not open"); } final int size = APA_START_FRAME_PACKET_LENGTH + APA_COLOR_PACKET_LENGTH * colors.length + APA_RESET_FRAME_PACKET_LENGTH + APA_END_FRAME_PACKET_LENGTH; int pos = 0; if (mLedData == null || mLedData.length < size) { mLedData = new byte[size]; Arrays.fill(mLedData, 0, APA_START_FRAME_PACKET_LENGTH, APA_START_DATA_BYTE); } pos += APA_START_FRAME_PACKET_LENGTH; byte brightness = (byte) (0xE0 | mLedBrightness); final Direction currentDirection = mDirection; for (int i = 0; i < colors.length; i++) { int di = currentDirection == Direction.NORMAL ? i : colors.length - i - 1; copyApaColorData(brightness, colors[di], mLedMode, mLedData, pos); pos += APA_COLOR_PACKET_LENGTH; } Arrays.fill(mLedData, pos, pos + APA_RESET_FRAME_PACKET_LENGTH, APA_RESET_DATA_BYTE); pos += APA_RESET_FRAME_PACKET_LENGTH; Arrays.fill(mLedData, pos, pos + APA_END_FRAME_PACKET_LENGTH, APA_END_DATA_BYTE); pos += APA_END_FRAME_PACKET_LENGTH; if (pos != size) { throw new IllegalStateException("end position: " + pos + " should match size: " + size); } mDevice.write(mLedData, size); } }
Apa102 implements AutoCloseable { public void write(int[] colors) throws IOException { if (mDevice == null) { throw new IllegalStateException("SPI device not open"); } final int size = APA_START_FRAME_PACKET_LENGTH + APA_COLOR_PACKET_LENGTH * colors.length + APA_RESET_FRAME_PACKET_LENGTH + APA_END_FRAME_PACKET_LENGTH; int pos = 0; if (mLedData == null || mLedData.length < size) { mLedData = new byte[size]; Arrays.fill(mLedData, 0, APA_START_FRAME_PACKET_LENGTH, APA_START_DATA_BYTE); } pos += APA_START_FRAME_PACKET_LENGTH; byte brightness = (byte) (0xE0 | mLedBrightness); final Direction currentDirection = mDirection; for (int i = 0; i < colors.length; i++) { int di = currentDirection == Direction.NORMAL ? i : colors.length - i - 1; copyApaColorData(brightness, colors[di], mLedMode, mLedData, pos); pos += APA_COLOR_PACKET_LENGTH; } Arrays.fill(mLedData, pos, pos + APA_RESET_FRAME_PACKET_LENGTH, APA_RESET_DATA_BYTE); pos += APA_RESET_FRAME_PACKET_LENGTH; Arrays.fill(mLedData, pos, pos + APA_END_FRAME_PACKET_LENGTH, APA_END_DATA_BYTE); pos += APA_END_FRAME_PACKET_LENGTH; if (pos != size) { throw new IllegalStateException("end position: " + pos + " should match size: " + size); } mDevice.write(mLedData, size); } Apa102(String spiBusPort, Mode ledMode);  Apa102(String spiBusPort, Mode ledMode, Direction direction);  Apa102(String spiBusPort, Mode ledMode, Direction direction, int spiMode); @VisibleForTesting  Apa102(SpiDevice device, Mode ledMode, Direction direction); }
Apa102 implements AutoCloseable { public void write(int[] colors) throws IOException { if (mDevice == null) { throw new IllegalStateException("SPI device not open"); } final int size = APA_START_FRAME_PACKET_LENGTH + APA_COLOR_PACKET_LENGTH * colors.length + APA_RESET_FRAME_PACKET_LENGTH + APA_END_FRAME_PACKET_LENGTH; int pos = 0; if (mLedData == null || mLedData.length < size) { mLedData = new byte[size]; Arrays.fill(mLedData, 0, APA_START_FRAME_PACKET_LENGTH, APA_START_DATA_BYTE); } pos += APA_START_FRAME_PACKET_LENGTH; byte brightness = (byte) (0xE0 | mLedBrightness); final Direction currentDirection = mDirection; for (int i = 0; i < colors.length; i++) { int di = currentDirection == Direction.NORMAL ? i : colors.length - i - 1; copyApaColorData(brightness, colors[di], mLedMode, mLedData, pos); pos += APA_COLOR_PACKET_LENGTH; } Arrays.fill(mLedData, pos, pos + APA_RESET_FRAME_PACKET_LENGTH, APA_RESET_DATA_BYTE); pos += APA_RESET_FRAME_PACKET_LENGTH; Arrays.fill(mLedData, pos, pos + APA_END_FRAME_PACKET_LENGTH, APA_END_DATA_BYTE); pos += APA_END_FRAME_PACKET_LENGTH; if (pos != size) { throw new IllegalStateException("end position: " + pos + " should match size: " + size); } mDevice.write(mLedData, size); } Apa102(String spiBusPort, Mode ledMode);  Apa102(String spiBusPort, Mode ledMode, Direction direction);  Apa102(String spiBusPort, Mode ledMode, Direction direction, int spiMode); @VisibleForTesting  Apa102(SpiDevice device, Mode ledMode, Direction direction); void setBrightness(int ledBrightness); int getBrightness(); void setDirection(Direction direction); Direction getDirection(); void write(int[] colors); @Override void close(); }
Apa102 implements AutoCloseable { public void write(int[] colors) throws IOException { if (mDevice == null) { throw new IllegalStateException("SPI device not open"); } final int size = APA_START_FRAME_PACKET_LENGTH + APA_COLOR_PACKET_LENGTH * colors.length + APA_RESET_FRAME_PACKET_LENGTH + APA_END_FRAME_PACKET_LENGTH; int pos = 0; if (mLedData == null || mLedData.length < size) { mLedData = new byte[size]; Arrays.fill(mLedData, 0, APA_START_FRAME_PACKET_LENGTH, APA_START_DATA_BYTE); } pos += APA_START_FRAME_PACKET_LENGTH; byte brightness = (byte) (0xE0 | mLedBrightness); final Direction currentDirection = mDirection; for (int i = 0; i < colors.length; i++) { int di = currentDirection == Direction.NORMAL ? i : colors.length - i - 1; copyApaColorData(brightness, colors[di], mLedMode, mLedData, pos); pos += APA_COLOR_PACKET_LENGTH; } Arrays.fill(mLedData, pos, pos + APA_RESET_FRAME_PACKET_LENGTH, APA_RESET_DATA_BYTE); pos += APA_RESET_FRAME_PACKET_LENGTH; Arrays.fill(mLedData, pos, pos + APA_END_FRAME_PACKET_LENGTH, APA_END_DATA_BYTE); pos += APA_END_FRAME_PACKET_LENGTH; if (pos != size) { throw new IllegalStateException("end position: " + pos + " should match size: " + size); } mDevice.write(mLedData, size); } Apa102(String spiBusPort, Mode ledMode);  Apa102(String spiBusPort, Mode ledMode, Direction direction);  Apa102(String spiBusPort, Mode ledMode, Direction direction, int spiMode); @VisibleForTesting  Apa102(SpiDevice device, Mode ledMode, Direction direction); void setBrightness(int ledBrightness); int getBrightness(); void setDirection(Direction direction); Direction getDirection(); void write(int[] colors); @Override void close(); static final int MAX_BRIGHTNESS; }