]>
Commit | Line | Data |
---|---|---|
1 | #include "button.h" | |
2 | #include "config.h" | |
3 | ||
4 | #include "hardware/gpio.h" | |
5 | ||
6 | #include "pico/stdlib.h" | |
7 | ||
8 | static callback_t button_callback = NULL; | |
9 | ||
10 | static void respond_to_gpio_interrupt(uint gpio, uint32_t event_mask) { | |
11 | if (gpio == button_pin && event_mask & GPIO_IRQ_EDGE_FALL && | |
12 | button_callback) { | |
13 | button_callback(); | |
14 | } | |
15 | } | |
16 | ||
17 | void begin_listening_for_button_press(callback_t callback) { | |
18 | button_callback = callback; | |
19 | ||
20 | gpio_set_pulls(button_pin, 1, 0); | |
21 | ||
22 | /* Allow some time for the pull-up to take effect. | |
23 | * I'm not sure if this is necessary. | |
24 | * https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf says | |
25 | * the internal GPIO pull-up resistors are 50k. */ | |
26 | sleep_ms(100); | |
27 | ||
28 | gpio_set_irq_enabled_with_callback(button_pin, GPIO_IRQ_EDGE_FALL, 1, | |
29 | respond_to_gpio_interrupt); | |
30 | } |