Kaleidoscope Style Guide conformance

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/389/head
Gergely Nagy 8 years ago
parent 14d236ed39
commit 38cf866870

@ -5,9 +5,9 @@
[travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-Cycle.svg?branch=master [travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-Cycle.svg?branch=master
[travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-Cycle [travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-Cycle
[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
If you ever wanted a key that works like keys on old cell phones, when you press If you ever wanted a key that works like keys on old cell phones, when you press
a key and it cycles through a number of options in a sequence, then the cycling a key and it cycles through a number of options in a sequence, then the cycling
@ -33,16 +33,15 @@ each time the cycling key triggers.
Key_Cycle Key_Cycle
// later in the Sketch: // later in the Sketch:
void cycleAction (Key previousKey, uint8_t cycleCount) { void cycleAction(Key previous_key, uint8_t cycle_count) {
if (previousKey.raw == Key_A.raw) { if (previous_key.raw == Key_A.raw) {
cycleThrough (Key_B, Key_C, Key_D); cycleThrough (Key_B, Key_C, Key_D);
} }
} }
void setup(void) { void setup(void) {
Kaleidoscope.setup (KEYMAP_SIZE); USE_PLUGINS(&Cycle);
Kaleidoscope.setup();
Kaleidoscope.use (&Cycle, NULL);
} }
``` ```
@ -83,7 +82,7 @@ method explained below.
## Overrideable methods ## Overrideable methods
### `cycleAction(previousKey, cycleCount)` ### `cycleAction(previous_key, cycle_count)`
> The heart and soul of the plugin, that must be defined in the Sketch. It will > The heart and soul of the plugin, that must be defined in the Sketch. It will
> be called whenever the cycling key triggers, and the two arguments are the > be called whenever the cycling key triggers, and the two arguments are the

@ -21,8 +21,7 @@
const Key keymaps[][ROWS][COLS] PROGMEM = { const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
( (Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey,
Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey,
Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab,
Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G,
Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape,
@ -36,27 +35,26 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
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_Cycle Key_Cycle),
),
}; };
void cycleAction (Key previousKey, uint8_t cycleCount) { void cycleAction(Key previous_key, uint8_t cycle_count) {
if (previousKey.raw == Key_E.raw) { if (previous_key.raw == Key_E.raw) {
if (cycleCount == 1) { if (cycle_count == 1) {
Cycle.replace(Key_F); Cycle.replace(Key_F);
} else if (cycleCount == 2) { } else if (cycle_count == 2) {
Cycle.replace(Key_G); Cycle.replace(Key_G);
} }
} }
if (previousKey.raw == Key_A.raw) { if (previous_key.raw == Key_A.raw) {
cycleThrough(Key_A, Key_B, Key_C, Key_D); cycleThrough(Key_A, Key_B, Key_C, Key_D);
} }
} }
void setup() { void setup() {
Kaleidoscope.setup (KEYMAP_SIZE); USE_PLUGINS(&Cycle);
Kaleidoscope.use (&Cycle, NULL); Kaleidoscope.setup();
} }
void loop() { void loop() {

@ -19,29 +19,25 @@
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-Cycle.h> #include <Kaleidoscope-Cycle.h>
using namespace KaleidoscopePlugins::Ranges; namespace kaleidoscope {
namespace KaleidoscopePlugins {
// --- state --- // --- state ---
Key Cycle::lastNonCycleKey; Key Cycle::last_non_cycle_key_;
uint8_t Cycle::cycleCount; uint8_t Cycle::cycle_count_;
// --- helpers --- // --- helpers ---
#define isCycle(k) (k.raw == CYCLE) #define isCycle(k) (k.raw == KaleidoscopePlugins::Ranges::CYCLE)
// --- api --- // --- api ---
Cycle::Cycle(void) { Cycle::Cycle(void) {
} }
void void Cycle::begin(void) {
Cycle::begin (void) { event_handler_hook_use(eventHandlerHook);
event_handler_hook_use (this->eventHandlerHook);
} }
void void Cycle::replace(Key key) {
Cycle::replace (Key key) {
handle_keyswitch_event(Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED); handle_keyswitch_event(Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED);
Keyboard.sendReport(); Keyboard.sendReport();
handle_keyswitch_event(Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, WAS_PRESSED | INJECTED); handle_keyswitch_event(Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, WAS_PRESSED | INJECTED);
@ -53,48 +49,45 @@ Cycle::replace (Key key) {
Keyboard.sendReport(); Keyboard.sendReport();
} }
void void Cycle::replace(uint8_t cycle_size, const Key cycle_steps[]) {
Cycle::replace (uint8_t cycleSize, const Key cycleSteps[]) { uint8_t idx = cycle_count_ % cycle_size;
uint8_t idx = cycleCount % cycleSize;
Key key; Key key;
key.raw = pgm_read_word (cycleSteps + idx); key.raw = pgm_read_word(cycle_steps + idx);
replace(key); replace(key);
} }
// --- hooks --- // --- hooks ---
Key Key Cycle::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
Cycle::eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState) { if (key_state & INJECTED)
if (keyState & INJECTED) return mapped_key;
return mappedKey;
if (!key_is_pressed (keyState) && !key_was_pressed (keyState)) { if (!key_is_pressed(key_state) && !key_was_pressed(key_state)) {
if (isCycle (mappedKey)) if (isCycle(mapped_key))
return Key_NoKey; return Key_NoKey;
return mappedKey; return mapped_key;
} }
if (!isCycle (mappedKey)) { if (!isCycle(mapped_key)) {
if (key_toggled_on (keyState)) { if (key_toggled_on(key_state)) {
lastNonCycleKey.raw = mappedKey.raw; last_non_cycle_key_.raw = mapped_key.raw;
cycleCount = 0; cycle_count_ = 0;
} }
return mappedKey; return mapped_key;
} }
if (!key_toggled_off (keyState)) if (!key_toggled_off(key_state))
return Key_NoKey; return Key_NoKey;
cycleCount++; ++cycle_count_;
cycleAction (lastNonCycleKey, cycleCount); cycleAction(last_non_cycle_key_, cycle_count_);
return Key_NoKey; return Key_NoKey;
} }
}; };
__attribute__((weak)) __attribute__((weak))
void void cycleAction(Key previous_key, uint8_t cycle_count) {
cycleAction (Key previousKey, uint8_t cycleCount) {
} }
KaleidoscopePlugins::Cycle Cycle; kaleidoscope::Cycle Cycle;

@ -18,33 +18,34 @@
#pragma once #pragma once
#include <Kaleidoscope.h>
#include <Kaleidoscope-Ranges.h> #include <Kaleidoscope-Ranges.h>
#define Key_Cycle (Key){ .raw = KaleidoscopePlugins::Ranges::CYCLE } #define Key_Cycle ((Key) { .raw = KaleidoscopePlugins::Ranges::CYCLE })
#define cycleThrough(...) ({ \ #define cycleThrough(...) ({ \
static const Key __k[] PROGMEM = { __VA_ARGS__ }; \ static const Key __k[] PROGMEM = { __VA_ARGS__ }; \
Cycle.replace(sizeof(__k) / sizeof(Key), &__k[0]); \ Cycle.replace(sizeof(__k) / sizeof(Key), &__k[0]); \
}) })
namespace KaleidoscopePlugins { namespace kaleidoscope {
class Cycle : public KaleidoscopePlugin { class Cycle : public KaleidoscopePlugin {
public: public:
Cycle(void); Cycle(void);
virtual void begin (void) final; void begin(void) final;
static void replace(Key key); static void replace(Key key);
static void replace (uint8_t cycleSize, const Key cycleSteps[]); static void replace(uint8_t cycle_size, const Key cycle_steps[]);
private: private:
static Key lastNonCycleKey; static Key last_non_cycle_key_;
static uint8_t cycleCount; static uint8_t cycle_count_;
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);
}; };
}; };
void cycleAction (Key previousKey, uint8_t cycleCount); void cycleAction(Key previous_key, uint8_t cycle_count);
extern KaleidoscopePlugins::Cycle Cycle; extern kaleidoscope::Cycle Cycle;

Loading…
Cancel
Save