]> git.scottworley.com Git - tattlekey/blob - client/tattlekey.c
client: Work in 1-second granularity
[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 uint32_t time_s() { return time_us_64() / 1000000ul; }
13
14 static void button_pressed() {
15 /* This runs in interrupt context; don't linger. */
16 static uint64_t last_button_press_time = 0;
17 uint32_t now = time_s();
18 uint32_t time_since_last_press = now - last_button_press_time;
19 if (time_since_last_press >= minimum_seconds_between_button_presses) {
20 last_button_press_time = now;
21 char zero = '\0';
22 /* We don't check for failure (full queue) here because there's not much to
23 * be done about it. */
24 queue_try_add(&queue, &zero);
25 }
26 }
27
28 int main() {
29 stdio_init_all();
30 if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA))
31 signal_error_by_blinking();
32 cyw43_arch_enable_sta_mode();
33 signal(3, 100);
34 if (cyw43_arch_wifi_connect_timeout_ms(wifi_ssid, wifi_pass,
35 CYW43_AUTH_WPA2_AES_PSK, 90000))
36 signal_error_by_blinking();
37 signal(2, 300);
38
39 queue_init(&queue, 1, 99);
40
41 begin_listening_for_button_press(button_pressed);
42
43 u16_t seq = 0;
44 while (1) {
45 char _;
46 queue_remove_blocking(&queue, &_);
47 seq++;
48 for (int i = 0; i < resend_count; i++) {
49 send_report(seq, i);
50 signal(i == 0 ? 2 : 1, 100);
51 sleep_ms(resend_interval_ms);
52 }
53 }
54 }