LEDPaletteTheme: Lift out a few helper methods

To be able to set a theme from the firmware itself, we need a couple of helper
methods. First, we need to be able to update the palette without using Focus,
and we also need to be able to update a single LED without committing it. On top
of that, we'll likely want to know if the theme is initialized.

To this end, we introduce `updatePaletteColor()`, which updates an entry in the
palette, but does not commit it, and `isThemeUninitialized()` which does as the
name suggests: it checks if the palette and the theme slices are all uninitialized.

We also change `updateColorIndexAtPosition()` to not commit. The single user of
it was FingerPainter, and we update that to do an explicit commit after.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/1188/head
Gergely Nagy 2 years ago
parent a61c211dee
commit 0c6f608704
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-FingerPainter -- On-the-fly keyboard painting. * Kaleidoscope-FingerPainter -- On-the-fly keyboard painting.
* Copyright (C) 2017-2021 Keyboard.io, Inc * Copyright (C) 2017-2022 Keyboard.io, Inc
* *
* This program is free software: you can redistribute it and/or modify it under * This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software * the terms of the GNU General Public License as published by the Free Software
@ -89,6 +89,7 @@ EventHandlerResult FingerPainter::onKeyEvent(KeyEvent &event) {
::LEDPaletteTheme.updateColorIndexAtPosition(color_base_, ::LEDPaletteTheme.updateColorIndexAtPosition(color_base_,
Runtime.device().getLedIndex(event.addr), Runtime.device().getLedIndex(event.addr),
color_index); color_index);
Runtime.storage().commit();
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-LED-Palette-Theme -- Palette-based LED theme foundation * Kaleidoscope-LED-Palette-Theme -- Palette-based LED theme foundation
* Copyright (C) 2017, 2018, 2019 Keyboard.io, Inc * Copyright (C) 2017-2022 Keyboard.io, Inc
* *
* This program is free software: you can redistribute it and/or modify it under * This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software * the terms of the GNU General Public License as published by the Free Software
@ -105,7 +105,21 @@ void LEDPaletteTheme::updateColorIndexAtPosition(uint16_t map_base, uint16_t pos
indexes = (color_index << 4) + other; indexes = (color_index << 4) + other;
} }
Runtime.storage().update(map_base + position / 2, indexes); Runtime.storage().update(map_base + position / 2, indexes);
Runtime.storage().commit(); }
void LEDPaletteTheme::updatePaletteColor(uint8_t palette_index, cRGB color) {
color.r ^= 0xff;
color.g ^= 0xff;
color.b ^= 0xff;
Runtime.storage().put(palette_base_ + palette_index * sizeof(color), color);
}
bool LEDPaletteTheme::isThemeUninitialized(uint16_t theme_base, uint8_t max_themes) {
bool paletteEmpty = Runtime.storage().isSliceUninitialized(palette_base_, 16 * sizeof(cRGB));
bool themeEmpty = Runtime.storage().isSliceUninitialized(theme_base, max_themes * Runtime.device().led_count / 2);
return paletteEmpty && themeEmpty;
} }
EventHandlerResult LEDPaletteTheme::onFocusEvent(const char *command) { EventHandlerResult LEDPaletteTheme::onFocusEvent(const char *command) {
@ -135,11 +149,7 @@ EventHandlerResult LEDPaletteTheme::onFocusEvent(const char *command) {
cRGB color; cRGB color;
::Focus.read(color); ::Focus.read(color);
color.r ^= 0xff; updatePaletteColor(i, color);
color.g ^= 0xff;
color.b ^= 0xff;
Runtime.storage().put(palette_base_ + i * sizeof(color), color);
i++; i++;
} }
Runtime.storage().commit(); Runtime.storage().commit();

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-LED-Palette-Theme -- Palette-based LED theme foundation * Kaleidoscope-LED-Palette-Theme -- Palette-based LED theme foundation
* Copyright (C) 2017, 2018 Keyboard.io, Inc * Copyright (C) 2017-2022 Keyboard.io, Inc
* *
* This program is free software: you can redistribute it and/or modify it under * This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software * the terms of the GNU General Public License as published by the Free Software
@ -37,6 +37,7 @@ class LEDPaletteTheme : public kaleidoscope::Plugin {
static const cRGB lookupColorAtPosition(uint16_t theme_base, uint16_t position); static const cRGB lookupColorAtPosition(uint16_t theme_base, uint16_t position);
static void updateColorIndexAtPosition(uint16_t theme_base, uint16_t position, uint8_t color_index); static void updateColorIndexAtPosition(uint16_t theme_base, uint16_t position, uint8_t color_index);
static void updatePaletteColor(uint8_t palette_index, cRGB color);
static const cRGB lookupPaletteColor(uint8_t palette_index); static const cRGB lookupPaletteColor(uint8_t palette_index);
EventHandlerResult onFocusEvent(const char *command); EventHandlerResult onFocusEvent(const char *command);
@ -44,6 +45,7 @@ class LEDPaletteTheme : public kaleidoscope::Plugin {
const char *expected_command, const char *expected_command,
uint16_t theme_base, uint16_t theme_base,
uint8_t max_themes); uint8_t max_themes);
static bool isThemeUninitialized(uint16_t theme_base, uint8_t max_themes);
private: private:
static uint16_t palette_base_; static uint16_t palette_base_;

Loading…
Cancel
Save