forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dk_buttons_and_leds.h
173 lines (151 loc) · 5.07 KB
/
dk_buttons_and_leds.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#ifndef DK_BUTTON_AND_LEDS_H__
#define DK_BUTTON_AND_LEDS_H__
/** @file dk_buttons_and_leds.h
* @brief Module for handling buttons and LEDs on Nordic DKs.
* @defgroup dk_buttons_and_leds DK buttons and LEDs
* @{
*/
#include <zephyr/types.h>
#include <zephyr/sys/slist.h>
#ifdef __cplusplus
extern "C" {
#endif
#define DK_NO_LEDS_MSK (0)
#define DK_LED1 0
#define DK_LED2 1
#define DK_LED3 2
#define DK_LED4 3
#define DK_LED1_MSK BIT(DK_LED1)
#define DK_LED2_MSK BIT(DK_LED2)
#define DK_LED3_MSK BIT(DK_LED3)
#define DK_LED4_MSK BIT(DK_LED4)
#define DK_ALL_LEDS_MSK (DK_LED1_MSK | DK_LED2_MSK |\
DK_LED3_MSK | DK_LED4_MSK)
#define DK_NO_BTNS_MSK (0)
#define DK_BTN1 0
#define DK_BTN2 1
#define DK_BTN3 2
#define DK_BTN4 3
#define DK_BTN1_MSK BIT(DK_BTN1)
#define DK_BTN2_MSK BIT(DK_BTN2)
#define DK_BTN3_MSK BIT(DK_BTN3)
#define DK_BTN4_MSK BIT(DK_BTN4)
#define DK_ALL_BTNS_MSK (DK_BTN1_MSK | DK_BTN2_MSK | \
DK_BTN3_MSK | DK_BTN4_MSK)
/**
* @typedef button_handler_t
* @brief Callback that is executed when a button state change is detected.
*
* @param button_state Bitmask of button states.
* @param has_changed Bitmask that shows which buttons have changed.
*/
typedef void (*button_handler_t)(uint32_t button_state, uint32_t has_changed);
/** Button handler list entry. */
struct button_handler {
button_handler_t cb; /**< Callback function. */
sys_snode_t node; /**< Linked list node, for internal use. */
};
/** @brief Initialize the library to control the LEDs.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int dk_leds_init(void);
/** @brief Initialize the library to read the button state.
*
* @param button_handler Callback handler for button state changes.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int dk_buttons_init(button_handler_t button_handler);
/** @brief Add a dynamic button handler callback.
*
* In addition to the button handler function passed to
* @ref dk_buttons_init, any number of button handlers can be added and removed
* at runtime.
*
* @param[in] handler Handler structure. Must point to statically allocated
* memory.
*/
void dk_button_handler_add(struct button_handler *handler);
/** @brief Remove a dynamic button handler callback.
*
* @param[in] handler Handler to remove.
*
* @retval 0 Successfully removed the handler.
* @retval -ENOENT This button handler was not present.
*/
int dk_button_handler_remove(struct button_handler *handler);
/** @brief Read current button states.
*
* @param button_state Bitmask of button states.
* @param has_changed Bitmask that shows which buttons have changed.
*/
void dk_read_buttons(uint32_t *button_state, uint32_t *has_changed);
/** @brief Get current button state from internal variable.
*
* @return Bitmask of button states.
*/
uint32_t dk_get_buttons(void);
/** @brief Set value of LED pins as specified in one bitmask.
*
* @param leds Bitmask that defines which LEDs to turn on and off.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int dk_set_leds(uint32_t leds);
/** @brief Set value of LED pins as specified in two bitmasks.
*
* @param leds_on_mask Bitmask that defines which LEDs to turn on.
* If this bitmask overlaps with @p leds_off_mask,
* @p leds_on_mask has priority.
*
* @param leds_off_mask Bitmask that defines which LEDs to turn off.
* If this bitmask overlaps with @p leds_on_mask,
* @p leds_on_mask has priority.
*
* @retval 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);
/** @brief Set a single LED value.
*
* This function turns a single LED on or off.
*
* @param led_idx Index of the LED.
* @param val Value for the LED: 1 - turn on, 0 - turn off
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*
* @sa dk_set_led_on, dk_set_led_off
*/
int dk_set_led(uint8_t led_idx, uint32_t val);
/** @brief Turn a single LED on.
*
* @param led_idx Index of the LED.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int dk_set_led_on(uint8_t led_idx);
/** @brief Turn a single LED off.
*
* @param led_idx Index of the LED.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int dk_set_led_off(uint8_t led_idx);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* DK_BUTTON_AND_LEDS_H__ */