From 0fc32dbd94ca18eb4b6c1a1318d4796b8d739975 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 1 Jun 2017 15:50:31 +0200 Subject: [PATCH] Add a number of helper functions Added `lookupColor(index)`, which looks up a color in the palette, by index. Made the `row, col` variant public, and added an `updateColor` method. These are there to help porting FingerPainter. Signed-off-by: Gergely Nagy --- src/Kaleidoscope/LED-Palette-Theme.cpp | 34 ++++++++++++++++++++++++-- src/Kaleidoscope/LED-Palette-Theme.h | 7 ++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Kaleidoscope/LED-Palette-Theme.cpp b/src/Kaleidoscope/LED-Palette-Theme.cpp index 394db4be..319fdaa1 100644 --- a/src/Kaleidoscope/LED-Palette-Theme.cpp +++ b/src/Kaleidoscope/LED-Palette-Theme.cpp @@ -56,8 +56,8 @@ LEDPaletteTheme::update (uint16_t themeBase, uint8_t theme) { } } -const bool -LEDPaletteTheme::lookupColor (uint16_t mapBase, uint16_t loc, cRGB *color) { +const uint8_t +LEDPaletteTheme::lookupColorIndex (uint16_t mapBase, uint16_t loc) { uint8_t colorIndex; colorIndex = EEPROM.read (mapBase + loc / 2); @@ -66,6 +66,13 @@ LEDPaletteTheme::lookupColor (uint16_t mapBase, uint16_t loc, cRGB *color) { else colorIndex >>= 4; + return colorIndex; +} + +const bool +LEDPaletteTheme::lookupColor (uint16_t mapBase, uint16_t loc, cRGB *color) { + uint8_t colorIndex = lookupColorIndex (mapBase, loc); + if (colorIndex == transparentIndex) return false; @@ -74,6 +81,29 @@ LEDPaletteTheme::lookupColor (uint16_t mapBase, uint16_t loc, cRGB *color) { return true; } +const cRGB +LEDPaletteTheme::lookupColor (uint8_t index) { + cRGB color; + + EEPROM.get (paletteBase + index * sizeof (cRGB), color); + return color; +} + +void +LEDPaletteTheme::updateColor (uint16_t mapBase, uint16_t loc, uint8_t index) { + uint8_t indexes; + + indexes = EEPROM.read (mapBase + loc / 2); + if (loc % 2) { + uint8_t other = indexes >> 4; + indexes = (other << 4) + index; + } else { + uint8_t other = indexes & ~0xf0; + indexes = (index << 4) + other; + } + EEPROM.update (mapBase + loc / 2, indexes); +} + bool LEDPaletteTheme::paletteFocusHook (const char *command) { if (strcmp_P (command, PSTR ("palette")) != 0) diff --git a/src/Kaleidoscope/LED-Palette-Theme.h b/src/Kaleidoscope/LED-Palette-Theme.h index 56f2b929..63bc6fe4 100644 --- a/src/Kaleidoscope/LED-Palette-Theme.h +++ b/src/Kaleidoscope/LED-Palette-Theme.h @@ -31,6 +31,11 @@ class LEDPaletteTheme : public KaleidoscopePlugin { static uint16_t reserveThemes (uint8_t maxThemes); static void update (uint16_t themeBase, uint8_t theme); + static const uint8_t lookupColorIndex (uint16_t mapBase, uint16_t loc); + static const cRGB lookupColor (uint8_t index); + static const bool lookupColor (uint16_t mapBase, uint16_t loc, cRGB *color); + static void updateColor (uint16_t mapBase, uint16_t loc, uint8_t index); + static bool paletteFocusHook (const char *command); static bool themeFocusHandler (const char *command, const char *expectedCommand, uint16_t themeBase, uint8_t maxThemes); @@ -39,8 +44,6 @@ class LEDPaletteTheme : public KaleidoscopePlugin { private: static uint16_t paletteBase; - - static const bool lookupColor (uint16_t mapBase, uint16_t loc, cRGB *color); }; };