Migrate to the onFocusEvent API

Migrate from the older `Focus` API to `onFocusEvent` provided by Kaleidoscope.

As a side-effect, this drops the per-layer focus command support, as that was a
workaround for a Chrysalis bug fixed since.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/389/head
Gergely Nagy 6 years ago
parent 2ef2f942ef
commit 3b3054d216

@ -24,7 +24,7 @@ full extent, we need to create our own plugin on top of it.
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-EEPROM-Settings.h> #include <Kaleidoscope-EEPROM-Settings.h>
#include <Kaleidoscope-LED-Palette-Theme.h> #include <Kaleidoscope-LED-Palette-Theme.h>
#include <Kaleidoscope-Focus.h> #include <Kaleidoscope-FocusSerial.h>
namespace example { namespace example {
@ -36,7 +36,7 @@ class TestLEDMode : public LEDMode {
void setup(void) final; void setup(void) final;
void update(void) final; void update(void) final;
static bool focusHook(const char *command); kaleidoscope::EventHandlerResult onFocusEvent(const char *command);
private: private:
static uint16_t map_base_; static uint16_t map_base_;
@ -52,27 +52,26 @@ void TestLEDMode::update(void) {
LEDPaletteTheme.updateHandler(map_base_, 0); LEDPaletteTheme.updateHandler(map_base_, 0);
} }
bool kaleidoscope::EventHandlerResult
TestLEDMode::focusHook(const char *command) { TestLEDMode::onFocusEvent(const char *command) {
return LEDPaletteTheme.themeFocusHandler(command, PSTR("testLedMode.map"), map_base_, 1); return LEDPaletteTheme.themeFocusEvent(command, PSTR("testLedMode.map"), map_base_, 1);
} }
} }
example::TestLEDMode TestLEDMode; example::TestLEDMode TestLEDMode;
void setup() { KALEIDOSCOPE_INIT_PLUGINS(
Serial.begin(9600); Focus,
LEDPaletteTheme,
Kaleidoscope.use(&Focus, &LEDPaletteTheme, &TestLEDMode, &EEPROMSettings); TestLEDMode,
EEPROMSettings
);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
EEPROMSettings.seal();
TestLEDMode.activate(); TestLEDMode.activate();
Focus.addHook(FOCUS_HOOK_LEDPALETTETHEME);
Focus.addHook(FOCUS_HOOK(TestLEDMode.focusHook, "testLEDMode.map"));
} }
``` ```
@ -100,7 +99,7 @@ The plugin provides the `LEDPaletteTheme` object, which has the following method
> The `theme` argument can be any index between zero and `max_themes`. How the > The `theme` argument can be any index between zero and `max_themes`. How the
> plugin decides which theme to display depends entirely on the plugin. > plugin decides which theme to display depends entirely on the plugin.
### `.themeFocusHandler(command, expected_command, theme_base, max_themes)` ### `.themeFocusEvent(command, expected_command, theme_base, max_themes)`
> To be used in a custom `Focus` handler: handles the `expected_command` Focus > To be used in a custom `Focus` handler: handles the `expected_command` Focus
> command, and provides a way to query and update the themes supported by the > command, and provides a way to query and update the themes supported by the
@ -109,14 +108,11 @@ The plugin provides the `LEDPaletteTheme` object, which has the following method
> When queried, it will list the color indexes. When used as a setter, it > When queried, it will list the color indexes. When used as a setter, it
> expects one index per key. > expects one index per key.
> >
> The palette can be set via the `palette` focus command, implemented by the > The palette can be set via the `palette` focus command, provided by the
> `FOCUS_HOOK_LEDPALETTETHEME` hook explained below. > `LEDPaletteTheme` plugin.
## Focus commands ## Focus commands
The plugin provides a single `Focus` hook, `FOCUS_HOOK_LEDPALETTETHEME`,
implementing the following command:
### `palette` ### `palette`
> Without arguments, prints the palette: RGB values for all 16 colors. > Without arguments, prints the palette: RGB values for all 16 colors.
@ -130,7 +126,7 @@ implementing the following command:
## Dependencies ## Dependencies
* [Kaleidoscope-EEPROM-Settings](https://github.com/keyboardio/Kaleidoscope-EEPROM-Settings) * [Kaleidoscope-EEPROM-Settings](https://github.com/keyboardio/Kaleidoscope-EEPROM-Settings)
* [Kaleidoscope-Focus](https://github.com/keyboardio/Kaleidoscope-Focus) * [Kaleidoscope-FocusSerial](https://github.com/keyboardio/Kaleidoscope-FocusSerial)
* [Kaleidoscope-LEDControl](https://github.com/keyboardio/Kaleidoscope-LEDControl) * [Kaleidoscope-LEDControl](https://github.com/keyboardio/Kaleidoscope-LEDControl)
## Further reading ## Further reading

@ -19,7 +19,7 @@
#include <Kaleidoscope-LEDControl.h> #include <Kaleidoscope-LEDControl.h>
#include <Kaleidoscope-LED-Palette-Theme.h> #include <Kaleidoscope-LED-Palette-Theme.h>
#include <Kaleidoscope-EEPROM-Settings.h> #include <Kaleidoscope-EEPROM-Settings.h>
#include <Kaleidoscope-Focus.h> #include <Kaleidoscope-FocusSerial.h>
namespace example { namespace example {
@ -27,7 +27,7 @@ class TestLEDMode : public kaleidoscope::LEDMode {
public: public:
TestLEDMode() {} TestLEDMode() {}
static bool focusHook(const char *command); EventHandlerResult onFocusEvent(const char *command);
protected: protected:
void setup() final; void setup() final;
@ -49,9 +49,9 @@ TestLEDMode::update(void) {
LEDPaletteTheme.updateHandler(map_base_, 0); LEDPaletteTheme.updateHandler(map_base_, 0);
} }
bool EventHandlerResult
TestLEDMode::focusHook(const char *command) { TestLEDMode::onFocusEvent(const char *command) {
return LEDPaletteTheme.themeFocusHandler(command, PSTR("testLedMode.map"), map_base_, 1); return LEDPaletteTheme.themeFocusEvent(command, PSTR("testLedMode.map"), map_base_, 1);
} }
} }
@ -83,15 +83,9 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
KALEIDOSCOPE_INIT_PLUGINS(Focus, LEDPaletteTheme, TestLEDMode, EEPROMSettings); KALEIDOSCOPE_INIT_PLUGINS(Focus, LEDPaletteTheme, TestLEDMode, EEPROMSettings);
void setup() { void setup() {
Serial.begin(9600);
Kaleidoscope.setup(); Kaleidoscope.setup();
EEPROMSettings.seal();
TestLEDMode.activate(); TestLEDMode.activate();
Focus.addHook(FOCUS_HOOK_LEDPALETTETHEME);
Focus.addHook(FOCUS_HOOK(TestLEDMode.focusHook, "testLEDMode.map"));
} }
void loop() { void loop() {

@ -17,7 +17,7 @@
#include <Kaleidoscope-LED-Palette-Theme.h> #include <Kaleidoscope-LED-Palette-Theme.h>
#include <Kaleidoscope-EEPROM-Settings.h> #include <Kaleidoscope-EEPROM-Settings.h>
#include <Kaleidoscope-Focus.h> #include <Kaleidoscope-FocusSerial.h>
#include <EEPROM.h> #include <EEPROM.h>
namespace kaleidoscope { namespace kaleidoscope {
@ -92,9 +92,14 @@ void LEDPaletteTheme::updateColorIndexAtPosition(uint16_t map_base, uint16_t pos
EEPROM.update(map_base + position / 2, indexes); EEPROM.update(map_base + position / 2, indexes);
} }
bool LEDPaletteTheme::paletteFocusHook(const char *command) { EventHandlerResult LEDPaletteTheme::onFocusEvent(const char *command) {
if (strcmp_P(command, PSTR("palette")) != 0) const char *cmd = PSTR("palette");
return false;
if (::Focus.handleHelp(command, cmd))
return EventHandlerResult::OK;
if (strcmp_P(command, cmd) != 0)
return EventHandlerResult::OK;
if (Serial.peek() == '\n') { if (Serial.peek() == '\n') {
for (uint8_t i = 0; i < 16; i++) { for (uint8_t i = 0; i < 16; i++) {
@ -105,7 +110,7 @@ bool LEDPaletteTheme::paletteFocusHook(const char *command) {
::Focus.printSpace(); ::Focus.printSpace();
} }
Serial.println(); Serial.println();
return true; return EventHandlerResult::EVENT_CONSUMED;
} }
uint8_t i = 0; uint8_t i = 0;
@ -120,13 +125,18 @@ bool LEDPaletteTheme::paletteFocusHook(const char *command) {
i++; i++;
} }
return true; return EventHandlerResult::EVENT_CONSUMED;
} }
bool LEDPaletteTheme::themeFocusHandler(const char *command, const char *expected_command, EventHandlerResult LEDPaletteTheme::themeFocusEvent(const char *command,
uint16_t theme_base, uint8_t max_themes) { const char *expected_command,
uint16_t theme_base,
uint8_t max_themes) {
if (::Focus.handleHelp(command, expected_command))
return EventHandlerResult::OK;
if (strcmp_P(command, expected_command) != 0) if (strcmp_P(command, expected_command) != 0)
return false; return EventHandlerResult::OK;
uint16_t max_index = (max_themes * ROWS * COLS) / 2; uint16_t max_index = (max_themes * ROWS * COLS) / 2;
@ -140,7 +150,7 @@ bool LEDPaletteTheme::themeFocusHandler(const char *command, const char *expecte
::Focus.printSpace(); ::Focus.printSpace();
} }
Serial.println(); Serial.println();
return true; return EventHandlerResult::EVENT_CONSUMED;
} }
uint16_t pos = 0; uint16_t pos = 0;
@ -154,45 +164,7 @@ bool LEDPaletteTheme::themeFocusHandler(const char *command, const char *expecte
pos++; pos++;
} }
return true; return EventHandlerResult::EVENT_CONSUMED;
}
bool LEDPaletteTheme::themeLayerFocusHandler(const char *command, const char *expected_command,
uint16_t theme_base, uint8_t max_themes) {
if (strcmp_P(command, expected_command) != 0)
return false;
uint16_t count_per_layer = (ROWS * COLS) / 2;
uint8_t layer = Serial.parseInt();
uint16_t offset = theme_base + (layer * count_per_layer);
if (Serial.peek() == '\n') {
for (uint16_t pos = 0; pos < count_per_layer; pos++) {
uint8_t indexes = EEPROM.read(offset + pos);
::Focus.printNumber(indexes >> 4);
::Focus.printSpace();
::Focus.printNumber(indexes & ~0xf0);
::Focus.printSpace();
}
Serial.println();
return true;
}
uint16_t pos = 0;
while ((Serial.peek() != '\n') && (pos < count_per_layer)) {
uint8_t idx1 = Serial.parseInt();
uint8_t idx2 = Serial.parseInt();
uint8_t indexes = (idx1 << 4) + idx2;
EEPROM.update(offset + pos, indexes);
pos++;
}
return true;
} }
} }

@ -36,15 +36,10 @@ class LEDPaletteTheme : public kaleidoscope::Plugin {
static const cRGB lookupPaletteColor(uint8_t palette_index); static const cRGB lookupPaletteColor(uint8_t palette_index);
static bool paletteFocusHook(const char *command); EventHandlerResult onFocusEvent(const char *command);
static bool themeFocusHandler(const char *command, const char *expected_command, EventHandlerResult themeFocusEvent(const char *command,
uint16_t theme_base, uint8_t max_themes);
static bool themeLayerFocusHandler(const char *command,
const char *expected_command, const char *expected_command,
uint16_t theme_base, uint16_t theme_base, uint8_t max_themes);
uint8_t max_themes);
EventHandlerResult onSetup(); EventHandlerResult onSetup();
private: private:
@ -54,7 +49,3 @@ class LEDPaletteTheme : public kaleidoscope::Plugin {
} }
extern kaleidoscope::LEDPaletteTheme LEDPaletteTheme; extern kaleidoscope::LEDPaletteTheme LEDPaletteTheme;
#define FOCUS_HOOK_LEDPALETTETHEME \
FOCUS_HOOK(LEDPaletteTheme.paletteFocusHook, \
"palette")

Loading…
Cancel
Save