Kaleidoscope Style Guide conformance

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/365/head
Gergely Nagy 8 years ago
parent 7da1fe702a
commit e8f3495f7c

@ -5,9 +5,9 @@
[travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-MagicCombo.svg?branch=master [travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-MagicCombo.svg?branch=master
[travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-MagicCombo [travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-MagicCombo
[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
The `MagicCombo` extension provides a way to perform custom actions when a The `MagicCombo` extension provides a way to perform custom actions when a
particular set of keys are held down together. The functionality assigned to particular set of keys are held down together. The functionality assigned to
@ -18,54 +18,67 @@ This can be used to tie complex actions to key chords.
## Using the extension ## Using the extension
To use the extension, we must include the header, create a dictionary, and To use the extension, we must include the header, create an array of combos we
configure the plugin to use it: want to work with, let the plugin know we want to work with those, and then use
a special function to handle the combos:
```c++ ```c++
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-MagicCombo.h> #include <Kaleidoscope-MagicCombo.h>
static const KaleidoscopePlugins::MagicCombo::dictionary_t dictionary[] PROGMEM = { static const KaleidoscopePlugins::MagicCombo::combo_t magic_combos[] PROGMEM = {
{R1C3 | R2C1 | R2C4 | R2C7, // left hand, {R1C3 | R2C1 | R2C4 | R2C7, // left hand,
R0C11 | R1C12 | R2C14 //right hand R0C11 | R1C12 | R2C14 //right hand
}, },
{0, 0} {0, 0}
}; };
void setup (void) { void magicComboActions(uint8_t combo_index, uint32_t left_hand, uint32_t right_hand) {
MagicCombo.configure (dictionary); switch (combo_index) {
case 0:
Serial.println("It's a kind of magic!");
break;
}
}
void setup() {
Serial.begin(9600);
USE_PLUGINS(&MagicCombo);
Kaleidoscope.setup (KEYMAP_SIZE); Kaleidoscope.setup();
Kaleidoscope.use (&MagicCombo, NULL);
MagicCombo.magic_combos = magic_combos;
} }
``` ```
The dictionary **must** reside in `PROGMEM`, and is a list of tuples. Each The combo list **must** reside in `PROGMEM`, and is a list of tuples. Each
element in the array has two fields: the left hand state, and the right hand element in the array has two fields: the left hand state, and the right hand
state upon which to trigger the custom action. Both of these are bit fields, state upon which to trigger the custom action. Both of these are bit fields,
each bit set tells the extension that the key with that index must be held for each bit set tells the extension that the key with that index must be held for
the action to trigger. It is recommended to use the `RxCy` macros of the core the action to trigger. It is recommended to use the `RxCy` macros of the core
`KaleidoscopeFirmware`, and *or* them together to form a bitfield. `KaleidoscopeFirmware`, and *or* them together to form a bitfield.
The dictionary **must** end with an element containing zero values for both the The combo list **must** end with an element containing zero values for both the
left and the right halves. left and the right halves.
## Extension methods ## Extension methods
The extension provides a `MagicCombo` singleton object, with the following method: The extension provides a `MagicCombo` singleton object, with the following
methods and properties:
### `.configure(dictionary)` ### `.magic_combos`
> Configures the extension to use the supplied dictionary. > Setting this property lets the plugin know which combinations of key presses
> we are interested in. If any of these are found active, the
> `magicComboActions()` function will be called.
### `.minInterval` ### `.min_interval`
> Restrict the magic action to fire at most once every `minInterval` > Restrict the magic action to fire at most once every `minInterval`
> milliseconds. > milliseconds.
> >
> Defaults to 500. > Defaults to 500.
>
> Not strictly a method, it is a variable one can assign a new value to.
## Overrideable methods ## Overrideable methods
@ -73,12 +86,12 @@ Whenever an combination is found to be held, the extension will trigger an
action, in each scan cycle until the keys remain held. This is done by calling action, in each scan cycle until the keys remain held. This is done by calling
the overrideable `magicComboActions` function: the overrideable `magicComboActions` function:
### `magicComboActions(comboIndex, leftHand, rightHand)` ### `magicComboActions(combo_index, left_hand, right_hand)`
> Called whenever a combination is found to be held. The function by default > Called whenever a combination is found to be held. The function by default
> does nothing, and it is recommended to override it from within the Sketch. > does nothing, and it is recommended to override it from within the Sketch.
> >
> The first argument will be the index in the dictionary, the other two are the > The first argument will be the index in the combo list, the other two are the
> key states on the left and right halves, respectively. > key states on the left and right halves, respectively.
> >
> Plugins that build upon this extensions *should not* override this function, > Plugins that build upon this extensions *should not* override this function,

@ -19,18 +19,18 @@
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-MagicCombo.h> #include <Kaleidoscope-MagicCombo.h>
void magicComboActions(uint8_t comboIndex, uint32_t leftHand, uint32_t rightHand) { void magicComboActions(uint8_t combo_index, uint32_t left_hand, uint32_t right_hand) {
switch (comboIndex) { switch (combo_index) {
case 0: case 0:
Serial.println("It's a kind of magic!"); Serial.println("It's a kind of magic!");
break; break;
} }
} }
static const KaleidoscopePlugins::MagicCombo::dictionary_t dictionary[] PROGMEM = { static const kaleidoscope::MagicCombo::combo_t magic_combos[] PROGMEM = {
{ {
R1C3 | R2C1 | R2C4 | R2C7, // left hand, R1C3 | R2C1 | R2C4 | R2C7, // left hand,
R0C11 | R1C12 | R2C14 //right hand R0C11 | R1C12 | R2C14 // right hand
}, },
{0, 0} {0, 0}
}; };
@ -53,17 +53,17 @@ 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_NoKey Key_NoKey),
),
}; };
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
MagicCombo.configure(dictionary); USE_PLUGINS(&MagicCombo);
Kaleidoscope.setup(KEYMAP_SIZE); Kaleidoscope.setup();
Kaleidoscope.use(&MagicCombo, NULL);
MagicCombo.magic_combos = magic_combos;
} }
void loop() { void loop() {

@ -28,44 +28,37 @@
#define RIGHTHANDSTATE KeyboardHardware.scanner.rightHandState #define RIGHTHANDSTATE KeyboardHardware.scanner.rightHandState
#endif #endif
namespace KaleidoscopePlugins { namespace kaleidoscope {
const MagicCombo::dictionary_t *MagicCombo::dictionary; const MagicCombo::combo_t *MagicCombo::magic_combos;
uint16_t MagicCombo::minInterval = 500; uint16_t MagicCombo::min_interval = 500;
uint32_t MagicCombo::endTime; uint32_t MagicCombo::end_time_;
MagicCombo::MagicCombo(void) { MagicCombo::MagicCombo(void) {
} }
void void MagicCombo::begin(void) {
MagicCombo::begin(void) { loop_hook_use(loopHook);
loop_hook_use(this->loopHook);
} }
void void MagicCombo::loopHook(bool is_post_clear) {
MagicCombo::configure(const MagicCombo::dictionary_t dictionary_[]) { if (!magic_combos || is_post_clear)
dictionary = (dictionary_t *)dictionary_;
}
void
MagicCombo::loopHook(bool postClear) {
if (!dictionary || postClear)
return; return;
for (byte i = 0;; i++) { for (byte i = 0;; i++) {
dictionary_t combo; combo_t combo;
combo.leftHand = pgm_read_dword(&(dictionary[i].leftHand)); combo.left_hand = pgm_read_dword(&(magic_combos[i].left_hand));
combo.rightHand = pgm_read_dword(&(dictionary[i].rightHand)); combo.right_hand = pgm_read_dword(&(magic_combos[i].right_hand));
if (combo.leftHand == 0 && combo.rightHand == 0) if (combo.left_hand == 0 && combo.right_hand == 0)
break; break;
if (LEFTHANDSTATE.all == combo.leftHand && if (LEFTHANDSTATE.all == combo.left_hand &&
RIGHTHANDSTATE.all == combo.rightHand) { RIGHTHANDSTATE.all == combo.right_hand) {
if (millis() >= endTime) { if (millis() >= end_time_) {
magicComboActions(i, combo.leftHand, combo.rightHand); magicComboActions(i, combo.left_hand, combo.right_hand);
endTime = millis() + minInterval; end_time_ = millis() + min_interval;
} }
break; break;
} }
@ -74,9 +67,7 @@ MagicCombo::loopHook(bool postClear) {
}; };
__attribute__((weak)) __attribute__((weak)) void magicComboActions(uint8_t comboIndex, uint32_t left_hand, uint32_t right_hand) {
void
magicComboActions(uint8_t comboIndex, uint32_t leftHand, uint32_t rightHand) {
} }
KaleidoscopePlugins::MagicCombo MagicCombo; kaleidoscope::MagicCombo MagicCombo;

@ -20,28 +20,29 @@
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
namespace KaleidoscopePlugins { namespace kaleidoscope {
class MagicCombo : public KaleidoscopePlugin { class MagicCombo : public KaleidoscopePlugin {
public: public:
typedef struct { typedef struct {
uint32_t leftHand, rightHand; uint32_t left_hand, right_hand;
} dictionary_t; } combo_t;
MagicCombo(void); MagicCombo(void);
void begin(void) final; void begin(void) final;
static void configure(const dictionary_t dictionary[]); static const combo_t *magic_combos;
static uint16_t minInterval; static uint16_t min_interval;
private: private:
static const dictionary_t *dictionary; static uint32_t end_time_;
static uint32_t endTime;
static void loopHook(bool postClear); static void loopHook(bool is_post_clear);
};
}; };
void magicComboActions(uint8_t comboIndex, uint32_t leftHand, uint32_t rightHand); }
void magicComboActions(uint8_t combo_index, uint32_t left_hand, uint32_t right_hand);
extern KaleidoscopePlugins::MagicCombo MagicCombo; extern kaleidoscope::MagicCombo MagicCombo;

Loading…
Cancel
Save