From 30ccd13b6980ed18054d1f58fae2c258dca729ba Mon Sep 17 00:00:00 2001 From: sciencewhiz Date: Tue, 18 Feb 2020 20:40:03 -0800 Subject: [PATCH] Add methods for getting color of an LED (#2366) Also fix rounding in Color. --- .../first/wpilibj/AddressableLEDBuffer.java | 26 ++++++++++++++++- .../edu/wpi/first/wpilibj/util/Color.java | 2 +- .../wpilibj/AddressableLEDBufferTest.java | 29 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLEDBuffer.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLEDBuffer.java index 3cc0763373..8b7642947a 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLEDBuffer.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLEDBuffer.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -116,4 +116,28 @@ public class AddressableLEDBuffer { public int getLength() { return m_buffer.length / 4; } + + /** + * Gets the color at the specified index. + * + * @param index the index to get + * @return the LED color at the specified index + */ + public Color8Bit getLED8Bit(int index) { + return new Color8Bit(m_buffer[index * 4 + 2] & 0xFF, m_buffer[index * 4 + 1] & 0xFF, + m_buffer[index * 4] & 0xFF); + } + + /** + * Gets the color at the specified index. + * + * @param index the index to get + * @return the LED color at the specified index + */ + public Color getLED(int index) { + return new Color((m_buffer[index * 4 + 2] & 0xFF) / 255.0, + (m_buffer[index * 4 + 1] & 0xFF) / 255.0, + (m_buffer[index * 4] & 0xFF) / 255.0); + } + } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java index e743495eed..b9b3dd9d40 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java @@ -69,7 +69,7 @@ public class Color { } private static double roundAndClamp(double value) { - final var rounded = Math.round(value / kPrecision) * kPrecision; + final var rounded = Math.round((value + kPrecision / 2) / kPrecision) * kPrecision; return MathUtil.clamp(rounded, 0.0, 1.0); } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/AddressableLEDBufferTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/AddressableLEDBufferTest.java index 13f1651088..0bd0113b8c 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/AddressableLEDBufferTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/AddressableLEDBufferTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -9,12 +9,17 @@ package edu.wpi.first.wpilibj; import java.util.stream.Stream; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import edu.wpi.first.wpilibj.util.Color; +import edu.wpi.first.wpilibj.util.Color8Bit; + import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.params.provider.Arguments.arguments; class AddressableLEDBufferTest { @@ -51,4 +56,26 @@ class AddressableLEDBufferTest { arguments(120, 255, 128, 0, 0, 128) // Navy ); } + + @Test + void getColorTest() { + AddressableLEDBuffer buffer = new AddressableLEDBuffer(4); + final Color8Bit denimColor8Bit = new Color8Bit(Color.kDenim); + final Color8Bit firstBlueColor8Bit = new Color8Bit(Color.kFirstBlue); + final Color8Bit firstRedColor8Bit = new Color8Bit(Color.kFirstRed); + + buffer.setLED(0, Color.kFirstBlue); + buffer.setLED(1, denimColor8Bit); + buffer.setLED(2, Color.kFirstRed); + buffer.setLED(3, Color.kFirstBlue); + + assertTrue(buffer.getLED(0).equals(Color.kFirstBlue)); + assertTrue(buffer.getLED(1).equals(Color.kDenim)); + assertTrue(buffer.getLED(2).equals(Color.kFirstRed)); + assertTrue(buffer.getLED(3).equals(Color.kFirstBlue)); + assertTrue(buffer.getLED8Bit(0).equals(firstBlueColor8Bit)); + assertTrue(buffer.getLED8Bit(1).equals(denimColor8Bit)); + assertTrue(buffer.getLED8Bit(2).equals(firstRedColor8Bit)); + assertTrue(buffer.getLED8Bit(3).equals(firstBlueColor8Bit)); + } }