-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.cpp
More file actions
523 lines (474 loc) · 12.5 KB
/
remote.cpp
File metadata and controls
523 lines (474 loc) · 12.5 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Computer Assisted Transceiver
*
* Handle checking the serial port for commands from a computer
*
* Using Serial.print a lot rather than sprintf(buf) / Serial.print(buf) actually saves a lot of progmem.
*/
#include "bitxultra.h"
#if HAVE_CAT
static const char ERR_INTX [] PROGMEM = "Not valid while TX";
static const char ERR_INVALID [] PROGMEM = "Invalid parameter";
static const char ERR_RANGE [] PROGMEM = "Out of range";
static const char ERR_DIS [] PROGMEM = "TX Disabled on freq";
static const char S_0 [] PROGMEM = "0";
static const char CMD_STATUS [] PROGMEM = "status";
static const char CMD_FREQ [] PROGMEM = "freq";
static const char CMD_RIT [] PROGMEM = "rit";
static const char CMD_MOD [] PROGMEM = "mod";
static const char CMD_VFO [] PROGMEM = "vfo";
static const char CMD_CHANNEL [] PROGMEM = "channel";
static const char CMD_STORE [] PROGMEM = "store";
static const char CMD_SIDETONE[] PROGMEM = "sidetone";
static const char CMD_WPM [] PROGMEM = "wpm";
static const char CMD_CAL [] PROGMEM = "cal";
static const char CMD_BFOTRIM [] PROGMEM = "bfotrim";
static const char CMD_TX [] PROGMEM = "tx";
static const char CMD_METER [] PROGMEM = "meter";
static const char CMD_ANN [] PROGMEM = "ann";
static const char CMD_CW [] PROGMEM = "cw";
static const char CMD_CWB [] PROGMEM = "cwb";
static const char CMD_FSQ [] PROGMEM = "fsq";
static const char CMD_FSQB [] PROGMEM = "fsqb";
static const char CMD_HELP [] PROGMEM = "help";
typedef PGM_P (*remoteHandler)(char *p);
struct cmd {
PGM_P cmdname;
remoteHandler handler;
};
#define UNUSED(x) if (x) { } // optimized away but stops unused var warning
#define SERIAL_IN_SIZE 30
static char serial_in[SERIAL_IN_SIZE+1];
static unsigned char serial_in_count = 0;
static PGM_P h_status(char *p) {
UNUSED(p)
byte i;
Serial.print(F("vfos:"));
Serial.println(state.vfoCount);
Serial.print(F("vfo:"));
Serial.println(state.vfoActive+'A');
#if HAVE_CHANNELS
Serial.print(F("chCount:"));
Serial.println(state.channelCount);
Serial.print(F("chActive:"));
Serial.println(state.channelActive);
#endif
#if HAVE_CW
Serial.print(F("sTone:"));
Serial.println(state.sideTone);
#if HAVE_CW==2
Serial.print(F("WPM:"));
Serial.println(state.wpm);
#endif
#endif
Serial.print(F("VFOMode:"));
Serial.println((state.useVFO ? FH(S_ON) : FH(S_OFF)));
for (i=0; i<state.vfoCount; i++) {
struct vfo *vfo = &vfos[i];
Serial.print(F("VFO"));
Serial.print((char)(i+'A'));
Serial.print(FH(S_COLON));
Serial.print(vfo->frequency);
Serial.print(FH(S_COMMA));
Serial.print(vfo->rit);
Serial.print(FH(S_COMMA));
Serial.print(vfo->ritOn);
Serial.print(FH(S_COMMA));
Serial.println(FH(mod_name(vfo->mod)));
}
return NULL;
}
static PGM_P h_freq(char *p) {
if (*p) {
Frequency f = atol(p);
if (inTx!=INTX_NONE) {
return ERR_INTX;
} else if (f>=LOWEST_FREQ && f<=HIGHEST_FREQ) {
vfos[state.vfoActive].frequency=f;
state.useVFO=true;
setFrequency(RIT_ON);
updateDisplay();
} else {
return ERR_INVALID;
}
} else {
Serial.print(F("FREQ:"));
Serial.println(vfos[state.vfoActive].frequency);
}
return NULL;
}
static PGM_P h_rit(char *p) {
if (*p) {
long rit = atoi(p);
if ((rit==0) && strcmp(p,"0"))
return ERR_INVALID;
if (rit>RIT_MAX || rit<RIT_MIN)
return ERR_RANGE;
if (inTx!=INTX_NONE)
return ERR_INTX;
vfos[state.vfoActive].rit = rit;
vfos[state.vfoActive].ritOn = (rit==0 ? false : true);
setFrequency(RIT_ON);
updateDisplay();
} else {
Serial.print(F("RIT:"));
Serial.print(vfos[state.vfoActive].rit);
Serial.print(F(","));
Serial.println(vfos[state.vfoActive].ritOn ? FH(S_ON) : FH(S_OFF));
}
return NULL;
}
static PGM_P h_mod(char *p) {
if (*p) {
if (inTx!=INTX_NONE) {
return ERR_INTX;
} else if (!strcmp_P(p, S_LSB)) {
vfos[state.vfoActive].mod=MOD_LSB;
} else if (!strcmp_P(p, S_USB)) {
vfos[state.vfoActive].mod=MOD_USB;
} else if (!strcmp_P(p, S_AUTO)) {
vfos[state.vfoActive].mod=MOD_USB;
} else {
return ERR_INVALID;
}
setFrequency(RIT_ON);
updateDisplay();
} else {
Serial.print(F("MOD:"));
Serial.println(strcpy_P(c,mod_name(vfos[state.vfoActive].mod)));
}
return NULL;
}
static PGM_P h_vfo(char *p) {
if (*p) {
char v=toupper(*p);
if (v<'A' || v>=(state.vfoCount+'A'))
return ERR_RANGE;
v-='A';
state.vfoActive=v;
setFrequency(RIT_ON);
updateDisplay();
} else {
Serial.print(F("VFO:"));
Serial.println(state.vfoActive+'A');
}
return NULL;
}
#if !CAT_MINIMAL
#if HAVE_CHANNELS
static PGM_P h_channel(char *p) {
if (*p) {
int ch=atoi(p);
if ((ch==0) && strcmp_P(p,S_0))
return ERR_INVALID;
if (ch<0 || ch>=state.channelCount)
return ERR_RANGE;
if (inTx!=INTX_NONE)
return ERR_INTX;
state.channelActive = ch;
state.useVFO=false;
get_channel((unsigned char)ch);
setFrequency(RIT_ON);
updateDisplay();
} else {
Serial.print(F("CHANNEL:"));
Serial.println(state.channelActive);
}
return NULL;
}
static PGM_P h_store(char *p) {
if (*p) {
int ch=atoi(p);
if ((ch==0) && strcmp_P(p,S_0))
return ERR_INVALID;
if (ch<0 || ch>=state.channelCount)
return ERR_RANGE;
put_channel(ch);
} else {
return ERR_INVALID;
}
return NULL;
}
#endif // HAVE_CHANNELS
#if HAVE_CW
static PGM_P h_sidetone(char *p) {
if (*p) {
int st=atoi(p);
if (st<SIDETONE_MIN || st>SIDETONE_MAX)
return ERR_RANGE;
state.sideTone=st;
} else {
Serial.print(F("SIDETONE:"));
Serial.println(state.sideTone);
}
return NULL;
}
#if HAVE_CW == 2
static PGM_P h_wpm(char *p) {
if (*p) {
int wpm=atoi(p);
if (wpm<WPM_MIN || wpm>WPM_MAX) {
return ERR_RANGE;
} else {
state.wpm=wpm;
}
} else {
Serial.print(F("WPM:"));
Serial.println(state.wpm);
}
return NULL;
}
#endif
#if HAVE_CW_SENDER
static PGM_P h_cwsender(char *p) {
if (*p) {
send_cw_string(p);
} else {
Serial.print(F("CW: send what?"));
}
return NULL;
}
#if HAVE_CW_BEACON
static PGM_P h_cwbeacon(char *p) {
if (*p) {
if (*p=='|') {
// copy the beacon string back out into the send buffer. interprets prosigns.
send_cw_string(EH(CWBEACON_EEPROM_START), CWBEACON_MAXLEN);
} else {
// store the beacon string to EEPROM
put_beacon_text(p);
}
} else {
Serial.print(F("CWB:"));
// dump the beacon text to Serial
print_beacon_text();
}
return NULL;
}
#endif
#endif
#if HAVE_FSQ_BEACON
static PGM_P h_fsqsender(char *p) {
// TODO: encode p into the FSQ buffer and send it
UNUSED(p);
return NULL;
}
static PGM_P h_fsqbeacon(char *p) {
// TODO: store p as the FSQ parameters.
UNUSED(p);
struct fsq_data data;
char *pa,*pb;
pa=p;
pb=p;
while (*pa && *pa!=',') pa++;
if (*pa) {
*pa='\0';
strcpy(data.call, pb);
pa++;
pb=pa;
while (*pa && *pa!=',') pa++;
if (*pa) {
*pa='\0';
strcpy(data.loc, pb);
pa++;
pb=pa;
while (*pa && *pa!=',') pa++;
if (*pa) {
*pa='\0';
data.dbm = atoi(pb);
pa++;
pb=pa;
strcpy(data.message, pb);
set_fsq_data(&data);
return NULL;
}
}
}
return ERR_INVALID;
}
#endif
#endif // HAVE_CW
static PGM_P h_cal(char *p) {
UNUSED(p)
int32_t cal;
if (*p) {
cal = atoi(p);
if (cal<-300000 || cal>300000)
return ERR_RANGE;
put_calibration(cal);
setFrequency(RIT_ON);
} else {
get_calibration(cal);
Serial.print(F("CAL:"));
Serial.println(cal);
}
return NULL;
}
#if HAVE_BFO
static PGM_P h_bfotrim(char *p) {
UNUSED(p)
if (*p) {
int trim = atoi(p);
if (trim<BFOTRIM_MIN || trim>BFOTRIM_MAX)
return ERR_RANGE;
state.bfo_trim = trim;
setBFO(bfo_freq);
setFrequency(RIT_AUTO);
} else {
Serial.print(F("BFOTRIM:"));
Serial.println(state.bfo_trim);
}
return NULL;
}
#endif // HAVE_BFO
#endif // !CAT_MINIMAL
static PGM_P h_tx(char *p) {
if (!strcmp_P(p,PSTR("on"))) {
if (TXon(INTX_CAT)) {
return ERR_DIS;
// next loop() will change back to whatever PTT is doing
}
} else {
TXoff();
}
return NULL;
}
#if HAVE_SMETER
static PGM_P h_meter(char *p) {
UNUSED(p)
if (inTx!=INTX_NONE) {
sprintf_P(c,PSTR("SWR:%1.1f"),last_swr/100);
Serial.println(c);
} else {
#define StoNum(s) (s<=9 ? s : (s-9) * 10)
Serial.print(F("S-METER:"));
Serial.print(StoNum(avg_s_level));
Serial.print(F(" p "));
Serial.println(StoNum(peak_s_level));
}
return NULL;
}
#endif
#if HAVE_ANALYSER
static PGM_P h_ann(char *p) {
UNUSED(p)
if (*p) mode=MODE_NORMAL; // any parameter exits analyser mode
else startAnalyser();
return NULL;
}
#endif
#if !CAT_MINIMAL
static PGM_P h_help(char *p);
#endif
static const struct cmd commandlist[] PROGMEM = {
{ CMD_STATUS, &h_status },
{ CMD_FREQ, &h_freq },
{ CMD_RIT, &h_rit },
{ CMD_MOD, &h_mod },
{ CMD_VFO, &h_vfo },
#if !CAT_MINIMAL
#if HAVE_CHANNELS
{ CMD_CHANNEL, &h_channel },
{ CMD_STORE, &h_store },
#endif
#if HAVE_CW
{ CMD_SIDETONE, &h_sidetone },
#if HAVE_CW == 2
{ CMD_WPM, &h_wpm },
#endif
#if HAVE_CW_SENDER
{ CMD_CW, &h_cwsender },
#endif
#if HAVE_CW_BEACON
{ CMD_CWB, &h_cwbeacon },
#endif
#if HAVE_FSQ_BEACON
{ CMD_FSQ, &h_fsqsender },
{ CMD_FSQB, &h_fsqbeacon },
#endif
#endif // HAVE_CW
{ CMD_CAL, &h_cal },
#if HAVE_BFO
{ CMD_BFOTRIM, &h_bfotrim },
#endif
#endif // !CAT_MINIMAL
#if HAVE_PTT
{ CMD_TX, &h_tx },
#endif
#if HAVE_SMETER
{ CMD_METER, &h_meter },
#endif
#if HAVE_ANALYSER
{ CMD_ANN, &h_ann },
#endif
#if !CAT_MINIMAL
{ CMD_HELP, &h_help },
#endif
};
#if !CAT_MINIMAL
static PGM_P h_help(char *p) {
UNUSED(p)
byte i;
Serial.print(F("Valid Commands:"));
for (i=0; i<(sizeof(commandlist)/sizeof(commandlist[0])); i++) {
Serial.print(FH(pgm_read_word(&(commandlist[i].cmdname))));
Serial.print(FH(S_COMMA));
}
Serial.println();
return NULL;
}
#endif // !CAT_MINIMAL
/**
* Called each time we get a newline on the serial port.
* cmd will be at least one char, NULL terminated, but may not be a valid command.
*/
void process_command(char *cmd)
{
char *p=cmd;
PGM_P err=NULL;
byte i;
while (*p && (*p!=' ')) p++; // find the first space
if (*p) *p++='\0'; // turn the first space into a NULL unless there wasn't one.
i=0;
while (i<(sizeof(commandlist)/sizeof(commandlist[0]))) {
if (!strcmp_P(cmd, pgm_read_word(&(commandlist[i].cmdname)))) {
remoteHandler handler = (remoteHandler)pgm_read_word(&(commandlist[i].handler));
err=handler(p);
if (err!=NULL) {
Serial.print(F("ERR: "));
Serial.println(FH(err));
} else {
Serial.println(F("OK"));
}
return;
}
i++;
}
Serial.println(F("ERR:Unknown Command"));
}
/**
* SerialEvent is called between each run of loop().
* We should avoid any slow actions in here (like delays)
* We should also minimize delays in loop() so this gets called regularly.
*/
void serialEvent()
{
while (Serial.available()) {
char ch = Serial.read();
switch (ch) {
case '\r':
case '\n': if (serial_in_count>0) {
serial_in[serial_in_count]='\0';
#if 0
Serial.print(F(">"));
Serial.println(serial_in);
#endif
process_command(serial_in);
serial_in_count=0;
}
break;
default: if (serial_in_count < (SERIAL_IN_SIZE-1)) serial_in[serial_in_count++]=ch;
}
}
}
#endif // HAVE_CAT