+#include "button.h"
+#include "config.h"
+
+#include "hardware/gpio.h"
+
+#include "pico/stdlib.h"
+
+static callback_t button_callback = NULL;
+
+static void respond_to_gpio_interrupt(uint gpio, uint32_t event_mask) {
+ if (gpio == button_pin && event_mask & GPIO_IRQ_EDGE_FALL &&
+ button_callback) {
+ button_callback();
+ }
+}
+
+void begin_listening_for_button_press(callback_t callback) {
+ button_callback = callback;
+
+ gpio_set_pulls(button_pin, 1, 0);
+
+ /* Allow some time for the pull-up to take effect.
+ * I'm not sure if this is necessary.
+ * https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf says
+ * the internal GPIO pull-up resistors are 50k. */
+ sleep_ms(100);
+
+ gpio_set_irq_enabled_with_callback(button_pin, GPIO_IRQ_EDGE_FALL, 1,
+ respond_to_gpio_interrupt);
+}