forked from ok1cdj/SX1281_QO100_TX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSX1281_QO100_TX.ino
1760 lines (1592 loc) · 57.1 KB
/
SX1281_QO100_TX.ino
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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// 2021-08 QO-100 SAT CW Transmitter with OLED Display
// by Jan Uhrin, OM2JU and Ondrej Kolonicny, OK1CDJ
//
//
// States of program with explanation:
//
// RUN - main state, displays freq. and status, responds to keyer or received UDP packets
// SET_PWM
// SET_KEYER
// SET_OFFSET
// SET_SSID
// SET_PWD
// WIFI_RECONNECT
// ...
//
//
// Power level settings - from documentation of LoRa128xF27 module
// LEV dBm mA Reg value
// 9 26.4 520 13
// 8 25.5 426 10
// 7 23.4 343 7
// 6 20.85 268 4
// 5 18.26 229 1
// 4 15.2 182 -2
// 3 12.3 155 -5
// 2 9.3 138 -8
// 1 6.0 130 -12
// 0 3.0 125 -15
//
// SX128x datasheet p. 73: Reg vale 0 = -18dBm, Reg value 31 = 13dBm ==> PA of the module has gain 32.2 dB
//
//
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Preferences.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncUDP.h>
#include <SPIFFS.h>
#include "time.h"
#include <SPI.h> //the lora device is SPI based so load the SPI library
#include <SX128XLT.h> //include the appropriate library
#include "Settings.h" //include the setiings file, frequencies, LoRa settings etc
#define UDP_PORT 6789 // UDP port for CW
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define PUSH_BTN_PRESSED 0
#define ReadPushBtnVal() pushBtnVal=digitalRead(ROTARY_ENC_PUSH)
#define WAIT_Push_Btn_Release(milisec) delay(milisec);while(digitalRead(ROTARY_ENC_PUSH)==0){}
#define TIMER_PERIOD_USEC 2500 // 2.5 msec
// Webserver
AsyncWebServer server(80);
AsyncUDP udp;
QueueHandle_t queue;
String ssid = "";
String ssid_ap = "QO100TX";
String password = "";
String apikey = "";
String sIP = "";
String sGateway = "";
String sSubnet = "";
String sPrimaryDNS = "";
String sSecondaryDNS = "";
String wifiNetworkList = "";
String spwr = "";
String message;
String sspeed = "20";
String sfreq;
String scmd;
uint32_t speed = 20;
// web server requests
const char* PARAM_MESSAGE = "message";
const char* PARAM_FRQ_INDEX = "frq_index";
const char* PARAM_SPEED = "speed";
const char* PARAM_PWR = "pwr";
const char* PARAM_SSID = "ssid";
const char* PARAM_PASSWORD = "password";
const char* PARAM_APIKEY = "apikey";
const char* PARAM_DHCP = "dhcp";
const char* PARAM_CON = "con";
const char* PARAM_SUBNET = "subnet";
const char* PARAM_GATEWAY = "gateway";
const char* PARAM_PDNS = "pdns";
const char* PARAM_SDNS = "sdns";
const char* PARAM_LOCALIP = "localip";
const char* PARAM_CMD_B = "cmd_B";
const char* PARAM_CMD_C = "cmd_C";
const char* PARAM_CMD_D = "cmd_D";
const char* PARAM_CMD_T = "cmd_T";
const char* PARAM_VAL = "val";
bool wifiConfigRequired = false;
bool wifiSoftAP = false;
bool dhcp = true; // dhcp enable disble
bool con = false; // connection type wifi false
IPAddress IP;
IPAddress gateway;
IPAddress subnet;
IPAddress primaryDNS;
IPAddress secondaryDNS;
// Program state
enum state_t {
S_RUN = 0,
S_RUN_RUN,
S_TOP_MENU_ITEMS,
S_RUN_CQ,
S_SET_SPEED_WPM,
S_SET_OUTPUT_POWER,
S_SET_KEYER_TYPE,
S_SET_FREQ_OFFSET,
S_SET_BUZZER_FREQ,
S_SET_TEXT_GENERIC,
S_WIFI_RECONNECT,
S_RUN_BEACON,
S_RUN_BEACON_HELL
} program_state;
enum set_text_state_t {
S_SET_MY_CALL = 0,
S_SET_WIFI_SSID,
S_SET_WIFI_PWD
} set_text_state;
//
// for($i=-1;$i<13;$i++) { print $i+1 . " "; print 18.26+0.7*$i . " " . 10**((18.26+0.7*$i)/10). "\n"; }
// 0 17.56 57.0164272280748
// 1 18.26 66.9884609416526
// 2 18.96 78.7045789695099
// 3 19.66 92.4698173938223
// 4 20.36 108.642562361707
// 5 21.06 127.643880881134
// 6 21.76 149.968483550237
// 7 22.46 176.197604641163
// 8 23.16 207.014134879104
// 9 23.86 243.220400907382
// 10 24.56 285.759054337495
// 11 25.26 335.737614242955
// 12 25.96 394.457302075279
// 13 26.66 463.446919736288
//
#define PowerArrayMiliWatt_Size 6
//
// { Power_mW, Reg-setting }
const uint32_t PowerArrayMiliWatt [][2] = {
{ 0, 0 }, // 0mW = no output power, special case
{ 50, 0 }, // cca 50 mW
{ 100, 4 }, // cca 100 mW
{ 200, 8 }, // cca 200 mW
{ 330, 11 }, // cca 300 mW
{ 460, 13 } // cca 460 mW
};
//
//
const char * TopMenuArray[] = {
"1. << Back ",
"2. CQ... ",
"3. Set WPM ",
"4. Set Out Power ",
"5. Set Keyer Typ ",
"6. Set Offset Hz ",
"7. Set Buzzer Freq",
"8. Set My Call ",
"9. Set WiFi SSID ",
"10. Set WiFi PWD ",
"11. WiFi Reconnect",
"12. Beacon (vvv) ",
"13. Beacon FHELL "
};
//
// Rotary Encoder structure
struct RotaryEncounters
{
int32_t cntVal;
int32_t cntMin;
int32_t cntMax;
int32_t cntIncr;
int32_t cntValOld;
};
hw_timer_t * timer = NULL;
//TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
SX128XLT LT; // Create a library class instance called LT
uint8_t rotaryA_Val = 0xFF;
uint8_t rotaryB_Val = 0xFF;
uint8_t ISR_cnt = 0;
uint8_t cntIncrISR;
uint32_t loopCnt = 0;
uint32_t menuIndex;
uint32_t timeout_cnt = 0;
uint8_t keyerVal = 1;
uint8_t pushBtnVal = 1;
uint8_t keyerCWstarted = 0;
uint8_t pushBtnPressed = 0;
uint8_t stop = 0;
uint32_t tmp32a, tmp32b;
uint32_t FreqWord = 0;
uint32_t FreqWordNoOffset = 0;
uint32_t WPM_dot_delay;
char freq_ascii_buf[20]; // Buffer for formatting of FREQ value
char general_ascii_buf[40];
uint8_t general_ascii_buf_index = 0;
char mycall_ascii_buf[40];
char wifi_ssid_ascii_buf[40];
char wifi_pwd_ascii_buf[40];
bool params_set_by_udp_packet = false;
char morse_c, morse_c_old;
String s_mycall_ascii_buf;
//String s_wifi_ssid_ascii_buf;
//String s_wifi_pwd_ascii_buf;
String s_general_ascii_buf;
String udpdataString;
char cq_message_buf[] = "CQ CQ DE ";
char cq_message_end_buf[] = " +K";
char beacon_message_buf[] = "VVV VVV VVV TEST ";
char eee_message_buf[] = "E E E E E E E E E E E E E E E E E E E E E E E E E";
RotaryEncounters RotaryEnc_FreqWord;
RotaryEncounters RotaryEnc_MenuSelection;
RotaryEncounters RotaryEnc_KeyerSpeedWPM;
RotaryEncounters RotaryEnc_KeyerType;
RotaryEncounters RotaryEnc_OffsetHz;
RotaryEncounters RotaryEnc_BuzzerFreq;
RotaryEncounters RotaryEnc_OutPowerMiliWatt;
RotaryEncounters RotaryEnc_TextInput_Char_Index;
RotaryEncounters RotaryEncISR;
Preferences preferences;
// Copy values of variable pointed by r1 to RotaryEncISR variable
void RotaryEncPush(RotaryEncounters *r1) {
RotaryEncISR = *r1;
RotaryEncISR.cntValOld = RotaryEncISR.cntVal + 1; // Make Old value different from actual in order to e.g. force initial update of display
}
// Copy values of RotaryEncISR variable to variable pointed by r1
void RotaryEncPop(RotaryEncounters *r1) {
*r1 = RotaryEncISR;
}
// Display - Prepare box and cursor for main field
void display_mainfield_begin(uint8_t x) {
display.setTextColor(WHITE);
//display.clearDisplay();
display.fillRect(1, 10, SCREEN_WIDTH - 2, 35, BLACK); // x, y, width, height
display.setTextSize(1);
display.setCursor(x, 25);
}
// Display - Prepare box and cursor for value field
void display_valuefield_begin () {
display.fillRect(5, 43 - 2, SCREEN_WIDTH - 10, 12, WHITE); // x, y, width, height
display.fillRect(6, 43 - 2 + 1, SCREEN_WIDTH - 10 - 2, 12 - 2, BLACK); // x, y, width, height
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 43);
//display.fillRect(10,30,90,50,BLACK);
}
// Display - Clear the valuefield box
void display_valuefield_clear () {
display.fillRect(5, 43 - 2, SCREEN_WIDTH - 10, 12, BLACK); // x, y, width, height
}
// Display - show values on status bar
void display_status_bar () {
// Delete old info by drawing rectangle
display.fillRect(0, SCREEN_HEIGHT - 9 - 9, SCREEN_WIDTH, 9 + 9, WHITE); // x, y, width, height
display.fillRect(0, 0, SCREEN_WIDTH, 9, WHITE); // x, y, width, height
//
display.setTextColor(BLACK);
display.setTextSize(1);
//
// Display MY_CALL, configured WPM (or Straight keyer) and configured power in mW
display.setCursor(1, 1); // 7 is the font height
display.print(s_mycall_ascii_buf);
display.setCursor(52, 1); // 7 is the font height
if (RotaryEnc_KeyerType.cntVal & 0x00000001) {
display.print("Manual,");
} else {
display.print(RotaryEnc_KeyerSpeedWPM.cntVal);
display.print("wpm, ");
}
display.print(PowerArrayMiliWatt[RotaryEnc_OutPowerMiliWatt.cntVal][0]);
display.print("mW");
// Display WiFi SSID and IP address
display.setCursor(1, SCREEN_HEIGHT - 8 - 8); // 7 is the font height
display.print(wifiSoftAP == false ? ssid.c_str() : ssid_ap.c_str());
display.setCursor(1, SCREEN_HEIGHT - 8); // 7 is the font height
display.print(IP);
display.display();
}
// Limit values of RotaryEncISR.cntVal
void limitRotaryEncISR_values() {
if (RotaryEncISR.cntVal >= RotaryEncISR.cntMax) {
RotaryEncISR.cntVal = RotaryEncISR.cntMax;
}
if (RotaryEncISR.cntVal <= RotaryEncISR.cntMin) {
RotaryEncISR.cntVal = RotaryEncISR.cntMin;
}
}
// Calculate duration of DOT from WPM
void Calc_WPM_dot_delay ( uint32_t wpm) {
WPM_dot_delay = (uint32_t) (double(1200.0) / (double) wpm);
}
//
// Refer to SX1280/1281 datasheet, state diagram in page 57. - Figure 10-1: Transceiver Circuit Modes
//
// 1. Command SET_TXCONTINUOUSWAVE will get the TRX state machine to TX mode
// 2. Command SET_FS will get the TRX state machine to FS mode (Freq. Synthesis)
//
// We should never go to STDBY mode since we want that PLL always runs
//
// Start CW - from FS to TX mode
void startCW() {
digitalWrite(LED1, LOW);
ledcWriteTone(9, RotaryEnc_BuzzerFreq.cntVal);
if (RotaryEnc_OutPowerMiliWatt.cntVal > 0) {
LT.txEnable();
LT.writeCommand(RADIO_SET_TXCONTINUOUSWAVE, 0, 0);
}
}
// Stop CW - from TX to FS mode
void stopCW() {
digitalWrite(LED1, HIGH);
ledcWriteTone(9, 0);
if (RotaryEnc_OutPowerMiliWatt.cntVal > 0) {
LT.writeCommand(RADIO_SET_FS, 0, 0); // This will terminate TXCONTINUOUSWAVE
LT.rxEnable(); // No real need for this but it saves power
}
}
//-----------------------------------------------------------------------------
// Encoding a character to Morse code and playing it
void morseEncode ( unsigned char rxd ) {
uint8_t i, j, m, mask, morse_len;
// rxd is already uppercase
//if (rxd >= 97 && rxd < 123) { // > 'a' && < 'z'
// rxd = rxd - 32; // make the character uppercase
//}
//
if ((rxd < 97) && (rxd > 12)) { // above 96 no valid Morse characters
m = Morse_Coding_table[rxd - 32];
morse_len = (m >> 5) & 0x07;
mask = 0x10;
if (morse_len >= 6) {
morse_len = 6;
mask = 0x20;
}
//
for (i = 0; i < morse_len; i++) {
startCW();
if ((m & mask) > 0x00) { // Dash
delay(WPM_dot_delay);
delay(WPM_dot_delay);
delay(WPM_dot_delay);
} else { // Dot
delay(WPM_dot_delay);
}
stopCW();
// Dot-wait between played dot/dash
delay(WPM_dot_delay);
mask = mask >> 1;
} //end for(i=0...
// Dash-wait between characters
delay(WPM_dot_delay);
delay(WPM_dot_delay);
delay(WPM_dot_delay);
//
} //if (rxd < 97...
}
// Morse encode including non playable characters with length up to 6 dots/dashes
// Ok, could be combined with former morseEncode...
void morseEncode2 ( uint8_t rxd ) {
uint8_t i, j, m, mask, morse_len;
rxd -= 128;
morse_len = Morse_Coding_table2[rxd]>>8;
m = Morse_Coding_table2[rxd] & 0xFF;
mask = 1<<(morse_len-1);
//
for (i = 0; i < morse_len; i++) {
startCW();
if ((m & mask) > 0x00) { // Dash
delay(WPM_dot_delay);
delay(WPM_dot_delay);
delay(WPM_dot_delay);
} else { // Dot
delay(WPM_dot_delay);
}
stopCW();
// Dot-wait between played dot/dash
delay(WPM_dot_delay);
mask = mask >> 1;
} //end for(i=0...
// Dash-wait between characters
delay(WPM_dot_delay);
delay(WPM_dot_delay);
delay(WPM_dot_delay);
//
}
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
// process replacement in html pages
String processor(const String& var) {
if (var == "FRQ_INDEX") {
return String(RotaryEncISR.cntVal);
}
if (var == "MYCALL") {
return s_mycall_ascii_buf;
}
if (var == "SPEED") {
return sspeed;
}
if (var == "NETWORKS" ) {
return wifiNetworkList;
}
if (var == "PWR" ) {
String rsp;
String pwst;
int n = PowerArrayMiliWatt_Size;
for (int i = 0; i < n; ++i) {
pwst = String(PowerArrayMiliWatt[i][0]);
rsp += "<option value=\"" + String(i) + "\" ";
if (RotaryEnc_OutPowerMiliWatt.cntVal == i) rsp += "SELECTED";
rsp += ">" + pwst + "</option>";
}
return rsp;
}
if (var == "APIKEY") {
return apikey;
}
if (var == "DHCP") {
String rsp = "";
if (dhcp) rsp = "checked";
return rsp;
}
if (var == "LOCALIP") {
return sIP;
}
if (var == "SUBNET") {
return sSubnet;
}
if (var == "GATEWAY") {
return sGateway;
}
if (var == "PDNS") {
return sPrimaryDNS;
}
if (var == "SDNS") {
return sSecondaryDNS;
}
return String();
}
void savePrefs()
{
//preferences.begin("my-app", false); -- We assume this has been already called
preferences.putString("ssid", ssid);
preferences.putString("password", password);
preferences.putString("apikey", apikey);
preferences.putBool("dhcp", dhcp);
preferences.putBool("con", con);
preferences.getString("ip", sIP);
preferences.getString("gateway", sGateway);
preferences.getString("subnet", sSubnet);
preferences.getString("pdns", sPrimaryDNS);
preferences.getString("sdns", sSecondaryDNS);
preferences.end(); // We can call preferences.end since after that we reboot
}
bool next_char_repeated_flag;
bool play_flag;
char morse_c_repeat_head;
// Task SendMorse
void SendMorse_old_no_Queue ( void * parameter) {
Serial.print("Send morse Task running on core ");
Serial.println(xPortGetCoreID());
for (;;) { //infinite loop
if (message != "") {
message.toUpperCase();
int stri = message.length();
for (int i = 0; i < stri; i++) {
morse_c = message.charAt(i);
if (morse_c >= 128) {
// if morse_c >=128 we are sending non/defined morse characted which is encoded by Encode2 routine
morseEncode2(morse_c);
} else {
// Encoding of regular Morse character
morseEncode(morse_c);
}
if (stri < message.length()) {
stri = message.length();
}
//
}
message = "";
}
vTaskDelay(20);
}
}
// Task SendMorse
void SendMorse ( void * parameter) {
Serial.print("Send morse Task running on core ");
Serial.println(xPortGetCoreID());
for (;;) { //infinite loop
if (xQueueReceive(queue, (void *)&morse_c, 0) == pdTRUE) {
//Serial.printf("M %d %d %d", morse_c, morse_c_old, next_char_repeated_flag);
//Serial.println();
// From UDP_KEYER we are receiving:
// 1. Normal Morse character
// 2. Repeated Morse character with header 0x0a (playable) or 0x15 (non-playable) followed by character itself (having value >= 128)
// Play-flag controls whether the character is played
play_flag = true;
//
// If actual one is repeated - check if it is the same as previous one and if not then we assume it was missed and therefore let it play
if (next_char_repeated_flag == true) {
if (morse_c_repeat_head == 0x0a) {
morse_c -= 128;
}
if (morse_c == morse_c_old) {
play_flag = false;
//Serial.println();
} else {
//Serial.println("Repeat activated !");
//Serial.println();
}
}
//
if (play_flag == true) {
if (morse_c >= 128) {
//
// We need to keep compatibility with CWDaemon, therefore it might seem that we make it complicated...
//
// if c >=128 we are sending undefined morse characted which is encoded by Encode2 routine
morseEncode2(morse_c);
} else {
// Encoding of regular Morse character
morseEncode(morse_c);
}
} // end play_flag...
//
// Check if it is repetition-header
if ((morse_c == 0x0a) || (morse_c == 0x15)) {
next_char_repeated_flag = true;
morse_c_repeat_head = morse_c;
} else {
next_char_repeated_flag = false;
morse_c_old = morse_c;
}
//
} // end xQueueReceive....
vTaskDelay(13);
}
}
// Send all characters in message String to Queue
void messageQueueSend() {
message.toUpperCase();
int stri = message.length();
for (int i = 0; i < stri; i++) {
char c = message.charAt(i);
xQueueSend(queue, &c, 1000);
}
}
void loop()
{
//-----------------------------------------
// -- Straight keyer
if (RotaryEnc_KeyerType.cntVal & 0x00000001) {
keyerVal = digitalRead(KEYER_DOT);
// Keyer pressed
if (keyerVal == 0) {
if (keyerCWstarted == 0) {
startCW();
}
keyerCWstarted = 1;
delay(10);
}
// Keyer released
if ((keyerVal == 1) && (keyerCWstarted == 1)) {
keyerCWstarted = 0;
stopCW();
delay(10);
}
} else {
// -- Iambic keywer
// Keyer pressed DOT
keyerVal = digitalRead(KEYER_DASH) << 1 | digitalRead(KEYER_DOT);
if ((keyerVal & 0x01) == 0) {
startCW();
delay(WPM_dot_delay);
stopCW();
delay(WPM_dot_delay);
}
// Keyer pressed DASH
keyerVal = digitalRead(KEYER_DASH) << 1 | digitalRead(KEYER_DOT);
if ((keyerVal & 0x02) == 0) {
startCW();
delay(WPM_dot_delay);
delay(WPM_dot_delay);
delay(WPM_dot_delay);
stopCW();
delay(WPM_dot_delay);
}
}
// Process timeout for display items other than main screen
if (program_state != S_RUN_RUN) {
if (timeout_cnt > 6000) {
display_valuefield_clear();
display_valuefield_begin();
display.print("Timeout...");
display.display();
delay(600);
timeout_cnt = 0;
RotaryEncPush(&RotaryEnc_FreqWord);
program_state = S_RUN;
}
}
//
// Main FSM
//-----------------------------------------
switch (program_state) {
//--------------------------------
case S_RUN:
timeout_cnt = 0;
display_valuefield_clear();
program_state = S_RUN_RUN;
break;
//--------------------------------
case S_RUN_RUN:
timeout_cnt = 0;
// Update display with Frequency info when it has changed
if (RotaryEncISR.cntVal != RotaryEncISR.cntValOld) {
limitRotaryEncISR_values();
display_mainfield_begin(23);
format_freq(RegWordToFreq(RotaryEncISR.cntVal), freq_ascii_buf, params_set_by_udp_packet);
params_set_by_udp_packet = false;
display.print(freq_ascii_buf);
display.display();
JUsetRfFrequency(RegWordToFreq(RotaryEncISR.cntVal), RotaryEnc_OffsetHz.cntVal);
display_status_bar();
}
RotaryEncISR.cntValOld = RotaryEncISR.cntVal;
// This has to be at very end since with RotaryEncPush we are making RotaryEncISR.cntValOld different from RotaryEncISR.cntVal
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
WAIT_Push_Btn_Release(200);
program_state = S_TOP_MENU_ITEMS;
RotaryEncPop(&RotaryEnc_FreqWord);
RotaryEncPush(&RotaryEnc_MenuSelection);
}
break;
//--------------------------------
case S_TOP_MENU_ITEMS:
// Wrap aroud
menuIndex = RotaryEncISR.cntVal % (sizeof(TopMenuArray) / sizeof(TopMenuArray[0]));
// Update display if there is change
if (RotaryEncISR.cntVal != RotaryEncISR.cntValOld) {
timeout_cnt = 0;
display_mainfield_begin(10);
display.print(TopMenuArray[menuIndex]);
display.display();
}
RotaryEncISR.cntValOld = RotaryEncISR.cntVal;
// Decide into which menu item to go based on selection
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
WAIT_Push_Btn_Release(200);
RotaryEncPop(&RotaryEnc_MenuSelection);
switch (menuIndex) {
// -----------------------------
case 0:
program_state = S_RUN;
RotaryEncPush(&RotaryEnc_FreqWord); // makes RotaryEncISR.cntValOld different from RotaryEncISR.cntVal ==> forces display update
break;
// -----------------------------
case 1:
program_state = S_RUN_CQ;
display_valuefield_begin();
display.print("CQ...");
display.display();
break;
// -----------------------------
case 2:
program_state = S_SET_SPEED_WPM;
RotaryEncPush(&RotaryEnc_KeyerSpeedWPM); // makes RotaryEncISR.cntValOld different from RotaryEncISR.cntVal ==> forces display update
break;
// -----------------------------
case 3:
program_state = S_SET_OUTPUT_POWER;
RotaryEncPush(&RotaryEnc_OutPowerMiliWatt); // makes RotaryEncISR.cntValOld different from RotaryEncISR.cntVal ==> forces display update
break;
// -----------------------------
case 4:
program_state = S_SET_KEYER_TYPE;
RotaryEncPush(&RotaryEnc_KeyerType); // makes RotaryEncISR.cntValOld different from RotaryEncISR.cntVal ==> forces display update
break;
// -----------------------------
case 5:
program_state = S_SET_FREQ_OFFSET;
RotaryEncPush(&RotaryEnc_OffsetHz); // makes RotaryEncISR.cntValOld different from RotaryEncISR.cntVal ==> forces display update
break;
// -----------------------------
case 6:
program_state = S_SET_BUZZER_FREQ;
RotaryEncPush(&RotaryEnc_BuzzerFreq); // makes RotaryEncISR.cntValOld different from RotaryEncISR.cntVal ==> forces display update
break;
// -----------------------------
case 7:
program_state = S_SET_TEXT_GENERIC;
set_text_state = S_SET_MY_CALL;
s_general_ascii_buf = s_mycall_ascii_buf.substring(0);
RotaryEncPush(&RotaryEnc_TextInput_Char_Index);
RotaryEncISR.cntVal = s_general_ascii_buf[0];
general_ascii_buf_index = 0;
break;
// -----------------------------
case 8:
program_state = S_SET_TEXT_GENERIC;
set_text_state = S_SET_WIFI_SSID;
//s_general_ascii_buf = s_wifi_ssid_ascii_buf.substring(0);
s_general_ascii_buf = ssid.substring(0);
RotaryEncPush(&RotaryEnc_TextInput_Char_Index);
RotaryEncISR.cntVal = s_general_ascii_buf[0];
general_ascii_buf_index = 0;
break;
// -----------------------------
case 9:
program_state = S_SET_TEXT_GENERIC;
set_text_state = S_SET_WIFI_PWD;
//s_general_ascii_buf = s_wifi_pwd_ascii_buf.substring(0);
s_general_ascii_buf = password.substring(0);
RotaryEncPush(&RotaryEnc_TextInput_Char_Index);
RotaryEncISR.cntVal = s_general_ascii_buf[0];
general_ascii_buf_index = 0;
break;
// -----------------------------
case 10:
program_state = S_WIFI_RECONNECT;
break;
// -----------------------------
case 11:
program_state = S_RUN_BEACON;
display_valuefield_begin();
display.print("BEACON...");
display.display();
break;
// -----------------------------
case 12:
program_state = S_RUN_BEACON_HELL;
display_valuefield_begin();
display.print("FHELL BEACON");
display.display();
break;
// -----------------------------
default:
program_state = S_RUN;
break;
}
}
break;
//--------------------------------
case S_RUN_CQ:
stop = 0;
for (int j = 0; (j < 3) && !stop; j++) {
// CQ
for (int i = 0; (i < sizeof(cq_message_buf)) && !stop; i++) {
morseEncode(cq_message_buf[i]);
timeout_cnt = 0; // do not allow to timeout this operation
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
stop++;
}
}
// My Call
for (int i = 0; (i < s_mycall_ascii_buf.length()) && !stop; i++) {
morseEncode(s_mycall_ascii_buf[i]);
timeout_cnt = 0; // do not allow to timeout this operation
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
stop++;
}
}
//
morseEncode(' ');
// My Call
for (int i = 0; (i < s_mycall_ascii_buf.length()) && !stop; i++) {
morseEncode(s_mycall_ascii_buf[i]);
timeout_cnt = 0; // do not allow to timeout this operation
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
stop++;
}
}
} // j
// +K
for (int i = 0; (i < sizeof(cq_message_end_buf)) && !stop; i++) {
morseEncode(cq_message_end_buf[i]);
timeout_cnt = 0; // do not allow to timeout this operation
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
stop++;
}
}
//
WAIT_Push_Btn_Release(200);
RotaryEncPush(&RotaryEnc_FreqWord);
program_state = S_RUN;
display.display();
break;
//--------------------------------
case S_SET_SPEED_WPM:
if (RotaryEncISR.cntVal != RotaryEncISR.cntValOld) {
timeout_cnt = 0;
limitRotaryEncISR_values();
display_valuefield_begin();
display.print(RotaryEncISR.cntVal);
display.display();
Calc_WPM_dot_delay(RotaryEncISR.cntVal); // this will set the WPM_dot_delay variable
}
RotaryEncISR.cntValOld = RotaryEncISR.cntVal;
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
WAIT_Push_Btn_Release(200);
speed = RotaryEncISR.cntVal;
sspeed = String(speed);
preferences.putInt("KeyerWPM", RotaryEncISR.cntVal);
RotaryEncPop(&RotaryEnc_KeyerSpeedWPM);
RotaryEncPush(&RotaryEnc_FreqWord);
program_state = S_RUN;
}
break;
//--------------------------------
case S_SET_OUTPUT_POWER:
if (RotaryEncISR.cntVal != RotaryEncISR.cntValOld) {
timeout_cnt = 0;
limitRotaryEncISR_values();
display_valuefield_begin();
//RotaryEncISR.cntVal = RotaryEncISR.cntVal % (sizeof(PowerArrayMiliWatt) / sizeof(uint32_t)); // safe
RotaryEncISR.cntVal = RotaryEncISR.cntVal % PowerArrayMiliWatt_Size;
display.print(PowerArrayMiliWatt[RotaryEncISR.cntVal][0]);
display.print(" mW ");
display.display();
LT.setTxParams(PowerArrayMiliWatt[RotaryEncISR.cntVal][1], RADIO_RAMP_10_US);
}
RotaryEncISR.cntValOld = RotaryEncISR.cntVal;
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
WAIT_Push_Btn_Release(200);
preferences.putInt("OutPower", RotaryEncISR.cntVal);
RotaryEncPop(&RotaryEnc_OutPowerMiliWatt);
RotaryEncPush(&RotaryEnc_FreqWord);
program_state = S_RUN;
}
break;
//--------------------------------
case S_SET_KEYER_TYPE:
if (RotaryEncISR.cntVal != RotaryEncISR.cntValOld) {
timeout_cnt = 0;
display_valuefield_begin();
display.print(RotaryEncISR.cntVal % 2 ? "Straight " : "Iambic ");
display.display();
}
RotaryEncISR.cntValOld = RotaryEncISR.cntVal;
//
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
WAIT_Push_Btn_Release(200);
preferences.putInt("KeyerType", RotaryEncISR.cntVal);
RotaryEncPop(&RotaryEnc_KeyerType);
RotaryEncPush(&RotaryEnc_FreqWord);
program_state = S_RUN;
}
break;
//--------------------------------
case S_SET_FREQ_OFFSET:
if (RotaryEncISR.cntVal != RotaryEncISR.cntValOld) {
timeout_cnt = 0;
limitRotaryEncISR_values();
JUsetRfFrequency(RegWordToFreq(RotaryEncISR.cntVal), RotaryEncISR.cntVal); // Offset
display_valuefield_begin();
display.print(RotaryEncISR.cntVal);
display.display();
}
RotaryEncISR.cntValOld = RotaryEncISR.cntVal;
//
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
WAIT_Push_Btn_Release(200);
preferences.putInt("OffsetHz", RotaryEncISR.cntVal);
RotaryEncPop(&RotaryEnc_OffsetHz);
RotaryEncPush(&RotaryEnc_FreqWord);
program_state = S_RUN;
}
break;
//--------------------------------
case S_SET_BUZZER_FREQ:
if (RotaryEncISR.cntVal != RotaryEncISR.cntValOld) {
timeout_cnt = 0;
display_valuefield_begin();
display.print(RotaryEncISR.cntVal);
display.display();
// Short beep with new tone value
ledcWriteTone(9, RotaryEncISR.cntVal);
delay(100);
ledcWriteTone(9, 0);
}
RotaryEncISR.cntValOld = RotaryEncISR.cntVal;
//
ReadPushBtnVal();
if (pushBtnVal == PUSH_BTN_PRESSED) {
WAIT_Push_Btn_Release(200);
preferences.putInt("BuzzerFreq", RotaryEncISR.cntVal);
RotaryEncPop(&RotaryEnc_BuzzerFreq);
RotaryEncPush(&RotaryEnc_FreqWord);
program_state = S_RUN;
}
break;
//--------------------------------
case S_SET_TEXT_GENERIC:
// Reset timeout_cnt when there is activity with encoder
if (RotaryEncISR.cntVal != RotaryEncISR.cntValOld) {
timeout_cnt = 0;
}
limitRotaryEncISR_values();
display_valuefield_begin();
// Blinking effect on selected character
if (loopCnt % 2) {
s_general_ascii_buf[general_ascii_buf_index] = RotaryEncISR.cntVal;
} else {
s_general_ascii_buf[general_ascii_buf_index] = ' ';
}
display.print(s_general_ascii_buf);
display.display();
// Set it back to valid character after displaying
s_general_ascii_buf[general_ascii_buf_index] = RotaryEncISR.cntVal;
//}
RotaryEncISR.cntValOld = RotaryEncISR.cntVal;
delay(150);
//