]> git.scottworley.com Git - tattlekey/blame_incremental - client/tattlekey.c
client: Specify license
[tattlekey] / client / tattlekey.c
... / ...
CommitLineData
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
18#include "pico/cyw43_arch.h"
19#include "pico/stdlib.h"
20#include "pico/util/pheap.h"
21#include "pico/util/queue.h"
22
23#include "blink.h"
24#include "button.h"
25#include "config.h"
26#include "net.h"
27#include "press.h"
28
29enum event_type { NEW_BUTTON_PRESS, RESEND_TIME };
30typedef struct {
31 enum event_type type;
32 union {
33 struct {
34 uint32_t timestamp;
35 } buttonpress;
36 };
37} event_t;
38
39queue_t queue;
40
41uint32_t time_s() { return time_us_64() / 1000000ul; }
42
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*.) */
50static void queue_try_add_ignoring_errors(queue_t *q, event_t *e) {
51 queue_try_add(q, e);
52}
53
54static void button_pressed() {
55 /* This runs in interrupt context; don't linger. */
56 static uint64_t last_button_press_time = 0;
57 uint32_t now = time_s();
58 uint32_t time_since_last_press = now - last_button_press_time;
59 if (time_since_last_press >= config_minimum_seconds_between_button_presses) {
60 last_button_press_time = now;
61 event_t e;
62 e.type = NEW_BUTTON_PRESS;
63 e.buttonpress.timestamp = now;
64 queue_try_add_ignoring_errors(&queue, &e);
65 }
66}
67
68static void time_to_send(uint _) {
69 /* This runs in interrupt context; don't linger. */
70 event_t e;
71 e.type = RESEND_TIME;
72 queue_try_add_ignoring_errors(&queue, &e);
73}
74
75void set_resend_alarm(int alarm, uint32_t now, uint32_t next_act_time) {
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();
80}
81
82void service_sleeps(int alarm, press_pile_t *pp) {
83 hardware_alarm_cancel(alarm);
84
85 while (1) {
86 uint32_t now = time_s();
87 int32_t act_time = next_scheduled_send(pp);
88 if (act_time == -1)
89 return;
90 if (act_time > now) {
91 set_resend_alarm(alarm, now, act_time);
92 return;
93 }
94 press_t press;
95 if (!get_press_due_for_resend(pp, now, &press))
96 signal_error_by_blinking();
97 uint32_t ago = now - press.timestamp;
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 }
103 }
104}
105
106void service_queue() {
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
112 press_pile_t *pp = create_press_pile();
113 u16_t seq = 0;
114 while (1) {
115 service_sleeps(alarm, pp);
116
117 event_t e;
118 queue_remove_blocking(&queue, &e);
119 switch (e.type) {
120 case NEW_BUTTON_PRESS: {
121 add_press(pp, &(press_t){e.buttonpress.timestamp, seq++, 0});
122 } break;
123 case RESEND_TIME: {
124 /* OK, we're awake. Cool. Thanks! (We actually do the sends in the
125 * service_sleeps() call at the top of the loop.) */
126 } break;
127 default:
128 signal_error_by_blinking();
129 }
130 }
131}
132
133int main() {
134 stdio_init_all();
135 if (cyw43_arch_init_with_country(CYW43_COUNTRY_USA))
136 signal_error_by_blinking();
137 cyw43_arch_enable_sta_mode();
138 signal(3, 100);
139 if (cyw43_arch_wifi_connect_timeout_ms(config_wifi_ssid, config_wifi_pass,
140 CYW43_AUTH_WPA2_AES_PSK, 90000))
141 signal_error_by_blinking();
142 signal(2, 300);
143
144 queue_init(&queue, sizeof(event_t), 99);
145
146 begin_listening_for_button_press(button_pressed);
147
148 service_queue();
149}