]> git.scottworley.com Git - tattlekey/blame - client/tattlekey.c
client: Respond to button press
[tattlekey] / client / tattlekey.c
CommitLineData
e2173399 1#include "pico/cyw43_arch.h"
5ec2b60a
SW
2#include "pico/stdlib.h"
3
dae35db7 4#include "blink.h"
d1521eda 5#include "button.h"
fbc57595 6#include "config.h"
1e0a316e 7#include "net.h"
d234f6b3 8
d1521eda
SW
9static u16_t seq = 0;
10
11static void button_pressed() {
12 /* TODO: This is interrupt context. We need to get out of interrupt context
13 * quickly; we should not be doing significant work here, & definitely
14 * shouldn't be sleeping here. We signal errors with blinking the LED, which
15 * involves sleeping, so this all has to move. */
16 send_report(seq++, 0);
17 signal(4, 200);
18}
19
20static void wait_forever() {
21 /* pico-examples/gpio/hello_gpio_irq/hello_gpio_irq.c implements wait-forever
22 * as "while (1);", but
23 * https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#gaf469c6d691230e9d1008
24 * says sleeping uses less power, so we sleep. */
25 while (1) {
26 /* https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#rpip7ce2cdc1662dce59296b
27 * says the maximum sleep is 2^32 - 1 microseconds (~71.58 minutes), so we
28 * sleep in chunks. */
29 sleep_ms(1 << 31); /* 35.79 minutes */
30 }
31}
32
75649fe3
SW
33int main() {
34 stdio_init_all();
d234f6b3 35 if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA))
75649fe3
SW
36 signal_error_by_blinking();
37 signal(1, 200);
d234f6b3 38 cyw43_arch_enable_sta_mode();
75649fe3 39 signal(2, 200);
fbc57595
SW
40 if (cyw43_arch_wifi_connect_timeout_ms(wifi_ssid, wifi_pass,
41 CYW43_AUTH_WPA2_AES_PSK, 10000))
d234f6b3 42 signal_error_by_blinking();
75649fe3 43 signal(3, 200);
d1521eda 44 begin_listening_for_button_press(button_pressed);
1e0a316e 45
d1521eda 46 wait_forever();
75649fe3 47}