]>
Commit | Line | Data |
---|---|---|
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 |
10 | enum event_type { BUTTONPRESS }; |
11 | typedef struct { | |
12 | enum event_type type; | |
13 | union { | |
14 | struct { | |
15 | uint32_t timestamp; | |
16 | } buttonpress; | |
17 | }; | |
18 | } event_t; | |
19 | ||
1427141a | 20 | queue_t queue; |
d1521eda | 21 | |
de14b62c SW |
22 | uint32_t time_s() { return time_us_64() / 1000000ul; } |
23 | ||
07b39467 SW |
24 | /* Often we don't bother checking for failure (full queue) because |
25 | * 1. The best thing to do in this unfortunate situation is to blithely | |
26 | * continue, dropping some events; continuing is better than stopping. | |
27 | * 2. Neither interrupt context nor queue-processing context can block | |
28 | * until space is available, or even sit around & blink the LED to | |
29 | * signal a problem. | |
30 | * (We also get a bit of type safety by taking event_t* rather than void*.) */ | |
31 | static void queue_try_add_ignoring_errors(queue_t *q, event_t *e) { | |
32 | queue_try_add(q, e); | |
33 | } | |
34 | ||
d1521eda | 35 | static void button_pressed() { |
1427141a | 36 | /* This runs in interrupt context; don't linger. */ |
2f7a1e89 | 37 | static uint64_t last_button_press_time = 0; |
de14b62c SW |
38 | uint32_t now = time_s(); |
39 | uint32_t time_since_last_press = now - last_button_press_time; | |
d7789e5b | 40 | if (time_since_last_press >= config_minimum_seconds_between_button_presses) { |
2f7a1e89 | 41 | last_button_press_time = now; |
e8d047a0 SW |
42 | event_t e; |
43 | e.type = BUTTONPRESS; | |
44 | e.buttonpress.timestamp = now; | |
07b39467 | 45 | queue_try_add_ignoring_errors(&queue, &e); |
2f7a1e89 | 46 | } |
d1521eda SW |
47 | } |
48 | ||
75649fe3 SW |
49 | int main() { |
50 | stdio_init_all(); | |
d234f6b3 | 51 | if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA)) |
75649fe3 | 52 | signal_error_by_blinking(); |
d234f6b3 | 53 | cyw43_arch_enable_sta_mode(); |
38d971ac | 54 | signal(3, 100); |
d7789e5b | 55 | if (cyw43_arch_wifi_connect_timeout_ms(config_wifi_ssid, config_wifi_pass, |
9efef936 | 56 | CYW43_AUTH_WPA2_AES_PSK, 90000)) |
d234f6b3 | 57 | signal_error_by_blinking(); |
38d971ac | 58 | signal(2, 300); |
1427141a | 59 | |
e8d047a0 | 60 | queue_init(&queue, sizeof(event_t), 99); |
1427141a | 61 | |
d1521eda | 62 | begin_listening_for_button_press(button_pressed); |
1e0a316e | 63 | |
1427141a SW |
64 | u16_t seq = 0; |
65 | while (1) { | |
e8d047a0 SW |
66 | event_t e; |
67 | queue_remove_blocking(&queue, &e); | |
68 | switch (e.type) { | |
69 | case BUTTONPRESS: | |
70 | seq++; | |
71 | for (int i = 0; i < config_resend_count; i++) { | |
72 | uint32_t now = time_s(); | |
73 | uint32_t ago = now - e.buttonpress.timestamp; | |
74 | send_report(seq, ago); | |
75 | signal(i == 0 ? 2 : 1, 100); | |
76 | sleep_ms(config_resend_interval_ms); | |
77 | } | |
78 | break; | |
79 | default: | |
80 | signal_error_by_blinking(); | |
ff379463 | 81 | } |
1427141a | 82 | } |
75649fe3 | 83 | } |