Updated to use the new plugin APIs

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/389/head
Gergely Nagy 6 years ago
parent 8949536491
commit d9ff915633

@ -30,9 +30,9 @@ the box, without any further configuration:
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-TypingBreaks.h> #include <Kaleidoscope-TypingBreaks.h>
void setup (void) { KALEIDOSCOPE_INIT_PLUGINS(TypingBreaks);
Kaleidoscope.use (&TypingBreaks);
void setup (void) {
Kaleidoscope.setup (); Kaleidoscope.setup ();
TypingBreaks.settings.idle_time_limit = 60; TypingBreaks.settings.idle_time_limit = 60;

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-TypingBreaks -- Enforced typing breaks * Kaleidoscope-TypingBreaks -- Enforced typing breaks
* Copyright (C) 2017 Gergely Nagy * Copyright (C) 2017, 2018 Gergely Nagy
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -19,6 +19,7 @@
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-TypingBreaks.h> #include <Kaleidoscope-TypingBreaks.h>
// *INDENT-OFF*
const Key keymaps[][ROWS][COLS] PROGMEM = { const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
( (
@ -32,16 +33,17 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip,
Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals,
Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote,
Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus,
Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,
Key_skip), Key_skip),
}; };
// *INDENT-ON*
void setup() { KALEIDOSCOPE_INIT_PLUGINS(TypingBreaks);
Kaleidoscope.use(&TypingBreaks);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
TypingBreaks.settings.idle_time_limit = 60; TypingBreaks.settings.idle_time_limit = 60;

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-TypingBreaks -- Enforced typing breaks * Kaleidoscope-TypingBreaks -- Enforced typing breaks
* Copyright (C) 2017 Gergely Nagy * Copyright (C) 2017, 2018 Gergely Nagy
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -37,27 +37,22 @@ uint16_t TypingBreaks::left_hand_keys_;
uint16_t TypingBreaks::right_hand_keys_; uint16_t TypingBreaks::right_hand_keys_;
uint16_t TypingBreaks::settings_base_; uint16_t TypingBreaks::settings_base_;
TypingBreaks::TypingBreaks(void) { EventHandlerResult TypingBreaks::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) {
}
void TypingBreaks::begin(void) {
Kaleidoscope.useEventHandlerHook(this->eventHandlerHook);
}
Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
uint32_t lock_length = settings.lock_length * 1000; uint32_t lock_length = settings.lock_length * 1000;
uint32_t idle_time_limit = settings.idle_time_limit * 1000; uint32_t idle_time_limit = settings.idle_time_limit * 1000;
uint32_t lock_time_out = settings.lock_time_out * 1000; uint32_t lock_time_out = settings.lock_time_out * 1000;
// If we are locked, and didn't time out yet, no key has to be pressed. // If we are locked, and didn't time out yet, no key has to be pressed.
if (lock_start_time_ && (millis() - lock_start_time_ <= lock_length)) if (lock_start_time_ && (millis() - lock_start_time_ <= lock_length)) {
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
}
// If we are locked... // If we are locked...
if (lock_start_time_) { if (lock_start_time_) {
// ...and the lock has not expired yet // ...and the lock has not expired yet
if (millis() - lock_start_time_ <= lock_length) if (millis() - lock_start_time_ <= lock_length) {
return Key_NoKey; // remain locked return EventHandlerResult::EVENT_CONSUMED; // remain locked
}
// ...otherwise clear the lock // ...otherwise clear the lock
lock_start_time_ = 0; lock_start_time_ = 0;
@ -81,14 +76,14 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k
if (settings.left_hand_max_keys && left_hand_keys_ >= settings.left_hand_max_keys) { if (settings.left_hand_max_keys && left_hand_keys_ >= settings.left_hand_max_keys) {
lock_start_time_ = millis(); lock_start_time_ = millis();
TypingBreak(true); TypingBreak(true);
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
// If we have a limit on the right hand, and we reached it, lock up! // If we have a limit on the right hand, and we reached it, lock up!
if (settings.right_hand_max_keys && right_hand_keys_ >= settings.right_hand_max_keys) { if (settings.right_hand_max_keys && right_hand_keys_ >= settings.right_hand_max_keys) {
lock_start_time_ = millis(); lock_start_time_ = millis();
TypingBreak(true); TypingBreak(true);
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
if (lock_time_out) { if (lock_time_out) {
@ -97,7 +92,7 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k
// Yeah, it is. // Yeah, it is.
lock_start_time_ = last_key_time_; lock_start_time_ = last_key_time_;
TypingBreak(true); TypingBreak(true);
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
} }
@ -112,7 +107,7 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k
last_key_time_ = millis(); last_key_time_ = millis();
} }
return mapped_key; return EventHandlerResult::OK;
} }
void TypingBreaks::enableEEPROM(void) { void TypingBreaks::enableEEPROM(void) {
@ -194,6 +189,22 @@ bool TypingBreaks::focusHook(const char *command) {
EEPROM.put(settings_base_, settings); EEPROM.put(settings_base_, settings);
return true; return true;
} }
// Legacy V1 API
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void TypingBreaks::begin() {
Kaleidoscope.useEventHandlerHook(legacyEventHandler);
}
Key TypingBreaks::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) {
EventHandlerResult r = ::TypingBreaks.onKeyswitchEvent(mapped_key, row, col, key_state);
if (r == EventHandlerResult::OK)
return mapped_key;
return Key_NoKey;
}
#endif
} }
kaleidoscope::TypingBreaks TypingBreaks; kaleidoscope::TypingBreaks TypingBreaks;

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-TypingBreaks -- Enforced typing breaks * Kaleidoscope-TypingBreaks -- Enforced typing breaks
* Copyright (C) 2017 Gergely Nagy * Copyright (C) 2017, 2018 Gergely Nagy
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -22,11 +22,9 @@
namespace kaleidoscope { namespace kaleidoscope {
class TypingBreaks : public KaleidoscopePlugin { class TypingBreaks : public kaleidoscope::Plugin {
public: public:
TypingBreaks(void); TypingBreaks(void) {}
void begin(void) final;
static void enableEEPROM(void); static void enableEEPROM(void);
static bool focusHook(const char *command); static bool focusHook(const char *command);
@ -41,6 +39,14 @@ class TypingBreaks : public KaleidoscopePlugin {
static settings_t settings; static settings_t settings;
EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state);
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
protected:
void begin();
static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state);
#endif
private: private:
static uint32_t session_start_time_; static uint32_t session_start_time_;
static uint32_t lock_start_time_; static uint32_t lock_start_time_;
@ -49,8 +55,6 @@ class TypingBreaks : public KaleidoscopePlugin {
static uint16_t right_hand_keys_; static uint16_t right_hand_keys_;
static uint16_t settings_base_; static uint16_t settings_base_;
static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state);
}; };
} }

Loading…
Cancel
Save