Skip to content

Commit bb57a1f

Browse files
committed
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 <[email protected]>
1 parent 8f1093b commit bb57a1f

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

plugins/Kaleidoscope-IdleLEDs/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ the following properties and methods.
9191
> Setting the timeout to 0 will disable the plugin until it is set to a higher
9292
> value.
9393
94+
### `.suspend()`, `.resume()`
95+
96+
> Suspends or resumes `IdleLEDs`. When suspended, `IdleLEDs` will immediately
97+
> enter idle state, and turn LEDs off. When resuming, it will turn the LEDs back
98+
> on, and start checking idleness again.
99+
>
100+
> The intended use of these functions is when LEDs are turned on or off by other
101+
> means, such as part of host power management, and syncing that state with
102+
> `IdleLEDs` is desired.
103+
94104
## Focus commands
95105

96106
The plugin provides a single [Focus][FocusSerial] command, but only when using

plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- mode: c++ -*-
22
* Kaleidoscope-Idle-LEDs -- Turn off the LEDs when the keyboard's idle
3-
* Copyright (C) 2018, 2019, 2020, 2021 Keyboard.io, Inc
3+
* Copyright (C) 2018, 2019, 2020, 2021, 2022 Keyboard.io, Inc
44
* Copyright (C) 2019 Dygma, Inc
55
*
66
* 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) {
4444
idle_time_limit = new_limit * 1000;
4545
}
4646

47+
void IdleLEDs::resume() {
48+
// Enabling LEDs is fairly expensive, so we only do it if we have to.
49+
if (!::LEDControl.isEnabled()) {
50+
::LEDControl.enable();
51+
}
52+
idle_ = false;
53+
start_time_ = Runtime.millisAtCycleStart();
54+
}
55+
56+
void IdleLEDs::suspend() {
57+
// Disabling LEDs is fairly expensive, so we only do it if we have to.
58+
if (::LEDControl.isEnabled()) {
59+
::LEDControl.disable();
60+
}
61+
62+
idle_ = true;
63+
}
64+
4765
EventHandlerResult IdleLEDs::beforeEachCycle() {
4866
if (idle_time_limit == 0)
4967
return EventHandlerResult::OK;
5068

51-
if (::LEDControl.isEnabled() &&
69+
if (!idle_ &&
5270
Runtime.hasTimeExpired(start_time_, idle_time_limit)) {
53-
::LEDControl.disable();
54-
idle_ = true;
71+
suspend();
5572
}
5673

5774
return EventHandlerResult::OK;
5875
}
5976

6077
EventHandlerResult IdleLEDs::onKeyEvent(KeyEvent &event) {
61-
if (idle_) {
62-
::LEDControl.enable();
63-
idle_ = false;
64-
}
65-
66-
start_time_ = Runtime.millisAtCycleStart();
78+
resume();
6779

6880
return EventHandlerResult::OK;
6981
}

plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- mode: c++ -*-
22
* Kaleidoscope-Idle-LEDs -- Turn off the LEDs when the keyboard's idle
3-
* Copyright (C) 2018, 2019, 2021 Keyboard.io, Inc
3+
* Copyright (C) 2018, 2019, 2021, 2022 Keyboard.io, Inc
44
* Copyright (C) 2019 Dygma, Inc
55
*
66
* This program is free software: you can redistribute it and/or modify it under
@@ -33,6 +33,8 @@ class IdleLEDs : public kaleidoscope::Plugin {
3333

3434
static uint32_t idleTimeoutSeconds();
3535
static void setIdleTimeoutSeconds(uint32_t new_limit);
36+
static void suspend();
37+
static void resume();
3638

3739
EventHandlerResult beforeEachCycle();
3840
EventHandlerResult onKeyEvent(KeyEvent &event);

0 commit comments

Comments
 (0)