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

Configurable LED brightness #3

Closed
diegolhambi opened this issue Oct 19, 2024 · 9 comments
Closed

Configurable LED brightness #3

diegolhambi opened this issue Oct 19, 2024 · 9 comments

Comments

@diegolhambi
Copy link

Is it possible to change LED brightness using PWM or something similar?

@caksoylar
Copy link
Owner

In theory I think it should be, currently we use the GPIO LEDs implementation so it is on/off only. I think it'd be nice if the implementation would work with both types and we define PWM LEDs for the supported boards along with brightness setting support. I hadn't looked into it before because it hasn't seemed necessary but I am happy to look into it when I have a chance or have someone else contribute it.

@diegolhambi
Copy link
Author

I tried to fiddle around the code, but I don't understand C and Zephyr very well. 😞

From what I searched the internal LEDs from XIAO can be used with PWM, it is correct?

@caksoylar
Copy link
Owner

I believe so. I see your attempts at main...diegolhambi:zmk-rgbled-widget:main which seems mostly correct but I don't see where you define pwm_led* nodes that you refer to. I assume they'll need to be defined in the pinctrl node of the board, like mentioned in https://zmk.dev/docs/features/backlight#multiple-backlight-leds. And seeeduino_xiao_ble board doesn't define them by default here.

I think you also want only one pinctrl node &pwm_led, then you can refer to indices n under it with pwms = <&pwm0 n PWM_MSEC(10) PWM_POLARITY_NORMAL>; etc., like documented in the backlight page.

If it helps, there is a PWM sample in Zephyr showing the code API and board overlays as well.

@diegolhambi
Copy link
Author

diegolhambi commented Oct 22, 2024

Thanks for the info, you helped a lot!

I've been able to make work declaring the pwm-leds in the overlay, however the red LED was always light up so I put a initialization resetting the brightness to 0 in the led_init_thread and worked as intended.

I found one bug, soon after powering up the board, all LEDs light up before the indicate_battery kick in and I don't have clue why. 😆

@caksoylar
Copy link
Owner

caksoylar commented Oct 22, 2024

That's great to hear 😄

Regarding the bad initialization, looking at this it looks like you might want to set nordic,invert; in the pinctrl node since we want the pin to be high for the LEDs to be disabled: https://docs.zephyrproject.org/latest/build/dts/api/bindings/pinctrl/nordic%2Cnrf-pinctrl.html

@diegolhambi
Copy link
Author

diegolhambi commented Oct 22, 2024

Setting nordic,invert; fixed the bad initialization 💯.

I removed the "reset" in the led_init_thread too.

@diegolhambi
Copy link
Author

I think I have made something like what you described.

#if IS_ENABLED(CONFIG_PWM)

#define LED_NODE_ID DT_COMPAT_GET_ANY_STATUS_OKAY(pwm_leds)

#else

#define LED_NODE_ID DT_COMPAT_GET_ANY_STATUS_OKAY(gpio_leds)

#endif

This will load the correct driver if the board have the PWM enabled.

@caksoylar
Copy link
Owner

caksoylar commented Oct 22, 2024

Nice! As long as you also gate the brightness setting function calls with the #if/else, should be all good. A PR would be welcome.

@diegolhambi
Copy link
Author

I'm going to close this PR because configuring PWM on the nRF activates an additional clock, and there are no effective gains in battery consumption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants