]> git.scottworley.com Git - tattlekey/blob - client/button.c
client: Change wire format!: Expand `ago` field: 16 bits → 32 bits
[tattlekey] / client / button.c
1 /* tattlekey: A one-key UDP keyboard
2 * Copyright (C) 2023 Scott Worley <scottworley@scottworley.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #include "button.h"
19 #include "config.h"
20
21 #include "hardware/gpio.h"
22
23 #include "pico/stdlib.h"
24
25 static callback_t button_callback = NULL;
26
27 static void respond_to_gpio_interrupt(uint gpio, uint32_t event_mask) {
28 if (gpio == config_button_pin && event_mask & GPIO_IRQ_EDGE_FALL &&
29 button_callback) {
30 button_callback();
31 }
32 }
33
34 void begin_listening_for_button_press(callback_t callback) {
35 button_callback = callback;
36
37 gpio_set_pulls(config_button_pin, 1, 0);
38
39 /* Allow some time for the pull-up to take effect.
40 * I'm not sure if this is necessary.
41 * https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf says
42 * the internal GPIO pull-up resistors are 50k. */
43 sleep_ms(100);
44
45 gpio_set_irq_enabled_with_callback(config_button_pin, GPIO_IRQ_EDGE_FALL, 1,
46 respond_to_gpio_interrupt);
47 }