Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #29 from keyboardio/f/onFocusEvent
Browse files Browse the repository at this point in the history
Migrate to the new onFocusEvent APIs
  • Loading branch information
obra committed Oct 5, 2018
2 parents e525aef + 7ae4cae commit ff791ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
35 changes: 21 additions & 14 deletions src/Kaleidoscope-LEDControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

#include "Kaleidoscope-LEDControl.h"
#include "Kaleidoscope-Focus.h"
#include "Kaleidoscope-FocusSerial.h"

namespace kaleidoscope {

Expand Down Expand Up @@ -175,16 +175,22 @@ kaleidoscope::EventHandlerResult LEDControl::beforeReportingState(void) {
return kaleidoscope::EventHandlerResult::OK;
}

bool LEDControl::focusHook(const char *command) {
EventHandlerResult FocusLEDCommand::onFocusEvent(const char *command) {
enum {
SETALL,
MODE,
AT,
THEME,
} subCommand;

if (::Focus.handleHelp(command, PSTR("led.at\n"
"led.setAll\n"
"led.mode\n"
"led.theme")))
return EventHandlerResult::OK;

if (strncmp_P(command, PSTR("led."), 4) != 0)
return false;
return EventHandlerResult::OK;
if (strcmp_P(command + 4, PSTR("at")) == 0)
subCommand = AT;
else if (strcmp_P(command + 4, PSTR("setAll")) == 0)
Expand All @@ -194,14 +200,14 @@ bool LEDControl::focusHook(const char *command) {
else if (strcmp_P(command + 4, PSTR("theme")) == 0)
subCommand = THEME;
else
return false;
return EventHandlerResult::OK;

switch (subCommand) {
case AT: {
uint8_t idx = Serial.parseInt();

if (Serial.peek() == '\n') {
cRGB c = getCrgbAt(idx);
cRGB c = ::LEDControl.getCrgbAt(idx);

::Focus.printColor(c.r, c.g, c.b);
Serial.println();
Expand All @@ -210,7 +216,7 @@ bool LEDControl::focusHook(const char *command) {

::Focus.readColor(c);

setCrgbAt(idx, c);
::LEDControl.setCrgbAt(idx, c);
}
break;
}
Expand All @@ -219,31 +225,31 @@ bool LEDControl::focusHook(const char *command) {

::Focus.readColor(c);

set_all_leds_to(c);
::LEDControl.set_all_leds_to(c);

break;
}
case MODE: {
char peek = Serial.peek();
if (peek == '\n') {
Serial.println(get_mode_index());
Serial.println(::LEDControl.get_mode_index());
} else if (peek == 'n') {
next_mode();
::LEDControl.next_mode();
Serial.read();
} else if (peek == 'p') {
prev_mode();
::LEDControl.prev_mode();
Serial.read();
} else {
uint8_t mode = Serial.parseInt();

set_mode(mode);
::LEDControl.set_mode(mode);
}
break;
}
case THEME: {
if (Serial.peek() == '\n') {
for (uint8_t idx = 0; idx < LED_COUNT; idx++) {
cRGB c = getCrgbAt(idx);
cRGB c = ::LEDControl.getCrgbAt(idx);

::Focus.printColor(c.r, c.g, c.b);
::Focus.printSpace();
Expand All @@ -258,16 +264,17 @@ bool LEDControl::focusHook(const char *command) {

::Focus.readColor(color);

setCrgbAt(idx, color);
::LEDControl.setCrgbAt(idx, color);
idx++;
}
break;
}
}

return true;
return EventHandlerResult::EVENT_CONSUMED;
}

}

kaleidoscope::LEDControl LEDControl;
kaleidoscope::FocusLEDCommand FocusLEDCommand;
16 changes: 8 additions & 8 deletions src/Kaleidoscope-LEDControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ class LEDControl : public kaleidoscope::Plugin {
static uint16_t syncDelay;
static bool paused;

static bool focusHook(const char *command);

kaleidoscope::EventHandlerResult onSetup();
kaleidoscope::EventHandlerResult onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState);
kaleidoscope::EventHandlerResult beforeReportingState();
Expand All @@ -154,12 +152,14 @@ class LEDControl : public kaleidoscope::Plugin {
static uint8_t mode;
};

class FocusLEDCommand : public Plugin {
public:
FocusLEDCommand() {}

EventHandlerResult onFocusEvent(const char *command);
};

}

extern kaleidoscope::LEDControl LEDControl;

#define FOCUS_HOOK_LEDCONTROL FOCUS_HOOK (LEDControl.focusHook, \
"led.at\n" \
"led.setAll\n" \
"led.theme\n" \
"led.mode")
extern kaleidoscope::FocusLEDCommand FocusLEDCommand;

0 comments on commit ff791ef

Please sign in to comment.