Skip to content

Commit

Permalink
🔀 Merge branch 'hugo/feature/Add-brightness-array-CoreLED' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Feb 21, 2022
2 parents 2ae8978 + 1a5ad22 commit 5b30162
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 37 deletions.
1 change: 1 addition & 0 deletions drivers/CoreLED/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_link_libraries(CoreLED
if (${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")

leka_unit_tests_sources(
tests/CoreLED_test_brightness.cpp
tests/CoreLED_test_setColor.cpp
tests/CoreLED_test_showHideColor.cpp
tests/CoreLED_test_status.cpp
Expand Down
44 changes: 35 additions & 9 deletions drivers/CoreLED/include/CoreLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@ template <size_t NumberOfLeds>
class CoreLED : public interface::LED
{
public:
explicit CoreLED(interface::SPI &spi) : _spi {spi} {};
explicit CoreLED(interface::SPI &spi) : _spi {spi}
{
std::fill(_brightness.begin(), _brightness.end(), kBrightnessDefault);
};

void setColor(const RGB &color) override { std::fill(_colors.begin(), _colors.end(), color); }
void setColor(const RGB &color) override
{
std::fill(_colors.begin(), _colors.end(), color);
std::fill(_brightness.begin(), _brightness.end(), kBrightnessDefault);
}

void setColorAtIndex(const unsigned index, const RGB &color) override
{
if (index >= NumberOfLeds) {
return;
}
_colors.at(index) = color;
_colors.at(index) = color;
_brightness.at(index) = kBrightnessDefault;
}

void setColorWithArray(const std::span<const RGB> color) override
{
std::copy(color.begin(), color.end(), _colors.begin());
std::fill(_brightness.begin(), _brightness.end(), kBrightnessDefault);
}

void setColorRange(unsigned start, unsigned end, const RGB &color) override
Expand All @@ -49,9 +58,23 @@ class CoreLED : public interface::LED
}

std::fill(_colors.begin() + start, _colors.begin() + end + 1, color);
std::fill(_brightness.begin() + start, _brightness.begin() + end + 1, kBrightnessDefault);
}

void fadeToBlackBy(const uint8_t &value) override
{
for (auto &b: _brightness) {
if (b - value <= kBrightnessMinimum) {
b = kBrightnessMinimum;
continue;
}

b = b - value;
}
show();
}

void showColor() override
void show() override
{
sendAndDisplay(_colors);

Expand All @@ -61,7 +84,7 @@ class CoreLED : public interface::LED
_is_color_shown = all_leds_are_not_black;
}

void hideColor() override
void hide() override
{
if (!isOn()) {
return;
Expand All @@ -78,6 +101,7 @@ class CoreLED : public interface::LED
}

[[nodiscard]] auto getColor() -> std::span<const RGB> override { return std::span(_colors); }
[[nodiscard]] auto getBrightness() -> std::span<const uint8_t> override { return std::span(_brightness); }

[[nodiscard]] auto isOn() const -> bool override { return _is_color_shown; }

Expand All @@ -86,22 +110,24 @@ class CoreLED : public interface::LED
bool _is_color_shown = false;

std::array<RGB, NumberOfLeds> _colors;
std::array<uint8_t, NumberOfLeds> _brightness;

static constexpr uint8_t brightness = 0xFF;
static constexpr auto kBrightnessDefault = 0xF0;
static constexpr auto kBrightnessMinimum = 0xE0;

struct frame {
static constexpr auto start = std::to_array<uint8_t>({0x00, 0x00, 0x00, 0x00});
static constexpr auto reset = std::to_array<uint8_t>({0x00, 0x00, 0x00, 0x00});
static constexpr auto end = std::to_array<uint8_t>({0x00, 0x00, 0x00, 0x00});
};

void sendAndDisplay(const std::array<RGB, NumberOfLeds> &colors)
void sendAndDisplay(std::span<const RGB> colors)
{
_spi.write(frame::start);

for (const auto &c: colors) {
for (auto i = 0; const auto &c: colors) {
// ? SK9822 LEDS are using BGR
auto data = std::to_array<uint8_t>({brightness, c.blue, c.green, c.red});
auto data = std::to_array<uint8_t>({_brightness.at(i), c.blue, c.green, c.red});
_spi.write(data);
}

Expand Down
255 changes: 255 additions & 0 deletions drivers/CoreLED/tests/CoreLED_test_brightness.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "CoreLED.h"
#include "CoreSPI.h"
#include "gtest/gtest.h"
#include "mocks/leka/SPI.h"

using namespace leka;

class CoreLedBrightnessTest : public ::testing::Test
{
protected:
CoreLedBrightnessTest() = default;

void SetUp() override { std::fill(expected_brightness.begin(), expected_brightness.end(), kBrightnessDefault); }
// void TearDown() override {}

static constexpr int number_of_leds = 20;
static constexpr auto kBrightnessDefault = 0xF0;
static constexpr auto kBrightnessMinimum = 0xE0;

std::array<uint8_t, number_of_leds> expected_brightness {};

CoreSPI spi {NC, NC, NC, NC};
CoreLED<number_of_leds> leds {spi};
};

TEST_F(CoreLedBrightnessTest, setBrightnessForAll)
{
std::fill(expected_brightness.begin(), expected_brightness.end(), kBrightnessDefault);

leds.setColor(RGB::pure_green);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessAtIndexFirst)
{
auto index = 0;
auto color = RGB::pure_green;

expected_brightness.at(index) = kBrightnessDefault;

leds.setColorAtIndex(index, RGB::pure_green);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessAtIndexMiddle)
{
auto index = number_of_leds / 2 - 1;
auto color = RGB::pure_green;

expected_brightness.at(index) = kBrightnessDefault;

leds.setColorAtIndex(index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessAtIndexLast)
{
auto index = number_of_leds - 1;
auto color = RGB::pure_green;

expected_brightness.at(index) = kBrightnessDefault;

leds.setColorAtIndex(index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessAtIndexEqualNumberOfLeds)
{
auto index = number_of_leds;
auto color = RGB::pure_green;

leds.setColorAtIndex(index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessAtIndexHigherThanNumberOfLeds)
{
auto index = number_of_leds + 100;
auto color = RGB::pure_green;

leds.setColorAtIndex(index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessAtIndexFirstMiddleEnd)
{
auto first_index = 0;
auto middle_index = number_of_leds / 2 - 1;
auto end_index = number_of_leds - 1;

auto first_color = RGB::pure_red;
auto middle_color = RGB::pure_green;
auto end_color = RGB::pure_blue;

expected_brightness.at(first_index) = kBrightnessDefault;
expected_brightness.at(middle_index) = kBrightnessDefault;
expected_brightness.at(end_index) = kBrightnessDefault;

leds.setColorAtIndex(first_index, first_color);
leds.setColorAtIndex(middle_index, middle_color);
leds.setColorAtIndex(end_index, end_color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessFromArray)
{
auto expected_colors = std::to_array<RGB>(
{RGB::pure_blue, RGB::pure_green, RGB::pure_red, RGB::pure_blue, RGB::yellow, RGB::cyan, RGB::magenta,
RGB::pure_green, RGB::pure_red, RGB::pure_blue, RGB::yellow, RGB::cyan, RGB::magenta, RGB::pure_green,
RGB::pure_red, RGB::pure_blue, RGB::yellow, RGB::cyan, RGB::magenta, RGB::black});

std::fill(expected_brightness.begin(), expected_brightness.end(), kBrightnessDefault);

leds.setColorWithArray(expected_colors);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessRange)
{
RGB color = RGB::pure_green;
int range_begin_index = 2;
int range_end_index = 5;

for (auto i = range_begin_index; i <= range_end_index; ++i) {
expected_brightness.at(i) = kBrightnessDefault;
}

leds.setColorRange(range_begin_index, range_end_index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessRangeFirstLast)
{
RGB color = RGB::pure_green;
int range_begin_index = 0;
int range_end_index = number_of_leds - 1;

for (auto i = range_begin_index; i <= range_end_index; ++i) {
expected_brightness.at(i) = kBrightnessDefault;
}

leds.setColorRange(range_begin_index, range_end_index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessRangeFirstMiddle)
{
RGB color = RGB::pure_green;
int range_begin_index = 0;
int range_end_index = number_of_leds / 2 - 1;

for (auto i = range_begin_index; i <= range_end_index; ++i) {
expected_brightness.at(i) = kBrightnessDefault;
}

leds.setColorRange(range_begin_index, range_end_index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessRangeMiddleLast)
{
RGB color = RGB::pure_green;
int range_begin_index = number_of_leds / 2 - 1;
int range_end_index = number_of_leds - 1;

for (auto i = range_begin_index; i <= range_end_index; ++i) {
expected_brightness.at(i) = kBrightnessDefault;
}

leds.setColorRange(range_begin_index, range_end_index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessRangeMiddleLastEqualNumberOfLeds)
{
RGB color = RGB::pure_green;
int range_begin_index = number_of_leds / 2 - 1;
int range_end_index = number_of_leds;

for (auto i = range_begin_index; i < number_of_leds; ++i) {
expected_brightness.at(i) = kBrightnessDefault;
}

leds.setColorRange(range_begin_index, range_end_index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, setBrightnessRangeMiddleLastHigherThanNumberOfLeds)
{
RGB color = RGB::pure_green;
int range_begin_index = number_of_leds / 2 - 1;
int range_end_index = number_of_leds + 100;

for (auto i = range_begin_index; i < number_of_leds; ++i) {
expected_brightness.at(i) = kBrightnessDefault;
}

leds.setColorRange(range_begin_index, range_end_index, color);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, fadeToBlackByOneFromDefaultBrightness)
{
std::fill(expected_brightness.begin(), expected_brightness.end(), kBrightnessDefault - 1);

leds.fadeToBlackBy(1);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, fadeToBlackByRandomFromDefaultBrightness)
{
std::fill(expected_brightness.begin(), expected_brightness.end(), kBrightnessDefault - 4);

leds.fadeToBlackBy(4);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, fadeToBlackToMuchFromDefaultBrightness)
{
std::fill(expected_brightness.begin(), expected_brightness.end(), kBrightnessMinimum);

leds.fadeToBlackBy(20);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}

TEST_F(CoreLedBrightnessTest, fadeToBlackAndSetColor)
{
std::fill(expected_brightness.begin(), expected_brightness.end(), kBrightnessDefault);

leds.fadeToBlackBy(100);
leds.setColor(RGB::pure_green);

EXPECT_TRUE(std::equal(expected_brightness.begin(), expected_brightness.end(), leds.getBrightness().begin()));
}
Loading

0 comments on commit 5b30162

Please sign in to comment.