DK Button and LEDs¶
The DK Button and LED library is a simple module to interface with the buttons and LEDs on a Nordic Semiconductor development kit. It supports reading the state of up to four buttons or switches and controlling up to four LEDs.
If you want to retrieve information about the button state, initialize the library with dk_buttons_init()
.
You can pass a callback function during initialization.
This function is then called every time the button state changes.
If you want to control the LEDs on the development kit, initialize the library with dk_leds_init()
.
You can then set the value of a single LED, or set all of them to a state specified through bitmasks.
API documentation¶
include/dk_buttons_and_leds.h
lib/dk_buttons_and_leds/
-
group
dk_buttons_and_leds
Defines
-
DK_NO_LEDS_MSK
¶
-
DK_LED1
¶
-
DK_LED2
¶
-
DK_LED3
¶
-
DK_LED4
¶
-
DK_LED1_MSK
¶
-
DK_LED2_MSK
¶
-
DK_LED3_MSK
¶
-
DK_LED4_MSK
¶
-
DK_ALL_LEDS_MSK
¶
-
DK_NO_BTNS_MSK
¶
-
DK_BTN1
¶
-
DK_BTN2
¶
-
DK_BTN3
¶
-
DK_BTN4
¶
-
DK_BTN1_MSK
¶
-
DK_BTN2_MSK
¶
-
DK_BTN3_MSK
¶
-
DK_BTN4_MSK
¶
-
DK_ALL_BTNS_MSK
¶
Typedefs
Callback that is executed when a button state change is detected.
- Parameters
button_state
: Bitmask of button states.has_changed
: Bitmask that shows which buttons have changed.
Functions
-
int
dk_leds_init
(void)¶ Initialize the library to control the LEDs.
- Return Value
0
: If the operation was successful. Otherwise, a (negative) error code is returned.
Initialize the library to read the button state.
- Parameters
button_handler
: Callback handler for button state changes.
- Return Value
0
: If the operation was successful. Otherwise, a (negative) error code is returned.
Add a dynamic button handler callback.
In addition to the button handler function passed to dk_buttons_init, any number of button handlers can be added and removed at runtime.
- Parameters
[in] handler
: Handler structure. Must point to statically allocated memory.
Remove a dynamic button handler callback.
- Parameters
[in] handler
: Handler to remove.
- Return Value
0
: Successfully removed the handler.-ENOENT
: This button handler was not present.
Read current button states.
- Parameters
button_state
: Bitmask of button states.has_changed
: Bitmask that shows which buttons have changed.
Get current button state from internal variable.
- Return
Bitmask of button states.
-
int
dk_set_leds
(uint32_t leds)¶ Set value of LED pins as specified in one bitmask.
- Parameters
leds
: Bitmask that defines which LEDs to turn on and off.
- Return Value
0
: If the operation was successful. Otherwise, a (negative) error code is returned.
-
int
dk_set_leds_state
(uint32_t leds_on_mask, uint32_t leds_off_mask)¶ Set value of LED pins as specified in two bitmasks.
- Parameters
leds_on_mask
: Bitmask that defines which LEDs to turn on. If this bitmask overlaps withleds_off_mask
,leds_on_mask
has priority.leds_off_mask
: Bitmask that defines which LEDs to turn off. If this bitmask overlaps withleds_on_mask
,leds_on_mask
has priority.
- Return Value
0
: If the operation was successful. Otherwise, a (negative) error code is returned.
-
int
dk_set_led
(uint8_t led_idx, uint32_t val)¶ Set a single LED value.
This function turns a single LED on or off.
- See
- Parameters
led_idx
: Index of the LED.val
: Value for the LED: 1 - turn on, 0 - turn off
- Return Value
0
: If the operation was successful. Otherwise, a (negative) error code is returned.
-
int
dk_set_led_on
(uint8_t led_idx)¶ Turn a single LED on.
- Parameters
led_idx
: Index of the LED.
- Return Value
0
: If the operation was successful. Otherwise, a (negative) error code is returned.
-
int
dk_set_led_off
(uint8_t led_idx)¶ Turn a single LED off.
- Parameters
led_idx
: Index of the LED.
- Return Value
0
: If the operation was successful. Otherwise, a (negative) error code is returned.
- #include <dk_buttons_and_leds.h>
Button handler list entry.
Public Members
Callback function.
Linked list node, for internal use.
-