1 #include "pico/cyw43_arch.h"
2 #include "pico/stdlib.h"
3 #include "pico/util/queue.h"
16 enum event_type
{ BUTTONPRESS
, SEND
};
29 uint32_t time_s() { return time_us_64() / 1000000ul; }
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
37 * (We also get a bit of type safety by taking event_t* rather than void*.) */
38 static void queue_try_add_ignoring_errors(queue_t
*q
, event_t
*e
) {
42 static void button_pressed() {
43 /* This runs in interrupt context; don't linger. */
44 static uint64_t last_button_press_time
= 0;
45 uint32_t now
= time_s();
46 uint32_t time_since_last_press
= now
- last_button_press_time
;
47 if (time_since_last_press
>= config_minimum_seconds_between_button_presses
) {
48 last_button_press_time
= now
;
51 e
.buttonpress
.timestamp
= now
;
52 queue_try_add_ignoring_errors(&queue
, &e
);
56 void service_queue() {
60 queue_remove_blocking(&queue
, &e
);
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
);
71 uint32_t now
= time_s();
72 uint32_t ago
= now
- e
.send
.timestamp
;
73 send_report(e
.send
.seq
, ago
);
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
);
82 signal_error_by_blinking();
89 if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA
))
90 signal_error_by_blinking();
91 cyw43_arch_enable_sta_mode();
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();
98 queue_init(&queue
, sizeof(event_t
), 99);
100 begin_listening_for_button_press(button_pressed
);