Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdleLEDs: Implement a .suspend() and a .resume() method #1289

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
IdleLEDs: Implement a .suspend() and a .resume() method
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 <algernon@keyboard.io>
algernon committed Oct 19, 2022
commit 8d0a9ac02ff9db0355f13e34ecb15426c6033ac3
10 changes: 10 additions & 0 deletions plugins/Kaleidoscope-IdleLEDs/README.md
Original file line number Diff line number Diff line change
@@ -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
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
@@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what way is it expensive to enable them? I guess running an animated effect is expensive, but there's a similar comment around disabling, so I'm not sure what this comment is referring to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It refers to LEDControl.enable() and .disable() doing this:

  refreshAll(); // set_all_leds_to(CRGB(0, 0, 0)); in case of disable()
  Runtime.device().syncLeds();

The .syncLeds() part is the most expensive, but we don't want to refreshAll() on every "resume" either, only when we change states.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. It might make sense to push that logic down into LEDControl, then. Maybe that should be a separate PR.

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;
}
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
@@ -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);