Skip to content

Commit

Permalink
IdleLEDs: Implement a .suspend() and a .resume() method
Browse files Browse the repository at this point in the history
Sometimes we want to control LEDs outside of `IdleLEDs`, like through
`HostPowerManagement`, and in these cases, we would like `IdleLEDs` to have an
up-to-date idea about what's going on. That is, if the host enters sleep,
`IdleLEDs` should be aware of that, and enter idle state. Once the host wakes
up, either via the keyboard or otherwise, `IdleLEDs` should resume its tasks.

With the new `.suspend()` and `.resume()` method, this becomes possible.

This addresses a big part of #1287.

Signed-off-by: Gergely Nagy <[email protected]>
  • Loading branch information
algernon committed Oct 19, 2022
1 parent 8f1093b commit bb57a1f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
10 changes: 10 additions & 0 deletions plugins/Kaleidoscope-IdleLEDs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ the following properties and methods.
> Setting the timeout to 0 will disable the plugin until it is set to a higher
> value.
### `.suspend()`, `.resume()`

> Suspends or resumes `IdleLEDs`. When suspended, `IdleLEDs` will immediately
> enter idle state, and turn LEDs off. When resuming, it will turn the LEDs back
> on, and start checking idleness again.
>
> The intended use of these functions is when LEDs are turned on or off by other
> means, such as part of host power management, and syncing that state with
> `IdleLEDs` is desired.
## Focus commands

The plugin provides a single [Focus][FocusSerial] command, but only when using
Expand Down
32 changes: 22 additions & 10 deletions plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Idle-LEDs -- Turn off the LEDs when the keyboard's idle
* Copyright (C) 2018, 2019, 2020, 2021 Keyboard.io, Inc
* Copyright (C) 2018, 2019, 2020, 2021, 2022 Keyboard.io, Inc
* Copyright (C) 2019 Dygma, Inc
*
* This program is free software: you can redistribute it and/or modify it under
Expand Down Expand Up @@ -44,26 +44,38 @@ void IdleLEDs::setIdleTimeoutSeconds(uint32_t new_limit) {
idle_time_limit = new_limit * 1000;
}

void IdleLEDs::resume() {
// Enabling LEDs is fairly expensive, so we only do it if we have to.
if (!::LEDControl.isEnabled()) {
::LEDControl.enable();
}
idle_ = false;
start_time_ = Runtime.millisAtCycleStart();
}

void IdleLEDs::suspend() {
// Disabling LEDs is fairly expensive, so we only do it if we have to.
if (::LEDControl.isEnabled()) {
::LEDControl.disable();
}

idle_ = true;
}

EventHandlerResult IdleLEDs::beforeEachCycle() {
if (idle_time_limit == 0)
return EventHandlerResult::OK;

if (::LEDControl.isEnabled() &&
if (!idle_ &&
Runtime.hasTimeExpired(start_time_, idle_time_limit)) {
::LEDControl.disable();
idle_ = true;
suspend();
}

return EventHandlerResult::OK;
}

EventHandlerResult IdleLEDs::onKeyEvent(KeyEvent &event) {
if (idle_) {
::LEDControl.enable();
idle_ = false;
}

start_time_ = Runtime.millisAtCycleStart();
resume();

return EventHandlerResult::OK;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Idle-LEDs -- Turn off the LEDs when the keyboard's idle
* Copyright (C) 2018, 2019, 2021 Keyboard.io, Inc
* Copyright (C) 2018, 2019, 2021, 2022 Keyboard.io, Inc
* Copyright (C) 2019 Dygma, Inc
*
* This program is free software: you can redistribute it and/or modify it under
Expand Down Expand Up @@ -33,6 +33,8 @@ class IdleLEDs : public kaleidoscope::Plugin {

static uint32_t idleTimeoutSeconds();
static void setIdleTimeoutSeconds(uint32_t new_limit);
static void suspend();
static void resume();

EventHandlerResult beforeEachCycle();
EventHandlerResult onKeyEvent(KeyEvent &event);
Expand Down

0 comments on commit bb57a1f

Please sign in to comment.