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

Header file: include/dk_buttons_and_leds.h
Source files: 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

typedef void (*button_handler_t)(uint32_t button_state, uint32_t has_changed)

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.

int dk_buttons_init(button_handler_t button_handler)

Initialize the library to read the button state.

Parameters
Return Value
  • 0: If the operation was successful. Otherwise, a (negative) error code is returned.

void dk_button_handler_add(struct button_handler *handler)

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.

int dk_button_handler_remove(struct button_handler *handler)

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.

void dk_read_buttons(uint32_t *button_state, uint32_t *has_changed)

Read current button states.

Parameters
  • button_state: Bitmask of button states.

  • has_changed: Bitmask that shows which buttons have changed.

uint32_t dk_get_buttons(void)

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 with leds_off_mask, leds_on_mask has priority.

  • leds_off_mask: Bitmask that defines which LEDs to turn off. If this bitmask overlaps with leds_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

dk_set_led_on, dk_set_led_off

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.

struct button_handler
#include <dk_buttons_and_leds.h>

Button handler list entry.

Public Members

button_handler_t cb

Callback function.

sys_snode_t node

Linked list node, for internal use.