Merge pull request #20 from keyboardio/bug/numlock-sync-report-spam

Bug/numlock sync report spam
pull/365/head
Jesse Vincent 6 years ago committed by GitHub
commit d931ae1efa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,49 +19,46 @@
#include "Kaleidoscope.h" #include "Kaleidoscope.h"
#include "layers.h" #include "layers.h"
byte NumPad_::row = 255, NumPad_::col = 255; byte NumPad_::numpadLayerToggleKeyRow = 255, NumPad_::numpadLayerToggleKeyCol = 255;
uint8_t NumPad_::numPadLayer; uint8_t NumPad_::numPadLayer;
bool NumPad_::cleanupDone = true; bool NumPad_::numlockUnsynced = false;
bool NumPad_::originalNumLockState = false; bool NumPad_::originalNumLockState = false;
cRGB NumPad_::color = CRGB(160, 0, 0); cRGB NumPad_::color = CRGB(160, 0, 0);
uint8_t NumPad_::lock_hue = 170; uint8_t NumPad_::lock_hue = 170;
kaleidoscope::EventHandlerResult NumPad_::onSetup(void) { kaleidoscope::EventHandlerResult NumPad_::onSetup(void) {
originalNumLockState = !!(kaleidoscope::hid::getKeyboardLEDs() & LED_NUM_LOCK); originalNumLockState = getNumlockState();
return kaleidoscope::EventHandlerResult::OK; return kaleidoscope::EventHandlerResult::OK;
} }
static bool getNumlockState() { bool NumPad_::getNumlockState() {
return !!(kaleidoscope::hid::getKeyboardLEDs() & LED_NUM_LOCK); return !!(kaleidoscope::hid::getKeyboardLEDs() & LED_NUM_LOCK);
} }
static void syncNumlock(bool state) { void NumPad_::syncNumlockState(bool state) {
bool numState = getNumlockState(); bool numLockLEDState = getNumlockState();
if (numState != state) { if (numLockLEDState != state) {
kaleidoscope::hid::pressKey(Key_KeypadNumLock); kaleidoscope::hid::pressKey(Key_KeypadNumLock);
} }
} }
kaleidoscope::EventHandlerResult NumPad_::afterEachCycle() {
if (!Layer.isOn(numPadLayer)) {
bool numState = getNumlockState();
if (!cleanupDone) {
LEDControl.set_mode(LEDControl.get_mode_index());
void NumPad_::cleanupNumlockState() {
if (!numlockUnsynced) {
bool numLockLEDState = getNumlockState();
LEDControl.set_mode(LEDControl.get_mode_index());
if (!originalNumLockState) { if (!originalNumLockState) {
syncNumlock(false); syncNumlockState(false);
numState = false; numLockLEDState = false;
} }
cleanupDone = true; originalNumLockState = numLockLEDState;
numlockUnsynced = true;
} }
originalNumLockState = numState;
return kaleidoscope::EventHandlerResult::OK;
} }
cleanupDone = false; void NumPad_::setKeyboardLEDColors(void) {
syncNumlock(true);
LEDControl.set_mode(LEDControl.get_mode_index()); LEDControl.set_mode(LEDControl.get_mode_index());
for (uint8_t r = 0; r < ROWS; r++) { for (uint8_t r = 0; r < ROWS; r++) {
@ -70,8 +67,8 @@ kaleidoscope::EventHandlerResult NumPad_::afterEachCycle() {
Key layer_key = Layer.getKey(numPadLayer, r, c); Key layer_key = Layer.getKey(numPadLayer, r, c);
if (k == LockLayer(numPadLayer)) { if (k == LockLayer(numPadLayer)) {
row = r; numpadLayerToggleKeyRow = r;
col = c; numpadLayerToggleKeyCol = c;
} }
if ((k != layer_key) || (k == Key_NoKey) || (k.flags != KEY_FLAGS)) { if ((k != layer_key) || (k == Key_NoKey) || (k.flags != KEY_FLAGS)) {
@ -82,13 +79,28 @@ kaleidoscope::EventHandlerResult NumPad_::afterEachCycle() {
} }
} }
if (row > ROWS || col > COLS) if ((numpadLayerToggleKeyRow <= ROWS) && (numpadLayerToggleKeyCol <= COLS)) {
return kaleidoscope::EventHandlerResult::OK;
cRGB lock_color = breath_compute(lock_hue); cRGB lock_color = breath_compute(lock_hue);
LEDControl.setCrgbAt(row, col, lock_color); LEDControl.setCrgbAt(numpadLayerToggleKeyRow, numpadLayerToggleKeyCol, lock_color);
}
}
kaleidoscope::EventHandlerResult NumPad_::afterEachCycle() {
if (!Layer.isOn(numPadLayer)) {
cleanupNumlockState();
} else {
if (numlockUnsynced) {
// If it's the first time we're in this loop after toggling the Numpad mode on
syncNumlockState(true);
numlockUnsynced = false;
}
setKeyboardLEDColors();
}
return kaleidoscope::EventHandlerResult::OK; return kaleidoscope::EventHandlerResult::OK;
} }
NumPad_ NumPad; NumPad_ NumPad;

@ -32,8 +32,15 @@ class NumPad_ : public kaleidoscope::Plugin {
kaleidoscope::EventHandlerResult afterEachCycle(); kaleidoscope::EventHandlerResult afterEachCycle();
private: private:
static byte row, col;
static bool cleanupDone; void cleanupNumlockState(void);
void setKeyboardLEDColors(void);
bool getNumlockState(void);
void syncNumlockState(bool);
static uint8_t numpadLayerToggleKeyRow;
static uint8_t numpadLayerToggleKeyCol;
static bool numlockUnsynced;
static bool originalNumLockState; static bool originalNumLockState;
}; };

Loading…
Cancel
Save