Skip to content

Commit

Permalink
Merge pull request #1355 from EvyBongers/feat/ActiveLayerLEDs
Browse files Browse the repository at this point in the history
Add plugin for applying LED colors to keys in layer
  • Loading branch information
obra authored Nov 1, 2023
2 parents 76705c2 + 0b0fa6d commit d183f49
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 0 deletions.
48 changes: 48 additions & 0 deletions plugins/Kaleidoscope-LED-ActiveLayerKeys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# LED-ActiveLayerKeys

A simple way to light up all keys in the top layer in uniform colors. Unlike
[Colormap](Kaleidoscope-Colormap.md), all keys will be the same color. But this
plugin uses considerably less resources, and is easier to set up as well. A
perfect solution when one wants to quickly see what layer they're on and which
keys are on that layer, with minimal resources and time investment.

## Using the plugin

To use the plugin, one needs to include the header, and activate the effect.
Then, one needs to configure a color map:

```c++
#include "Kaleidoscope.h"
#include "Kaleidoscope-LEDControl.h"
#include "Kaleidoscope-LED-ActiveLayerKeys.h"

KALEIDOSCOPE_INIT_PLUGINS(LEDControl,
LEDActiveLayerKeysEffect);

void setup () {
static const cRGB layerColormap[] PROGMEM = {
CRGB(0xff, 0x00, 0x00), // red, for the first layer
CRGB(0x00, 0xff, 0x00), // green, for the second layer
CRGB(0x00, 0x00, 0xff), // blue, for the third layer
};

Kaleidoscope.setup();
LEDActiveLayerKeysEffect.setColormap(layerColormap);
}
```
## Plugin properties
The plugin provides the `LEDActiveLayerKeysEffect` object, which has the following
method:
### `.setColormap(colormap)`
> Sets the colormap to the supplied map. Each element of the map should be a
> `cRGB` color, and the map should reside in PROGMEM. The array should have the
> same amount of items as there are layers. Any layer that doesn't have a
> matching entry in the array, will have leds turned off.
## Dependencies
* [Kaleidoscope-LEDControl](Kaleidoscope-LEDControl.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// -*- mode: c++ -*-
/* Kaleidoscope - Firmware for computer input devices
* Copyright (C) 2017-2020 Bart Nagel
*
* 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-LEDControl.h"
#include "Kaleidoscope-LED-ActiveLayerKeys.h"

// clang-format off

KEYMAPS(
[0] = KEYMAP_STACKED
(XXX, Key_1, Key_2, Key_3, Key_4, Key_5, Key_LEDEffectNext,
Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab,
Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G,
Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape,

Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift,
XXX,

XXX, Key_6, Key_7, Key_8, Key_9, Key_0, XXX,
Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals,
Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote,
Key_RightAlt, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus,

Key_RightShift, Key_LeftAlt, Key_Spacebar, Key_RightControl,
XXX),

[1] = KEYMAP_STACKED
(___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___,
___,

M(MACRO_VERSION_INFO), ___, Key_7, Key_8, Key_9, Key_KeypadSubtract, ___,
___, ___, Key_4, Key_5, Key_6, Key_KeypadAdd, ___,
___, Key_1, Key_2, Key_3, Key_Equals, ___,
___, ___, Key_0, Key_Period, Key_KeypadMultiply, Key_KeypadDivide, Key_Enter,
___, ___, ___, ___,
___),

[2] = KEYMAP_STACKED
(___, Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, Key_CapsLock,
Key_Tab, ___, Key_mouseUp, ___, Key_mouseBtnR, Key_mouseWarpEnd, Key_mouseWarpNE,
Key_Home, Key_mouseL, Key_mouseDn, Key_mouseR, Key_mouseBtnL, Key_mouseWarpNW,
Key_End, Key_PrintScreen, Key_Insert, ___, Key_mouseBtnM, Key_mouseWarpSW, Key_mouseWarpSE,
___, Key_Delete, ___, ___,
___,

Consumer_ScanPreviousTrack, Key_F6, Key_F7, Key_F8, Key_F9, Key_F10, Key_F11,
Consumer_PlaySlashPause, Consumer_ScanNextTrack, Key_LeftCurlyBracket, Key_RightCurlyBracket, Key_LeftBracket, Key_RightBracket, Key_F12,
Key_LeftArrow, Key_DownArrow, Key_UpArrow, Key_RightArrow, ___, ___,
Key_PcApplication, Consumer_Mute, Consumer_VolumeDecrement, Consumer_VolumeIncrement, ___, Key_Backslash, Key_Pipe,
___, ___, Key_Enter, ___,
___)
) // KEYMAPS(

// clang-format on

KALEIDOSCOPE_INIT_PLUGINS(
// LEDControl provides support for other LED modes
LEDControl,

// Support for ActiveLayerKeys
LEDActiveLayerKeysEffect);


void setup() {
static const cRGB layerColormap[] PROGMEM = {
CRGB(0xff, 0x00, 0x00), // red, for the first layer
CRGB(0x00, 0xff, 0x00), // green, for the second layer
CRGB(0x00, 0x00, 0xff), // blue, for the third layer
};

Kaleidoscope.setup();
LEDActiveLayerKeysEffect.setColormap(layerColormap);
LEDActiveLayerKeysEffect.activate();
}

void loop() {
Kaleidoscope.loop();
}
7 changes: 7 additions & 0 deletions plugins/Kaleidoscope-LED-ActiveLayerKeys/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name=Kaleidoscope-LED-ActiveLayerKeys
version=0.0.0
sentence=Light up the LEDs based on the keys specified in the active layer
maintainer=
url=https://github.com/keyboardio/Kaleidoscope
author=EvyBongers
paragraph=
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* -*- mode: c++ -*-
* Kaleidoscope-LED-ActiveLayerKeys -- Light up the LEDs based on the keys specified in the active layer
* 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/>.
*/

#pragma once

#include "kaleidoscope/plugin/LED-ActiveLayerKeys.h" // IWYU pragma: export
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* -*- mode: c++ -*-
* Kaleidoscope-LED-ActiveLayerKeys -- Light up the LEDs based on the keys specified in the active layer
* Copyright (C) 2018 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify it under 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 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 along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "kaleidoscope/plugin/LED-ActiveLayerKeys.h"

#include <Arduino.h> // for pgm_read_byte
#include <stdint.h> // for uint8_t

#include "kaleidoscope/KeyAddr.h" // for KeyAddr
#include "kaleidoscope/Runtime.h" // for Runtime
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult, EventHandlerResult::OK
#include "kaleidoscope/layers.h" // for Layer
#include "kaleidoscope/plugin/LEDControl.h" // for LEDControl

namespace kaleidoscope {
namespace plugin {

cRGB LEDActiveLayerKeysEffect::default_layer_color_ = CRGB(0, 0, 0);

LEDActiveLayerKeysEffect::TransientLEDMode::TransientLEDMode(
const LEDActiveLayerKeysEffect *parent)
: parent_(parent) {}

cRGB LEDActiveLayerKeysEffect::TransientLEDMode::getLayerColor(uint8_t layer) {
cRGB color;

if (layer < parent_->colormap_size_) {
color.r = pgm_read_byte(&(parent_->colormap_[layer].r));
color.g = pgm_read_byte(&(parent_->colormap_[layer].g));
color.b = pgm_read_byte(&(parent_->colormap_[layer].b));
} else {
color = LEDActiveLayerKeysEffect::default_layer_color_;
}

return color;
}

void LEDActiveLayerKeysEffect::TransientLEDMode::onActivate() {
if (!Runtime.has_leds)
return;

uint8_t top_layer = ::Layer.mostRecent();
cRGB active_color_ = getLayerColor(top_layer);

for (auto key_addr : KeyAddr::all()) {
Key k = Layer.lookupOnActiveLayer(key_addr);
Key layer_key = Layer.getKey(top_layer, key_addr);

if ((k == layer_key) && (k != Key_NoKey) && (k != Key_Transparent)) {
::LEDControl.setCrgbAt(KeyAddr(key_addr), active_color_);
}
}
}

EventHandlerResult LEDActiveLayerKeysEffect::onLayerChange() {
if (::LEDControl.get_mode_index() == led_mode_id_)
::LEDControl.get_mode<TransientLEDMode>()->onActivate();

return EventHandlerResult::OK;
}

} // namespace plugin
} // namespace kaleidoscope

kaleidoscope::plugin::LEDActiveLayerKeysEffect LEDActiveLayerKeysEffect;
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* -*- mode: c++ -*-
* Kaleidoscope-LED-ActiveLayerKeys -- Light up the LEDs based on the keys specified in the active layer
* Copyright (C) 2016, 2017, 2018 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify it under 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 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 along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "kaleidoscope/KeyAddr.h" // for KeyAddr
#include "kaleidoscope/device/device.h" // for cRGB
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/plugin.h" // for Plugin
#include "kaleidoscope/plugin/AccessTransientLEDMode.h" // for AccessTransientLEDMode
#include "kaleidoscope/plugin/LEDMode.h" // for LEDMode
#include "kaleidoscope/plugin/LEDModeInterface.h" // for LEDModeInterface

namespace kaleidoscope {
namespace plugin {
class LEDActiveLayerKeysEffect : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
EventHandlerResult onLayerChange();
template<uint8_t _colormap_size>
void setColormap(cRGB const (&colormap)[_colormap_size]) {
colormap_ = colormap;
colormap_size_ = _colormap_size;
}
static void setDefaultColor(cRGB color) {
default_layer_color_ = color;
}

// This class' instance has dynamic lifetime
//
class TransientLEDMode : public LEDMode {
public:
// Please note that storing the parent ptr is only required
// for those LED modes that require access to
// members of their parent class. Most LED modes can do without.
//
explicit TransientLEDMode(const LEDActiveLayerKeysEffect *parent);

protected:
void onActivate() final;

private:
const LEDActiveLayerKeysEffect *parent_;

cRGB getLayerColor(uint8_t layer);

friend class LEDActiveLayerKeysEffect;
};

private:
// An array of Colormap entries in PROGMEM.
cRGB const *colormap_{nullptr};
uint8_t colormap_size_{0};

static cRGB default_layer_color_;
};

} // namespace plugin
} // namespace kaleidoscope

extern kaleidoscope::plugin::LEDActiveLayerKeysEffect LEDActiveLayerKeysEffect;

0 comments on commit d183f49

Please sign in to comment.