]> git.scottworley.com Git - tattlekey/blame - client/tattlekey.c
client: Set up for multiple types of events to go into the queue
[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
e8d047a0
SW
10enum event_type { BUTTONPRESS };
11typedef struct {
12 enum event_type type;
13 union {
14 struct {
15 uint32_t timestamp;
16 } buttonpress;
17 };
18} event_t;
19
1427141a 20queue_t queue;
d1521eda 21
de14b62c
SW
22uint32_t time_s() { return time_us_64() / 1000000ul; }
23
d1521eda 24static void button_pressed() {
1427141a 25 /* This runs in interrupt context; don't linger. */
2f7a1e89 26 static uint64_t last_button_press_time = 0;
de14b62c
SW
27 uint32_t now = time_s();
28 uint32_t time_since_last_press = now - last_button_press_time;
d7789e5b 29 if (time_since_last_press >= config_minimum_seconds_between_button_presses) {
2f7a1e89 30 last_button_press_time = now;
e8d047a0
SW
31 event_t e;
32 e.type = BUTTONPRESS;
33 e.buttonpress.timestamp = now;
2f7a1e89
SW
34 /* We don't check for failure (full queue) here because there's not much to
35 * be done about it. */
e8d047a0 36 queue_try_add(&queue, &e);
2f7a1e89 37 }
d1521eda
SW
38}
39
75649fe3
SW
40int main() {
41 stdio_init_all();
d234f6b3 42 if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA))
75649fe3 43 signal_error_by_blinking();
d234f6b3 44 cyw43_arch_enable_sta_mode();
38d971ac 45 signal(3, 100);
d7789e5b 46 if (cyw43_arch_wifi_connect_timeout_ms(config_wifi_ssid, config_wifi_pass,
9efef936 47 CYW43_AUTH_WPA2_AES_PSK, 90000))
d234f6b3 48 signal_error_by_blinking();
38d971ac 49 signal(2, 300);
1427141a 50
e8d047a0 51 queue_init(&queue, sizeof(event_t), 99);
1427141a 52
d1521eda 53 begin_listening_for_button_press(button_pressed);
1e0a316e 54
1427141a
SW
55 u16_t seq = 0;
56 while (1) {
e8d047a0
SW
57 event_t e;
58 queue_remove_blocking(&queue, &e);
59 switch (e.type) {
60 case BUTTONPRESS:
61 seq++;
62 for (int i = 0; i < config_resend_count; i++) {
63 uint32_t now = time_s();
64 uint32_t ago = now - e.buttonpress.timestamp;
65 send_report(seq, ago);
66 signal(i == 0 ? 2 : 1, 100);
67 sleep_ms(config_resend_interval_ms);
68 }
69 break;
70 default:
71 signal_error_by_blinking();
ff379463 72 }
1427141a 73 }
75649fe3 74}