]> git.scottworley.com Git - tattlekey/blame - client/tattlekey.c
client: Extract pending-send object as send_t
[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
e210fa94
SW
10typedef struct {
11 uint32_t timestamp;
12 u16_t seq;
13 u8_t send_count;
14} send_t;
15
64a38d74 16enum event_type { BUTTONPRESS, SEND };
e8d047a0
SW
17typedef struct {
18 enum event_type type;
19 union {
20 struct {
21 uint32_t timestamp;
22 } buttonpress;
e210fa94 23 send_t send;
e8d047a0
SW
24 };
25} event_t;
26
1427141a 27queue_t queue;
d1521eda 28
de14b62c
SW
29uint32_t time_s() { return time_us_64() / 1000000ul; }
30
07b39467
SW
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*.) */
38static void queue_try_add_ignoring_errors(queue_t *q, event_t *e) {
39 queue_try_add(q, e);
40}
41
d1521eda 42static void button_pressed() {
1427141a 43 /* This runs in interrupt context; don't linger. */
2f7a1e89 44 static uint64_t last_button_press_time = 0;
de14b62c
SW
45 uint32_t now = time_s();
46 uint32_t time_since_last_press = now - last_button_press_time;
d7789e5b 47 if (time_since_last_press >= config_minimum_seconds_between_button_presses) {
2f7a1e89 48 last_button_press_time = now;
e8d047a0
SW
49 event_t e;
50 e.type = BUTTONPRESS;
51 e.buttonpress.timestamp = now;
07b39467 52 queue_try_add_ignoring_errors(&queue, &e);
2f7a1e89 53 }
d1521eda
SW
54}
55
ff999046 56void service_queue() {
1427141a
SW
57 u16_t seq = 0;
58 while (1) {
e8d047a0
SW
59 event_t e;
60 queue_remove_blocking(&queue, &e);
61 switch (e.type) {
64a38d74
SW
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;
e8d047a0
SW
81 default:
82 signal_error_by_blinking();
ff379463 83 }
1427141a 84 }
75649fe3 85}
ff999046
SW
86
87int 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}