Ws2801 implements AutoCloseable { public void write(int[] colors) throws IOException { byte[] ledData = new byte[WS2801_PACKET_LENGTH * colors.length]; for (int i = 0; i < colors.length; i++) { int outputPosition = i * WS2801_PACKET_LENGTH; int di = direction == Direction.NORMAL ? i : colors.length - i - 1; System.arraycopy(colorUnpacker.unpack(colors[di]), 0, ledData, outputPosition, WS2801_PACKET_LENGTH); } device.write(ledData, ledData.length); } Ws2801(SpiDevice device, ColorUnpacker colorUnpacker, Direction direction); static Ws2801 create(String spiBusPort); static Ws2801 create(String spiBusPort, Mode ledMode); static Ws2801 create(String spiBusPort, Mode ledMode, Direction direction); void write(int[] colors); @Override void close();  }
@Test public void writesToSpiDeviceWhenWriting() throws Exception { int[] anyColors = {Color.RED, Color.DKGRAY, Color.GREEN, Color.WHITE, Color.YELLOW}; driver.write(anyColors); verify(device).write(any(byte[].class), anyInt()); }
@Test public void reversesColorsWhenUsingReversedDirection() throws Exception { driver = new Ws2801(device, unpacker, Direction.REVERSED); driver.write(new int[]{0x0, 0x1}); verify(unpacker).unpack(0x1); verify(unpacker).unpack(0X0); }
@Test public void doesNotReverseColorsWhenUsingNormalDirection() throws Exception { driver = new Ws2801(device, unpacker, Direction.NORMAL); driver.write(new int[]{0x0, 0x1}); verify(unpacker).unpack(0x0); verify(unpacker).unpack(0x1); }