You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
283 lines
6.4 KiB
283 lines
6.4 KiB
6 years ago
|
/* Kaleidoscope-LEDControl - LED control plugin for Kaleidoscope
|
||
|
* Copyright (C) 2017-2018 Keyboard.io, Inc.
|
||
|
*
|
||
|
* 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
|
||
|
* Foundation, version 3.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
|
* details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License along with
|
||
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
8 years ago
|
#include "Kaleidoscope-LEDControl.h"
|
||
6 years ago
|
#include "Kaleidoscope-FocusSerial.h"
|
||
8 years ago
|
|
||
7 years ago
|
namespace kaleidoscope {
|
||
6 years ago
|
namespace plugin {
|
||
8 years ago
|
|
||
7 years ago
|
LEDMode *LEDControl::modes[LED_MAX_MODES];
|
||
|
uint8_t LEDControl::mode;
|
||
|
uint16_t LEDControl::syncDelay = 16;
|
||
7 years ago
|
uint16_t LEDControl::syncTimer;
|
||
7 years ago
|
bool LEDControl::paused = false;
|
||
7 years ago
|
|
||
|
void LEDMode::activate(void) {
|
||
|
::LEDControl.activate(this);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
kaleidoscope::EventHandlerResult LEDMode::onSetup() {
|
||
7 years ago
|
::LEDControl.mode_add(this);
|
||
|
setup();
|
||
7 years ago
|
|
||
|
return EventHandlerResult::OK;
|
||
|
}
|
||
|
|
||
7 years ago
|
LEDControl::LEDControl(void) {
|
||
|
mode = 0;
|
||
8 years ago
|
memset(modes, 0, LED_MAX_MODES * sizeof(modes[0]));
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
void LEDControl::next_mode(void) {
|
||
8 years ago
|
mode++;
|
||
|
|
||
7 years ago
|
if (mode >= LED_MAX_MODES || !modes[mode]) {
|
||
|
return set_mode(0);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
return set_mode(mode);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
void LEDControl::prev_mode(void) {
|
||
7 years ago
|
if (mode == 0) {
|
||
|
// wrap around
|
||
7 years ago
|
mode = LED_MAX_MODES - 1;
|
||
7 years ago
|
// then count down until reaching a valid mode
|
||
|
while (mode > 0 && !modes[mode]) mode--;
|
||
|
} else {
|
||
|
mode--;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
return set_mode(mode);
|
||
7 years ago
|
}
|
||
|
|
||
8 years ago
|
void
|
||
7 years ago
|
LEDControl::set_mode(uint8_t mode_) {
|
||
|
if (mode_ >= LED_MAX_MODES)
|
||
|
return;
|
||
|
|
||
|
mode = mode_;
|
||
7 years ago
|
refreshAll();
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
uint8_t LEDControl::get_mode_index(void) {
|
||
8 years ago
|
return mode;
|
||
|
}
|
||
|
|
||
7 years ago
|
LEDMode *LEDControl::get_mode(void) {
|
||
|
return modes[mode];
|
||
|
}
|
||
|
|
||
|
void LEDControl::activate(LEDMode *mode) {
|
||
8 years ago
|
for (uint8_t i = 0; i < LED_MAX_MODES; i++) {
|
||
|
if (modes[i] == mode)
|
||
|
return set_mode(i);
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
int8_t LEDControl::mode_add(LEDMode *mode) {
|
||
8 years ago
|
for (int i = 0; i < LED_MAX_MODES; i++) {
|
||
|
if (modes[i])
|
||
|
continue;
|
||
|
|
||
|
modes[i] = mode;
|
||
|
return i;
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
7 years ago
|
void LEDControl::set_all_leds_to(uint8_t r, uint8_t g, uint8_t b) {
|
||
8 years ago
|
cRGB color;
|
||
8 years ago
|
color.r = r;
|
||
|
color.g = g;
|
||
|
color.b = b;
|
||
8 years ago
|
set_all_leds_to(color);
|
||
|
}
|
||
|
|
||
7 years ago
|
void LEDControl::set_all_leds_to(cRGB color) {
|
||
8 years ago
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
||
8 years ago
|
setCrgbAt(i, color);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
void LEDControl::setCrgbAt(uint8_t i, cRGB crgb) {
|
||
8 years ago
|
KeyboardHardware.setCrgbAt(i, crgb);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
void LEDControl::setCrgbAt(byte row, byte col, cRGB color) {
|
||
8 years ago
|
KeyboardHardware.setCrgbAt(row, col, color);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
cRGB LEDControl::getCrgbAt(uint8_t i) {
|
||
8 years ago
|
return KeyboardHardware.getCrgbAt(i);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
void LEDControl::syncLeds(void) {
|
||
8 years ago
|
KeyboardHardware.syncLeds();
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
kaleidoscope::EventHandlerResult LEDControl::onSetup() {
|
||
8 years ago
|
set_all_leds_to({0, 0, 0});
|
||
8 years ago
|
|
||
|
for (uint8_t i = 0; i < LED_MAX_MODES; i++) {
|
||
|
if (modes[i])
|
||
|
(modes[i]->setup)();
|
||
|
}
|
||
|
|
||
8 years ago
|
syncTimer = millis() + syncDelay;
|
||
7 years ago
|
|
||
|
return EventHandlerResult::OK;
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
kaleidoscope::EventHandlerResult LEDControl::onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState) {
|
||
8 years ago
|
if (mappedKey.flags != (SYNTHETIC | IS_INTERNAL | LED_TOGGLE))
|
||
7 years ago
|
return kaleidoscope::EventHandlerResult::OK;
|
||
8 years ago
|
|
||
7 years ago
|
if (keyToggledOn(keyState)) {
|
||
7 years ago
|
if (mappedKey == Key_LEDEffectNext) {
|
||
7 years ago
|
next_mode();
|
||
7 years ago
|
} else if (mappedKey == Key_LEDEffectPrevious) {
|
||
7 years ago
|
prev_mode();
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
return kaleidoscope::EventHandlerResult::EVENT_CONSUMED;
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
kaleidoscope::EventHandlerResult LEDControl::beforeReportingState(void) {
|
||
|
if (paused)
|
||
|
return kaleidoscope::EventHandlerResult::OK;
|
||
8 years ago
|
|
||
7 years ago
|
// unsigned subtraction means that as syncTimer rolls over
|
||
|
// the same interval is kept
|
||
7 years ago
|
uint16_t elapsed = Kaleidoscope.millisAtCycleStart() - syncTimer;
|
||
7 years ago
|
// on some platforms, the subtraction in the comparison results in a signed
|
||
|
// operation, resulting in syncLeds() no longer getting called.
|
||
7 years ago
|
if (elapsed > syncDelay) {
|
||
8 years ago
|
syncLeds();
|
||
7 years ago
|
syncTimer += syncDelay;
|
||
8 years ago
|
}
|
||
8 years ago
|
update();
|
||
7 years ago
|
|
||
|
return kaleidoscope::EventHandlerResult::OK;
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
EventHandlerResult FocusLEDCommand::onFocusEvent(const char *command) {
|
||
8 years ago
|
enum {
|
||
|
SETALL,
|
||
|
MODE,
|
||
8 years ago
|
AT,
|
||
|
THEME,
|
||
8 years ago
|
} subCommand;
|
||
|
|
||
6 years ago
|
if (::Focus.handleHelp(command, PSTR("led.at\n"
|
||
6 years ago
|
"led.setAll\n"
|
||
|
"led.mode\n"
|
||
|
"led.theme")))
|
||
6 years ago
|
return EventHandlerResult::OK;
|
||
|
|
||
8 years ago
|
if (strncmp_P(command, PSTR("led."), 4) != 0)
|
||
6 years ago
|
return EventHandlerResult::OK;
|
||
8 years ago
|
if (strcmp_P(command + 4, PSTR("at")) == 0)
|
||
8 years ago
|
subCommand = AT;
|
||
8 years ago
|
else if (strcmp_P(command + 4, PSTR("setAll")) == 0)
|
||
8 years ago
|
subCommand = SETALL;
|
||
8 years ago
|
else if (strcmp_P(command + 4, PSTR("mode")) == 0)
|
||
8 years ago
|
subCommand = MODE;
|
||
8 years ago
|
else if (strcmp_P(command + 4, PSTR("theme")) == 0)
|
||
8 years ago
|
subCommand = THEME;
|
||
8 years ago
|
else
|
||
6 years ago
|
return EventHandlerResult::OK;
|
||
8 years ago
|
|
||
|
switch (subCommand) {
|
||
8 years ago
|
case AT: {
|
||
|
uint8_t idx = Serial.parseInt();
|
||
8 years ago
|
|
||
8 years ago
|
if (Serial.peek() == '\n') {
|
||
6 years ago
|
cRGB c = ::LEDControl.getCrgbAt(idx);
|
||
8 years ago
|
|
||
7 years ago
|
::Focus.printColor(c.r, c.g, c.b);
|
||
8 years ago
|
Serial.println();
|
||
|
} else {
|
||
|
cRGB c;
|
||
8 years ago
|
|
||
7 years ago
|
::Focus.readColor(c);
|
||
8 years ago
|
|
||
6 years ago
|
::LEDControl.setCrgbAt(idx, c);
|
||
8 years ago
|
}
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
case SETALL: {
|
||
|
cRGB c;
|
||
8 years ago
|
|
||
7 years ago
|
::Focus.readColor(c);
|
||
8 years ago
|
|
||
6 years ago
|
::LEDControl.set_all_leds_to(c);
|
||
8 years ago
|
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
case MODE: {
|
||
|
char peek = Serial.peek();
|
||
|
if (peek == '\n') {
|
||
6 years ago
|
Serial.println(::LEDControl.get_mode_index());
|
||
8 years ago
|
} else if (peek == 'n') {
|
||
6 years ago
|
::LEDControl.next_mode();
|
||
8 years ago
|
Serial.read();
|
||
|
} else if (peek == 'p') {
|
||
6 years ago
|
::LEDControl.prev_mode();
|
||
8 years ago
|
Serial.read();
|
||
|
} else {
|
||
|
uint8_t mode = Serial.parseInt();
|
||
|
|
||
6 years ago
|
::LEDControl.set_mode(mode);
|
||
8 years ago
|
}
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
case THEME: {
|
||
|
if (Serial.peek() == '\n') {
|
||
|
for (uint8_t idx = 0; idx < LED_COUNT; idx++) {
|
||
6 years ago
|
cRGB c = ::LEDControl.getCrgbAt(idx);
|
||
8 years ago
|
|
||
7 years ago
|
::Focus.printColor(c.r, c.g, c.b);
|
||
|
::Focus.printSpace();
|
||
8 years ago
|
}
|
||
8 years ago
|
Serial.println();
|
||
8 years ago
|
break;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
uint8_t idx = 0;
|
||
|
while (idx < LED_COUNT && Serial.peek() != '\n') {
|
||
|
cRGB color;
|
||
8 years ago
|
|
||
7 years ago
|
::Focus.readColor(color);
|
||
8 years ago
|
|
||
6 years ago
|
::LEDControl.setCrgbAt(idx, color);
|
||
8 years ago
|
idx++;
|
||
8 years ago
|
}
|
||
8 years ago
|
break;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
return EventHandlerResult::EVENT_CONSUMED;
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
kaleidoscope::plugin::LEDControl LEDControl;
|
||
|
kaleidoscope::plugin::FocusLEDCommand FocusLEDCommand;
|