Skip to content

Commit

Permalink
This PR enables logically grouping keys and apply layer switch and to…
Browse files Browse the repository at this point in the history
…ggle operation to selected key groups only. Examples are provide that demonstrate grouping keys by keyboard hand sides, via condition expressions and with keymap-style color arrays.

The basic idea that led to this PR was the question: How to switch one half of the keyboard to another layer while the other half should stay at the base layer. My intention was to switch via the thumb key of the Model01 one half of the keyboard to a modifier layer where all modifiers are supposed to reside on the home row. The other half of the keyboard should stay at the base layer. By means of that I intent to make typing modifier shortcuts with combined modifiers and keys more convenient.

Of coures, this could already be done with the existing Kaleidoscope, e.g. by defining two individual additional layers whose keys for one half match those of the base layer and the other half the modifier layer. The redundancy is obvious.

That's how the idea emerged to modity the layer class in a way that it stores not only one pair of `layer_state_` and `top_active_layer_` but several, each pair of those variables for a separate region of the keyboard. This makes it possible to make separate regions of the keyboard behave as individual keyboards in terms of their layer state. Lacking a better name, I called those keys assigned to such a region a "key group".

Keys can be arbitrarily assigned to key groups. Layer switches/toggles can be defined to affect one or more key groups (see examples).

By default only two key groups are defined. One for the left and one for the right hand side of the keyboard.

* added header `src/Kaleidoscope-KeyGroups.h`
* hardwares implement static constexpr bool isOnLeftHalf(uint8_t row, uint8_t col) convenience methods to test for the hand a key belongs to (one-handed keybords could just unconditionally return true)
* added header `key_groups.h`
* layers class tracks state of up to six key groups now
* all public methods that affect the layer states now can be passed a key group flags to name the affected layers
* method `extern uint8_t groupOfKey(uint8_t row, uint8_t col)` introduced that can be overridden from within the sketch
* fixed Colormap plugin to work with multiple key groups
* fixed plugin LED-ActiveLayerColor to work with multiple key groups
* fixed plugin LED-ActiveModColor to work with multiple key groups
* added TODO-comments in contexts (plugins EEPROM-KeymapOProgrmmer and NumPad) where layer-methods are used but no layer group information is available
* added TODO-comments about the implementation of method `isOnLeftHalf` in some keyboards
* examples were added to illustrate different use cases

The PR will not break any user code or plugins. Only if keygroups are assigned to layer switch operations, the firmware behavior will differ from what it does currently.

Changes will probably not noticeabley affect runtime performance.

Stock firmware: Groth of program size from 25192 to 26992 bytes. Data usage grows from 1414 to 1459 bytes.

Carefully check commit line changes containing TODOs and fix those. Especially concerning plugins EEPROM-KeymapOProgrmmer and NumPad.

This PR comes with quite some changes and both program size and Data usage grow significantly. Nonetheless, this feature might be worth it.

If Arduino eventually would introduce a global per-sketch configuration header (there's an ongoing discussion about this in the Arduino community) we could support several alternative implementations of the layer system, let the user select one and select a default one otherwise.

As this is currently not possible, I chose to "upgrade" the existing layer class.

Signed-off-by: Florian Fleissner <[email protected]>
  • Loading branch information
Florian Fleissner authored and Florian Fleissner committed Mar 5, 2019
1 parent b8d4e90 commit 2c33904
Show file tree
Hide file tree
Showing 22 changed files with 611 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* -*- mode: c++ -*-
* Basic -- A very basic Kaleidoscope example
* Copyright (C) 2018 Keyboard.io, Inc.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "Kaleidoscope.h"

#include "Kaleidoscope-LED-ActiveLayerColor.h"

// This example demonstrates how keys can be grouped by
// a decision function that relies on a keymap layer kind of array
// that stores the key group id of every individual key.
//
// Please note that the function groupOfKey(..) may only return values
// in the range [0;5].

enum { AMap, BMap };

/* *INDENT-OFF* */
KEYMAPS(
[AMap] = KEYMAP_STACKED
(
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,

Key_A, Key_A, Key_A, Key_A,
KeyGroup(KEY_GROUP_2, ShiftToLayer(BMap)),

Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,

Key_A, Key_A, Key_A, Key_A,
KeyGroup(KEY_GROUP_1, ShiftToLayer(BMap))
),

[BMap] = KEYMAP_STACKED
(
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,

Key_B, Key_B, Key_B, Key_B,
___,

Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,

Key_B, Key_B, Key_B, Key_B,
___
)
)
/* *INDENT-ON* */

KALEIDOSCOPE_INIT_PLUGINS(
LEDControl,
LEDActiveLayerColorEffect
)

KEY_GROUP_IDS_STACKED(
0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0,
0,

0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2,
0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0,
0
)

uint8_t groupOfKey(uint8_t row, uint8_t col) {
return GROUP_OF_KEY(row, col);
}

void setup() {
Kaleidoscope.setup();

static const cRGB layerColormap[] PROGMEM = {
CRGB(128, 0, 0),
CRGB(0, 128, 0)
};

LEDActiveLayerColorEffect.setColormap(layerColormap);
}

void loop() {
Kaleidoscope.loop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* -*- mode: c++ -*-
* Basic -- A very basic Kaleidoscope example
* Copyright (C) 2018 Keyboard.io, Inc.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "Kaleidoscope.h"

#include "Kaleidoscope-LED-ActiveLayerColor.h"

// This example demonstrates how keys can be grouped using conditionals
// in a decision function.
//
// Please note that the function groupOfKey(...) may only return values
// in the range [0;5].

enum { AMap, BMap };

/* *INDENT-OFF* */
KEYMAPS(
[AMap] = KEYMAP_STACKED
(
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,

Key_A, Key_A, Key_A, Key_A,
KeyGroup(KEY_GROUP_1, ShiftToLayer(BMap)),

Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,

Key_A, Key_A, Key_A, Key_A,
KeyGroup(KEY_GROUP_0 | KEY_GROUP_2, ShiftToLayer(BMap))
),

[BMap] = KEYMAP_STACKED
(
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,

Key_B, Key_B, Key_B, Key_B,
___,

Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,

Key_B, Key_B, Key_B, Key_B,
___
)
)
/* *INDENT-ON* */

KALEIDOSCOPE_INIT_PLUGINS(
LEDControl,
LEDActiveLayerColorEffect
)

uint8_t groupOfKey(uint8_t row, uint8_t col) {
return row;
}

void setup() {
Kaleidoscope.setup();

static const cRGB layerColormap[] PROGMEM = {
CRGB(128, 0, 0),
CRGB(0, 128, 0)
};

LEDActiveLayerColorEffect.setColormap(layerColormap);
}

void loop() {
Kaleidoscope.loop();
}
88 changes: 88 additions & 0 deletions examples/Features/KeyGroups/Default/Default.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* -*- mode: c++ -*-
* Basic -- A very basic Kaleidoscope example
* Copyright (C) 2018 Keyboard.io, Inc.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "Kaleidoscope.h"

#include "Kaleidoscope-LED-ActiveLayerColor.h"

// This example demonstrates the default key grouping mechanism
// where the left hand is assigned to key group 0 (KEY_GROUP_LEFT_HAND),
// the right hand to key group 1 (KEY_GROUP_RIGHT_HAND).

enum { AMap, BMap };

/* *INDENT-OFF* */
KEYMAPS(
[AMap] = KEYMAP_STACKED
(
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,

Key_A, Key_A, Key_A, Key_A,
KeyGroup(KEY_GROUP_RIGHT_HAND, ShiftToLayer(BMap)),

Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,

Key_A, Key_A, Key_A, Key_A,
KeyGroup(KEY_GROUP_LEFT_HAND, ShiftToLayer(BMap))
),

[BMap] = KEYMAP_STACKED
(
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,

Key_B, Key_B, Key_B, Key_B,
___,

Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,

Key_B, Key_B, Key_B, Key_B,
___
)
)
/* *INDENT-ON* */

KALEIDOSCOPE_INIT_PLUGINS(
LEDControl,
LEDActiveLayerColorEffect
)

void setup() {
Kaleidoscope.setup();

static const cRGB layerColormap[] PROGMEM = {
CRGB(128, 0, 0),
CRGB(0, 128, 0)
};

LEDActiveLayerColorEffect.setColormap(layerColormap);
}

void loop() {
Kaleidoscope.loop();
}
20 changes: 20 additions & 0 deletions src/Kaleidoscope-KeyGroups.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Hardware-EZ-ErgoDox -- ErgoDox hardware support for Kaleidoscope
* Copyright (C) 2018 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once
#include "kaleidoscope/key_groups.h"
7 changes: 7 additions & 0 deletions src/kaleidoscope/Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@
typedef struct cRGB cRGB;

namespace kaleidoscope {

/** Kaleidoscope Hardware base class.
* Essential methods all hardware libraries must implement.
*/
class Hardware {
public:

// This class method can be overridden by hardware implementations.
constexpr static bool isOnLeftHalf(uint8_t /*row*/, uint8_t /*col*/) {
return true;
}

/**
* @defgroup kaleidoscope_hardware_leds Kaleidoscope::Hardware/LEDs
* @{
Expand Down
4 changes: 4 additions & 0 deletions src/kaleidoscope/hardware/ez/ErgoDox.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class ErgoDox : public kaleidoscope::Hardware {
static constexpr byte matrix_rows = 14;
static constexpr int8_t led_count = 0;

static constexpr bool isOnLeftHalf(uint8_t row, uint8_t /*col*/) {
return row < 7;
}

void scanMatrix(void);
void readMatrix(void);
void actOnMatrixScan(void);
Expand Down
3 changes: 3 additions & 0 deletions src/kaleidoscope/hardware/kbdfans/KBD4x.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class KBD4x: public kaleidoscope::hardware::ATMegaKeyboard {

static constexpr int8_t led_count = 0;

// TODO: Implement
// static constexpr bool isOnLeftHalf(uint8_t row, uint8_t col);

void resetDevice();
};

Expand Down
1 change: 0 additions & 1 deletion src/kaleidoscope/hardware/keyboardio/Model01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ void Model01::rebootBootloader() {
// halves, with eight keys per logical row.

constexpr byte HIGH_BIT = B10000000;
constexpr byte HAND_BIT = B00001000;
constexpr byte ROW_BITS = B00110000;
constexpr byte COL_BITS = B00000111;

Expand Down
8 changes: 8 additions & 0 deletions src/kaleidoscope/hardware/keyboardio/Model01.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ namespace keyboardio {

class Model01 : public kaleidoscope::Hardware {
public:

static constexpr byte HAND_BIT = B00001000;

static constexpr bool isOnLeftHalf(uint8_t /*row*/, uint8_t col) {
// If HAND_BIT is set, we are on the right hand side
return !(col & HAND_BIT);
}

Model01(void);

static constexpr byte matrix_rows = 4;
Expand Down
3 changes: 3 additions & 0 deletions src/kaleidoscope/hardware/olkb/Planck.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Planck: public kaleidoscope::hardware::ATMegaKeyboard {
COL_PIN_LIST({ PIN_F1, PIN_F0, PIN_B0, PIN_C7, PIN_F4, PIN_F5, PIN_F6, PIN_F7, PIN_D4, PIN_D6, PIN_B4, PIN_D7 })
);

// TODO: Implement
// static constexpr bool isOnLeftHalf(uint8_t row, uint8_t col);

static constexpr int8_t led_count = 0;
};

Expand Down
Loading

0 comments on commit 2c33904

Please sign in to comment.