From 3b3c53bcc076ab41882eddab3f1f4c8efadea3ba Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 17 Oct 2018 09:15:49 +0200 Subject: [PATCH] New plugin: LEDEffect-BootAnimation This replaces the former `bootAnimation()` method with a plugin that does something very similar, without locking up the keyboard for the duration of the animation, and scans the keymap instead of using hard-coded coordinates. Signed-off-by: Gergely Nagy --- doc/plugin/LEDEffect-BootAnimation.md | 45 +++++++++ src/Kaleidoscope-LEDEffect-BootAnimation.h | 20 ++++ .../plugin/LEDEffect-BootAnimation.cpp | 91 +++++++++++++++++++ .../plugin/LEDEffect-BootAnimation.h | 45 +++++++++ 4 files changed, 201 insertions(+) create mode 100644 doc/plugin/LEDEffect-BootAnimation.md create mode 100644 src/Kaleidoscope-LEDEffect-BootAnimation.h create mode 100644 src/kaleidoscope/plugin/LEDEffect-BootAnimation.cpp create mode 100644 src/kaleidoscope/plugin/LEDEffect-BootAnimation.h diff --git a/doc/plugin/LEDEffect-BootAnimation.md b/doc/plugin/LEDEffect-BootAnimation.md new file mode 100644 index 00000000..213cfd51 --- /dev/null +++ b/doc/plugin/LEDEffect-BootAnimation.md @@ -0,0 +1,45 @@ +# Kaleidoscope-LEDEffect-BootAnimation + +With this plugin enabled, the keyboard will play a little boot animation when +starting up (this animation does not inhibit typing, you can still use the +keyboard while the animation plays). + +## Using the plugin + +To use the plugin, include the header, and tell `Kaleidoscope` to use the plugin: + +```c++ +#include +#include +#include + +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + BootAnimationEffect + LEDOff); + +void setup() { + Kaleidoscope.setup(); +} +``` + +## Plugin properties + +The plugin provides the `BootAnimationEffect` object, with the following +properties: + +### `.timeout` + +> This property specifies the timeout (in milliseconds) each step of the +> animation is displayed. +> +> Defaults to `1000` ms, or one second. + +### `.color` + +> This property sets the color the animation is played with. +> +> The default is a red color. + +## Dependencies + +* [Kaleidoscope-LEDControl](LEDControl.md) diff --git a/src/Kaleidoscope-LEDEffect-BootAnimation.h b/src/Kaleidoscope-LEDEffect-BootAnimation.h new file mode 100644 index 00000000..3bd37e82 --- /dev/null +++ b/src/Kaleidoscope-LEDEffect-BootAnimation.h @@ -0,0 +1,20 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-LEDEffect-BootAnimation -- Small greeting at boot time + * 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 . + */ + +#pragma once + +#include "kaleidoscope/plugin/LEDEffect-BootAnimation.h" diff --git a/src/kaleidoscope/plugin/LEDEffect-BootAnimation.cpp b/src/kaleidoscope/plugin/LEDEffect-BootAnimation.cpp new file mode 100644 index 00000000..e0ce70f5 --- /dev/null +++ b/src/kaleidoscope/plugin/LEDEffect-BootAnimation.cpp @@ -0,0 +1,91 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-LEDEffect-BootAnimation -- Small greeting at boot time + * 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 . + */ + +#include "Kaleidoscope-LEDEffect-BootAnimation.h" + +namespace kaleidoscope { +namespace plugin { + +bool BootAnimationEffect::done_ = false; +cRGB BootAnimationEffect::color = CRGB(150, 0, 0); +uint16_t BootAnimationEffect::start_time_ = 0; +uint16_t BootAnimationEffect::timeout = 1000; +uint8_t BootAnimationEffect::current_index_ = 0; +const uint8_t BootAnimationEffect::greeting_[11] PROGMEM = { + Key_K.keyCode, + Key_E.keyCode, + Key_Y.keyCode, + Key_B.keyCode, + Key_O.keyCode, + Key_A.keyCode, + Key_R.keyCode, + Key_D.keyCode, + Key_Period.keyCode, + Key_I.keyCode, + Key_O.keyCode +}; + +EventHandlerResult BootAnimationEffect::onSetup() { + start_time_ = Kaleidoscope.millisAtCycleStart(); + return EventHandlerResult::OK; +} + +EventHandlerResult BootAnimationEffect::afterEachCycle() { + //If already done or we're not in a ready state, bail + if (done_) { + return EventHandlerResult::OK; + } + + byte row = 255, col = 255; + + for (uint8_t r = 0; r < ROWS; r++) { + for (uint8_t c = 0; c < COLS; c++) { + Key k = Layer.lookupOnActiveLayer(r, c); + Key g; + g.flags = 0; + g.keyCode = pgm_read_word(&greeting_[current_index_]); + + if (k.raw == g.raw) { + row = r; + col = c; + break; + } + } + } + + if ((Kaleidoscope.millisAtCycleStart() - start_time_) > timeout) { + current_index_++; + if (current_index_ == sizeof(greeting_)) + done_ = true; + + start_time_ = Kaleidoscope.millisAtCycleStart(); + if (row != 255 && col != 255) + ::LEDControl.refreshAt(row, col); + return EventHandlerResult::OK; + } + + if (row != 255 && col != 255) { + ::LEDControl.setCrgbAt(row, col, color); + } + + return EventHandlerResult::OK; +} + +} +} + +kaleidoscope::plugin::BootAnimationEffect BootAnimationEffect; diff --git a/src/kaleidoscope/plugin/LEDEffect-BootAnimation.h b/src/kaleidoscope/plugin/LEDEffect-BootAnimation.h new file mode 100644 index 00000000..2081ffa1 --- /dev/null +++ b/src/kaleidoscope/plugin/LEDEffect-BootAnimation.h @@ -0,0 +1,45 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-LEDEffect-BootAnimation -- Small greeting at boot time + * 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 . + */ + +#pragma once + +#include "Kaleidoscope.h" +#include "Kaleidoscope-LEDControl.h" + +namespace kaleidoscope { +namespace plugin { +class BootAnimationEffect : public kaleidoscope::Plugin { + public: + BootAnimationEffect(void) {} + + static uint16_t timeout; + static cRGB color; + + EventHandlerResult afterEachCycle(); + EventHandlerResult onSetup(); + + private: + static const uint8_t greeting_[11]; + + static bool done_; + static uint16_t start_time_; + static uint8_t current_index_; +}; +} +} + +extern kaleidoscope::plugin::BootAnimationEffect BootAnimationEffect;