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(underglow): maximum brightness in Kconfig #669

Closed
wants to merge 5 commits into from

Conversation

jrhrsmit
Copy link
Contributor

@jrhrsmit jrhrsmit commented Feb 6, 2021

Current draw over USB is dominated by the underglow in every board that has it (or the battery charger).
Defining a maximum brightness to limit the maximum current can prevent the keyboard from exceeding the USB spec of 500mA (or at least reduce it a little).

I also added ranges to ZMK_RGB_UNDERGLOW_BRT_START and ZMK_RGB_UNDERGLOW_BRT_MAX, but this is not done for the other options. Should I add/remove them for consistency?

@petejohanson
Copy link
Contributor

I have some work to refactor this in #626, we'll probably need to rebase this once that's merged, the code changes should be pretty obvious, but might need tweaks since I replaced all uses of that 100 constant w/ a preprocessor define.

@petejohanson petejohanson added core Core functionality/behavior of ZMK enhancement New feature or request labels Feb 9, 2021
@jrhrsmit
Copy link
Contributor Author

Alright I replaced BRT_MAX by CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX.

The stuff about the inconsistency with the ranges in the Kconfig still stands though.

@joric
Copy link

joric commented Feb 17, 2021

Ack. Good fix, my first move when the keyboard lights up is to set the brightness to minumum, nRFMicro heats up pretty substantially with 28 LEDs (not sure why, maybe it's just SOD-323 vs SOD-123 as on 1.1 and on Pro Micros) There's another related issue, there's like zero brightness it's very confusing and I don't think it's intended. Need to be capped at 1 or 8 or whatever the lowest setting is, not at 0 (unless it's the same as turning LEDs off, which is complicated considering EXT_VCC).

@jrhrsmit
Copy link
Contributor Author

There's another related issue, there's like zero brightness it's very confusing and I don't think it's intended. Need to be capped at 1 or 8 or whatever the lowest setting is, not at 0 (unless it's the same as turning LEDs off, which is complicated considering EXT_VCC).

That seems logical, I'll add it.

@joric
Copy link

joric commented Feb 17, 2021

@jrhrsmit maybe add ZMK_RGB_UNDERGLOW_BRT_MIN then?

@jrhrsmit
Copy link
Contributor Author

jrhrsmit commented Feb 17, 2021

@joric Even though that would be a simple and small change, I don't really see the added value of that option to be honest. It seems to me the issue you're describing is a hardware issue. I'd be willing to add it though if there are more people who could use it.

Although it would be nice if the LED strip would turn off automatically when the brightness is set to 0, but that's for another PR I guess.

@joric
Copy link

joric commented Feb 17, 2021

@jrhrsmit I'm using the most popular shield of all time, corne keyboard so no it's not just my hardware issue. I'm not sure about the lowest value for LEDs (they start working at something way above 1 AFAIR) so having a user variable there would be nice.

@jrhrsmit
Copy link
Contributor Author

jrhrsmit commented Feb 17, 2021

@joric Although the shield you're using might be very popular, transistors are usually not supposed to heat up when there is no current flowing. Anyway, I looked at it a little, it seems the implementation of a minimum brightness setting is non-trivial, as something like

     if (b < CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN) {
        b = 0;

would not be enough to fix your problem (if I understood correctly, your transistors heat up at every value below 8 or so, so setting it to 0 wouldn't help). So instead of b = 0;, zmk_rgb_underglow_off() would have to be called then, which also implies that turning up the brightness afterwards wouldn't turn on the LEDs in the current state of the code, so in the end it would look something like this:

struct zmk_led_hsb zmk_rgb_underglow_calc_brt(int direction) {
    struct zmk_led_hsb color = state.color;

    int b = color.b + (direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP);
    if (state.on) {
        if (b < CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN) {
            b = 0;
            zmk_rgb_underglow_off();
        } else if (b > CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX) {
            b = CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX;
        }
        color.b = b;
    } else {
        if (direction > 0) {
            if ( b <CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN) {
                b =  CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN;
            }
            color.b = b;
            zmk_rgb_underglow_on();
        }
    }
    
    return color;
}

Which thus implies turning the rgb on and off inside zmk_rgb_underglow_calc_brt(). Let me know what your thoughts are on this.

edit; I think it would be better to move the evaluation of state.on and the calling of zmk_rgb_underglow_off() and zmk_rgb_underglow_on() to zmk_rgb_underglow_change_brt(), but it's the same idea.

@joric
Copy link

joric commented Feb 17, 2021

Why would I set them to 0 at all? I won't set them to 0 ever, it's confusing (there should be a single point of entry) and it's power inefficient (they consume up to 1 mA each in standby mode at 0 brightness). They dont "heat up" at every value below 8 or so they just don't light up. I need minimum brightness value to see if they're enabled or not.

The code is complicated, not sure I follow. I just wanted to have some lower value for the brightness, not just ZMK_RGB_UNDERGLOW_BRT_START and ZMK_RGB_UNDERGLOW_BRT_MAX but ZMK_RGB_UNDERGLOW_BRT_MIN as well because it's kind of vital to know if RGB is enabled or not.

Upd: Ah ok code tries to turn them off. I don't think I like that very much (again, single point of entry) and it probably conflicts with the EXT_VCC switch (or maybe not, not sure). I thought min value would be easier and more logical.

@jrhrsmit
Copy link
Contributor Author

jrhrsmit commented Feb 17, 2021

@joric Alright, then the most recent change which just adds the ZMK_RGB_UNDERGLOW_BRT_MIN should be just fine. I also realised the changing of the brightness doesn't have to imply turning the LEDs on or off, so the code snippet from earlier can be disregarded.

Btw I also took the liberty of adding the ranges to the other underglow Kconfig options and changing the descriptions a little.

app/src/rgb_underglow.c Outdated Show resolved Hide resolved
@jrhrsmit jrhrsmit force-pushed the max_brightness branch 2 times, most recently from 8e8a20f to 0e03da1 Compare February 17, 2021 18:06
app/Kconfig Show resolved Hide resolved
@@ -335,7 +335,7 @@ int zmk_rgb_underglow_toggle() {
}

int zmk_rgb_underglow_set_hsb(struct zmk_led_hsb color) {
if (color.h > HUE_MAX || color.s > SAT_MAX || color.b > BRT_MAX) {
if (color.h > HUE_MAX || color.s > SAT_MAX || color.b > CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX) {
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 wondering if CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX should be used as a cap here rather than an actual hard limit. For example, if someone has a key that sets the HSB to 0, 100, 100, should that be 100% of the cap, or should it really just error?

Copy link
Contributor

@Megamannen Megamannen Mar 1, 2021

Choose a reason for hiding this comment

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

100% of CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX, I see that as a hardware limitation implemented in software. (don't know what BRT_MAX is though ;) )

You can also draw a paralell to dimmers in houses, they have an "installation setting" MIN and MAX that depends on the installed lightning.
From the user side a dimmer value of 100% = MAX and an user value of 1% = MIN (0% is off)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like that scenario should be impossible. I proposed this change with hardware limitations in mind rather than a preference or something.

Copy link
Member

Choose a reason for hiding this comment

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

This scenario is definitely possible. All someone needs to do is add a &rgb_ug RGB_COLOR_HSB(100, 100, 100) and now that key silently fails to do anything because of this check.

@Nicell
Copy link
Member

Nicell commented Mar 1, 2021

I think we should also consider the possibility of a max/min brightness that's saved in settings that's outside of this range. On retrieval of RGB settings, we should probably do a check on if it's outside the Kconfig range and correct that.

@jrhrsmit
Copy link
Contributor Author

jrhrsmit commented Mar 3, 2021

I think we should also consider the possibility of a max/min brightness that's saved in settings that's outside of this range. On retrieval of RGB settings, we should probably do a check on if it's outside the Kconfig range and correct that.

In theory it would be impossible to set a brightness value in the settings that is outside of the Kconfig range, right?

@Nicell
Copy link
Member

Nicell commented Mar 3, 2021

In theory it would be impossible to set a brightness value in the settings that is outside of the Kconfig range, right?

It's impossible if the Kconfig range is set from the first start-up of the device and never changes. That's a rather big assumption in my opinion. I can definitely see people tweaking this value as they see fit.

@jrhrsmit jrhrsmit force-pushed the max_brightness branch 4 times, most recently from 56dc1d3 to 4cc10db Compare March 9, 2021 17:30
@jrhrsmit
Copy link
Contributor Author

jrhrsmit commented Mar 9, 2021

It's impossible if the Kconfig range is set from the first start-up of the device and never changes. That's a rather big assumption in my opinion. I can definitely see people tweaking this value as they see fit.

Ah true, I've added a check for that on lines 252-256, which should fix that.

@EmilFlach
Copy link

Hi @Nicell & @Megamannen!

Could this be merged? I've accidentally set my maximum brightness too high, causing some warnings. This PR would help me out. All feedback was implemented and all checks have passed, any reason not to merge?

app/src/rgb_underglow.c Outdated Show resolved Hide resolved
app/src/rgb_underglow.c Outdated Show resolved Hide resolved
app/src/rgb_underglow.c Outdated Show resolved Hide resolved
app/src/rgb_underglow.c Outdated Show resolved Hide resolved
@jrhrsmit
Copy link
Contributor Author

jrhrsmit commented Oct 7, 2021

@malinges should be good like this right?

app/src/rgb_underglow.c Outdated Show resolved Hide resolved
Comment on lines +61 to +64
double v = ((float)hsb.b / (float)BRT_MAX *
(float)(CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX - CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN) +
(float)CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN) /
(float)BRT_MAX;
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 how simple this solution is, but it feels like this min/max conversion of brightness should probably be preprocessed before being sent to the hsb_to_rgb function, as now this function does more than hsb_to_rgb functionality.

@Nicell
Copy link
Member

Nicell commented Oct 11, 2021

As stated in #944, thanks for your work on this PR @jrhrsmit. I'm happy with the final outcome of this feature. The other PR does lack the Kconfig updates to add ranges and update the descriptions that you've done here, so if you'd like to make a separate PR for that, that'd be awesome!

@Nicell Nicell closed this Oct 11, 2021
jrhrsmit pushed a commit to jrhrsmit/zmk that referenced this pull request Oct 11, 2021
@jrhrsmit
Copy link
Contributor Author

Well at least I'm happy we finally have this feature after eight months

Nicell pushed a commit that referenced this pull request Oct 11, 2021
ivandaho pushed a commit to ivandaho/zmk that referenced this pull request Oct 21, 2021
fsargent pushed a commit to fsargent/zmk that referenced this pull request Nov 19, 2021
Luberry pushed a commit to Luberry/zmk that referenced this pull request Nov 30, 2021
PedroDiogo pushed a commit to PedroDiogo/zmk that referenced this pull request Feb 7, 2022
tyalie pushed a commit to tyalie/zmk that referenced this pull request Nov 15, 2022
NikolaRavic pushed a commit to NikolaRavic/zmk-firmware that referenced this pull request Feb 27, 2023
yuanbin pushed a commit to yuanbin/zmk that referenced this pull request Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core functionality/behavior of ZMK enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants