/*------------------------------------------------------------------------- NeoPixelBus library wrapper template class that provides overall brightness control Written by Michael C. Miller. I invest time and resources providing this open source code, please support me by dontating (see https://github.com/Makuna/NeoPixelBus) ------------------------------------------------------------------------- This file is part of the Makuna/NeoPixelBus library. NeoPixelBus is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. NeoPixelBus is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with NeoPixel. If not, see . -------------------------------------------------------------------------*/ #pragma once #include "NeoPixelBus.h" template class NeoPixelBrightnessBus : public NeoPixelBus { private: void ScaleColor(uint16_t scale, typename T_COLOR_FEATURE::ColorObject* color) { uint8_t* ptr = (uint8_t*)color; uint8_t* ptrEnd = ptr + sizeof(typename T_COLOR_FEATURE::ColorObject); while (ptr != ptrEnd) { uint16_t value = *ptr; *ptr++ = (value * scale) >> 8; } } void ConvertColor(typename T_COLOR_FEATURE::ColorObject* color) { uint16_t scale = _brightness + 1; ScaleColor(scale, color); } void RecoverColor(typename T_COLOR_FEATURE::ColorObject* color) const { uint8_t* ptr = (uint8_t*)color; uint8_t* ptrEnd = ptr + sizeof(typename T_COLOR_FEATURE::ColorObject); uint16_t scale = _brightness + 1; while (ptr != ptrEnd) { uint16_t value = *ptr; *ptr++ = (value << 8) / scale; } } public: NeoPixelBrightnessBus(uint16_t countPixels, uint8_t pin) : NeoPixelBus(countPixels, pin), _brightness(255) { } NeoPixelBrightnessBus(uint16_t countPixels, uint8_t pinClock, uint8_t pinData) : NeoPixelBus(countPixels, pinClock, pinData), _brightness(255) { } NeoPixelBrightnessBus(uint16_t countPixels) : NeoPixelBus(countPixels), _brightness(255) { } void SetBrightness(uint8_t brightness) { // Only update if there is a change if (brightness != _brightness) { uint16_t scale = (((uint16_t)brightness + 1) << 8) / ((uint16_t)_brightness + 1); // scale existing pixels // for (uint16_t indexPixel = 0; indexPixel < NeoPixelBus::PixelCount(); indexPixel++) { typename T_COLOR_FEATURE::ColorObject color = NeoPixelBus::GetPixelColor(indexPixel); ScaleColor(scale, &color); NeoPixelBus::SetPixelColor(indexPixel, color); } _brightness = brightness; this->Dirty(); } } uint8_t GetBrightness() const { return _brightness; } void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) { ConvertColor(&color); NeoPixelBus::SetPixelColor(indexPixel, color); } typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const { typename T_COLOR_FEATURE::ColorObject color = NeoPixelBus::GetPixelColor(indexPixel); RecoverColor(&color); return color; } void ClearTo(typename T_COLOR_FEATURE::ColorObject color) { ConvertColor(&color); NeoPixelBus::ClearTo(color); }; void ClearTo(typename T_COLOR_FEATURE::ColorObject color, uint16_t first, uint16_t last) { ConvertColor(&color); NeoPixelBus::ClearTo(color, first, last); } protected: uint8_t _brightness; };