From 38cf866870a83d5495636f658f21f01e54ebd503 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 3 Jun 2017 09:06:35 +0200 Subject: [PATCH] Kaleidoscope Style Guide conformance Signed-off-by: Gergely Nagy --- README.md | 19 +++++---- examples/Cycle/Cycle.ino | 52 ++++++++++++------------ src/Kaleidoscope/Cycle.cpp | 81 +++++++++++++++++--------------------- src/Kaleidoscope/Cycle.h | 25 ++++++------ 4 files changed, 84 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index be34bc31..966c587f 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ [travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-Cycle.svg?branch=master [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: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 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 @@ -33,16 +33,15 @@ each time the cycling key triggers. Key_Cycle // later in the Sketch: -void cycleAction (Key previousKey, uint8_t cycleCount) { - if (previousKey.raw == Key_A.raw) { +void cycleAction(Key previous_key, uint8_t cycle_count) { + if (previous_key.raw == Key_A.raw) { cycleThrough (Key_B, Key_C, Key_D); } } -void setup (void) { - Kaleidoscope.setup (KEYMAP_SIZE); - - Kaleidoscope.use (&Cycle, NULL); +void setup(void) { + USE_PLUGINS(&Cycle); + Kaleidoscope.setup(); } ``` @@ -83,7 +82,7 @@ method explained below. ## 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 > be called whenever the cycling key triggers, and the two arguments are the diff --git a/examples/Cycle/Cycle.ino b/examples/Cycle/Cycle.ino index b515f344..b31fb593 100644 --- a/examples/Cycle/Cycle.ino +++ b/examples/Cycle/Cycle.ino @@ -21,44 +21,42 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED - ( - 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_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_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_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_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, - Key_Cycle, + Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, + Key_Cycle, - 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_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_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_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_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, - Key_Cycle - ), + Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, + Key_Cycle), }; -void cycleAction (Key previousKey, uint8_t cycleCount) { - if (previousKey.raw == Key_E.raw) { - if (cycleCount == 1) { - Cycle.replace (Key_F); - } else if (cycleCount == 2) { - Cycle.replace (Key_G); +void cycleAction(Key previous_key, uint8_t cycle_count) { + if (previous_key.raw == Key_E.raw) { + if (cycle_count == 1) { + Cycle.replace(Key_F); + } else if (cycle_count == 2) { + Cycle.replace(Key_G); } } - if (previousKey.raw == Key_A.raw) { - cycleThrough (Key_A, Key_B, Key_C, Key_D); + if (previous_key.raw == Key_A.raw) { + cycleThrough(Key_A, Key_B, Key_C, Key_D); } } -void setup () { - Kaleidoscope.setup (KEYMAP_SIZE); +void setup() { + USE_PLUGINS(&Cycle); - Kaleidoscope.use (&Cycle, NULL); + Kaleidoscope.setup(); } -void loop () { - Kaleidoscope.loop (); +void loop() { + Kaleidoscope.loop(); } diff --git a/src/Kaleidoscope/Cycle.cpp b/src/Kaleidoscope/Cycle.cpp index fa215e38..be1adf1d 100644 --- a/src/Kaleidoscope/Cycle.cpp +++ b/src/Kaleidoscope/Cycle.cpp @@ -19,82 +19,75 @@ #include #include -using namespace KaleidoscopePlugins::Ranges; - -namespace KaleidoscopePlugins { +namespace kaleidoscope { // --- state --- -Key Cycle::lastNonCycleKey; -uint8_t Cycle::cycleCount; +Key Cycle::last_non_cycle_key_; +uint8_t Cycle::cycle_count_; // --- helpers --- -#define isCycle(k) (k.raw == CYCLE) +#define isCycle(k) (k.raw == KaleidoscopePlugins::Ranges::CYCLE) // --- api --- -Cycle::Cycle (void) { +Cycle::Cycle(void) { } -void -Cycle::begin (void) { - event_handler_hook_use (this->eventHandlerHook); +void Cycle::begin(void) { + event_handler_hook_use(eventHandlerHook); } -void -Cycle::replace (Key key) { - handle_keyswitch_event (Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED); - Keyboard.sendReport (); - handle_keyswitch_event (Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, WAS_PRESSED | INJECTED); - Keyboard.sendReport (); - - handle_keyswitch_event (key, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED); - Keyboard.sendReport (); - handle_keyswitch_event (key, UNKNOWN_KEYSWITCH_LOCATION, WAS_PRESSED | INJECTED); - Keyboard.sendReport (); +void Cycle::replace(Key key) { + handle_keyswitch_event(Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED); + Keyboard.sendReport(); + handle_keyswitch_event(Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, WAS_PRESSED | INJECTED); + Keyboard.sendReport(); + + handle_keyswitch_event(key, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED); + Keyboard.sendReport(); + handle_keyswitch_event(key, UNKNOWN_KEYSWITCH_LOCATION, WAS_PRESSED | INJECTED); + Keyboard.sendReport(); } -void -Cycle::replace (uint8_t cycleSize, const Key cycleSteps[]) { - uint8_t idx = cycleCount % cycleSize; +void Cycle::replace(uint8_t cycle_size, const Key cycle_steps[]) { + uint8_t idx = cycle_count_ % cycle_size; Key key; - key.raw = pgm_read_word (cycleSteps + idx); - replace (key); + key.raw = pgm_read_word(cycle_steps + idx); + replace(key); } // --- hooks --- -Key -Cycle::eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState) { - if (keyState & INJECTED) - return mappedKey; +Key Cycle::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { + if (key_state & INJECTED) + return mapped_key; - if (!key_is_pressed (keyState) && !key_was_pressed (keyState)) { - if (isCycle (mappedKey)) + if (!key_is_pressed(key_state) && !key_was_pressed(key_state)) { + if (isCycle(mapped_key)) return Key_NoKey; - return mappedKey; + return mapped_key; } - if (!isCycle (mappedKey)) { - if (key_toggled_on (keyState)) { - lastNonCycleKey.raw = mappedKey.raw; - cycleCount = 0; + if (!isCycle(mapped_key)) { + if (key_toggled_on(key_state)) { + last_non_cycle_key_.raw = mapped_key.raw; + cycle_count_ = 0; } - return mappedKey; + return mapped_key; } - if (!key_toggled_off (keyState)) + if (!key_toggled_off(key_state)) return Key_NoKey; - cycleCount++; - cycleAction (lastNonCycleKey, cycleCount); + ++cycle_count_; + cycleAction(last_non_cycle_key_, cycle_count_); return Key_NoKey; } }; __attribute__((weak)) -void -cycleAction (Key previousKey, uint8_t cycleCount) { +void cycleAction(Key previous_key, uint8_t cycle_count) { } -KaleidoscopePlugins::Cycle Cycle; +kaleidoscope::Cycle Cycle; diff --git a/src/Kaleidoscope/Cycle.h b/src/Kaleidoscope/Cycle.h index 6289fa6e..ece5deb5 100644 --- a/src/Kaleidoscope/Cycle.h +++ b/src/Kaleidoscope/Cycle.h @@ -18,33 +18,34 @@ #pragma once +#include #include -#define Key_Cycle (Key){ .raw = KaleidoscopePlugins::Ranges::CYCLE } +#define Key_Cycle ((Key) { .raw = KaleidoscopePlugins::Ranges::CYCLE }) #define cycleThrough(...) ({ \ 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 { public: - Cycle (void); + Cycle(void); - virtual void begin (void) final; + void begin(void) final; - static void replace (Key key); - static void replace (uint8_t cycleSize, const Key cycleSteps[]); + static void replace(Key key); + static void replace(uint8_t cycle_size, const Key cycle_steps[]); private: - static Key lastNonCycleKey; - static uint8_t cycleCount; + static Key last_non_cycle_key_; + 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;