Kaleidoscope Style Guide conformance

...and an update to use the new Ranges APIs.

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

@ -5,9 +5,9 @@
[travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-Syster.svg?branch=master [travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-Syster.svg?branch=master
[travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-Syster [travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-Syster
[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
Syster is a way to input symbols in a different way: instead of macros, Leader Syster is a way to input symbols in a different way: instead of macros, Leader
sequences or the like, we trigger the special input mode, and enter the symbol's sequences or the like, we trigger the special input mode, and enter the symbol's
@ -28,18 +28,18 @@ will handle the symbol actions:
#include <Kaleidoscope-Syster.h> #include <Kaleidoscope-Syster.h>
#include <Kaleidoscope-Unicode.h> #include <Kaleidoscope-Unicode.h>
void systerAction (KaleidoscopePlugins::Syster::action_t action, const char *symbol) { void systerAction(kaleidoscope::Syster::action_t action, const char *symbol) {
switch (action) { switch (action) {
case KaleidoscopePlugins::Syster::StartAction: case kaleidoscope::Syster::StartAction:
Unicode.type (0x2328); Unicode.type (0x2328);
break; break;
case KaleidoscopePlugins::Syster::EndAction: case kaleidoscope::Syster::EndAction:
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);
Keyboard.sendReport (); Keyboard.sendReport ();
break; break;
case KaleidoscopePlugins::Syster::SymbolAction: case kaleidoscope::Syster::SymbolAction:
Serial.print ("systerAction: symbol="); Serial.print ("systerAction: symbol=");
Serial.println (symbol); Serial.println (symbol);
if (strcmp (symbol, "coffee") == 0) { if (strcmp (symbol, "coffee") == 0) {
@ -49,11 +49,12 @@ void systerAction (KaleidoscopePlugins::Syster::action_t action, const char *sym
} }
} }
void setup () { void setup() {
Serial.begin (9600); Serial.begin(9600);
USE_PLUGINS(&Unicode, &Syster);
Kaleidoscope.setup (KEYMAP_SIZE); Kaleidoscope.setup ();
Kaleidoscope.use (&Unicode, &Syster, NULL);
} }
``` ```
@ -70,20 +71,18 @@ methods outside of the object, however, that can be overridden:
> Called whenever an action needs to be taken, which can happen in three cases: > Called whenever an action needs to be taken, which can happen in three cases:
> First, when the `Syster` key is pressed, and the alternate processing starts. > First, when the `Syster` key is pressed, and the alternate processing starts.
> In this case, `action` will be set to > In this case, `action` will be set to `kaleidoscope::Syster::StartAction`, and
> `KaleidoscopePlugins::Syster::StartAction`, and `symbol` will be `NULL`. This > `symbol` will be `NULL`. This function can be used to do some setup to make it
> function can be used to do some setup to make it more obvious that the Syster > more obvious that the Syster input mode is active, such as sending a Unicode
> input mode is active, such as sending a Unicode symbol to the host, or > symbol to the host, or lighting up LEDs, or anything else we'd like.
> lighting up LEDs, or anything else we'd like.
> >
> Second, when the sequence is finished with a `Space`. In this case, `action` > Second, when the sequence is finished with a `Space`. In this case, `action`
> will be set to `KaleidoscopePlugins::Syster::EndAction`, and `symbol` will be > will be set to `kaleidoscope::Syster::EndAction`, and `symbol` will be `NULL`.
> `NULL`. This can be used to undo anything that the start action did, if need > This can be used to undo anything that the start action did, if need be.
> be.
> >
> Third, when the action for the symbol should be made. In this case, `action` > Third, when the action for the symbol should be made. In this case, `action`
> is set to `KaleidoscopePlugins::Syster::SymbolAction`, and `symbol` will be a > is set to `kaleidoscope::Syster::SymbolAction`, and `symbol` will be a C
> C string. It is up to us, what we do with this information, how we handle it. > string. It is up to us, what we do with this information, how we handle it.
### `keyToChar(key)` ### `keyToChar(key)`

@ -39,22 +39,21 @@ 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,
SYSTER SYSTER),
),
}; };
void systerAction(KaleidoscopePlugins::Syster::action_t action, const char *symbol) { void systerAction(kaleidoscope::Syster::action_t action, const char *symbol) {
switch (action) { switch (action) {
case KaleidoscopePlugins::Syster::StartAction: case kaleidoscope::Syster::StartAction:
Unicode.type(0x2328); Unicode.type(0x2328);
break; break;
case KaleidoscopePlugins::Syster::EndAction: case kaleidoscope::Syster::EndAction:
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);
Keyboard.sendReport(); Keyboard.sendReport();
break; break;
case KaleidoscopePlugins::Syster::SymbolAction: case kaleidoscope::Syster::SymbolAction:
Serial.print("systerAction: symbol="); Serial.print("systerAction: symbol=");
Serial.println(symbol); Serial.println(symbol);
if (strcmp(symbol, "coffee") == 0) { if (strcmp(symbol, "coffee") == 0) {
@ -65,8 +64,9 @@ void systerAction(KaleidoscopePlugins::Syster::action_t action, const char *symb
} }
void setup() { void setup() {
Kaleidoscope.setup(KEYMAP_SIZE); USE_PLUGINS(&Unicode, &Syster);
Kaleidoscope.use(&Unicode, &Syster, NULL);
Kaleidoscope.setup();
} }
void loop() { void loop() {

@ -18,61 +18,58 @@
#include <Kaleidoscope-Syster.h> #include <Kaleidoscope-Syster.h>
using namespace KaleidoscopePlugins::Ranges; #undef SYSTER
namespace kaleidoscope {
namespace KaleidoscopePlugins {
// --- state --- // --- state ---
char Syster::symbol[SYSTER_MAX_SYMBOL_LENGTH + 1]; char Syster::symbol_[SYSTER_MAX_SYMBOL_LENGTH + 1];
uint8_t Syster::symbolPos; uint8_t Syster::symbol_pos_;
bool Syster::isActive; bool Syster::is_active_;
// --- helpers --- // --- helpers ---
#define isSyster(k) (k == kaleidoscope::ranges::SYSTER)
#define isSyster(k) (k == SYSTER)
// --- api --- // --- api ---
Syster::Syster(void) { Syster::Syster(void) {
} }
void void Syster::begin(void) {
Syster::begin(void) { event_handler_hook_use(eventHandlerHook);
event_handler_hook_use(this->eventHandlerHook);
} }
void void Syster::reset(void) {
Syster::reset(void) { symbol_pos_ = 0;
symbolPos = 0; symbol_[0] = 0;
symbol[0] = 0; is_active_ = false;
isActive = false;
} }
// --- hooks --- // --- hooks ---
Key Key Syster::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
Syster::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) { if (!is_active_) {
if (!isActive) { if (!isSyster(mapped_key))
if (!isSyster(mappedKey)) return mapped_key;
return mappedKey;
if (key_toggled_on(key_state)) {
if (key_toggled_on(keyState)) { is_active_ = true;
isActive = true;
systerAction(StartAction, NULL); systerAction(StartAction, NULL);
} }
return Key_NoKey; return Key_NoKey;
} }
if (keyState & INJECTED) if (key_state & INJECTED)
return mappedKey; return mapped_key;
if (isSyster(mappedKey)) if (isSyster(mapped_key))
return Key_NoKey; return Key_NoKey;
if (mappedKey == Key_Backspace && symbolPos == 0) if (mapped_key == Key_Backspace && symbol_pos_ == 0)
return Key_NoKey; return Key_NoKey;
if (key_toggled_off(keyState)) { if (key_toggled_off(key_state)) {
if (mappedKey == Key_Spacebar) { if (mapped_key == Key_Spacebar) {
for (uint8_t i = 0; i <= symbolPos; i++) { for (uint8_t i = 0; i <= symbol_pos_; i++) {
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);
@ -81,32 +78,31 @@ Syster::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) {
systerAction(EndAction, NULL); systerAction(EndAction, NULL);
symbol[symbolPos] = 0; symbol_[symbol_pos_] = 0;
systerAction(SymbolAction, symbol); systerAction(SymbolAction, symbol_);
reset(); reset();
return Key_NoKey; return Key_NoKey;
} }
} }
if (key_toggled_on(keyState)) { if (key_toggled_on(key_state)) {
if (mappedKey == Key_Backspace) { if (mapped_key == Key_Backspace) {
if (symbolPos > 0) if (symbol_pos_ > 0)
symbolPos--; symbol_pos_--;
} else { } else {
const char c = keyToChar(mappedKey); const char c = keyToChar(mapped_key);
if (c) if (c)
symbol[symbolPos++] = c; symbol_[symbol_pos_++] = c;
} }
} }
return mappedKey; return mapped_key;
}
} }
};
__attribute__((weak)) __attribute__((weak)) const char keyToChar(Key key) {
const char
keyToChar(Key key) {
if (key.flags != 0) if (key.flags != 0)
return 0; return 0;
@ -120,9 +116,7 @@ keyToChar(Key key) {
return 0; return 0;
} }
__attribute__((weak)) __attribute__((weak)) void systerAction(kaleidoscope::Syster::action_t action, const char *symbol) {
void
systerAction(KaleidoscopePlugins::Syster::action_t action, const char *symbol) {
} }
KaleidoscopePlugins::Syster Syster; kaleidoscope::Syster Syster;

@ -23,9 +23,10 @@
#define SYSTER_MAX_SYMBOL_LENGTH 32 #define SYSTER_MAX_SYMBOL_LENGTH 32
#define SYSTER (Key){ .raw = KaleidoscopePlugins::Ranges::SYSTER } #define SYSTER ((Key) { .raw = kaleidoscope::ranges::SYSTER })
namespace kaleidoscope {
namespace KaleidoscopePlugins {
class Syster : public KaleidoscopePlugin { class Syster : public KaleidoscopePlugin {
public: public:
typedef enum { typedef enum {
@ -40,15 +41,15 @@ class Syster : public KaleidoscopePlugin {
static void reset(void); static void reset(void);
private: private:
static char symbol[SYSTER_MAX_SYMBOL_LENGTH + 1]; static char symbol_[SYSTER_MAX_SYMBOL_LENGTH + 1];
static uint8_t symbolPos; static uint8_t symbol_pos_;
static bool isActive; static bool is_active_;
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);
}; };
}; };
const char keyToChar(Key key); const char keyToChar(Key key);
void systerAction(KaleidoscopePlugins::Syster::action_t action, const char *symbol); void systerAction(kaleidoscope::Syster::action_t action, const char *symbol);
extern KaleidoscopePlugins::Syster Syster; extern kaleidoscope::Syster Syster;

Loading…
Cancel
Save