45 #include "nordic_common.h"
46 #include "nrf_drv_gpiote.h"
47 #include "nrf_assert.h"
50 #include "lib/sensors.h"
54 #define DEBOUNCE_DURATION (CLOCK_SECOND >> 5)
59 struct timer debounce;
61 clock_time_t duration;
64 static struct btn_timer btn_timer[BUTTONS_NUMBER];
65 static int btn_state = 0;
77 int id = pin - BUTTON_START;
91 if(nrf_drv_gpiote_in_is_set(pin) == 0) {
93 btn_timer[id].duration = 0;
95 btn_timer[id].duration =
clock_time() - btn_timer[id].start;
97 sensors_changed(&buttons[
id]);
111 config(
int type,
int c, nrf_drv_gpiote_pin_t pin)
113 int id = pin - BUTTON_START;
116 case SENSORS_HW_INIT: {
117 nrf_drv_gpiote_in_config_t
config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(
false);
118 config.pull = NRF_GPIO_PIN_PULLUP;
123 case SENSORS_ACTIVE: {
125 nrf_drv_gpiote_in_event_enable(pin,
true);
126 btn_state |= (1 << id);
128 nrf_drv_gpiote_in_event_disable(pin);
129 btn_state &= ~(1 << id);
148 return config(type, value, BSP_BUTTON_0);
161 return config(type, value, BSP_BUTTON_1);
174 return config(type, value, BSP_BUTTON_2);
187 return config(type, value, BSP_BUTTON_3);
201 value(
int type, nrf_drv_gpiote_pin_t pin)
205 return nrf_drv_gpiote_in_is_set(pin) == 0 ?
206 BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
208 return btn_timer[pin - BUTTON_START].duration;
222 return value(type, BSP_BUTTON_0);
233 return value(type, BSP_BUTTON_1);
244 return value(type, BSP_BUTTON_2);
255 return value(type, BSP_BUTTON_3);
265 status(
int type, nrf_drv_gpiote_pin_t pin)
270 return (btn_state & (1 << (pin - BUTTON_START)));
285 return status(type, BSP_BUTTON_0);
296 return status(type, BSP_BUTTON_1);
307 return status(type, BSP_BUTTON_2);
318 return status(type, BSP_BUTTON_3);
321 const struct sensors_sensor buttons[BUTTONS_NUMBER] = {
static int status(int type, nrf_drv_gpiote_pin_t pin)
Get status of a given button.
#define DEBOUNCE_DURATION
Delay before button state is assumed to be stable.
static int value_button_3(int type)
Return current state of a button 3.
static int value_button_4(int type)
Return current state of a button 4.
static void start(void)
Start measurement.
static int status_button_2(int type)
Status function for button 2.
CCIF clock_time_t clock_time(void)
Get the current clock time.
#define BUTTON_SENSOR_VALUE_DURATION
Can be passed to value() function to get low state duration.
static int config_button_1(int type, int value)
Configuration function for button 1.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
static int config_button_2(int type, int value)
Configuration function for button 2.
static int config_button_4(int type, int value)
Configuration function for button 4.
static int config(int type, int c, nrf_drv_gpiote_pin_t pin)
Configuration function for the button sensor for all buttons.
static int status_button_4(int type)
Status function for button 3.
static int value_button_2(int type)
Return current state of a button 2.
static int status_button_1(int type)
Status function for button 1.
static int status_button_3(int type)
Status function for button 3.
#define BUTTON_SENSOR_VALUE_STATE
Can be passed to value() function to get current button state.
static int config_button_3(int type, int value)
Configuration function for button 3.
Header file for the nRF52dk button driver.
static int value_button_1(int type)
Return current state of a button 1.
static int value(int type, nrf_drv_gpiote_pin_t pin)
Return current state of a button.
int timer_expired(struct timer *t)
Check if a timer has expired.
static void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
Button toggle handler.