-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.cpp
957 lines (875 loc) · 25.6 KB
/
actions.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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
///\file actions.cpp Implementation of application-specific actions
#include "Arduino.h"
#include <EEPROM.h>
#include "EEPROMAnything.h"
#include "settings.h"
#include "actions.h"
#include "mac.h"
#include "notes.h"
#include "sound.h"
#include "prosign.h"
#include "stack.h"
#include "teensy3_morse_bsp.h"
#include "Time.h"
const char* version("1.3");
const char* getVersion() {
return version;
}
ActionFn actions[256];
ActionFn savedActions[256];
#define actionSize (sizeof actions/sizeof actions[0])
void doNothing(MorseToken) {}
void initActions() {
for (size_t i(0); i < actionSize; ++i) {
actions[i] = doNothing;
}
}
uint8_t codeHash(MorseToken code) {
return code >> (wordBits - CHAR_BIT);
}
void assignAction(MorseToken code, ActionFn action) {
actions[codeHash(code)] = action;
}
void doAction(MorseToken code) {
actions[codeHash(code)](code);
}
/// A little safeguard against smashing the saved copy of the actions
/// and not being able to get back to the primary commands.
bool restorePending(false);
void saveActions() {
if (!restorePending) {
restorePending = true;
for (size_t i(0); i < actionSize; ++i) {
savedActions[i] = actions[i];
}
}
}
void restoreActions() {
if (restorePending) {
for (size_t i(0); i < actionSize; ++i) {
actions[i] = savedActions[i];
}
restorePending = false;
} else {
txError();
}
}
void push1RestoreAll(MorseToken code) {
pushSymbolStack(code);
restoreActions();
}
void dispatchOnStack(MorseToken code) {
pushSymbolStack(code);
if (matchScore("V") == 255) {
// Pop the 'V'.
popN(1);
// Transmit the top of the stack.
txSymbolStackTop(MorseToken());
} else if (matchScore("QRS") == 255) {
// Increase the dit time.
slowDown();
popN(3);
} else if (matchScore("QRQ") == 255) {
// Decrease the dit time.
speedUp();
popN(3);
} else if (matchScore("QTR?") == 255) {
// Ask the time.
txTimeHHMM(MorseToken());
popN(4);
} else if (matchScore("C") == 255) {
// Clear the stack.
clearSymbolStack(MorseToken());
Serial.println("Cleared stack.");
} else if (matchScore("nA") == 255) {
// Set the low voltage threshold.
popN(1);
uint32_t n(stackDigitsToUnsigned());
const uint32_t hi(getMaximumVoltage() - getLowVoltageThresholdDefault());
if (n - getLowVoltageThresholdDefault() < hi) {
setLowVoltageThreshold(n);
txString("K");
} else {
txError();
txString("RANGE");
}
} else if (matchScore("nB") == 255) {
// Set the high voltage threshold.
popN(1);
uint32_t n(stackDigitsToUnsigned());
const uint32_t hi(getMaximumVoltage() - getLowVoltageThresholdDefault());
if (n - getLowVoltageThresholdDefault() < hi) {
setHighVoltageThreshold(n);
txString("K");
} else {
txError();
txString("RANGE");
}
} else if (matchScore("nD") == 255) {
// Set the dit duration.
popN(1);
uint32_t n(stackDigitsToUnsigned());
if (n - 20 <= 200 - 20) {
setDitMicros(1000 * n);
txString("K");
} else {
txError();
txString("RANGE");
}
} else if (matchScore("F") == 255) {
// Get the announcement format.
popN(1);
txString("TX MSG END WITH ^AR^ K");
char buffer[128];
rxString(buffer, sizeof buffer);
if (buffer[0]) {
txString("K");
setAnnouncementFormat(buffer);
} else {
txString("QTA");
}
} else if (matchScore("G") == 255) {
// Copy the alarm format.
popN(1);
txString("TX MSG END WITH ^AR^ K");
char buffer[128];
rxString(buffer, sizeof buffer);
if (buffer[0]) {
txString("K");
setAlarmLowFormat(buffer);
} else {
txString("QTA");
}
} else if (matchScore("H") == 255) {
// Copy the alarm format.
popN(1);
txString("TX MSG END WITH ^AR^ K");
char buffer[128];
rxString(buffer, sizeof buffer);
if (buffer[0]) {
txString("K");
setAlarmHighFormat(buffer);
} else {
txString("QTA");
}
} else if (matchScore("c^SS^") == 255) {
// Erase the top of the stack.
popN(2);
txListSymbolStack(MorseToken());
txString("K");
} else if (matchScore("nI") == 255) {
// Set the nominal announcement repetition interval.
popN(1);
uint32_t n(stackDigitsToUnsigned());
if (n - 20 < 86400 - 20) {
setAnnouncementInterval(n);
txString("K");
} else {
txError();
txString("RANGE");
}
} else if (matchScore("nJ") == 255) {
// Set the alarm announcement repetition interval.
popN(1);
uint32_t n(stackDigitsToUnsigned());
if (n - 20 <= 86400 - 20) {
setAlarmInterval(n);
txString("K");
} else {
txError();
txString("RANGE");
}
} else if (matchScore("nQTR") == 255 || matchScore("nT") == 255) {
// Set the time without the four-digit requirement.
popN(1);
uint32_t n(stackDigitsToUnsigned());
uint32_t hour(n / 100);
uint32_t minute(n % 100);
if (hour < 24 && minute < 60) {
setTime(hour, minute, 0, 0, 0, 0);
setUptime(0);
txString("K");
} else {
txError();
txString("RANGE");
}
} else if (matchScore("O") == 255) {
initSettings();
Serial.println("Settings restored.\r\n");
printLog();
txString("K");
} else if (matchScore("nP") == 255) {
// Set the pitch of the side tone.
popN(1);
uint32_t n(stackDigitsToUnsigned());
if (n - 55 <= 1760 - 55) {
setTxHz(n);
txString("K");
} else {
txError();
txString("RANGE");
}
} else if (matchScore("X") == 255) {
// Exchange the top two elements of the stack.
popN(1);
exchangeStack(MorseToken());
txListSymbolStack(MorseToken());
txString("K");
} else if (matchScore("?") == 255) {
// Transmit the help text.
popN(1);
txHelp(MorseToken());
}
}
//----
void txSymbolStackTop(MorseToken) {
txLiteral(s(0));
}
void txListSymbolStack(MorseToken) {
Serial.print("Stack: ");
for (size_t i(0); i < symbolStackSize(); ++i) {
txLiteral(s(i));
Serial.print(s(i).toChar());
}
Serial.println();
}
void txTimeHHMM(MorseToken code) {
char buffer[] = "HHMM";
unsigned int m(minute());
unsigned int h(hour());
buffer[0] = '0' + h / 10;
buffer[1] = '0' + h % 10;
buffer[2] = '0' + m / 10;
buffer[3] = '0' + m % 10;
txString(buffer);
}
void txTimeVerbose(MorseToken code) {
// 0 1 2
// 012345678901234567890
char buffer[] = "QRT QRT HH MM ? HH MM";
unsigned int m(minute());
unsigned int h(hour());
buffer[8] = '0' + h / 10;
buffer[9] = '0' + h % 10;
buffer[11] = '0' + m / 10;
buffer[12] = '0' + m % 10;
buffer[16] = buffer[8];
buffer[17] = buffer[9];
buffer[19] = buffer[11];
buffer[20] = buffer[12];
txString(buffer);
}
void txUptime(MorseToken) {
Serial.print("uptime: ");
if (getUptime() < 1000) Serial.print('0');
if (getUptime() < 100) Serial.print('0');
if (getUptime() < 10) Serial.print('0');
Serial.println(getUptime());
txUnsigned(getUptime());
}
void txInternalTemperatureC(MorseToken) {
int degC(getInternalTemperatureC() + 0.5);
txUnsigned(degC);
Serial.println(degC);
}
void txInternalTemperatureF(MorseToken) {
int degF(1.8 * getInternalTemperatureC() + 32.0);
txUnsigned(degF);
Serial.println(degF);
}
void txMacAddress(MorseToken) {
read_mac();
print_mac();
for (uint8_t* p(mac); p < mac + 6; ++p) {
uint8_t octet1(*p >> 4);
uint8_t octet2(*p && 0x0F);
txLetter("0123456789ABCDEF"[octet1]);
txLetter("0123456789ABCDEF"[octet2]);
delay(200);
}
}
void slowDown() {
Serial.println("QRS - slower");
txString("QRS ");
int d(getDitMicros());
setDitMicros(d + 5000);
txString("QRS");
}
void speedUp() {
Serial.println("QRQ - faster");
txString("QRQ ");
int d(getDitMicros());
if (25000 <= d) {
setDitMicros(d - 5000);
txString("QRQ");
} else {
txError();
txString("RANGE");
}
}
void txAR(MorseToken) {
txString("^AR^ ^AR^ ^AR^");
}
void txEcho(MorseToken code) {
txString("II "); // I say again.
txLiteral(code);
txString(" ^AR^"); // End of message.
}
void txGreeting(MorseToken code) {
txString("CQ CQ CQ DE ET1 ET1 K");
Serial.println();
}
void getPressure(MorseToken code) {
txString("");
pwmFrequency(0);
pwmWrite(getDuty());
while (!touchPoll(dahPin)) {
uint32_t ci(touchRead(ditPin));
Serial.println(ci);
uint32_t f(map(ci, 500, 65535, 33, 4186));
pwmFrequency(f);
pwmWrite(getDuty());
delayMicroseconds(getDitMicrosDefault());
}
txString("^AR^");
}
void txTick(MorseToken) {
playTone(noteHz[96], getDitMicrosDefault()/2);
}
void txTiTick(MorseToken) {
playTone(noteHz[98], getDitMicrosDefault()/4);
delayMicroseconds(getDitMicrosDefault()/2);
playTone(noteHz[98], getDitMicrosDefault()/4);
}
void txTock(MorseToken) {
playTone(noteHz[84], getDitMicrosDefault()/2);
}
void printTouchThresholds() {
char pinLabel2[] = "Pin xx (dit) touch capacitance threshold: ";
if (ditPin < 10) {
pinLabel2[4] = ditPin + '0';
memmove(pinLabel2 + 5, pinLabel2 + 6, 37);
} else {
pinLabel2[4] = ditPin / 10 + '0';
pinLabel2[5] = ditPin % 10 + '0';
}
printLabelValueUnits(pinLabel2, getPinThreshold(ditPin), " pF");
if (ditPin < 10) {
memmove(pinLabel2 + 6, pinLabel2 + 5, 36);
}
pinLabel2[9] = 'a';
pinLabel2[10] = 'h';
if (dahPin < 10) {
pinLabel2[4] = dahPin + '0';
memmove(pinLabel2 + 5, pinLabel2 + 6, 37);
} else {
pinLabel2[4] = dahPin / 10 + '0';
pinLabel2[5] = dahPin % 10 + '0';
}
printLabelValueUnits(pinLabel2, getPinThreshold(dahPin), " pF");
}
uint32_t getVoltagePollInterval() {
return 30UL;
}
/// Voltage state: Low, Nominal, or High. Just for fun, the Hamming
/// distance between these ASCII codes is selected to be greater than
/// 1. Start in an alarm state. First reading should clear it.
enum alarmStateEnum {
voltageLow = 'v', // 111 0110
voltageNominal = '-', // 010 1101
voltageHigh = '^' // 101 1110
} alarmState = voltageLow;
void setAlarmState(char newState) {
alarmState = static_cast<alarmStateEnum>(newState);
}
char getAlarmState() {
return alarmState;
}
uint32_t getIntervalForState() {
uint32_t result;
if (alarmState != voltageNominal) {
result = getAlarmInterval();
} else {
result = getAnnouncementInterval();
}
return result;
}
const char* getFormatForState() {
switch (alarmState) {
case voltageLow: return getAlarmLowFormat();
case voltageNominal: return getAnnouncementFormat();
case voltageHigh: return getAlarmHighFormat();
default:
txError();
Serial.print("INTERNAL ERROR: Undefined state in getFormatForState().");
Serial.println((char) alarmState);
Serial.println((int) alarmState);
return 0;
}
}
/// Report settings and measurements over the serial port.
void printLog() {
Serial.print(
"Didah Voltage Monitor\r\n"
"=====================\r\n");
printLabelValueUnits("Version: ", getVersion(), "");
Serial.print(
"Documentation: http://pictographer.com/didah\r\n"
"\r\n"
"Device to periodically announce input voltage in Morse code.\r\n"
"\r\n");
Serial.print(
"Measurements\r\n"
"--------\r\n");
// 0 1 2
// 012345678901234567890
char buffer[] = "HHMM";
unsigned int m(minute());
unsigned int h(hour());
buffer[0] = '0' + h / 10;
buffer[1] = '0' + h % 10;
buffer[2] = '0' + m / 10;
buffer[3] = '0' + m % 10;
printLabelValueUnits("Time: ", buffer,
isTimeSet() ? "" : " (Unset. Use T to set.)");
// Uptime is incremented only when the time is set.
uint32_t u(getUptime());
// If this is still running 7,656 years from now, send me a bug report
// about the overflow.
printLabelValueUnits("Uptime: ", 15 * u,
isTimeSet() ? " minutes" : " minutes (Previous run.)");
const char* vl[] = {
" (INTERNAL ERROR)",
" mV ALARM LOW!",
" mV (Nominal)",
" mV ALARM HIGH!"
};
size_t vi(0);
switch (getAlarmState()) {
case voltageLow: vi = 1; break;
case voltageNominal: vi = 2; break;
case voltageHigh: vi = 3; break;
default: vi = 0;
}
printLabelValueUnits("Measured voltage: ", readMillivolts(), vl[vi]);
printLabelValueUnits("Temperature: ", getInternalTemperatureC(), " C");
Serial.println();
Serial.print(
"Settings\r\n"
"--------\r\n");
printLabelValueUnits(
"Nominal announcement format: ", getAnnouncementFormat(), "");
printLabelValueUnits(
"Nominal announcement interval: ", getAnnouncementInterval(), " seconds");
printLabelValueUnits(
"Low-voltage alarm threshold: ", getLowVoltageThreshold(), " mV");
printLabelValueUnits(
"Low-voltage alarm announcement format: ", getAlarmLowFormat(), "");
printLabelValueUnits(
"High-voltage alarm threshold: ", getHighVoltageThreshold(), " mV");
printLabelValueUnits(
"High-voltage alarm announcement format: ", getAlarmHighFormat(), "");
printLabelValueUnits(
"Alarm interval: ", getAlarmInterval(), " seconds");
printLabelValueUnits("Dit duration: ", getDitMicros()/1000, " ms");
printLabelValueUnits("Output frequency: ", getTxHz(), " Hz");
Serial.println();
}
void printBanner() {
Serial.print(
"Commands\r\n"
"--------\r\n"
"? - Help\r\n"
"A - Set low-voltage alarm threshold in millivolts [2700-16000]\r\n"
"B - Set high-voltage alarm threshold in millivolts [2700-16000]\r\n"
"D - Set dit duration in milliseconds [20-200]\r\n"
"F - Set normal announcement format\r\n"
" A-Z literal character\r\n"
" 0-9 literal character\r\n"
" <space> pause for one dit unit\r\n"
" <punctuation>\r\n"
" Literal punctuation: ");
Serial.print(morsePunctuation);
Serial.println(
"\r\n"
" ^<ps>^ prosign of up to seven symbols.\r\n"
" For example prosign ^BK^ is transmitted -....-.\r\n"
" %V monitor voltage in millivolts\r\n"
" %F temperature in degrees Fahrenheit\r\n"
" %C temperature in degrees Celsius\r\n"
" %T time of day in four-digit 24-hour time: HHMM\r\n"
" %U uptime in four digits: HHMM\r\n"
"G - Set low-voltage announcement format\r\n"
"H - Set high-voltage announcement format\r\n"
"I - Set normal announcement interval in seconds [20-86400]\r\n"
"J - Set alarm announcement interval in seconds [20-86400]\r\n"
"L - Print settings and log\r\n"
"O - Load settings from EEPROM\r\n"
"P - Set output frequency (pitch) in Hz [55-1760]\r\n"
"S - Save settings to EEPROM\r\n"
"T - Set the 24-hour time [0000-2359]\r\n"
);
}
//----
void setVolume(MorseToken) {
size_t n(symbolStackSize());
size_t dutyCycle(0);
(2 < n) && (dutyCycle += 100 * s(2).m2i());
(1 < n) && (dutyCycle += 10 * s(1).m2i());
(0 < n) && (dutyCycle += 1 * s(0).m2i());
popN(3 - (2 < n) - (1 < n));
setDuty(dutyCycle);
}
bool readRange(uint32_t& result,
const char* prompt,
uint32_t minimum,
uint32_t maximum)
{
Serial.print(prompt);
uint32_t v;
bool gotOne(readNumber(v));
bool isInRange(v - minimum <= maximum - minimum);
if (gotOne) {
if (isInRange) {
result = v;
} else {
Serial.print("\r\nOut of range. Value must be between ");
Serial.print(minimum);
Serial.print(" and ");
Serial.print(maximum);
Serial.println(".");
}
} else {
Serial.println("\r\nInvalid number. Aborting.");
}
return gotOne && isInRange;
}
void readEdit(char* buffer, bool& isNotTooLate,
const char* end, IntPredicate isIncluded, uint32_t timeout)
{
const char ASCII_BS = '\b';
const char ASCII_DEL = '\x7f';
char* p(buffer);
elapsedMicros elapsed;
do {
if (Serial.available()) {
char in(Serial.read());
if (isIncluded(in)) {
*p = in;
Serial.write(*p);
++p;
} else if (in == '\r') {
*p = 0;
break;
} else if (p != buffer && (in == ASCII_BS || in == ASCII_DEL)) {
Serial.write(ASCII_BS); // back up one
Serial.write(' '); // erase by writing a space
Serial.write(ASCII_BS); // back over the space
*p = 0;
--p;
}
}
isNotTooLate = elapsed < timeout;
} while (p != end && isNotTooLate);
if (isNotTooLate || p != buffer) {
*p = 0;
}
}
bool readLine(char* buffer, size_t n, uint32_t timeout) {
bool isNotTooLate(true);
if (!n) {
// No buffer space. You ask for nothing? You got it!
return isNotTooLate;
}
const char* e(buffer + n - 1);
readEdit(buffer, isNotTooLate, e, isprint, timeout);
return isNotTooLate;
}
bool readNumber(uint32_t& v, uint32_t timeout) {
bool isNotTooLate(true);
char buffer[32];
const char* e(buffer + sizeof buffer - 1);
readEdit(buffer, isNotTooLate, e, isdigit, timeout);
v = strtoul(buffer, 0, 10);
return isNotTooLate;
}
void mapToUpper(char* buffer, size_t n) {
for (size_t i(0); i < n; ++i) {
buffer[i] = toupper(buffer[i]);
}
}
bool isValidMorseFormat(const char* buffer, size_t n) {
bool result(true);
for (size_t i(0); i < n; ++i) {
const char c(buffer[i]);
if (ispunct(c)) {
if (strchr(morsePunctuation, c)) {
; // ok
} else if (c == '%' && i + 1 < n && strchr("CFTUV", buffer[i + 1])) {
; // ok
} else if (c == '^') {
; // ok, not checking prosign overflow, matched delimiters, or
// embedded escape sequences, e.g.
// ^^
// ^%F^
} else {
result = false;
break;
}
} else if (!c) {
break;
}
}
return result;
}
bool readMessageFormat(char* buffer, size_t bufferSize, const char* prompt) {
bool result(false);
Serial.print(prompt);
bool ok(readLine(buffer, bufferSize));
if (ok) {
mapToUpper(buffer, bufferSize);
size_t inputLength(strlen(buffer));
if (isValidMorseFormat(buffer, inputLength)) {
if (inputLength) {
Serial.print("\r\nFormat set to '");
Serial.print(buffer);
if (inputLength == bufferSize - 1) {
Serial.print("'. (Truncated at ");
Serial.print(bufferSize - 1);
Serial.println(" characters.)");
} else {
Serial.println("'.");
}
} else {
Serial.print("\r\nFormat set to default '");
Serial.print(getAnnouncementFormatDefault());
Serial.println("'.");
strcat(buffer, getAnnouncementFormatDefault());
}
result = true;
} else {
Serial.println("\r\nInvalid character or sequence. Aborting.");
}
} else {
Serial.println("\r\nKeyboard timer expired. Aborting.");
}
return result;
}
void readEvalPrint() {
char cmd(toupper(Serial.read()));
Serial.println(cmd);
if (cmd == '\r') {
Serial.print("> ");
} else if (cmd == '?') {
printLog();
printBanner();
Serial.print("> ");
} else if (cmd == 'A') {
uint32_t v;
if (readRange(v,
"Enter low-voltage threshold in mV [2700-16000]: ",
getLowVoltageThresholdDefault(),
getMaximumVoltage()))
{
printLabelValueUnits("\r\nLow-voltage threshold: ", v, " mV");
setLowVoltageThreshold(v);
}
Serial.print("> ");
} else if (cmd == 'B') {
uint32_t v;
if (readRange(v,
"Enter high-voltage threshold in mV [2700-16000]: ",
getLowVoltageThresholdDefault(),
getMaximumVoltage()))
{
printLabelValueUnits("\r\nHigh-voltage threshold: ", v, " mV");
setHighVoltageThreshold(v);
}
Serial.print("> ");
} else if (cmd == 'D') {
uint32_t v;
if (readRange(v, "Enter 'dit' duration in ms [20-200]: ", 20, 200)) {
printLabelValueUnits("\r\nDit duration: ", v, " ms");
setDitMicros(v * 1000);
}
Serial.print("> ");
} else if (cmd == 'F') {
const size_t bufferSize(getAnnouncementBufferSize());
(void) readMessageFormat(getAnnouncementBuffer(), bufferSize,
"Enter announcement format "
"[BAT %V MV ? %V MV]: ");
Serial.print("> ");
} else if (cmd == 'G') {
const size_t bufferSize(getAlarmLowBufferSize());
(void) readMessageFormat(getAlarmLowBuffer(), bufferSize,
"Enter low-voltage alarm format "
"[BAT %V MV ? %V MV]: ");
Serial.print("> ");
} else if (cmd == 'H') {
const size_t bufferSize(getAlarmHighBufferSize());
(void) readMessageFormat(getAlarmHighBuffer(), bufferSize,
"Enter high-voltage alarm format "
"[BAT %V MV ? %V MV]: ");
Serial.print("> ");
} else if (cmd == 'I') {
uint32_t v;
if (readRange(v, "Enter nominal announcement interval in "
"seconds [20-86400]: ", 20, 86400))
{
printLabelValueUnits("\r\nNominal announcement interval: ",
v, " seconds");
setAnnouncementInterval(v);
}
Serial.print("> ");
} else if (cmd == 'J') {
uint32_t v;
if (readRange(v, "Enter low-voltage alarm announcement interval in "
"seconds [20-86400]: ", 20, 86400))
{
printLabelValueUnits("\r\nAlarm announcement interval: ",
v, " seconds");
setAlarmInterval(v);
}
Serial.print("> ");
} else if (cmd == 'L') {
printLog();
Serial.print("> ");
} else if (cmd == 'O') {
initSettings();
Serial.println("Settings restored.\r\n");
printLog();
Serial.print("> ");
} else if (cmd == 'P') {
uint32_t v;
if (readRange(v, "Enter output frequency in Hz [55-1760]: ", 55, 1760)) {
printLabelValueUnits("\r\nOutput frequency: ", v, " Hz");
setTxHz(v);
}
Serial.print("> ");
} else if (cmd == 'R') {
txRaw(MorseToken());
} else if (cmd == 'S') {
saveSettings();
Serial.println("Settings saved.\r\n");
Serial.print("> ");
} else if (cmd == 'T') {
uint32_t v;
if (readRange(v, "Enter the 24-hour time [0000-2359]: ", 0, 2359)) {
printLabelValueUnits("\r\nTime set to: ", v, "");
setTime(v / 100, v % 100, 0, 0, 0, 0);
setUptime(0);
}
Serial.print("> ");
} else if (cmd == 'U') {
Serial.print("hasRTC(): ");
Serial.println(hasRTC() ? "yes" : "no");
describeRTC();
updateUptime();
switch (timeStatus()) {
case timeNotSet:
Serial.println("timeNotSet");
break;
case timeNeedsSync:
Serial.println("timeNeedsSync");
break;
case timeSet:
Serial.println("timeSet");
break;
default:
Serial.print("timeStatus() returned unexpected value: ");
Serial.println(timeStatus());
}
} else {
Serial.println("Unrecognized commmand. Enter '?' for help.");
Serial.print("> ");
}
flushTouch();
}
void txHelp(MorseToken) {
txString(
"DIDAH ANNOUNCES VOLTAGE ON TX. "
"SEE HTTP://PICTOGRAPHER.COM/DIDAH. "
"USB SERIAL 9600 BAUD NO PARITY NO STOP BITS. "
"OPEN CASE TO WIRE UP. "
"MIN V 2700 MV. "
"M FOR MENU. "
"K");
}
void txMenu(MorseToken) {
txString(
"A LOW V IN MV, "
"B HIGH V IN MV, "
"C CLEAR, "
"D DIT MS, "
"F MSG FMT, "
"G ALARM LOW FMT, "
"H ALARM HIGH FMT, "
"I RPT SECS, "
"J RPT ALARM SECS, "
"L LIST, "
"M MENU, "
"O LOAD, "
"P TONE HZ, "
"S SAVE, "
"T TIME, "
"U UPTIME. K"
);
}
void txSettings(MorseToken) {
txString("SETTINGS. "
"MSG RPT SKED IS ");
txUnsigned(getAnnouncementInterval());
txString(" SECONDS. "
"MSG FORMAT IS ");
char buf[128];
{
const char* a(getAnnouncementFormat());
// Change % to ?. Do the reverse on data entry.
strcpy(buf, a);
char* p(buf);
while (*p) {
if (*p == '%') {
*p = '?';
}
++p;
}
}
txString(buf);
txString(" . "
"ALARM LOW-VOLTAGE IS ");
txUnsigned(getLowVoltageThreshold());
txString(" MV. "
"ALARM MSG RPT SKED IS ");
txUnsigned(getAlarmInterval());
txString(" SECONDS. "
"ALARM LOW MSG FORMAT IS ");
{
const char* a(getAlarmLowFormat());
strcpy(buf, a);
char* p(buf);
while (*p) {
if (*p == '%') {
*p = '?';
}
++p;
}
}
txString(buf);
txString("ALARM HIGH MSG FORMAT IS ");
{
const char* a(getAlarmLowFormat());
strcpy(buf, a);
char* p(buf);
while (*p) {
if (*p == '%') {
*p = '?';
}
++p;
}
}
txString(buf);
txString(" .");
}
void txRaw(MorseToken) {
Serial.println(getRawVoltage());
}