]> git.scottworley.com Git - tattlekey/blame - client/button.c
client: Specify license
[tattlekey] / client / button.c
CommitLineData
a4a617fd
SW
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
d1521eda
SW
18#include "button.h"
19#include "config.h"
20
21#include "hardware/gpio.h"
22
23#include "pico/stdlib.h"
24
25static callback_t button_callback = NULL;
26
27static void respond_to_gpio_interrupt(uint gpio, uint32_t event_mask) {
d7789e5b 28 if (gpio == config_button_pin && event_mask & GPIO_IRQ_EDGE_FALL &&
d1521eda
SW
29 button_callback) {
30 button_callback();
31 }
32}
33
34void begin_listening_for_button_press(callback_t callback) {
35 button_callback = callback;
36
d7789e5b 37 gpio_set_pulls(config_button_pin, 1, 0);
d1521eda
SW
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
d7789e5b 45 gpio_set_irq_enabled_with_callback(config_button_pin, GPIO_IRQ_EDGE_FALL, 1,
d1521eda
SW
46 respond_to_gpio_interrupt);
47}