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