-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton_control.c
More file actions
233 lines (187 loc) · 7.94 KB
/
button_control.c
File metadata and controls
233 lines (187 loc) · 7.94 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "button_control.h"
#include "EPD_driver.h" // For digitalRead, pinMode macros
#include "constants.h"
#include "main.h" // For timestamp(), updatemsdata(), is_ble_active(), advertising_restart_with_updated_msd()
#include "nrf_drv_gpiote.h"
#include "nrf_gpio.h"
#include "nrf_log.h"
#include "structs.h"
extern struct GlobalConfig globalConfig;
extern uint8_t dynamicreturndata[11]; // Defined in main.c
static ButtonState buttonStates[MAX_BUTTONS] = {0};
static uint8_t buttonStateCount = 0;
static volatile bool buttonEventPending = false;
static volatile uint8_t lastChangedButtonIndex = BUTTON_INVALID_INDEX;
static void handle_button_isr(uint8_t buttonIndex);
static void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
for (uint8_t i = 0; i < buttonStateCount; i++) {
if (buttonStates[i].initialized && buttonStates[i].pin == pin) {
handle_button_isr(i);
break;
}
}
}
static void handle_button_isr(uint8_t buttonIndex) {
if (buttonIndex >= MAX_BUTTONS || !buttonStates[buttonIndex].initialized) {
return;
}
ButtonState* btn = &buttonStates[buttonIndex];
bool pinState = digitalRead(btn->pin);
bool pressed = btn->inverted ? !pinState : pinState;
uint8_t newState = pressed ? 1 : 0;
if (newState != btn->current_state) {
btn->current_state = newState;
lastChangedButtonIndex = buttonIndex;
if (pressed) {
if (btn->press_count < BUTTON_MAX_PRESS_COUNT) {
btn->press_count++;
}
}
buttonEventPending = true;
}
}
void button_init(void) {
if (!nrf_drv_gpiote_is_init()) {
ret_code_t err_code = nrf_drv_gpiote_init();
if (err_code != NRF_SUCCESS) {
NRF_LOG_ERROR("Failed to initialize GPIOTE: %d", err_code);
return;
}
}
buttonStateCount = 0;
for (uint8_t i = 0; i < MAX_BUTTONS; i++) {
buttonStates[i].initialized = false;
buttonStates[i].button_id = 0;
buttonStates[i].press_count = 0;
buttonStates[i].last_press_time = 0;
buttonStates[i].current_state = 0;
buttonStates[i].byte_index = BUTTON_INVALID_INDEX;
buttonStates[i].pin = GPIO_PIN_UNUSED;
buttonStates[i].instance_index = BUTTON_INVALID_INDEX;
}
if (globalConfig.binary_input_count == 0) {
return;
}
for (uint8_t instanceIdx = 0; instanceIdx < globalConfig.binary_input_count; instanceIdx++) {
struct BinaryInputs* input = &globalConfig.binary_inputs[instanceIdx];
if (input->input_type != 1) { // 1 = button type
continue;
}
if (input->button_data_byte_index >= 11) {
NRF_LOG_WARNING("BinaryInputs instance %d has invalid byte_index (%d), skipping",
instanceIdx, input->button_data_byte_index);
continue;
}
uint8_t* instancePins[8] = {
&input->reserved_pin_1,
&input->reserved_pin_2,
&input->reserved_pin_3,
&input->reserved_pin_4,
&input->reserved_pin_5,
&input->reserved_pin_6,
&input->reserved_pin_7,
&input->reserved_pin_8
};
for (uint8_t pinIdx = 0; pinIdx < 8; pinIdx++) {
uint8_t pin = *instancePins[pinIdx];
if (pin == GPIO_PIN_UNUSED) {
continue;
}
if (buttonStateCount >= MAX_BUTTONS) {
NRF_LOG_WARNING("Maximum button count (%d) reached, skipping remaining pins", MAX_BUTTONS);
break;
}
ButtonState* btn = &buttonStates[buttonStateCount];
btn->button_id = ((input->instance_number * 8) + pinIdx) % 8;
btn->byte_index = input->button_data_byte_index;
btn->pin = pin;
btn->instance_index = instanceIdx;
btn->press_count = 0;
btn->last_press_time = 0;
btn->pin_offset = pinIdx;
btn->inverted = (input->invert & (1 << pinIdx)) != 0;
bool hasPullup = (input->pullups & (1 << pinIdx)) != 0;
bool hasPulldown = (input->pulldowns & (1 << pinIdx)) != 0;
if (hasPullup) {
pinMode(pin, INPUT_PULLUP);
} else if (hasPulldown) {
pinMode(pin, INPUT_PULLDOWN);
} else {
pinMode(pin, INPUT);
}
nrf_delay_ms(BUTTON_PIN_SETTLE_MS);
bool initialPinState = digitalRead(pin);
bool initialPressed = btn->inverted ? !initialPinState : initialPinState;
btn->current_state = initialPressed ? 1 : 0;
nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
if (hasPullup) {
in_config.pull = NRF_GPIO_PIN_PULLUP;
} else if (hasPulldown) {
in_config.pull = NRF_GPIO_PIN_PULLDOWN;
} else {
in_config.pull = NRF_GPIO_PIN_NOPULL;
}
ret_code_t err_code = nrf_drv_gpiote_in_init(pin, &in_config, gpiote_event_handler);
if (err_code != NRF_SUCCESS) {
NRF_LOG_ERROR("Failed to initialize GPIOTE for pin %d: %d", pin, err_code);
continue;
}
nrf_drv_gpiote_in_event_enable(pin, true);
btn->initialized = true;
buttonStateCount++;
}
}
}
void process_button_events(void) {
if (!buttonEventPending) {
return;
}
buttonEventPending = false;
uint32_t currentTime = timestamp(); // Current time in seconds
uint8_t changedButtonIndex = lastChangedButtonIndex;
lastChangedButtonIndex = BUTTON_INVALID_INDEX;
if (changedButtonIndex >= MAX_BUTTONS || !buttonStates[changedButtonIndex].initialized) {
return;
}
ButtonState* btn = &buttonStates[changedButtonIndex];
if (btn->current_state == 1) { // Button is pressed
bool resetCount = false;
if (btn->last_press_time == 0 || (currentTime - btn->last_press_time) > BUTTON_PRESS_TIMEOUT_SEC) {
resetCount = true;
}
if (btn->last_press_time > 0) {
for (uint8_t j = 0; j < buttonStateCount; j++) {
if (j != changedButtonIndex && buttonStates[j].initialized &&
buttonStates[j].last_press_time > 0 &&
buttonStates[j].last_press_time > btn->last_press_time &&
(currentTime - buttonStates[j].last_press_time) < BUTTON_PRESS_TIMEOUT_SEC) {
resetCount = true;
break;
}
}
}
if (resetCount) {
for (uint8_t j = 0; j < buttonStateCount; j++) {
if (buttonStates[j].initialized) {
buttonStates[j].press_count = 0;
}
}
btn->press_count = 1;
}
btn->last_press_time = currentTime;
}
bool pinState = digitalRead(btn->pin);
bool logicalPressed = btn->inverted ? !pinState : pinState;
uint8_t logicalState = logicalPressed ? 1 : 0;
btn->current_state = logicalState;
uint8_t buttonData = (btn->button_id & BUTTON_ID_MASK) | // Bits 0-2: button_id
((btn->press_count & PRESS_COUNT_MASK) << PRESS_COUNT_SHIFT) | // Bits 3-6: press_count (0-15)
((btn->current_state & BUTTON_STATE_MASK) << BUTTON_STATE_SHIFT); // Bit 7: current_state
if (btn->byte_index < 11) {
dynamicreturndata[btn->byte_index] = buttonData;
}
updatemsdata();
if (!is_ble_active()) {
advertising_restart_with_updated_msd();
}
}