Kaleidoscope Style Guide conformance

Also updated to use the new Ranges APIs, while there.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/389/head
Gergely Nagy 8 years ago
parent 6be3cc0010
commit f79b3b5f7d

@ -5,9 +5,9 @@
[travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-OneShot.svg?branch=master [travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-OneShot.svg?branch=master
[travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-OneShot [travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-OneShot
[st:stable]: https://img.shields.io/badge/stable-✔-black.png?style=flat&colorA=44cc11&colorB=494e52 [st:stable]: https://img.shields.io/badge/stable-✔-black.svg?style=flat&colorA=44cc11&colorB=494e52
[st:broken]: https://img.shields.io/badge/broken-X-black.png?style=flat&colorA=e05d44&colorB=494e52 [st:broken]: https://img.shields.io/badge/broken-X-black.svg?style=flat&colorA=e05d44&colorB=494e52
[st:experimental]: https://img.shields.io/badge/experimental----black.png?style=flat&colorA=dfb317&colorB=494e52 [st:experimental]: https://img.shields.io/badge/experimental----black.svg?style=flat&colorA=dfb317&colorB=494e52
One-shots are a new kind of behaviour for your standard modifier and momentary One-shots are a new kind of behaviour for your standard modifier and momentary
layer keys: instead of having to hold them while pressing other keys, they can layer keys: instead of having to hold them while pressing other keys, they can
@ -39,8 +39,9 @@ plugin:
OSM(LeftControl), OSL(_FN) OSM(LeftControl), OSL(_FN)
void setup() { void setup() {
Kaleidoscope.setup ();
USE_PLUGINS(&OneShot); USE_PLUGINS(&OneShot);
Kaleidoscope.setup();
} }
``` ```
@ -65,7 +66,7 @@ There are two macros the plugin provides:
## Plugin methods ## Plugin methods
The plugin provides one object, `OneShot`, which implements both one-shot The plugin provides one object, `OneShot`, which implements both one-shot
modifiers and one-shot layer keys. It has the following methods: modifiers and one-shot layer keys. It has the following methods and properties:
### `.isActive()` ### `.isActive()`
@ -81,33 +82,31 @@ modifiers and one-shot layer keys. It has the following methods:
> with `Keyboard.isModifierActive` to catch cases where a one-shot modifier is > with `Keyboard.isModifierActive` to catch cases where a one-shot modifier is
> active, but not registered yet. > active, but not registered yet.
### `.cancel([withStickies])` ### `.cancel([with_stickies])`
> The `cancel()` method can be used to cancel any pending one-shot effects, > The `cancel()` method can be used to cancel any pending one-shot effects,
> useful when one changed their minds, and does not wish to wait for the > useful when one changed their minds, and does not wish to wait for the
> timeout. > timeout.
> >
> The optional `withStickies` argument, if set to `true`, will also cancel > The optional `with_stickies` argument, if set to `true`, will also cancel
> sticky one-shot effects. If omitted, it defaults to `false`, and not canceling > sticky one-shot effects. If omitted, it defaults to `false`, and not canceling
> stickies. > stickies.
### `.timeOut` ### `.time_out`
> The number of milliseconds to wait before timing out and cancelling the > Set this property to the number of milliseconds to wait before timing out and
> one-shot effect, unless interrupted or cancelled before by any other means. > cancelling the one-shot effect (unless interrupted or cancelled before by any
> > other means).
> Not strictly a method, it is a variable one can assign a new value to.
> >
> Defaults to 2500. > Defaults to 2500.
### `.holdTimeOut` ### `.hold_time_out`
> The number of milliseconds to wait before considering a held one-shot key as > Set this property to the number of milliseconds to wait before considering a
> intentionally held. In this case, the one-shot effect will not trigger when > held one-shot key as intentionally held. In this case, the one-shot effect
> the key is released. In other words, holding a one-shot key at least this > will not trigger when the key is released. In other words, holding a one-shot
> long, and then releasing it, will not trigger the one-shot effect. > key at least this long, and then releasing it, will not trigger the one-shot
> > effect.
> Not strictly a method, it is a variable one can assign a new value to.
> >
> Defaults to 200. > Defaults to 200.

@ -58,9 +58,9 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
}; };
void setup() { void setup() {
Kaleidoscope.setup();
USE_PLUGINS(&OneShot); USE_PLUGINS(&OneShot);
Kaleidoscope.setup();
} }
void loop() { void loop() {

@ -18,49 +18,47 @@
#include <Kaleidoscope-OneShot.h> #include <Kaleidoscope-OneShot.h>
using namespace KaleidoscopePlugins::Ranges; namespace kaleidoscope {
namespace KaleidoscopePlugins {
// ---- state --------- // ---- state ---------
uint32_t OneShot::startTime = 0; uint32_t OneShot::start_time_ = 0;
uint16_t OneShot::timeOut = 2500; uint16_t OneShot::time_out = 2500;
uint16_t OneShot::holdTimeOut = 250; uint16_t OneShot::hold_time_out = 250;
OneShot::state_t OneShot::State; OneShot::state_t OneShot::state_;
OneShot::state_t OneShot::stickyState; OneShot::state_t OneShot::sticky_state_;
OneShot::state_t OneShot::pressedState; OneShot::state_t OneShot::pressed_state_;
uint32_t OneShot::leftMask; uint32_t OneShot::left_mask_;
uint32_t OneShot::rightMask; uint32_t OneShot::right_mask_;
Key OneShot::prevKey; Key OneShot::prev_key_;
bool OneShot::shouldCancel = false; bool OneShot::should_cancel_ = false;
bool OneShot::shouldCancelStickies = false; bool OneShot::should_cancel_stickies_ = false;
// --- helper macros ------ // --- helper macros ------
#define isOS(key) (key.raw >= OS_FIRST && key.raw <= OS_LAST) #define isOS(key) (key.raw >= ranges::OS_FIRST && key.raw <= ranges::OS_LAST)
#define isModifier(key) (key.raw >= Key_LeftControl.raw && key.raw <= Key_RightGui.raw) #define isModifier(key) (key.raw >= Key_LeftControl.raw && key.raw <= Key_RightGui.raw)
#define isLayerKey(key) (key.flags == (KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP) && key.keyCode >= MOMENTARY_OFFSET && key.keyCode <= MOMENTARY_OFFSET + 23) #define isLayerKey(key) (key.flags == (KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP) && key.keyCode >= MOMENTARY_OFFSET && key.keyCode <= MOMENTARY_OFFSET + 23)
#define isOneShot(idx) (bitRead (State.all, (idx))) #define isOneShot(idx) (bitRead (state_.all, (idx)))
#define setOneShot(idx) (bitWrite (State.all, idx, 1)) #define setOneShot(idx) (bitWrite (state_.all, idx, 1))
#define clearOneShot(idx) (bitWrite (State.all, idx, 0)) #define clearOneShot(idx) (bitWrite (state_.all, idx, 0))
#define isSticky(idx) (bitRead (stickyState.all, idx)) #define isSticky(idx) (bitRead (sticky_state_.all, idx))
#define setSticky(idx) (bitWrite (stickyState.all, idx, 1)) #define setSticky(idx) (bitWrite (sticky_state_.all, idx, 1))
#define clearSticky(idx) bitWrite (stickyState.all, idx, 0) #define clearSticky(idx) bitWrite (sticky_state_.all, idx, 0)
#define setPressed(idx) bitWrite(pressedState.all, idx, 1) #define setPressed(idx) bitWrite(pressed_state_.all, idx, 1)
#define clearPressed(idx) bitWrite(pressedState.all, idx, 0) #define clearPressed(idx) bitWrite(pressed_state_.all, idx, 0)
#define isPressed(idx) bitRead (pressedState.all, idx) #define isPressed(idx) bitRead (pressed_state_.all, idx)
#define isSameAsPrevious(key) (key.raw == prevKey.raw) #define isSameAsPrevious(key) (key.raw == prev_key_.raw)
#define saveAsPrevious(key) prevKey.raw = key.raw #define saveAsPrevious(key) prev_key_.raw = key.raw
#define hasTimedOut() (millis () - startTime >= timeOut) #define hasTimedOut() (millis () - start_time_ >= time_out)
// ---- OneShot stuff ---- // ---- OneShot stuff ----
void void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) {
OneShot::injectNormalKey(uint8_t idx, uint8_t keyState) {
Key key; Key key;
if (idx < 8) { if (idx < 8) {
@ -71,85 +69,79 @@ OneShot::injectNormalKey(uint8_t idx, uint8_t keyState) {
key.keyCode = MOMENTARY_OFFSET + idx - 8; key.keyCode = MOMENTARY_OFFSET + idx - 8;
} }
handle_keyswitch_event(key, UNKNOWN_KEYSWITCH_LOCATION, keyState | INJECTED); handle_keyswitch_event(key, UNKNOWN_KEYSWITCH_LOCATION, key_state | INJECTED);
} }
void void OneShot::activateOneShot(uint8_t idx) {
OneShot::activateOneShot(uint8_t idx) {
injectNormalKey(idx, IS_PRESSED); injectNormalKey(idx, IS_PRESSED);
} }
void void OneShot::cancelOneShot(uint8_t idx) {
OneShot::cancelOneShot(uint8_t idx) {
clearOneShot(idx); clearOneShot(idx);
injectNormalKey(idx, WAS_PRESSED); injectNormalKey(idx, WAS_PRESSED);
} }
void void OneShot::mask(byte row, byte col) {
OneShot::mask(byte row, byte col) {
if (row >= ROWS || col >= COLS) if (row >= ROWS || col >= COLS)
return; return;
if (col >= 8) { if (col >= 8) {
col = col - 8; col = col - 8;
rightMask |= SCANBIT(row, col); right_mask_ |= SCANBIT(row, col);
} else { } else {
leftMask |= SCANBIT(row, col); left_mask_ |= SCANBIT(row, col);
} }
} }
void void OneShot::unmask(byte row, byte col) {
OneShot::unmask(byte row, byte col) {
if (row >= ROWS || col >= COLS) if (row >= ROWS || col >= COLS)
return; return;
if (col >= 8) { if (col >= 8) {
col = col - 8; col = col - 8;
rightMask &= ~(SCANBIT(row, col)); right_mask_ &= ~(SCANBIT(row, col));
} else { } else {
leftMask &= ~(SCANBIT(row, col)); left_mask_ &= ~(SCANBIT(row, col));
} }
} }
bool bool OneShot::isMasked(byte row, byte col) {
OneShot::isMasked(byte row, byte col) {
if (row >= ROWS || col >= COLS) if (row >= ROWS || col >= COLS)
return false; return false;
if (col >= 8) { if (col >= 8) {
col = col - 8; col = col - 8;
return rightMask & SCANBIT(row, col); return right_mask_ & SCANBIT(row, col);
} else { } else {
return leftMask & SCANBIT(row, col); return left_mask_ & SCANBIT(row, col);
} }
} }
Key Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
OneShot::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) {
uint8_t idx; uint8_t idx;
if (keyState & INJECTED) if (key_state & INJECTED)
return mappedKey; return mapped_key;
if (!State.all) { if (!state_.all) {
if (!isOS(mappedKey)) { if (!isOS(mapped_key)) {
if (isMasked(row, col)) { if (isMasked(row, col)) {
if (key_toggled_off(keyState)) if (key_toggled_off(key_state))
unmask(row, col); unmask(row, col);
return Key_NoKey; return Key_NoKey;
} }
return mappedKey; return mapped_key;
} }
idx = mappedKey.raw - OS_FIRST; idx = mapped_key.raw - ranges::OS_FIRST;
if (key_toggled_off(keyState)) { if (key_toggled_off(key_state)) {
clearPressed(idx); clearPressed(idx);
} else if (key_toggled_on(keyState)) { } else if (key_toggled_on(key_state)) {
startTime = millis(); start_time_ = millis();
setPressed(idx); setPressed(idx);
setOneShot(idx); setOneShot(idx);
saveAsPrevious(mappedKey); saveAsPrevious(mapped_key);
activateOneShot(idx); activateOneShot(idx);
} }
@ -157,37 +149,37 @@ OneShot::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) {
return Key_NoKey; return Key_NoKey;
} }
if (!key_is_pressed(keyState) && !key_was_pressed(keyState)) if (!key_is_pressed(key_state) && !key_was_pressed(key_state))
return mappedKey; return mapped_key;
if (isOS(mappedKey)) { if (isOS(mapped_key)) {
idx = mappedKey.raw - OS_FIRST; idx = mapped_key.raw - ranges::OS_FIRST;
if (isSticky(idx)) { if (isSticky(idx)) {
if (key_toggled_on(keyState)) { // maybe on _off instead? if (key_toggled_on(key_state)) { // maybe on _off instead?
saveAsPrevious(mappedKey); saveAsPrevious(mapped_key);
clearSticky(idx); clearSticky(idx);
cancelOneShot(idx); cancelOneShot(idx);
} }
} else { } else {
if (key_toggled_off(keyState)) { if (key_toggled_off(key_state)) {
clearPressed(idx); clearPressed(idx);
if ((millis() - startTime) >= holdTimeOut) { if ((millis() - start_time_) >= hold_time_out) {
cancelOneShot(idx); cancelOneShot(idx);
} }
} }
if (key_toggled_on(keyState)) { if (key_toggled_on(key_state)) {
setPressed(idx); setPressed(idx);
if (isSameAsPrevious(mappedKey)) { if (isSameAsPrevious(mapped_key)) {
setSticky(idx); setSticky(idx);
saveAsPrevious(mappedKey); saveAsPrevious(mapped_key);
} else { } else {
startTime = millis(); start_time_ = millis();
setOneShot(idx); setOneShot(idx);
saveAsPrevious(mappedKey); saveAsPrevious(mapped_key);
activateOneShot(idx); activateOneShot(idx);
} }
@ -199,28 +191,27 @@ OneShot::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) {
// ordinary key here, with some event // ordinary key here, with some event
if (key_is_pressed(keyState)) { if (key_is_pressed(key_state)) {
mask(row, col); mask(row, col);
saveAsPrevious(mappedKey); saveAsPrevious(mapped_key);
shouldCancel = true; should_cancel_ = true;
} }
return mappedKey; return mapped_key;
} }
void void OneShot::loopHook(bool is_post_clear) {
OneShot::loopHook(bool postClear) { if (!state_.all)
if (!State.all)
return; return;
if (postClear) { if (is_post_clear) {
if (hasTimedOut()) if (hasTimedOut())
cancel(); cancel();
for (uint8_t i = 0; i < 32; i++) { for (uint8_t i = 0; i < 32; i++) {
if (shouldCancel) { if (should_cancel_) {
if (isSticky(i)) { if (isSticky(i)) {
if (shouldCancelStickies) { if (should_cancel_stickies_) {
clearSticky(i); clearSticky(i);
} }
} else if (isOneShot(i) && !isPressed(i)) { } else if (isOneShot(i) && !isPressed(i)) {
@ -229,9 +220,9 @@ OneShot::loopHook(bool postClear) {
} }
} }
if (shouldCancel) { if (should_cancel_) {
shouldCancel = false; should_cancel_ = false;
shouldCancelStickies = false; should_cancel_stickies_ = false;
} }
} else { } else {
for (uint8_t i = 0; i < 8; i++) { for (uint8_t i = 0; i < 8; i++) {
@ -247,36 +238,31 @@ OneShot::loopHook(bool postClear) {
OneShot::OneShot(void) { OneShot::OneShot(void) {
} }
void void OneShot::begin(void) {
OneShot::begin(void) {
event_handler_hook_use(eventHandlerHook); event_handler_hook_use(eventHandlerHook);
loop_hook_use(loopHook); loop_hook_use(loopHook);
} }
bool bool OneShot::isActive(void) {
OneShot::isActive(void) { return (state_.all && !hasTimedOut());
return (State.all && !hasTimedOut());
} }
bool bool OneShot::isModifierActive(Key key) {
OneShot::isModifierActive(Key key) {
if (key.raw < Key_LeftControl.raw || key.raw > Key_RightGui.raw) if (key.raw < Key_LeftControl.raw || key.raw > Key_RightGui.raw)
return false; return false;
return isOneShot(key.keyCode - Key_LeftControl.keyCode); return isOneShot(key.keyCode - Key_LeftControl.keyCode);
} }
void void OneShot::cancel(bool with_stickies) {
OneShot::cancel(bool withStickies) { should_cancel_ = true;
shouldCancel = true; should_cancel_stickies_ = with_stickies;
shouldCancelStickies = withStickies;
} }
void void OneShot::inject(Key key, uint8_t key_state) {
OneShot::inject(Key key, uint8_t keyState) { eventHandlerHook(key, UNKNOWN_KEYSWITCH_LOCATION, key_state);
eventHandlerHook(key, 255, 255, keyState);
} }
}; }
KaleidoscopePlugins::OneShot OneShot; kaleidoscope::OneShot OneShot;

@ -21,10 +21,11 @@
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-Ranges.h> #include <Kaleidoscope-Ranges.h>
#define OSM(kc) (Key) {.raw = KaleidoscopePlugins::Ranges::OSM_FIRST + (Key_ ## kc).keyCode - Key_LeftControl.keyCode} #define OSM(kc) (Key) {.raw = kaleidoscope::ranges::OSM_FIRST + (Key_ ## kc).keyCode - Key_LeftControl.keyCode}
#define OSL(n) (Key) {.raw = KaleidoscopePlugins::Ranges::OSL_FIRST + n} #define OSL(n) (Key) {.raw = kaleidoscope::ranges::OSL_FIRST + n}
namespace kaleidoscope {
namespace KaleidoscopePlugins {
class OneShot : public KaleidoscopePlugin { class OneShot : public KaleidoscopePlugin {
public: public:
OneShot(void); OneShot(void);
@ -32,16 +33,16 @@ class OneShot : public KaleidoscopePlugin {
void begin(void) final; void begin(void) final;
static bool isActive(void); static bool isActive(void);
static void cancel(bool withStickies); static void cancel(bool with_stickies);
static void cancel(void) { static void cancel(void) {
cancel(false); cancel(false);
} }
static uint16_t timeOut; static uint16_t time_out;
static uint16_t holdTimeOut; static uint16_t hold_time_out;
static bool isModifierActive(Key key); static bool isModifierActive(Key key);
void inject(Key key, uint8_t keyState); void inject(Key key, uint8_t key_state);
private: private:
typedef union { typedef union {
@ -51,17 +52,17 @@ class OneShot : public KaleidoscopePlugin {
}; };
uint16_t all; uint16_t all;
} state_t; } state_t;
static uint32_t startTime; static uint32_t start_time_;
static state_t State; static state_t state_;
static state_t stickyState; static state_t sticky_state_;
static state_t pressedState; static state_t pressed_state_;
static uint32_t leftMask; static uint32_t left_mask_;
static uint32_t rightMask; static uint32_t right_mask_;
static Key prevKey; static Key prev_key_;
static bool shouldCancel; static bool should_cancel_;
static bool shouldCancelStickies; static bool should_cancel_stickies_;
static void injectNormalKey(uint8_t idx, uint8_t keyState); static void injectNormalKey(uint8_t idx, uint8_t key_state);
static void activateOneShot(uint8_t idx); static void activateOneShot(uint8_t idx);
static void cancelOneShot(uint8_t idx); static void cancelOneShot(uint8_t idx);
@ -69,9 +70,10 @@ class OneShot : public KaleidoscopePlugin {
static void unmask(byte row, byte col); static void unmask(byte row, byte col);
static bool isMasked(byte row, byte col); static bool isMasked(byte row, byte col);
static Key eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState); static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state);
static void loopHook(bool postClear); static void loopHook(bool is_post_clear);
};
}; };
extern KaleidoscopePlugins::OneShot OneShot; }
extern kaleidoscope::OneShot OneShot;

Loading…
Cancel
Save