From 29841ec02e3346cc5c96c407c189ad24a7baad70 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 2 Mar 2017 14:15:30 +0100 Subject: [PATCH] Add a LEDMode based on AlphaSquare. The new singleton objects implements a LED mode where each pressed key will light up the appropriate symbol on the LEDs, on the side it was pressed on. We use different timers for each half. Signed-off-by: Gergely Nagy --- examples/LED-AlphaSquare/LED-AlphaSquare.ino | 4 +- src/Kaleidoscope-LED-AlphaSquare.h | 1 + src/Kaleidoscope/AlphaSquare-Effect.cpp | 76 ++++++++++++++++++++ src/Kaleidoscope/AlphaSquare-Effect.h | 43 +++++++++++ src/Kaleidoscope/LED-AlphaSquare.h | 2 + 5 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 src/Kaleidoscope/AlphaSquare-Effect.cpp create mode 100644 src/Kaleidoscope/AlphaSquare-Effect.h diff --git a/examples/LED-AlphaSquare/LED-AlphaSquare.ino b/examples/LED-AlphaSquare/LED-AlphaSquare.ino index f60f43bc..a31cc7cb 100644 --- a/examples/LED-AlphaSquare/LED-AlphaSquare.ino +++ b/examples/LED-AlphaSquare/LED-AlphaSquare.ino @@ -75,9 +75,9 @@ const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { } void setup () { - Kaleidoscope.setup (KEYMAP_SIZE); + Kaleidoscope.setup (); - Kaleidoscope.use (&LEDControl, &AlphaSquare, &Macros, NULL); + USE_PLUGINS (&AlphaSquare, &AlphaSquareEffect, &Macros); AlphaSquare.color = { 0xcb, 0xc0, 0xff }; } diff --git a/src/Kaleidoscope-LED-AlphaSquare.h b/src/Kaleidoscope-LED-AlphaSquare.h index 5c456b52..b9190e23 100644 --- a/src/Kaleidoscope-LED-AlphaSquare.h +++ b/src/Kaleidoscope-LED-AlphaSquare.h @@ -19,3 +19,4 @@ #pragma once #include +#include diff --git a/src/Kaleidoscope/AlphaSquare-Effect.cpp b/src/Kaleidoscope/AlphaSquare-Effect.cpp new file mode 100644 index 00000000..71824410 --- /dev/null +++ b/src/Kaleidoscope/AlphaSquare-Effect.cpp @@ -0,0 +1,76 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-LED-AlphaSquare -- 4x4 pixel LED alphabet + * Copyright (C) 2017 Gergely Nagy + * + * 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 . + */ + +#include + +namespace KaleidoscopePlugins { + namespace LEDEffects { + uint16_t AlphaSquareEffect::length = 200; + uint32_t AlphaSquareEffect::endTimeLeft, AlphaSquareEffect::endTimeRight; + Key AlphaSquareEffect::lastKeyLeft, AlphaSquareEffect::lastKeyRight; + + AlphaSquareEffect::AlphaSquareEffect (void) { + } + + void + AlphaSquareEffect::begin (void) { + Kaleidoscope.useEventHandlerHook (eventHandlerHook); + LEDMode::begin (); + } + + void + AlphaSquareEffect::update (void) { + if (endTimeLeft && millis () > endTimeLeft) { + ::AlphaSquare.clear (lastKeyLeft); + endTimeLeft = 0; + } + if (endTimeRight && millis () > endTimeRight) { + ::AlphaSquare.clear (lastKeyRight); + endTimeRight = 0; + } + } + + Key + AlphaSquareEffect::eventHandlerHook (Key key, byte row, byte col, uint8_t keyState) { + if (keyState & INJECTED) + return key; + + if (key < Key_A || key > Key_0) + return key; + + if (!key_is_pressed (keyState)) + return key; + + uint8_t displayCol = 2; + + if (col < COLS / 2) { + lastKeyLeft = key; + endTimeLeft = millis () + length; + } else { + lastKeyRight = key; + endTimeRight = millis () + length; + displayCol = 10; + } + + ::AlphaSquare.display (key, displayCol); + return key; + } + }; +}; + +KaleidoscopePlugins::LEDEffects::AlphaSquareEffect AlphaSquareEffect; diff --git a/src/Kaleidoscope/AlphaSquare-Effect.h b/src/Kaleidoscope/AlphaSquare-Effect.h new file mode 100644 index 00000000..7cc0d2f5 --- /dev/null +++ b/src/Kaleidoscope/AlphaSquare-Effect.h @@ -0,0 +1,43 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-LED-AlphaSquare -- 4x4 pixel LED alphabet + * Copyright (C) 2017 Gergely Nagy + * + * 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 . + */ + +#pragma once + +#include +#include + +namespace KaleidoscopePlugins { + namespace LEDEffects { + class AlphaSquareEffect : public LEDMode { + public: + AlphaSquareEffect (void); + + virtual void begin (void) final; + virtual void update (void) final; + + static uint16_t length; + private: + static uint32_t endTimeLeft, endTimeRight; + static Key lastKeyLeft, lastKeyRight; + + static Key eventHandlerHook (Key key, uint8_t row, uint8_t col, uint8_t keyState); + }; + }; +}; + +extern KaleidoscopePlugins::LEDEffects::AlphaSquareEffect AlphaSquareEffect; diff --git a/src/Kaleidoscope/LED-AlphaSquare.h b/src/Kaleidoscope/LED-AlphaSquare.h index 9517cfb4..a26b5b3a 100644 --- a/src/Kaleidoscope/LED-AlphaSquare.h +++ b/src/Kaleidoscope/LED-AlphaSquare.h @@ -16,6 +16,8 @@ * along with this program. If not, see . */ +#pragma once + #include #include