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

(feat/enhance) Tie RGB Underglow into sleep statuses #516

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mcrosson
Copy link
Contributor

This PR includes the following

  • Ties the underglow on/off into the sleep status events so the underglow will blank when a device goes to sleep
  • Mild refactor of rgb underglow toggle to be a dedicated set of on/off methods
  • Slightly re-orders methods to be more clear / ordered

@mcrosson mcrosson changed the title Tie RGB Underglow into sleep statuses (feat/enhance) Tie RGB Underglow into sleep statuses Dec 15, 2020
Copy link
Member

@Nicell Nicell left a comment

Choose a reason for hiding this comment

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

Is there any chance you could separate the re-ordering of functions and the additions you've made into separate commits? It's hard to read the diff for me at the moment.

app/src/rgb_underglow.c Outdated Show resolved Hide resolved
@mcrosson mcrosson force-pushed the power-underglow-sleeps-support branch from 382ffdd to 609476f Compare December 15, 2020 21:29
@mcrosson
Copy link
Contributor Author

@Nicell force pushed an update that undoes the method order changes and should make the change log easier to follow

@mcrosson mcrosson force-pushed the power-underglow-sleeps-support branch from ab2805f to c81767f Compare December 18, 2020 00:17
@innovaker innovaker added enhancement New feature or request lighting labels Dec 20, 2020
@mcrosson mcrosson requested a review from Nicell December 29, 2020 01:39
Copy link
Member

@Nicell Nicell left a comment

Choose a reason for hiding this comment

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

I'm wondering if the optional save to state of the ext_power settings should be in a separate PR that also improves upon #417. I'm liking the direction you're going with optional save to state.

Comment on lines 290 to 293
// Don't auto on underglow unless it was on before the board went to sleep
if (state.previous_on) {
zmk_rgb_underglow_on(false);
}
Copy link
Member

Choose a reason for hiding this comment

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

I like the addition of the optional save to state, but is there any reason we can't then pull the previous state from settings instead of saving previous state on the state (the previous_on variable)? For example settings_load_subtree("rgb/underglow") will load the state in settings to the state variable. I think that could be used instead, and then we could get other previous state rather than just on state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem here is the settings save timeout(s). If the settings save timeout trips we don't know what previous state was valid relative to what was saved. I added the previous_on to work around the settings being saved and breaking the actual pre-sleep + pre-save state.

Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit confused. You've added the optional save to state parameter, so that settings save doesn't happen when sleeping etc, but now you don't trust the settings store to be proper?

Let's say I do this:

  • Turn off RGB
  • RGB is turned off in settings
  • Board goes to sleep and turns off RGB again, settings aren't saved due to the optional
  • Board wakes up and then pulls from settings, RGB is off as it should

And then alternatively I do this:

  • Turn on RGB
  • RGB is turned on in settings
  • Board goes to sleep and turns off RGB, settings aren't saved due to the optinal
  • Board wakes up and then pulls from settings, RGB is on as it should

In both these cases, settings is the source of truth. It seems to me that previous_on is the opposite of state.on, which in the first exercise above would cause RGB to turn on when it shouldn't.

I should also note that when the device goes into sleep mode, the settings can't be saved because the board is going into deep sleep. Also currently RGB turns off automatically at sleep with #417. I think maybe this should be refactored to work more alongside ZMK_ACTIVITY_IDLE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Couple notes that I caught while re-visiting this PR:

  • Updated to use settings_load_subtree("rgb/underglow") (will be pushed shortly)
  • Was already taking ZMK_ACTIVITY_IDLE into account so this will fire when the board sleeps or goes idle

Copy link
Contributor

Choose a reason for hiding this comment

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

I tested this out for a while, but I had a lot of trouble with the state being saved (basically what mcrosson described earlier).
Because I set the idle timeout to 5 seconds, the save timeout (CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE) had to be shorter than that (I set it to 3s) to save any changed settings before it goes to sleep. However, this configuration wasn't stable at all (I don't really know why, but it kept crashing. I guess NVM erases take too long or something).
My guess is that I'm the only one setting the idle timeout so low, and when the idle timeout is 10mins and the save debounce is 1min everything just works.
Although I do like the idea of the saved settings being the single source of truth, I do think saving so often to NVM is a bad thing in this case.
The easy solution would be to just add the previous_on again.

Besides all that, I would love to see a fade in/out when turning off and on. At some point I had implemented this as effects, but to transition back to the old effect it also requires a previous_effect and also the pixel values from when it went to sleep (although that could be implemented in a different way).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Were you able to try this when previous_on was present in the code?

Copy link
Contributor

@jrhrsmit jrhrsmit Jan 28, 2021

Choose a reason for hiding this comment

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

No, but when I just add the previous_on and this segment:

    case ZMK_ACTIVITY_ACTIVE:
        // Don't auto on underglow unless it was on before the board went to sleep
        if (state.previous_on) {
            zmk_rgb_underglow_on(false);
        }
        break;
    case ZMK_ACTIVITY_IDLE:
    case ZMK_ACTIVITY_SLEEP:
        state.previous_on = state.on;
        zmk_rgb_underglow_off(false);
        break;

Then it works.
Also, I just got an error that I haven't seen before (without the above edit):
<err> settings: set-value failure. key: rgb/underglow/state error(-22)
this error comes from zephyr/subsys/settings/src/settings.c:220, which might be what's causing my problem, but I haven't found what the return code means yet...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's about what I had for the previous_on code before.

The error could be related to the write not finishing or working due to sleep / idle related state.

This definitely needs additional work and I'm inclined to add previous_on back to the code as long as @Nicell agrees based on your feedback.

@mcrosson
Copy link
Contributor Author

I'm wondering if the optional save to state of the ext_power settings should be in a separate PR that also improves upon #417. I'm liking the direction you're going with optional save to state.

I'd like to keep it trim for this PR so the underglow turning off during sleep can get in faster for users to enhance battery life.

Maybe a new ticket / feature request to do better about when to save settings would be better. I feel like being better about setting saves (ie. only if saving differences) would be better as a dedicated PR and unit of work.

@mcrosson mcrosson requested a review from Nicell December 30, 2020 22:30
@mcrosson mcrosson force-pushed the power-underglow-sleeps-support branch from c81767f to f4f0ff5 Compare January 23, 2021 00:54
@mcrosson
Copy link
Contributor Author

@Nicell Updated the sources and rebased to include latest main branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lighting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants