Appiko
button_ui.c
1 
19 #include "nrf.h"
20 #include "button_ui.h"
21 #include "hal_gpio.h"
22 #include "boards.h"
23 
24 #if ISR_MANAGER == 1
25 #include "isr_manager.h"
26 #endif
27 
28 #ifndef BUTTON_ACTIVE_STATE
29 #error "The board definition file must specify the GPIO state on button press"
30 #endif
31 
32 #if BUTTON_ACTIVE_STATE == 1
33 #define GPIO_PULL_RESISTOR HAL_GPIO_PULL_DOWN
34 #define GPIO_PIN_SENSE GPIO_PIN_CNF_SENSE_High
35 #define BUTTON_PRESSED 1
36 #define BUTTON_RELEASED 0
37 #else
38 #define GPIO_PULL_RESISTOR HAL_GPIO_PULL_UP
39 #define GPIO_PIN_SENSE GPIO_PIN_CNF_SENSE_Low
40 #define BUTTON_PRESSED 0
41 #define BUTTON_RELEASED 1
42 #endif
43 
44 uint32_t btn_pin;
45 void (*handler)(button_ui_steps step, button_ui_action act);
46 //This bool is used generate an event of release action for the
47 //wake step, when the step count is still at 0, i.e. before press
48 bool wake_evt = false, btn_press_start = false;
49 
50 #if ISR_MANAGER == 1
51 void button_ui_gpiote_Handler ()
52 #else
53 void GPIOTE_IRQHandler(void)
54 #endif
55 {
56  wake_evt = true;
58 #if ISR_MANAGER == 0
59  NRF_GPIOTE->EVENTS_PORT = 0;
60  (void) NRF_GPIOTE->EVENTS_PORT;
61 #endif
62 
63  btn_press_start = true;
64 }
65 
66 void button_ui_init(uint32_t button_pin,
67  uint32_t irq_priority, void (*button_ui_handler)
69 {
70  hal_gpio_cfg(button_pin, GPIO_PIN_CNF_DIR_Input,
71  GPIO_PIN_CNF_INPUT_Connect, GPIO_PULL_RESISTOR,
72  GPIO_PIN_CNF_DRIVE_S0S1, GPIO_PIN_SENSE);
73 
74  btn_pin = button_pin;
75  handler = button_ui_handler;
76 
77  NRF_GPIOTE->EVENTS_PORT = 0;
78  NRF_GPIOTE->INTENSET = (GPIOTE_INTENSET_PORT_Enabled
79  << GPIOTE_INTENSET_PORT_Pos);
80 
81  NVIC_ClearPendingIRQ(GPIOTE_IRQn);
82  NVIC_SetPriority(GPIOTE_IRQn, irq_priority);
83  NVIC_EnableIRQ(GPIOTE_IRQn);
84 }
85 
86 void button_ui_add_tick(uint32_t ui_ticks)
87 {
88  static uint32_t ticks = 0, step = 0;
89 
90  if(btn_press_start == true)
91  {
92  ui_ticks = 1;
93  btn_press_start = false;
94  }
95 
96  if((hal_gpio_pin_read(btn_pin) == BUTTON_RELEASED)
97  && (0 == step))
98  {
99  ticks = 0;
100  if(wake_evt == true)
101  {
102  wake_evt = false;
104  }
105  return ;
106  }
107 
108  if((hal_gpio_pin_read(btn_pin) == BUTTON_PRESSED)){
109  ticks += ui_ticks;
110  }
111 
112  if(ticks > press_duration[step])
113  {
114  handler((button_ui_steps) step, BUTTON_UI_ACT_CROSS);
115  step++;
116  }
117 
118  if(hal_gpio_pin_read(btn_pin) == BUTTON_RELEASED)
119  {
120  uint32_t temp_step = step;
121  wake_evt = false;
122  step = ticks = 0;
123  handler((button_ui_steps) (temp_step - 1),
125  }
126 }
127 
128 void button_ui_config_wake(bool set_wake_on)
129 {
130  if (set_wake_on)
131  {
132  NRF_GPIOTE->INTENSET = (GPIOTE_INTENSET_PORT_Enabled
133  << GPIOTE_INTENSET_PORT_Pos);
134  }
135  else
136  {
137  NRF_GPIOTE->INTENCLR = (GPIOTE_INTENCLR_PORT_Enabled
138  << GPIOTE_INTENCLR_PORT_Pos);
139  }
140 }
141 
When a button press crosses a particular time.
Definition: button_ui.h:44
button_ui_action
Definition: button_ui.h:42
void button_ui_init(uint32_t button_pin, uint32_t irq_priority, void(*button_ui_handler)(button_ui_steps step, button_ui_action act))
Initialize the button ui event generator module.
Definition: button_ui.c:66
When a button is released.
Definition: button_ui.h:45
button_ui_steps
Definition: button_ui.h:51
void GPIOTE_IRQHandler()
Definition: main.c:192
void button_ui_config_wake(bool set_wake_on)
To enable/disable the button UI GPIOTE IRQ.
Definition: button_ui.c:128
For the moment a button is pressed.
Definition: button_ui.h:56
void button_ui_add_tick(uint32_t ui_ticks)
The ticks that need to be sent to the button UI module for its processing.
Definition: button_ui.c:86