diff --git a/doc/plugin/HostPowerManagement.md b/doc/plugin/HostPowerManagement.md new file mode 100644 index 00000000..bd9973f3 --- /dev/null +++ b/doc/plugin/HostPowerManagement.md @@ -0,0 +1,47 @@ +# Kaleidoscope-HostPowerManagement + +Support performing custom actions whenever the host suspends, resumes, or is +sleeping. + +## Using the plugin + +To use the plugin, one needs to include the header, and activate it. No further +configuration is necessary, unless one wants to perform custom actions. + +```c++ +#include +#include + +KALEIDOSCOPE_INIT_PLUGINS(HostPowerManagement); + +void setup () { + Kaleidoscope.setup (); +} +``` + +## Plugin methods + +The plugin provides the `HostPowerManagement` object, with no public methods. + +## Overrideable methods + +### `hostPowerManagementEventHandler(event)` + +> The `hostPowerManagementEventHandler` method is the brain of the plugin: this function +> tells it what action to perform in response to the various events. +> +> Currently supported events are: +> `kaleidoscope::plugin::HostPowerManagement::Suspend` is fired once when the +> host suspends; `kaleidoscope::plugin::HostPowerManagement::Sleep` is fired +> every cycle while the host is suspended; +> `kaleidoscope::plugin::HostPowerManagement::Resume` is fired once when the +> host wakes up. +> +> The default implementation is empty. + +## Further reading + +Starting from the [example][plugin:example] is the recommended way of getting +started with the plugin. + + [plugin:example]: ../../examples/HostPowerManagement/HostPowerManagement.ino diff --git a/examples/HostPowerManagement/HostPowerManagement.ino b/examples/HostPowerManagement/HostPowerManagement.ino new file mode 100644 index 00000000..72d6f9df --- /dev/null +++ b/examples/HostPowerManagement/HostPowerManagement.ino @@ -0,0 +1,71 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-HostPowerManagement -- Host power management support plugin. + * Copyright (C) 2017, 2018 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 +#include +#include + +// *INDENT-OFF* +const Key keymaps[][ROWS][COLS] PROGMEM = { + [0] = KEYMAP_STACKED + ( + Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey, + 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, + Key_NoKey, + + Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, + 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_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, + + Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, + Key_NoKey + ), +}; +// *INDENT-ON* + +void hostPowerManagementEventHandler(kaleidoscope::plugin::HostPowerManagement::Event event) { + switch (event) { + case kaleidoscope::plugin::HostPowerManagement::Suspend: + LEDControl.paused = true; + LEDControl.set_all_leds_to({0, 0, 0}); + LEDControl.syncLeds(); + break; + case kaleidoscope::plugin::HostPowerManagement::Resume: + LEDControl.paused = false; + LEDControl.refreshAll(); + break; + case kaleidoscope::plugin::HostPowerManagement::Sleep: + break; + } +} + +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + HostPowerManagement); + +void setup() { + Kaleidoscope.setup(); +} + +void loop() { + Kaleidoscope.loop(); +} diff --git a/src/Kaleidoscope-HostPowerManagement.h b/src/Kaleidoscope-HostPowerManagement.h new file mode 100644 index 00000000..f6e56935 --- /dev/null +++ b/src/Kaleidoscope-HostPowerManagement.h @@ -0,0 +1,21 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-HostPowerManagement -- Host power management support plugin. + * 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 diff --git a/src/kaleidoscope/plugin/HostPowerManagement.cpp b/src/kaleidoscope/plugin/HostPowerManagement.cpp new file mode 100644 index 00000000..ce4c9826 --- /dev/null +++ b/src/kaleidoscope/plugin/HostPowerManagement.cpp @@ -0,0 +1,64 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-HostPowerManagement -- Host power management support plugin. + * Copyright (C) 2017, 2018 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 +#include +#include + +// This is a terrible hack until Arduino#6964 gets implemented. +// It makes the `_usbSuspendState` symbol available to us. +extern uint8_t _usbSuspendState; + +namespace kaleidoscope { +namespace plugin { + +bool HostPowerManagement::was_suspended_ = false; +bool HostPowerManagement::initial_suspend_ = true; + +EventHandlerResult HostPowerManagement::beforeEachCycle() { + +#ifdef __AVR__ + if ((_usbSuspendState & (1 << SUSPI))) { + if (!initial_suspend_) { + if (!was_suspended_) { + was_suspended_ = true; + hostPowerManagementEventHandler(Suspend); + } else { + hostPowerManagementEventHandler(Sleep); + } + } + } else { + if (initial_suspend_) + initial_suspend_ = false; + if (was_suspended_) { + was_suspended_ = false; + hostPowerManagementEventHandler(Resume); + } + } +#endif + + return EventHandlerResult::OK; +} + +} +} + +__attribute__((weak)) void hostPowerManagementEventHandler(kaleidoscope::plugin::HostPowerManagement::Event event) { +} + +kaleidoscope::plugin::HostPowerManagement HostPowerManagement; diff --git a/src/kaleidoscope/plugin/HostPowerManagement.h b/src/kaleidoscope/plugin/HostPowerManagement.h new file mode 100644 index 00000000..2dbb36f7 --- /dev/null +++ b/src/kaleidoscope/plugin/HostPowerManagement.h @@ -0,0 +1,57 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-HostPowerManagement -- Host power management support plugin. + * Copyright (C) 2017, 2018 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 + +#define _DEPRECATED_MESSAGE_ENABLEWAKEUP \ + "The HostPowerManagement.enableWakeup() call is not necessary anymore,\n" \ + "the firmware supports wakeup by default now. The line can be safely\n" \ + "removed." + +namespace kaleidoscope { +namespace plugin { +class HostPowerManagement : public kaleidoscope::Plugin { + public: + typedef enum { + Suspend, + Sleep, + Resume, + } Event; + + HostPowerManagement(void) {} + + void enableWakeup(void) DEPRECATED(ENABLEWAKEUP) {} + + EventHandlerResult beforeEachCycle(); + + private: + static bool was_suspended_; + static bool initial_suspend_; +}; +} + +// Backwards compatibility +typedef plugin::HostPowerManagement HostPowerManagement; + +} + +void hostPowerManagementEventHandler(kaleidoscope::plugin::HostPowerManagement::Event event); + +extern kaleidoscope::plugin::HostPowerManagement HostPowerManagement;