]> git.scottworley.com Git - tattlekey/blob - client/tattlekey.c
78d1dcfd8925c576b362d6fa0d20e09f7686de54
[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 typedef struct {
11 uint32_t timestamp;
12 u16_t seq;
13 u8_t send_count;
14 } send_t;
15
16 enum event_type { BUTTONPRESS, SEND };
17 typedef struct {
18 enum event_type type;
19 union {
20 struct {
21 uint32_t timestamp;
22 } buttonpress;
23 send_t send;
24 };
25 } event_t;
26
27 queue_t queue;
28
29 uint32_t time_s() { return time_us_64() / 1000000ul; }
30
31 /* Often we don't bother checking for failure (full queue) because
32 * 1. The best thing to do in this unfortunate situation is to blithely
33 * continue, dropping some events; continuing is better than stopping.
34 * 2. Neither interrupt context nor queue-processing context can block
35 * until space is available, or even sit around & blink the LED to
36 * signal a problem.
37 * (We also get a bit of type safety by taking event_t* rather than void*.) */
38 static void queue_try_add_ignoring_errors(queue_t *q, event_t *e) {
39 queue_try_add(q, e);
40 }
41
42 static void button_pressed() {
43 /* This runs in interrupt context; don't linger. */
44 static uint64_t last_button_press_time = 0;
45 uint32_t now = time_s();
46 uint32_t time_since_last_press = now - last_button_press_time;
47 if (time_since_last_press >= config_minimum_seconds_between_button_presses) {
48 last_button_press_time = now;
49 event_t e;
50 e.type = BUTTONPRESS;
51 e.buttonpress.timestamp = now;
52 queue_try_add_ignoring_errors(&queue, &e);
53 }
54 }
55
56 void service_queue() {
57 u16_t seq = 0;
58 while (1) {
59 event_t e;
60 queue_remove_blocking(&queue, &e);
61 switch (e.type) {
62 case BUTTONPRESS: {
63 event_t send_e;
64 send_e.type = SEND;
65 send_e.send.timestamp = e.buttonpress.timestamp;
66 send_e.send.seq = seq++;
67 send_e.send.send_count = 0;
68 queue_try_add_ignoring_errors(&queue, &send_e);
69 } break;
70 case SEND: {
71 uint32_t now = time_s();
72 uint32_t ago = now - e.send.timestamp;
73 send_report(e.send.seq, ago);
74 e.send.send_count++;
75 if (e.send.send_count < config_resend_count)
76 queue_try_add_ignoring_errors(&queue, &e);
77 /* TODO: Sleep elsewhere. */
78 signal(e.send.send_count == 1 ? 2 : 1, 100);
79 sleep_ms(config_resend_interval_ms);
80 } break;
81 default:
82 signal_error_by_blinking();
83 }
84 }
85 }
86
87 int main() {
88 stdio_init_all();
89 if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA))
90 signal_error_by_blinking();
91 cyw43_arch_enable_sta_mode();
92 signal(3, 100);
93 if (cyw43_arch_wifi_connect_timeout_ms(config_wifi_ssid, config_wifi_pass,
94 CYW43_AUTH_WPA2_AES_PSK, 90000))
95 signal_error_by_blinking();
96 signal(2, 300);
97
98 queue_init(&queue, sizeof(event_t), 99);
99
100 begin_listening_for_button_press(button_pressed);
101
102 service_queue();
103 }