ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_task_button.c
Go to the documentation of this file.
1 
15 #include "ruuvi_task_button.h"
16 #if RT_BUTTON_ENABLED
17 #include "ruuvi_driver_error.h"
18 #include "ruuvi_interface_gpio.h"
19 #include "ruuvi_interface_log.h"
20 #include "ruuvi_task_button.h"
21 #include "ruuvi_task_gpio.h"
22 
23 #include <stddef.h>
24 
25 static rd_status_t init_input_check (const rt_button_init_t * const rt_init)
26 {
27  rd_status_t err_code;
28 
29  if (NULL == rt_init)
30  {
31  err_code = RD_ERROR_NULL;
32  }
33  else if (NULL == rt_init->p_button_pins
34  || NULL == rt_init->p_button_active
35  || NULL == rt_init->p_button_handlers)
36  {
37  err_code = RD_ERROR_NULL;
38  }
39  else if (false == rt_gpio_is_init())
40  {
41  err_code = RD_ERROR_INVALID_STATE;
42  }
43  else
44  {
45  err_code = RD_SUCCESS;
46  }
47 
48  return err_code;
49 }
50 
51 rd_status_t rt_button_init (const rt_button_init_t * const rt_init)
52 {
53  rd_status_t err_code = init_input_check (rt_init);
54 
55  if (RD_SUCCESS == err_code)
56  {
57  for (size_t ii = 0; (ii < rt_init->num_buttons) && (RD_SUCCESS == err_code); ii++)
58  {
60 
61  if (NULL == rt_init->p_button_handlers[ii])
62  {
63  err_code |= RD_ERROR_NULL;
64  }
65 
66  if (RI_GPIO_HIGH == rt_init->p_button_active[ii])
67  {
69  }
70  else if (RI_GPIO_LOW == rt_init->p_button_active[ii])
71  {
73  }
74  else
75  {
76  err_code |= RD_ERROR_INVALID_PARAM;
77  }
78 
79  if (RD_SUCCESS == err_code)
80  {
81  err_code = ri_gpio_interrupt_enable (rt_init->p_button_pins[ii],
83  rt_init->p_button_handlers[ii]);
84  }
85  }
86 
87  if (RD_SUCCESS != err_code)
88  {
89  // Ignore any further errors, return original
90  (void) rt_button_uninit (rt_init);
91  }
92  }
93 
94  return err_code;
95 }
96 
97 rd_status_t rt_button_uninit (const rt_button_init_t * const rt_init)
98 {
99  rd_status_t err_code = init_input_check (rt_init);
100 
101  if (RD_SUCCESS == err_code)
102  {
103  for (size_t ii = 0; (ii < rt_init->num_buttons) && (RD_SUCCESS == err_code); ii++)
104  {
105  err_code = ri_gpio_interrupt_disable (rt_init->p_button_pins[ii]);
106  }
107  }
108 
109  return err_code;
110 }
111 
112 #else // RT_BUTTON_ENABLED
114 {
115  return RD_ERROR_NOT_ENABLED;
116 }
117 
119 {
120  return RD_ERROR_NOT_ENABLED;
121 }
122 #endif
#define RD_ERROR_INVALID_PARAM
Invalid Parameter.
#define RD_ERROR_NULL
Null Pointer.
uint32_t rd_status_t
bitfield for representing errors
#define RD_ERROR_NOT_ENABLED
Driver is not enabled.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
rd_status_t ri_gpio_interrupt_enable(const ri_gpio_id_t pin, const ri_gpio_slope_t slope, const ri_gpio_mode_t mode, const ri_gpio_interrupt_fp_t handler)
Enable interrupt on a pin.
rd_status_t ri_gpio_interrupt_disable(const ri_gpio_id_t pin)
Disable interrupt on a pin.
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
ri_gpio_mode_t
@ RI_GPIO_MODE_INPUT_PULLUP
Input, can be read. Pulled up by internal resistor, value depends on IC.
@ RI_GPIO_MODE_HIGH_Z
High-impedance mode, electrically disconnected.
@ RI_GPIO_MODE_INPUT_PULLDOWN
Input, can be read. Pulled dpwn by internal resistor, value depends on IC.
@ RI_GPIO_LOW
GPIO electrically low.
@ RI_GPIO_HIGH
GPIO electrically high.
rd_status_t rt_button_init(const rt_button_init_t *const rt_init)
Button initialization function.
rd_status_t rt_button_uninit(const rt_button_init_t *const rt_init)
Button uninitialization function.
bool rt_gpio_is_init(void)
check that GPIO is initialized.
Struct for initializing buttons.
const ri_gpio_state_t * p_button_active
Array of button active states.
const size_t num_buttons
Number of buttons to initialize.
const rt_button_fp_t * p_button_handlers
Array of button handlers.
const ri_gpio_id_t * p_button_pins
Array of button pins.