-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.c
126 lines (106 loc) · 3.04 KB
/
gpio.c
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
#include "gpio.h"
#define NRF_LOG_MODULE_NAME "GPIO"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "clock.h"
#include "encoder.h"
#define LONG_CLICK_MS 1000
#define LONG_LONG_CLICK_MS 3000
#define DOUBLE_CLICK_MS 300
static volatile uint8_t* m_gpio_button_flag;
static volatile uint8_t* m_gpio_long_button_flag;
static volatile uint8_t* m_gpio_long_long_button_flag;
static volatile uint8_t* m_gpio_double_button_flag;
static uint32_t last_click = 0;
static uint32_t pulse_start = 0;
static uint32_t pulse_stop = 0;
static uint32_t now = 0;
static uint32_t pulse_len = 0;
static uint8_t long_timeout = 1;
static uint8_t long_long_timeout = 1;
static uint8_t click_timeout = 1;
static uint8_t old_in = 0;
static uint8_t new_in = 0;
void gpio_init() {
nrf_gpio_cfg_input(SW_PIN,NRF_GPIO_PIN_PULLUP);
}
void gpio_button_set_flag(volatile uint8_t* main_button_flag)
{
m_gpio_button_flag = main_button_flag;
}
void gpio_long_button_set_flag(volatile uint8_t* main_long_button_flag)
{
m_gpio_long_button_flag = main_long_button_flag;
}
void gpio_long_long_button_set_flag(volatile uint8_t* main_long_long_button_flag)
{
m_gpio_long_long_button_flag = main_long_long_button_flag;
}
void gpio_double_button_set_flag(volatile uint8_t* main_double_button_flag)
{
m_gpio_double_button_flag = main_double_button_flag;
}
void gpio_process(void) {
new_in = nrf_gpio_pin_read(SW_PIN);
nrf_delay_ms(1);
if (new_in != nrf_gpio_pin_read(SW_PIN)){
return;
}
if (new_in){
encoder_play();
} else {
encoder_pause();
}
now = clock_get_timestamp();
if (old_in != new_in){
if (!new_in){
pulse_start = now;
click_timeout = 0;
long_timeout = 0;
long_long_timeout = 0;
} else {
pulse_stop = now;
pulse_len = pulse_stop - pulse_start;
if (pulse_len < LONG_LONG_CLICK_MS){
if ((now - last_click) <= DOUBLE_CLICK_MS){
*m_gpio_double_button_flag = 1;
NRF_LOG_INFO("double_button!\r\n");
click_timeout = 1;
}
last_click = now;
}
long_timeout = 0;
long_long_timeout = 0;
}
}
if (new_in && (now >= (last_click + DOUBLE_CLICK_MS)) && !click_timeout){
*m_gpio_button_flag = 1;
NRF_LOG_INFO("simple_button!\r\n");
click_timeout = 1;
long_timeout = 1;
long_long_timeout = 1;
}
if (!new_in && !long_timeout){
pulse_len = now - pulse_start;
if (pulse_len > LONG_CLICK_MS){
*m_gpio_long_button_flag = 1;
NRF_LOG_INFO("long_button!\r\n");
long_timeout = 1;
click_timeout = 1;
}
}
if (!new_in && !long_long_timeout){
pulse_len = now - pulse_start;
if (pulse_len > LONG_LONG_CLICK_MS){
*m_gpio_long_long_button_flag = 1;
NRF_LOG_INFO("long_long_button!\r\n");
long_timeout = 1;
long_long_timeout = 1;
click_timeout = 1;
}
}
old_in = new_in;
NRF_LOG_FLUSH();
};