]>
Commit | Line | Data |
---|---|---|
a4a617fd SW |
1 | /* tattlekey: A one-key UDP keyboard |
2 | * Copyright (C) 2023 Scott Worley <scottworley@scottworley.com> | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
e2173399 | 18 | #include "pico/cyw43_arch.h" |
5ec2b60a | 19 | #include "pico/stdlib.h" |
4db97133 | 20 | #include "pico/util/pheap.h" |
1427141a | 21 | #include "pico/util/queue.h" |
5ec2b60a | 22 | |
dae35db7 | 23 | #include "blink.h" |
d1521eda | 24 | #include "button.h" |
fbc57595 | 25 | #include "config.h" |
1e0a316e | 26 | #include "net.h" |
00ca9fa6 | 27 | #include "press.h" |
4db97133 | 28 | |
9d623c50 | 29 | enum event_type { NEW_BUTTON_PRESS, RESEND_TIME }; |
e8d047a0 SW |
30 | typedef struct { |
31 | enum event_type type; | |
32 | union { | |
33 | struct { | |
34 | uint32_t timestamp; | |
35 | } buttonpress; | |
36 | }; | |
37 | } event_t; | |
38 | ||
1427141a | 39 | queue_t queue; |
d1521eda | 40 | |
de14b62c SW |
41 | uint32_t time_s() { return time_us_64() / 1000000ul; } |
42 | ||
07b39467 SW |
43 | /* Often we don't bother checking for failure (full queue) because |
44 | * 1. The best thing to do in this unfortunate situation is to blithely | |
45 | * continue, dropping some events; continuing is better than stopping. | |
46 | * 2. Neither interrupt context nor queue-processing context can block | |
47 | * until space is available, or even sit around & blink the LED to | |
48 | * signal a problem. | |
49 | * (We also get a bit of type safety by taking event_t* rather than void*.) */ | |
50 | static void queue_try_add_ignoring_errors(queue_t *q, event_t *e) { | |
51 | queue_try_add(q, e); | |
52 | } | |
53 | ||
d1521eda | 54 | static void button_pressed() { |
1427141a | 55 | /* This runs in interrupt context; don't linger. */ |
2f7a1e89 | 56 | static uint64_t last_button_press_time = 0; |
de14b62c SW |
57 | uint32_t now = time_s(); |
58 | uint32_t time_since_last_press = now - last_button_press_time; | |
d7789e5b | 59 | if (time_since_last_press >= config_minimum_seconds_between_button_presses) { |
2f7a1e89 | 60 | last_button_press_time = now; |
e8d047a0 | 61 | event_t e; |
9d623c50 | 62 | e.type = NEW_BUTTON_PRESS; |
e8d047a0 | 63 | e.buttonpress.timestamp = now; |
07b39467 | 64 | queue_try_add_ignoring_errors(&queue, &e); |
2f7a1e89 | 65 | } |
d1521eda SW |
66 | } |
67 | ||
4db97133 SW |
68 | static void time_to_send(uint _) { |
69 | /* This runs in interrupt context; don't linger. */ | |
70 | event_t e; | |
9d623c50 | 71 | e.type = RESEND_TIME; |
4db97133 SW |
72 | queue_try_add_ignoring_errors(&queue, &e); |
73 | } | |
74 | ||
42b14805 | 75 | void set_resend_alarm(int alarm, uint32_t now, uint32_t next_act_time) { |
f523aaab SW |
76 | uint32_t sleep_duration = next_act_time - now; |
77 | if (hardware_alarm_set_target(alarm, | |
78 | make_timeout_time_ms(sleep_duration * 1000))) | |
79 | signal_error_by_blinking(); | |
f523aaab SW |
80 | } |
81 | ||
e3ff9e0d | 82 | void service_sleeps(int alarm, press_pile_t *pp) { |
4db97133 SW |
83 | hardware_alarm_cancel(alarm); |
84 | ||
85 | while (1) { | |
86 | uint32_t now = time_s(); | |
e3ff9e0d | 87 | int32_t act_time = next_scheduled_send(pp); |
b512ec26 | 88 | if (act_time == -1) |
4db97133 | 89 | return; |
b512ec26 | 90 | if (act_time > now) { |
42b14805 | 91 | set_resend_alarm(alarm, now, act_time); |
4db97133 SW |
92 | return; |
93 | } | |
9ae691e9 SW |
94 | press_t press; |
95 | if (!get_press_due_for_resend(pp, now, &press)) | |
96 | signal_error_by_blinking(); | |
9266b741 | 97 | u32_t ago = now - press.timestamp; |
9ae691e9 SW |
98 | send_report_packet(press.seq, ago); |
99 | press.send_count++; | |
100 | if (press.send_count < config_resend_count) { | |
101 | add_press(pp, &press); | |
102 | } | |
4db97133 SW |
103 | } |
104 | } | |
105 | ||
ff999046 | 106 | void service_queue() { |
4db97133 SW |
107 | int alarm = hardware_alarm_claim_unused(true); |
108 | if (alarm == -1) | |
109 | signal_error_by_blinking(); | |
110 | hardware_alarm_set_callback(alarm, time_to_send); | |
111 | ||
e3ff9e0d | 112 | press_pile_t *pp = create_press_pile(); |
1427141a SW |
113 | u16_t seq = 0; |
114 | while (1) { | |
e3ff9e0d | 115 | service_sleeps(alarm, pp); |
4db97133 | 116 | |
e8d047a0 SW |
117 | event_t e; |
118 | queue_remove_blocking(&queue, &e); | |
119 | switch (e.type) { | |
9d623c50 | 120 | case NEW_BUTTON_PRESS: { |
f68e05b2 | 121 | add_press(pp, &(press_t){e.buttonpress.timestamp, seq++, 0}); |
c13b9063 SW |
122 | if (seq == 0) |
123 | new_epoch(); | |
64a38d74 | 124 | } break; |
9d623c50 | 125 | case RESEND_TIME: { |
4db97133 SW |
126 | /* OK, we're awake. Cool. Thanks! (We actually do the sends in the |
127 | * service_sleeps() call at the top of the loop.) */ | |
64a38d74 | 128 | } break; |
e8d047a0 SW |
129 | default: |
130 | signal_error_by_blinking(); | |
ff379463 | 131 | } |
1427141a | 132 | } |
75649fe3 | 133 | } |
ff999046 SW |
134 | |
135 | int main() { | |
136 | stdio_init_all(); | |
137 | if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA)) | |
138 | signal_error_by_blinking(); | |
139 | cyw43_arch_enable_sta_mode(); | |
140 | signal(3, 100); | |
141 | if (cyw43_arch_wifi_connect_timeout_ms(config_wifi_ssid, config_wifi_pass, | |
142 | CYW43_AUTH_WPA2_AES_PSK, 90000)) | |
143 | signal_error_by_blinking(); | |
144 | signal(2, 300); | |
145 | ||
146 | queue_init(&queue, sizeof(event_t), 99); | |
147 | ||
148 | begin_listening_for_button_press(button_pressed); | |
149 | ||
150 | service_queue(); | |
151 | } |