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 Dec 5, 2019
1 parent 9158661 commit dedc9e0
Show file tree
Hide file tree
Showing 19 changed files with 599 additions and 84 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(KeyAddr key_addr) {
return GROUP_OF_KEY(key_addr);
}

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(KeyAddr key_addr) {
return key_addr.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"
1 change: 1 addition & 0 deletions src/kaleidoscope/Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*/

namespace kaleidoscope {

/** Kaleidoscope Hardware base class.
* Essential methods all hardware libraries must implement.
*/
Expand Down
18 changes: 15 additions & 3 deletions src/kaleidoscope/device/ez/ErgoDox.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ namespace kaleidoscope {
namespace device {
namespace ez {

struct ErgoDoxKeyScannerProps : kaleidoscope::driver::keyscanner::BaseProps {
KEYSCANNER_PROPS(14, 6);
};

class ErgoDoxKeyScanner : public kaleidoscope::device::ATmega32U4KeyboardProps::KeyScanner {
public:

static constexpr bool isOnLeftHalf(ErgoDoxKeyScannerProps::KeyAddr key_addr) {
return key_addr.row() < 7;
}
};

struct ErgoDoxProps : public kaleidoscope::device::ATmega32U4KeyboardProps {
struct KeyScannerProps : kaleidoscope::driver::keyscanner::BaseProps {
KEYSCANNER_PROPS(14, 6);
};

typedef ErgoDoxKeyScannerProps KeyScannerProps;
typedef ErgoDoxKeyScanner KeyScanner;
typedef kaleidoscope::driver::bootloader::avr::HalfKay Bootloader;
};

Expand Down
2 changes: 1 addition & 1 deletion src/kaleidoscope/device/keyboardio/Model01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void Model01KeyScanner::scanMatrix() {
// halves, with eight keys per logical row.

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

Expand Down
8 changes: 8 additions & 0 deletions src/kaleidoscope/device/keyboardio/Model01.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ class Model01LEDDriver;

struct Model01KeyScannerProps : public kaleidoscope::driver::keyscanner::BaseProps {
KEYSCANNER_PROPS(4, 16);

static constexpr byte HAND_BIT = B00001000;
};

#ifndef KALEIDOSCOPE_VIRTUAL_BUILD
class Model01KeyScanner : public kaleidoscope::driver::keyscanner::Base<Model01KeyScannerProps> {
private:
typedef Model01KeyScanner ThisType;
public:

static void setup();
static void scanMatrix();
static void readMatrix();
Expand All @@ -98,6 +101,11 @@ class Model01KeyScanner : public kaleidoscope::driver::keyscanner::Base<Model01K

static void setKeyscanInterval(uint8_t interval);

static constexpr bool isOnLeftHalf(KeyAddr key_addr) {
// If HAND_BIT is set, we are on the right hand side
return !(key_addr.col() & Model01KeyScannerProps::HAND_BIT);
}

protected:
static keydata_t leftHandState;
static keydata_t rightHandState;
Expand Down
Loading

0 comments on commit dedc9e0

Please sign in to comment.