From 8c01d045de38314efa8f268924ad5676b06b81f1 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Mon, 22 Mar 2021 14:23:34 +0100 Subject: [PATCH] Introduced virtual interface to try out all methods at runtime --- src/NeoPixelBrightnessBus.h | 17 ++++++++++++----- src/NeoPixelBus.h | 28 ++++++++++++++++++---------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/NeoPixelBrightnessBus.h b/src/NeoPixelBrightnessBus.h index 3bea54b..44bd16b 100644 --- a/src/NeoPixelBrightnessBus.h +++ b/src/NeoPixelBrightnessBus.h @@ -28,11 +28,18 @@ License along with NeoPixel. If not, see #include "NeoPixelBus.h" +class NeoPixelBrightnessBusInterface +{ +public: + virtual ~NeoPixelBrightnessBusInterface() = default; + + virtual void SetBrightness(uint8_t brightness) = 0; +}; + template class NeoPixelBrightnessBus : - public NeoPixelBus + public NeoPixelBus, public NeoPixelBrightnessBusInterface { private: - void ScaleColor(uint16_t scale, typename T_COLOR_FEATURE::ColorObject* color) { uint8_t* ptr = (uint8_t*)color; @@ -83,7 +90,7 @@ public: { } - void SetBrightness(uint8_t brightness) + void SetBrightness(uint8_t brightness) override { // Only update if there is a change if (brightness != _brightness) @@ -109,13 +116,13 @@ public: return _brightness; } - void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) + void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) override { ConvertColor(&color); NeoPixelBus::SetPixelColor(indexPixel, color); } - typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const + typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const override { typename T_COLOR_FEATURE::ColorObject color = NeoPixelBus::GetPixelColor(indexPixel); RecoverColor(&color); diff --git a/src/NeoPixelBus.h b/src/NeoPixelBus.h index abba615..26791c7 100644 --- a/src/NeoPixelBus.h +++ b/src/NeoPixelBus.h @@ -110,10 +110,20 @@ License along with NeoPixel. If not, see #error "Platform Currently Not Supported, please add an Issue at Github/Makuna/NeoPixelBus" #endif +template +class NeoPixelBusInterface +{ +public: + virtual ~NeoPixelBusInterface() = default; + virtual void Begin() = 0; + virtual void Show(bool maintainBufferConsistency = true) = 0; + virtual bool CanShow() const = 0; + virtual void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) = 0; + virtual typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const = 0; +}; - -template class NeoPixelBus +template class NeoPixelBus : public NeoPixelBusInterface { public: // Constructor: number of LEDs, pin number @@ -140,9 +150,7 @@ public: { } - ~NeoPixelBus() - { - } + ~NeoPixelBus() override = default; operator NeoBufferContext() { @@ -150,7 +158,7 @@ public: return NeoBufferContext(_pixels(), PixelsSize()); } - void Begin() + void Begin() override { _method.Initialize(); Dirty(); @@ -163,7 +171,7 @@ public: Dirty(); } - void Show(bool maintainBufferConsistency = true) + void Show(bool maintainBufferConsistency = true) override { if (!IsDirty()) { @@ -175,7 +183,7 @@ public: ResetDirty(); } - inline bool CanShow() const + inline bool CanShow() const override { return _method.IsReadyToUpdate(); }; @@ -215,7 +223,7 @@ public: return _countPixels; }; - void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) + void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) override { if (indexPixel < _countPixels) { @@ -224,7 +232,7 @@ public: } }; - typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const + typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const override { if (indexPixel < _countPixels) {