]> git.scottworley.com Git - tattlekey/blob - client/tattlekey.c
client: debounce
[tattlekey] / client / tattlekey.c
1 #include "pico/cyw43_arch.h"
2 #include "pico/stdlib.h"
3 #include "pico/util/queue.h"
4
5 #include "blink.h"
6 #include "button.h"
7 #include "config.h"
8 #include "net.h"
9
10 queue_t queue;
11
12 static void button_pressed() {
13 /* This runs in interrupt context; don't linger. */
14 static uint64_t last_button_press_time = 0;
15 uint64_t now = time_us_64();
16 uint64_t time_since_last_press = now - last_button_press_time;
17 if (time_since_last_press > minimum_microseconds_between_button_presses) {
18 last_button_press_time = now;
19 char zero = '\0';
20 /* We don't check for failure (full queue) here because there's not much to
21 * be done about it. */
22 queue_try_add(&queue, &zero);
23 }
24 }
25
26 int main() {
27 stdio_init_all();
28 if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA))
29 signal_error_by_blinking();
30 signal(1, 200);
31 cyw43_arch_enable_sta_mode();
32 signal(2, 200);
33 if (cyw43_arch_wifi_connect_timeout_ms(wifi_ssid, wifi_pass,
34 CYW43_AUTH_WPA2_AES_PSK, 90000))
35 signal_error_by_blinking();
36 signal(3, 200);
37
38 queue_init(&queue, 1, 99);
39
40 begin_listening_for_button_press(button_pressed);
41
42 u16_t seq = 0;
43 while (1) {
44 char _;
45 queue_remove_blocking(&queue, &_);
46 send_report(seq++, 0);
47 signal(4, 200);
48 }
49 }