]>
Commit | Line | Data |
---|---|---|
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 | 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 | ||
20 | queue_t queue; | |
21 | ||
22 | uint32_t time_s() { return time_us_64() / 1000000ul; } | |
23 | ||
24 | static void button_pressed() { | |
25 | /* This runs in interrupt context; don't linger. */ | |
26 | static uint64_t last_button_press_time = 0; | |
27 | uint32_t now = time_s(); | |
28 | uint32_t time_since_last_press = now - last_button_press_time; | |
29 | if (time_since_last_press >= config_minimum_seconds_between_button_presses) { | |
30 | last_button_press_time = now; | |
31 | event_t e; | |
32 | e.type = BUTTONPRESS; | |
33 | e.buttonpress.timestamp = now; | |
34 | /* We don't check for failure (full queue) here because there's not much to | |
35 | * be done about it. */ | |
36 | queue_try_add(&queue, &e); | |
37 | } | |
38 | } | |
39 | ||
40 | int main() { | |
41 | stdio_init_all(); | |
42 | if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA)) | |
43 | signal_error_by_blinking(); | |
44 | cyw43_arch_enable_sta_mode(); | |
45 | signal(3, 100); | |
46 | if (cyw43_arch_wifi_connect_timeout_ms(config_wifi_ssid, config_wifi_pass, | |
47 | CYW43_AUTH_WPA2_AES_PSK, 90000)) | |
48 | signal_error_by_blinking(); | |
49 | signal(2, 300); | |
50 | ||
51 | queue_init(&queue, sizeof(event_t), 99); | |
52 | ||
53 | begin_listening_for_button_press(button_pressed); | |
54 | ||
55 | u16_t seq = 0; | |
56 | while (1) { | |
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(); | |
72 | } | |
73 | } | |
74 | } |