From b66c17c77ddcb7847a3998890cdc8192261e016a Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 27 Mar 2017 17:14:53 +0200 Subject: [PATCH] Add an optional Focus hook Implements the `layer.on`, `layer.off`, and `layer.getState` commands, which can be used to control the active layers from the host. Signed-off-by: Gergely Nagy --- src/Kaleidoscope.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/Kaleidoscope.h | 7 +++++++ 2 files changed, 50 insertions(+) diff --git a/src/Kaleidoscope.cpp b/src/Kaleidoscope.cpp index 876d095c..69be3bb4 100644 --- a/src/Kaleidoscope.cpp +++ b/src/Kaleidoscope.cpp @@ -97,4 +97,47 @@ Kaleidoscope_::useLoopHook(loopHook hook) { appendLoopHook (hook); } +bool +Kaleidoscope_::focusHook(const char *command) { + enum { + ON, + OFF, + GETSTATE, + } subCommand; + + if (strncmp_P(command, PSTR("layer."), 6) != 0) + return false; + + if (strcmp_P (command + 6, PSTR("on")) == 0) + subCommand = ON; + else if (strcmp_P(command + 6, PSTR("off")) == 0) + subCommand = OFF; + else if (strcmp_P(command + 6, PSTR("getState")) == 0) + subCommand = GETSTATE; + else + return false; + + switch (subCommand) { + case ON: + { + uint8_t layer = Serial.parseInt(); + Layer.on(layer); + break; + } + + case OFF: + { + uint8_t layer = Serial.parseInt(); + Layer.off(layer); + break; + } + + case GETSTATE: + Serial.println(Layer.getLayerState(), BIN); + break; + } + + return true; +} + Kaleidoscope_ Kaleidoscope; diff --git a/src/Kaleidoscope.h b/src/Kaleidoscope.h index fbdeb8dc..5ab8cfa0 100644 --- a/src/Kaleidoscope.h +++ b/src/Kaleidoscope.h @@ -94,10 +94,17 @@ class Kaleidoscope_ { static void replaceLoopHook(loopHook oldHook, loopHook newHook); static void appendLoopHook(loopHook hook); static void useLoopHook(loopHook hook); + + static bool focusHook(const char *command); }; extern Kaleidoscope_ Kaleidoscope; +#define FOCUS_HOOK_KALEIDOSCOPE FOCUS_HOOK(Kaleidoscope.focusHook, \ + "layer.on\n" \ + "layer.off\n" \ + "layer.getState") + /* -- DEPRECATED aliases; remove them when there are no more users. -- */ #define event_handler_hook_use(hook) Kaleidoscope.useEventHandlerHook(hook);