Controlling User Buttons and LEDs
This application note describes how to configure User Buttons and LEDs in Zephyr BSP and use them in the user application.
The User Button devices are declared in nrf9151som_nrf9151_common.dtsi.dts:
buttons: buttons {
compatible = "gpio-keys";
button0: button_0 {
gpios = <&gpio0 18 (GPIO_ACTIVE_LOW)>;
label = "Push button S3";
zephyr,code = <INPUT_KEY_0>;
};
button1: button_1 {
gpios = <&gpio0 19 (GPIO_ACTIVE_LOW)>;
label = "Push button S4";
zephyr,code = <INPUT_KEY_1>;
};
};The devicetree node configures the following parameters:
Parameter | Value | Description |
|---|---|---|
| ||
|
| Enables the |
| ||
|
| GPIO pin. |
|
| Descriptive name of the key. |
|
| Key code to emit, which can be used by the application to process the key input. |
| ||
|
| GPIO pin. |
|
| Descriptive name of the key. |
|
| Key code to emit, which can be used by the application to process the key input. |
The User LED devices are declared in nrf9151som_nrf9151_common.dtsi.dts:
pwmleds0: pwmleds0 {
compatible = "pwm-leds";
label = "LED1";
user1_pwm_led: user1_pwm_led {
pwms = <&pwm0 0 PWM_MSEC(20) PWM_POLARITY_NORMAL>;
};
};
pwmleds1: pwmleds1 {
compatible = "pwm-leds";
label = "LED2";
user2_pwm_led: user2_pwm_led {
pwms = <&pwm1 0 PWM_MSEC(20) PWM_POLARITY_NORMAL>;
};
};Parameter | Value | Description |
|---|---|---|
| ||
|
| Enables the |
|
| Descriptive name of the LED. |
| ||
|
| PWM associated with LED1. |
| ||
|
| Enables the |
|
| Descriptive name of the LED. |
| ||
|
| PWM associated with LED2 |
The LEDs also require the PWM to be configured in the device tree:
&pwm0 {
status = "okay";
pinctrl-0 = <&pwm0_default>;
pinctrl-1 = <&pwm0_sleep>;
pinctrl-names = "default", "sleep";
};
&pwm1 {
status = "okay";
pinctrl-0 = <&pwm1_default>;
pinctrl-1 = <&pwm1_sleep>;
pinctrl-names = "default", "sleep";
};The button and LED-related modules are implemented in buttons.c, led_module.c and UI module files:
buttons.cdefines a callback for theinput_gpio_keys.cdriver. Once the button is pressed or released, the callback generates one of the following events:BUTTON_EVT_USER1_PRESSEDandBUTTON_EVT_USER1_RELEASEDfor User Button 1.BUTTON_EVT_USER2_PRESSED and BUTTON_EVT_USER2_RELEASEDfor User Button 2.
led_module.cmakes use of the Nordic CAF library, which is able to perform complex LED effects. The LED defines the following conditions and effects for User LED1 and User LED2:LED1 starts blinking with 500 ms period while the User Button 1 is pressed and the
BUTTON_EVT_USER1_PRESSEDevent is received by the module. The LED stops blinking once the button is released and the module receives theBUTTON_EVT_USER1_RELEASEDevent.LED2 starts blinking with 500 ms period while the User Button 2 is pressed and the
BUTTON_EVT_USER1_PRESSEDevent is received by the module. The LED stops blinking once the button is again.
the
ui_module.cis used to send a mask representation of the buttons state to the cloud every time when one of the button events is received by the module:0 if none of the buttons is pressed.
1 if only User 1 button is pressed.
2 if only User 2 button is pressed.
3 if both buttons are pressed.