-
Notifications
You must be signed in to change notification settings - Fork 41
/
cat.cpp
321 lines (300 loc) · 10.2 KB
/
cat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "cat.h"
#include "ui.h"
#include <algorithm>
#include "pico/stdlib.h"
void process_cat_control(rx_settings & settings_to_apply, rx_status & status, rx &receiver, uint32_t settings[])
{
const uint16_t buffer_length = 64;
static char buf[buffer_length];
static uint16_t read_idx = 0;
static uint16_t write_idx = 0;
static uint16_t items_in_buffer = 0;
//write any new data into circular buffer
{
const uint16_t bytes_to_end = buffer_length-write_idx-1;
if(bytes_to_end > 0)
{
int32_t retval = stdio_get_until(buf+write_idx, bytes_to_end, make_timeout_time_us(100));
if(retval != PICO_ERROR_TIMEOUT)
{
items_in_buffer += retval;
write_idx += retval;
if(write_idx > buffer_length) write_idx -= buffer_length;
}
}
else
{
int32_t retval = stdio_get_until(buf, sizeof(buf)-items_in_buffer, make_timeout_time_us(100));
if(retval != PICO_ERROR_TIMEOUT)
{
items_in_buffer += retval;
write_idx += retval;
if(write_idx > buffer_length) write_idx -= buffer_length;
}
}
}
//copy command from circular buffer to command buffer
static char cmd[buffer_length];
const uint16_t bytes_to_read = items_in_buffer;
const uint16_t bytes_to_end = std::min(bytes_to_read, (uint16_t)(buffer_length-1u-read_idx));
const uint16_t bytes_remaining = bytes_to_read - bytes_to_end;
memcpy(cmd, buf+read_idx, bytes_to_end);
memcpy(cmd+bytes_to_end, buf, bytes_remaining);
//read command from circular_buffer
char *command_end = (char*)memchr(cmd, ';', items_in_buffer);
if(command_end == NULL) {
//buffer full discard
if(items_in_buffer == buffer_length){
items_in_buffer = 0;
read_idx = 0;
write_idx = 0;
}
return;
}
//remove the command from circular buffer
uint32_t command_length = command_end - cmd + 1;
read_idx += command_length;
if(read_idx > buffer_length) read_idx -= buffer_length;
items_in_buffer -= command_length;
bool settings_changed = false;
const char mode_translation[] = "551243";
if (strncmp(cmd, "FA", 2) == 0) {
// Handle mode set/get commands
if (cmd[2] == ';') {
printf("FA%011lu;", settings[idx_frequency]);
} else {
uint32_t frequency_Hz;
sscanf(cmd+2, "%lu", &frequency_Hz);
if(frequency_Hz <= 30000000)
{
settings[idx_frequency]=frequency_Hz;
settings_changed = true;
}
else
{
stdio_puts_raw("?;");
}
}
} else if (strncmp(cmd, "SM", 2) == 0) {
// Handle mode set/get commands
if (cmd[3] == ';') {
receiver.access(false);
float power_dBm = status.signal_strength_dBm;
receiver.release();
float power_scaled = 020*((power_dBm - (-127))/114);
power_scaled = std::min((float)0x20, power_scaled);
power_scaled = std::max((float)0, power_scaled);
printf("SM%05X;", (uint16_t)power_scaled);
} else {
stdio_puts_raw("?;");
}
} else if (strncmp(cmd, "MD", 2) == 0) {
// Handle mode set/get commands
if (cmd[2] == ';') {
char mode_status = mode_translation[settings[idx_mode]];
printf("MD%c;", mode_status);
} else if (cmd[2] == '1') {
settings_changed = true;
settings[idx_mode] = MODE_LSB;
} else if (cmd[2] == '2') {
settings_changed = true;
settings[idx_mode] = MODE_USB;
} else if (cmd[2] == '3') {
settings_changed = true;
settings[idx_mode] = MODE_CW;
} else if (cmd[2] == '4') {
settings_changed = true;
settings[idx_mode] = MODE_FM;
} else if (cmd[2] == '5') {
settings_changed = true;
settings[idx_mode] = MODE_AM;
}
} else if (strncmp(cmd, "IF", 2) == 0) {
if (cmd[2] == ';') {
printf("IF%011lu00000+0000000000%c0000000;", settings[idx_frequency], mode_translation[settings[idx_mode]]);
}
//fake TX for now
} else if (strncmp(cmd, "ID", 2) == 0) {
if (cmd[2] == ';') {
printf("ID020;");
}
} else if (strncmp(cmd, "AI", 2) == 0) {
if (cmd[2] == ';') {
printf("AI0;");
}
} else if (strncmp(cmd, "AG", 2) == 0) {
if (cmd[2] == ';') {
printf("AG0;");
}
} else if (strncmp(cmd, "XT", 2) == 0) {
if (cmd[2] == ';') {
printf("XT1;");
}
} else if (strncmp(cmd, "RT", 2) == 0) {
if (cmd[2] == ';') {
printf("RT1;");
}
} else if (strncmp(cmd, "RC", 2) == 0) {
if (cmd[2] == ';') {
printf("RC;");
}
} else if (strncmp(cmd, "FL", 2) == 0) {
if (cmd[2] == ';') {
printf("FL0;");
}
} else if (strncmp(cmd, "PS", 2) == 0) {
if (cmd[2] == ';') {
printf("PS1;");
}
} else if (strncmp(cmd, "VX", 2) == 0) {
if (cmd[2] == ';') {
printf("VX0;");
}
} else if (strncmp(cmd, "RS", 2) == 0) {
if (cmd[2] == ';') {
printf("RS0;");
}
} else if (strncmp(cmd, "FL", 2) == 0) {
if (cmd[2] == ';') {
printf("FL0;");
}
} else if (strncmp(cmd, "AC", 2) == 0) {
if (cmd[2] == ';') {
printf("AC010;");
}
} else if (strncmp(cmd, "PR", 2) == 0) {
if (cmd[2] == ';') {
printf("PR0;");
}
} else if (strncmp(cmd, "NB", 2) == 0) {
if (cmd[2] == ';') {
printf("NB0;");
}
} else if (strncmp(cmd, "LK", 2) == 0) {
if (cmd[2] == ';') {
printf("LK00;");
}
} else if (strncmp(cmd, "MG", 2) == 0) {
if (cmd[2] == ';') {
printf("MG000;");
}
} else if (strncmp(cmd, "PL", 2) == 0) {
if (cmd[2] == ';') {
printf("PL000000;");
}
} else if (strncmp(cmd, "VD", 2) == 0) {
if (cmd[2] == ';') {
printf("VD0000;");
}
} else if (strncmp(cmd, "VG", 2) == 0) {
if (cmd[2] == ';') {
printf("VG000;");
}
} else if (strncmp(cmd, "BC", 2) == 0) {
if (cmd[2] == ';') {
printf("BC0;");
}
} else if (strncmp(cmd, "ML", 2) == 0) {
if (cmd[2] == ';') {
printf("ML000;");
}
} else if (strncmp(cmd, "NR", 2) == 0) {
if (cmd[2] == ';') {
printf("NR0;");
}
} else if (strncmp(cmd, "SD", 2) == 0) {
if (cmd[2] == ';') {
printf("SD0000;");
}
} else if (strncmp(cmd, "KS", 2) == 0) {
if (cmd[2] == ';') {
printf("KS010;");
}
} else if (strncmp(cmd, "EX", 2) == 0) {
if (cmd[2] == ';') {
printf("EX000000000;");
}
} else if (strncmp(cmd, "RL", 2) == 0) {
if (cmd[2] == ';') {
printf("RL00;");
}
} else if (strncmp(cmd, "SQ", 2) == 0) {
if (cmd[2] == ';') {
printf("SQ0000;");
}
} else if (strncmp(cmd, "RG", 2) == 0) {
if (cmd[2] == ';') {
printf("RG000;");
}
} else if (strncmp(cmd, "RM", 2) == 0) {
if (cmd[2] == ';') {
printf("RM10000;");
}
} else if (strncmp(cmd, "PA", 2) == 0) {
if (cmd[2] == ';') {
printf("PA00;");
}
} else if (strncmp(cmd, "RA", 2) == 0) {
if (cmd[2] == ';') {
printf("RA0000;");
}
} else if (strncmp(cmd, "GT", 2) == 0) {
if (cmd[2] == ';') {
printf("GT000;");
}
} else if (strncmp(cmd, "PC", 2) == 0) {
if (cmd[2] == ';') {
printf("PC005;");
}
} else if (strncmp(cmd, "FW", 2) == 0) {
if (cmd[2] == ';') {
printf("FW0000;");
}
} else if (strncmp(cmd, "TX", 2) == 0) {
static uint8_t tx_status = 0;
// Set or Get TX mode command (TX)
if (cmd[2] == ';') {
// Get current transmit status
printf("TX%d;", tx_status);
} else if (cmd[2] == '1') {
// Switch to TX mode
tx_status = 1;
} else if (cmd[2] == '0') {
// Switch to RX mode
tx_status = 0;
} else {
// Invalid TX command format
stdio_puts_raw("?;");
}
} else {
// Unknown command
stdio_puts_raw("?;");
}
//apply settings to receiver
if(settings_changed)
{
receiver.access(true);
settings_to_apply.tuned_frequency_Hz = settings[idx_frequency];
settings_to_apply.agc_speed = settings[idx_agc_speed];
settings_to_apply.enable_auto_notch = settings[idx_rx_features] >> flag_enable_auto_notch & 1;
settings_to_apply.mode = settings[idx_mode];
settings_to_apply.volume = settings[idx_volume];
settings_to_apply.squelch = settings[idx_squelch];
settings_to_apply.step_Hz = step_sizes[settings[idx_step]];
settings_to_apply.cw_sidetone_Hz = settings[idx_cw_sidetone]*100;
settings_to_apply.gain_cal = settings[idx_gain_cal];
settings_to_apply.suspend = false;
settings_to_apply.swap_iq = (settings[idx_hw_setup] >> flag_swap_iq) & 1;
settings_to_apply.bandwidth = (settings[idx_bandwidth_spectrum] & mask_bandwidth) >> flag_bandwidth;
settings_to_apply.deemphasis = (settings[idx_rx_features] & mask_deemphasis) >> flag_deemphasis;
settings_to_apply.band_1_limit = ((settings[idx_band1] >> 0) & 0xff);
settings_to_apply.band_2_limit = ((settings[idx_band1] >> 8) & 0xff);
settings_to_apply.band_3_limit = ((settings[idx_band1] >> 16) & 0xff);
settings_to_apply.band_4_limit = ((settings[idx_band1] >> 24) & 0xff);
settings_to_apply.band_5_limit = ((settings[idx_band2] >> 0) & 0xff);
settings_to_apply.band_6_limit = ((settings[idx_band2] >> 8) & 0xff);
settings_to_apply.band_7_limit = ((settings[idx_band2] >> 16) & 0xff);
settings_to_apply.ppm = (settings[idx_hw_setup] & mask_ppm) >> flag_ppm;
receiver.release();
}
}