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: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:broken]: https://img.shields.io/badge/broken-X-black.png?style=flat&colorA=e05d44&colorB=494e52
[st:experimental]: https://img.shields.io/badge/experimental----black.png?style=flat&colorA=dfb317&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.svg?style=flat&colorA=e05d44&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
layer keys: instead of having to hold them while pressing other keys, they can
@ -38,9 +38,10 @@ plugin:
// somewhere in the keymap...
OSM(LeftControl), OSL(_FN)
void setup () {
Kaleidoscope.setup ();
USE_PLUGINS (&OneShot);
void setup() {
USE_PLUGINS(&OneShot);
Kaleidoscope.setup();
}
```
@ -65,7 +66,7 @@ There are two macros the plugin provides:
## Plugin methods
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()`
@ -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
> active, but not registered yet.
### `.cancel([withStickies])`
### `.cancel([with_stickies])`
> 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
> 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
> stickies.
### `.timeOut`
### `.time_out`
> The number of milliseconds to wait before timing out and 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.
> Set this property to the number of milliseconds to wait before timing out and
> cancelling the one-shot effect (unless interrupted or cancelled before by any
> other means).
>
> Defaults to 2500.
### `.holdTimeOut`
### `.hold_time_out`
> The number of milliseconds to wait before considering a held one-shot key as
> intentionally held. In this case, the one-shot effect will not trigger when
> the key is released. In other words, holding a one-shot 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.
> Set this property to the number of milliseconds to wait before considering a
> held one-shot key as intentionally held. In this case, the one-shot effect
> will not trigger when the key is released. In other words, holding a one-shot
> key at least this long, and then releasing it, will not trigger the one-shot
> effect.
>
> Defaults to 200.

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

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

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

Loading…
Cancel
Save