Skip to content

Commit dedc9e0

Browse files
Florian FleissnerFlorian Fleissner
authored andcommitted
This PR enables logically grouping keys and apply layer switch and toggle 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]>
1 parent 9158661 commit dedc9e0

File tree

19 files changed

+599
-84
lines changed

19 files changed

+599
-84
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* -*- mode: c++ -*-
2+
* Basic -- A very basic Kaleidoscope example
3+
* Copyright (C) 2018 Keyboard.io, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under
6+
* the terms of the GNU General Public License as published by the Free Software
7+
* Foundation, version 3.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12+
* details.
13+
*
14+
* You should have received a copy of the GNU General Public License along with
15+
* this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "Kaleidoscope.h"
19+
20+
#include "Kaleidoscope-LED-ActiveLayerColor.h"
21+
22+
// This example demonstrates how keys can be grouped by
23+
// a decision function that relies on a keymap layer kind of array
24+
// that stores the key group id of every individual key.
25+
//
26+
// Please note that the function groupOfKey(..) may only return values
27+
// in the range [0;5].
28+
29+
enum { AMap, BMap };
30+
31+
/* *INDENT-OFF* */
32+
KEYMAPS(
33+
[AMap] = KEYMAP_STACKED
34+
(
35+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
36+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
37+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
38+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
39+
40+
Key_A, Key_A, Key_A, Key_A,
41+
KeyGroup(KEY_GROUP_2, ShiftToLayer(BMap)),
42+
43+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
44+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
45+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
46+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
47+
48+
Key_A, Key_A, Key_A, Key_A,
49+
KeyGroup(KEY_GROUP_1, ShiftToLayer(BMap))
50+
),
51+
52+
[BMap] = KEYMAP_STACKED
53+
(
54+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
55+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
56+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
57+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
58+
59+
Key_B, Key_B, Key_B, Key_B,
60+
___,
61+
62+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
63+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
64+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
65+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
66+
67+
Key_B, Key_B, Key_B, Key_B,
68+
___
69+
)
70+
)
71+
/* *INDENT-ON* */
72+
73+
KALEIDOSCOPE_INIT_PLUGINS(
74+
LEDControl,
75+
LEDActiveLayerColorEffect
76+
)
77+
78+
KEY_GROUP_IDS_STACKED(
79+
0, 0, 0, 0, 0, 0, 0,
80+
1, 1, 1, 1, 1, 1, 1,
81+
1, 1, 1, 1, 1, 1,
82+
0, 0, 0, 0, 0, 0, 0,
83+
84+
0, 0, 0, 0,
85+
0,
86+
87+
0, 0, 0, 0, 0, 0, 0,
88+
2, 2, 2, 2, 2, 2, 2,
89+
2, 2, 2, 2, 2, 2,
90+
0, 0, 0, 0, 0, 0, 0,
91+
92+
0, 0, 0, 0,
93+
0
94+
)
95+
96+
uint8_t groupOfKey(KeyAddr key_addr) {
97+
return GROUP_OF_KEY(key_addr);
98+
}
99+
100+
void setup() {
101+
Kaleidoscope.setup();
102+
103+
static const cRGB layerColormap[] PROGMEM = {
104+
CRGB(128, 0, 0),
105+
CRGB(0, 128, 0)
106+
};
107+
108+
LEDActiveLayerColorEffect.setColormap(layerColormap);
109+
}
110+
111+
void loop() {
112+
Kaleidoscope.loop();
113+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* -*- mode: c++ -*-
2+
* Basic -- A very basic Kaleidoscope example
3+
* Copyright (C) 2018 Keyboard.io, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under
6+
* the terms of the GNU General Public License as published by the Free Software
7+
* Foundation, version 3.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12+
* details.
13+
*
14+
* You should have received a copy of the GNU General Public License along with
15+
* this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "Kaleidoscope.h"
19+
20+
#include "Kaleidoscope-LED-ActiveLayerColor.h"
21+
22+
// This example demonstrates how keys can be grouped using conditionals
23+
// in a decision function.
24+
//
25+
// Please note that the function groupOfKey(...) may only return values
26+
// in the range [0;5].
27+
28+
enum { AMap, BMap };
29+
30+
/* *INDENT-OFF* */
31+
KEYMAPS(
32+
[AMap] = KEYMAP_STACKED
33+
(
34+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
35+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
36+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
37+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
38+
39+
Key_A, Key_A, Key_A, Key_A,
40+
KeyGroup(KEY_GROUP_1, ShiftToLayer(BMap)),
41+
42+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
43+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
44+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
45+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
46+
47+
Key_A, Key_A, Key_A, Key_A,
48+
KeyGroup(KEY_GROUP_0 | KEY_GROUP_2, ShiftToLayer(BMap))
49+
),
50+
51+
[BMap] = KEYMAP_STACKED
52+
(
53+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
54+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
55+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
56+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
57+
58+
Key_B, Key_B, Key_B, Key_B,
59+
___,
60+
61+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
62+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
63+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
64+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
65+
66+
Key_B, Key_B, Key_B, Key_B,
67+
___
68+
)
69+
)
70+
/* *INDENT-ON* */
71+
72+
KALEIDOSCOPE_INIT_PLUGINS(
73+
LEDControl,
74+
LEDActiveLayerColorEffect
75+
)
76+
77+
uint8_t groupOfKey(KeyAddr key_addr) {
78+
return key_addr.row();
79+
}
80+
81+
void setup() {
82+
Kaleidoscope.setup();
83+
84+
static const cRGB layerColormap[] PROGMEM = {
85+
CRGB(128, 0, 0),
86+
CRGB(0, 128, 0)
87+
};
88+
89+
LEDActiveLayerColorEffect.setColormap(layerColormap);
90+
}
91+
92+
void loop() {
93+
Kaleidoscope.loop();
94+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* -*- mode: c++ -*-
2+
* Basic -- A very basic Kaleidoscope example
3+
* Copyright (C) 2018 Keyboard.io, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under
6+
* the terms of the GNU General Public License as published by the Free Software
7+
* Foundation, version 3.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12+
* details.
13+
*
14+
* You should have received a copy of the GNU General Public License along with
15+
* this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "Kaleidoscope.h"
19+
20+
#include "Kaleidoscope-LED-ActiveLayerColor.h"
21+
22+
// This example demonstrates the default key grouping mechanism
23+
// where the left hand is assigned to key group 0 (KEY_GROUP_LEFT_HAND),
24+
// the right hand to key group 1 (KEY_GROUP_RIGHT_HAND).
25+
26+
enum { AMap, BMap };
27+
28+
/* *INDENT-OFF* */
29+
KEYMAPS(
30+
[AMap] = KEYMAP_STACKED
31+
(
32+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
33+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
34+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
35+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
36+
37+
Key_A, Key_A, Key_A, Key_A,
38+
KeyGroup(KEY_GROUP_RIGHT_HAND, ShiftToLayer(BMap)),
39+
40+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
41+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
42+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
43+
Key_A, Key_A, Key_A, Key_A, Key_A, Key_A, Key_A,
44+
45+
Key_A, Key_A, Key_A, Key_A,
46+
KeyGroup(KEY_GROUP_LEFT_HAND, ShiftToLayer(BMap))
47+
),
48+
49+
[BMap] = KEYMAP_STACKED
50+
(
51+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
52+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
53+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
54+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
55+
56+
Key_B, Key_B, Key_B, Key_B,
57+
___,
58+
59+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
60+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
61+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
62+
Key_B, Key_B, Key_B, Key_B, Key_B, Key_B, Key_B,
63+
64+
Key_B, Key_B, Key_B, Key_B,
65+
___
66+
)
67+
)
68+
/* *INDENT-ON* */
69+
70+
KALEIDOSCOPE_INIT_PLUGINS(
71+
LEDControl,
72+
LEDActiveLayerColorEffect
73+
)
74+
75+
void setup() {
76+
Kaleidoscope.setup();
77+
78+
static const cRGB layerColormap[] PROGMEM = {
79+
CRGB(128, 0, 0),
80+
CRGB(0, 128, 0)
81+
};
82+
83+
LEDActiveLayerColorEffect.setColormap(layerColormap);
84+
}
85+
86+
void loop() {
87+
Kaleidoscope.loop();
88+
}

src/Kaleidoscope-KeyGroups.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* -*- mode: c++ -*-
2+
* Kaleidoscope-Hardware-EZ-ErgoDox -- ErgoDox hardware support for Kaleidoscope
3+
* Copyright (C) 2018 Keyboard.io, Inc
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#pragma once
20+
#include "kaleidoscope/key_groups.h"

src/kaleidoscope/Hardware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646

4747
namespace kaleidoscope {
48+
4849
/** Kaleidoscope Hardware base class.
4950
* Essential methods all hardware libraries must implement.
5051
*/

src/kaleidoscope/device/ez/ErgoDox.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,22 @@ namespace kaleidoscope {
4848
namespace device {
4949
namespace ez {
5050

51+
struct ErgoDoxKeyScannerProps : kaleidoscope::driver::keyscanner::BaseProps {
52+
KEYSCANNER_PROPS(14, 6);
53+
};
54+
55+
class ErgoDoxKeyScanner : public kaleidoscope::device::ATmega32U4KeyboardProps::KeyScanner {
56+
public:
57+
58+
static constexpr bool isOnLeftHalf(ErgoDoxKeyScannerProps::KeyAddr key_addr) {
59+
return key_addr.row() < 7;
60+
}
61+
};
62+
5163
struct ErgoDoxProps : public kaleidoscope::device::ATmega32U4KeyboardProps {
52-
struct KeyScannerProps : kaleidoscope::driver::keyscanner::BaseProps {
53-
KEYSCANNER_PROPS(14, 6);
54-
};
64+
65+
typedef ErgoDoxKeyScannerProps KeyScannerProps;
66+
typedef ErgoDoxKeyScanner KeyScanner;
5567
typedef kaleidoscope::driver::bootloader::avr::HalfKay Bootloader;
5668
};
5769

src/kaleidoscope/device/keyboardio/Model01.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void Model01KeyScanner::scanMatrix() {
197197
// halves, with eight keys per logical row.
198198

199199
constexpr byte HIGH_BIT = B10000000;
200-
constexpr byte HAND_BIT = B00001000;
200+
constexpr byte HAND_BIT = Model01KeyScannerProps::HAND_BIT;
201201
constexpr byte ROW_BITS = B00110000;
202202
constexpr byte COL_BITS = B00000111;
203203

src/kaleidoscope/device/keyboardio/Model01.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,16 @@ class Model01LEDDriver;
7373

7474
struct Model01KeyScannerProps : public kaleidoscope::driver::keyscanner::BaseProps {
7575
KEYSCANNER_PROPS(4, 16);
76+
77+
static constexpr byte HAND_BIT = B00001000;
7678
};
7779

7880
#ifndef KALEIDOSCOPE_VIRTUAL_BUILD
7981
class Model01KeyScanner : public kaleidoscope::driver::keyscanner::Base<Model01KeyScannerProps> {
8082
private:
8183
typedef Model01KeyScanner ThisType;
8284
public:
85+
8386
static void setup();
8487
static void scanMatrix();
8588
static void readMatrix();
@@ -98,6 +101,11 @@ class Model01KeyScanner : public kaleidoscope::driver::keyscanner::Base<Model01K
98101

99102
static void setKeyscanInterval(uint8_t interval);
100103

104+
static constexpr bool isOnLeftHalf(KeyAddr key_addr) {
105+
// If HAND_BIT is set, we are on the right hand side
106+
return !(key_addr.col() & Model01KeyScannerProps::HAND_BIT);
107+
}
108+
101109
protected:
102110
static keydata_t leftHandState;
103111
static keydata_t rightHandState;

0 commit comments

Comments
 (0)