From 1971249379c6d9038f42c14c36d1ace589e1960b Mon Sep 17 00:00:00 2001 From: Jan Delgado Date: Thu, 24 Feb 2022 21:34:35 +0100 Subject: [PATCH] Fix sequence methods not chainable (#89) --- CHANGELOG.md | 5 +++++ library.json | 2 +- library.properties | 2 +- src/jled.h | 4 ++-- src/jled_base.h | 14 +++++++------- test/test_jled_sequence.cpp | 15 ++++++++++++--- 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 014921a..f3ddae5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # JLed changelog (github.com/jandelgado/jled) +## [2022-02-24] 4.9.1 + +* fix: make sure JLedSequence methods like `Repeat` and `Forever` are chainable + like in the `JLed` class + ## [2022-02-13] 4.9.0 * new: support ESP-IDF platform for the ESP32 (#87, thanks to @troky for the diff --git a/library.json b/library.json index 20d5aed..a3334cf 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "JLed", - "version": "4.9.0", + "version": "4.9.1", "description": "An embedded library to control LEDs", "license": "MIT", "frameworks": ["espidf", "arduino", "mbed"], diff --git a/library.properties b/library.properties index 1a0f0f3..fad74ae 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=JLed -version=4.9.0 +version=4.9.1 author=Jan Delgado maintainer=Jan Delgado sentence=An Arduino library to control LEDs diff --git a/src/jled.h b/src/jled.h index 4b16a8a..41b21d1 100644 --- a/src/jled.h +++ b/src/jled.h @@ -58,8 +58,8 @@ class JLed : public TJLed { }; // a group of JLed objects which can be controlled simultanously -class JLedSequence : public TJLedSequence { - using TJLedSequence::TJLedSequence; +class JLedSequence : public TJLedSequence { + using TJLedSequence::TJLedSequence; }; }; // namespace jled diff --git a/src/jled_base.h b/src/jled_base.h index 5d8eb46..16380e6 100644 --- a/src/jled_base.h +++ b/src/jled_base.h @@ -472,7 +472,7 @@ template T* ptr(T *obj) {return obj;} // a group of JLed objects which can be controlled simultanously, in parallel // or sequentially. -template +template class TJLedSequence { protected: // update all leds parallel. Returns true while any of the JLeds is @@ -509,9 +509,9 @@ class TJLedSequence { TJLedSequence() = delete; template - TJLedSequence(eMode mode, T (&leds)[N]) : TJLedSequence(mode, leds, N) {} + TJLedSequence(eMode mode, L (&leds)[N]) : TJLedSequence(mode, leds, N) {} - TJLedSequence(eMode mode, T* leds, size_t n) + TJLedSequence(eMode mode, L* leds, size_t n) : mode_{mode}, leds_{leds}, cur_{0}, n_{n} {} bool Update() { @@ -549,18 +549,18 @@ class TJLedSequence { } // set number of repetitions for the sequence - TJLedSequence Repeat(uint16_t num_repetitions) { + B& Repeat(uint16_t num_repetitions) { num_repetitions_ = num_repetitions; - return *this; + return static_cast(*this); } // repeat Forever - TJLedSequence Forever() { return Repeat(kRepeatForever); } + B& Forever() { return Repeat(kRepeatForever); } bool IsForever() const { return num_repetitions_ == kRepeatForever; } private: const eMode mode_; - T* leds_; + L* leds_; size_t cur_; const size_t n_; static constexpr uint16_t kRepeatForever = 65535; diff --git a/test/test_jled_sequence.cpp b/test/test_jled_sequence.cpp index b6d9ebe..dea6f8b 100644 --- a/test/test_jled_sequence.cpp +++ b/test/test_jled_sequence.cpp @@ -15,13 +15,13 @@ class TestJLed : public TJLed { }; // a group of JLed objects which can be controlled simultanously -class TestJLedSequence : public TJLedSequence { - using TJLedSequence::TJLedSequence; +class TestJLedSequence : public TJLedSequence { + using TJLedSequence::TJLedSequence; }; // instanciate for test coverage measurement template class TJLed; -template class TJLedSequence; +template class TJLedSequence; TEST_CASE("parallel sequence performs all updates", "[jled_sequence]") { constexpr uint8_t expected1[] = {255, 0, 0}; @@ -155,6 +155,15 @@ TEST_CASE("Forever flag is set by call to Forever()", "[jled_sequence]") { } } +TEST_CASE("Forever and Repeat calls can be chained", "[jled_sequence]") { + // this is a compile time check only + TestJLed leds[] = {TestJLed(0)}; + TestJLedSequence hal[[gnu::unused]] = + TestJLedSequence(TestJLedSequence::eMode::PARALLEL, leds) + .Repeat(1) + .Forever(); +} + TEST_CASE("reset on sequence resets all JLeds", "[jled_sequence]") { constexpr uint8_t expected[]{/* 1ms on */ 255, /* 1ms off */ 0, 255, 0,