forked from afarhan/ubitxv6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubitx_ui.cpp
870 lines (724 loc) · 19.7 KB
/
ubitx_ui.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
#include <Arduino.h>
#include <EEPROM.h>
#include "morse.h"
#include "ubitx.h"
#include "nano_gui.h"
/**
* The user interface of the ubitx consists of the encoder, the push-button on top of it
* and the 16x2 LCD display.
* The upper line of the display is constantly used to display frequency and status
* of the radio. Occasionally, it is used to provide a two-line information that is
* quickly cleared up.
*/
#define BUTTON_SELECTED 1
struct Button {
int x, y, w, h;
char *text;
char *morse;
};
#define MAX_BUTTONS 17
const struct Button btn_set[MAX_BUTTONS] PROGMEM = {
//const struct Button btn_set [] = {
{0, 10, 159, 36, "VFOA", "A"},
{160, 10, 159, 36, "VFOB", "B"},
{0, 80, 60, 36, "RIT", "R"},
{64, 80, 60, 36, "USB", "U"},
{128, 80, 60, 36, "LSB", "L"},
{192, 80, 60, 36, "CW", "M"},
{256, 80, 60, 36, "SPL", "S"},
{0, 120, 60, 36, "80", "8"},
{64, 120, 60, 36, "40", "4"},
{128, 120, 60, 36, "30", "3"},
{192, 120, 60, 36, "20", "2"},
{256, 120, 60, 36, "17", "7"},
{0, 160, 60, 36, "15", "5"},
{64, 160, 60, 36, "10", "1"},
{128, 160, 60, 36, "WPM", "W"},
{192, 160, 60, 36, "TON", "T"},
{256, 160, 60, 36, "FRQ", "F"},
};
#define MAX_KEYS 17
const struct Button keypad[MAX_KEYS] PROGMEM = {
{0, 80, 60, 36, "1", "1"},
{64, 80, 60, 36, "2", "2"},
{128, 80, 60, 36, "3", "3"},
{192, 80, 60, 36, "", ""},
{256, 80, 60, 36, "OK", "K"},
{0, 120, 60, 36, "4", "4"},
{64, 120, 60, 36, "5", "5"},
{128, 120, 60, 36, "6", "6"},
{192, 120, 60, 36, "0", "0"},
{256, 120, 60, 36, "<-", "B"},
{0, 160, 60, 36, "7", "7"},
{64, 160, 60, 36, "8", "8"},
{128, 160, 60, 36, "9", "9"},
{192, 160, 60, 36, "", ""},
{256, 160, 60, 36, "Can", "C"},
};
boolean getButton(char *text, struct Button *b){
for (int i = 0; i < MAX_BUTTONS; i++){
memcpy_P(b, btn_set + i, sizeof(struct Button));
if (!strcmp(text, b->text)){
return true;
}
}
return false;
}
/*
* This formats the frequency given in f
*/
void formatFreq(long f, char *buff) {
// tks Jack Purdum W8TEE
// replaced fsprint commmands by str commands for code size reduction
memset(buff, 0, 10);
memset(b, 0, sizeof(b));
ultoa(f, b, DEC);
//one mhz digit if less than 10 M, two digits if more
if (f < 10000000l){
buff[0] = ' ';
strncat(buff, b, 4);
strcat(buff, ".");
strncat(buff, &b[4], 2);
}
else {
strncat(buff, b, 5);
strcat(buff, ".");
strncat(buff, &b[5], 2);
}
}
void drawCommandbar(char *text){
displayFillrect(30,45,280, 32, DISPLAY_NAVY);
displayRawText(text, 30, 45, DISPLAY_WHITE, DISPLAY_NAVY);
}
/** A generic control to read variable values
*/
int getValueByKnob(int minimum, int maximum, int step_size, int initial, char* prefix, char *postfix)
{
int knob = 0;
int knob_value;
while (btnDown())
active_delay(100);
active_delay(200);
knob_value = initial;
strcpy(b, prefix);
itoa(knob_value, c, 10);
strcat(b, c);
strcat(b, postfix);
drawCommandbar(b);
while(!btnDown() && digitalRead(PTT) == HIGH){
knob = enc_read();
if (knob != 0){
if (knob_value > minimum && knob < 0)
knob_value -= step_size;
if (knob_value < maximum && knob > 0)
knob_value += step_size;
strcpy(b, prefix);
itoa(knob_value, c, 10);
strcat(b, c);
strcat(b, postfix);
drawCommandbar(b);
}
checkCAT();
}
displayFillrect(30,41,280, 32, DISPLAY_NAVY);
return knob_value;
}
void printCarrierFreq(unsigned long freq){
memset(c, 0, sizeof(c));
memset(b, 0, sizeof(b));
ultoa(freq, b, DEC);
strncat(c, b, 2);
strcat(c, ".");
strncat(c, &b[2], 3);
strcat(c, ".");
strncat(c, &b[5], 1);
displayText(c, 110, 100, 100, 30, DISPLAY_CYAN, DISPLAY_NAVY, DISPLAY_NAVY);
}
void displayDialog(char *title, char *instructions){
displayClear(DISPLAY_BLACK);
displayRect(10,10,300,220, DISPLAY_WHITE);
displayHline(20,45,280,DISPLAY_WHITE);
displayRect(12,12,296,216, DISPLAY_WHITE);
displayRawText(title, 20, 20, DISPLAY_CYAN, DISPLAY_NAVY);
displayRawText(instructions, 20, 200, DISPLAY_CYAN, DISPLAY_NAVY);
}
char vfoDisplay[12];
void displayVFO(int vfo){
int x, y;
int displayColor, displayBorder;
Button b;
if (vfo == VFO_A){
getButton("VFOA", &b);
if (splitOn){
if (vfoActive == VFO_A)
strcpy(c, "R:");
else
strcpy(c, "T:");
}
else
strcpy(c, "A:");
if (vfoActive == VFO_A){
formatFreq(frequency, c+2);
displayColor = DISPLAY_WHITE;
displayBorder = DISPLAY_BLACK;
}else{
formatFreq(vfoA, c+2);
displayColor = DISPLAY_GREEN;
displayBorder = DISPLAY_BLACK;
}
}
if (vfo == VFO_B){
getButton("VFOB", &b);
if (splitOn){
if (vfoActive == VFO_B)
strcpy(c, "R:");
else
strcpy(c, "T:");
}
else
strcpy(c, "B:");
if (vfoActive == VFO_B){
formatFreq(frequency, c+2);
displayColor = DISPLAY_WHITE;
displayBorder = DISPLAY_WHITE;
} else {
displayColor = DISPLAY_GREEN;
displayBorder = DISPLAY_BLACK;
formatFreq(vfoB, c+2);
}
}
if (vfoDisplay[0] == 0){
displayFillrect(b.x, b.y, b.w, b.h, DISPLAY_BLACK);
if (vfoActive == vfo)
displayRect(b.x, b.y, b.w , b.h, DISPLAY_WHITE);
else
displayRect(b.x, b.y, b.w , b.h, DISPLAY_NAVY);
}
x = b.x + 6;
y = b.y + 3;
char *text = c;
for (int i = 0; i <= strlen(c); i++){
char digit = c[i];
if (digit != vfoDisplay[i]){
displayFillrect(x, y, 15, b.h-6, DISPLAY_BLACK);
//checkCAT();
displayChar(x, y + TEXT_LINE_HEIGHT + 3, digit, displayColor, DISPLAY_BLACK);
checkCAT();
}
if (digit == ':' || digit == '.')
x += 7;
else
x += 16;
text++;
}//end of the while loop of the characters to be printed
strcpy(vfoDisplay, c);
}
void btnDraw(struct Button *b){
if (!strcmp(b->text, "VFOA")){
memset(vfoDisplay, 0, sizeof(vfoDisplay));
displayVFO(VFO_A);
}
else if(!strcmp(b->text, "VFOB")){
memset(vfoDisplay, 0, sizeof(vfoDisplay));
displayVFO(VFO_B);
}
else if ((!strcmp(b->text, "RIT") && ritOn == 1) ||
(!strcmp(b->text, "USB") && isUSB == 1) ||
(!strcmp(b->text, "LSB") && isUSB == 0) ||
(!strcmp(b->text, "SPL") && splitOn == 1))
displayText(b->text, b->x, b->y, b->w, b->h, DISPLAY_BLACK, DISPLAY_ORANGE, DISPLAY_DARKGREY);
else if (!strcmp(b->text, "CW") && cwMode == 1)
displayText(b->text, b->x, b->y, b->w, b->h, DISPLAY_BLACK, DISPLAY_ORANGE, DISPLAY_DARKGREY);
else
displayText(b->text, b->x, b->y, b->w, b->h, DISPLAY_GREEN, DISPLAY_BLACK, DISPLAY_DARKGREY);
}
void displayRIT(){
displayFillrect(0,41,320,30, DISPLAY_NAVY);
if (ritOn){
strcpy(c, "TX:");
formatFreq(ritTxFrequency, c+3);
if (vfoActive == VFO_A)
displayText(c, 0, 45,159, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY);
else
displayText(c, 160, 45,159, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY);
}
else {
if (vfoActive == VFO_A)
displayText("", 0, 45,159, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY);
else
displayText("", 160, 45,159, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY);
}
}
void fastTune(){
int encoder;
//if the btn is down, wait until it is up
while(btnDown())
active_delay(50);
active_delay(300);
displayRawText("Fast tune", 100, 55, DISPLAY_CYAN, DISPLAY_NAVY);
while(1){
checkCAT();
//exit after debouncing the btnDown
if (btnDown()){
displayFillrect(100, 55, 120, 30, DISPLAY_NAVY);
//wait until the button is realsed and then return
while(btnDown())
active_delay(50);
active_delay(300);
return;
}
encoder = enc_read();
if (encoder != 0){
if (encoder > 0 && frequency < 30000000l)
frequency += 50000l;
else if (encoder < 0 && frequency > 600000l)
frequency -= 50000l;
setFrequency(frequency);
displayVFO(vfoActive);
}
}// end of the event loop
}
void enterFreq(){
//force the display to refresh everything
//display all the buttons
int f;
for (int i = 0; i < MAX_KEYS; i++){
struct Button b;
memcpy_P(&b, keypad + i, sizeof(struct Button));
btnDraw(&b);
}
int cursor_pos = 0;
memset(c, 0, sizeof(c));
f = frequency / 1000l;
while(1){
checkCAT();
if(!readTouch())
continue;
scaleTouch(&ts_point);
int total = sizeof(btn_set)/sizeof(struct Button);
for (int i = 0; i < MAX_KEYS; i++){
struct Button b;
memcpy_P(&b, keypad + i, sizeof(struct Button));
int x2 = b.x + b.w;
int y2 = b.y + b.h;
if (b.x < ts_point.x && ts_point.x < x2 &&
b.y < ts_point.y && ts_point.y < y2){
if (!strcmp(b.text, "OK")){
long f = atol(c);
if(30000 >= f && f > 100){
frequency = f * 1000l;
setFrequency(frequency);
if (vfoActive == VFO_A)
vfoA = frequency;
else
vfoB = frequency;
saveVFOs();
}
guiUpdate();
return;
}
else if (!strcmp(b.text, "<-")){
c[cursor_pos] = 0;
if (cursor_pos > 0)
cursor_pos--;
c[cursor_pos] = 0;
}
else if (!strcmp(b.text, "Can")){
guiUpdate();
return;
}
else if('0' <= b.text[0] && b.text[0] <= '9'){
c[cursor_pos++] = b.text[0];
c[cursor_pos] = 0;
}
}
} // end of the button scanning loop
strcpy(b, c);
strcat(b, " KHz");
displayText(b, 0, 42, 320, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY);
delay(300);
while(readTouch())
checkCAT();
} // end of event loop : while(1)
}
void drawCWStatus(){
displayFillrect(0, 201, 320, 39, DISPLAY_NAVY);
strcpy(b, " cw:");
int wpm = 1200/cwSpeed;
itoa(wpm,c, 10);
strcat(b, c);
strcat(b, "wpm, ");
itoa(sideTone, c, 10);
strcat(b, c);
strcat(b, "hz");
displayRawText(b, 0, 210, DISPLAY_CYAN, DISPLAY_NAVY);
}
void drawTx(){
if (inTx)
displayText("TX", 280, 48, 37, 28, DISPLAY_BLACK, DISPLAY_ORANGE, DISPLAY_BLUE);
else
displayFillrect(280, 48, 37, 28, DISPLAY_NAVY);
}
void drawStatusbar(){
drawCWStatus();
}
void guiUpdate(){
/*
if (doingCAT)
return;
*/
// use the current frequency as the VFO frequency for the active VFO
displayClear(DISPLAY_NAVY);
memset(vfoDisplay, 0, 12);
displayVFO(VFO_A);
checkCAT();
memset(vfoDisplay, 0, 12);
displayVFO(VFO_B);
checkCAT();
displayRIT();
checkCAT();
//force the display to refresh everything
//display all the buttons
for (int i = 0; i < MAX_BUTTONS; i++){
struct Button b;
memcpy_P(&b, btn_set + i, sizeof(struct Button));
btnDraw(&b);
checkCAT();
}
drawStatusbar();
checkCAT();
}
// this builds up the top line of the display with frequency and mode
void updateDisplay() {
displayVFO(vfoActive);
}
int enc_prev_state = 3;
/**
* The A7 And A6 are purely analog lines on the Arduino Nano
* These need to be pulled up externally using two 10 K resistors
*
* There are excellent pages on the Internet about how these encoders work
* and how they should be used. We have elected to use the simplest way
* to use these encoders without the complexity of interrupts etc to
* keep it understandable.
*
* The enc_state returns a two-bit number such that each bit reflects the current
* value of each of the two phases of the encoder
*
* The enc_read returns the number of net pulses counted over 50 msecs.
* If the puluses are -ve, they were anti-clockwise, if they are +ve, the
* were in the clockwise directions. Higher the pulses, greater the speed
* at which the enccoder was spun
*/
/*
byte enc_state (void) {
//Serial.print(digitalRead(ENC_A)); Serial.print(":");Serial.println(digitalRead(ENC_B));
return (digitalRead(ENC_A) == 1 ? 1 : 0) + (digitalRead(ENC_B) == 1 ? 2: 0);
}
int enc_read(void) {
int result = 0;
byte newState;
int enc_speed = 0;
long stop_by = millis() + 200;
while (millis() < stop_by) { // check if the previous state was stable
newState = enc_state(); // Get current state
// if (newState != enc_prev_state)
// active_delay(20);
if (enc_state() != newState || newState == enc_prev_state)
continue;
//these transitions point to the encoder being rotated anti-clockwise
if ((enc_prev_state == 0 && newState == 2) ||
(enc_prev_state == 2 && newState == 3) ||
(enc_prev_state == 3 && newState == 1) ||
(enc_prev_state == 1 && newState == 0)){
result--;
}
//these transitions point o the enccoder being rotated clockwise
if ((enc_prev_state == 0 && newState == 1) ||
(enc_prev_state == 1 && newState == 3) ||
(enc_prev_state == 3 && newState == 2) ||
(enc_prev_state == 2 && newState == 0)){
result++;
}
enc_prev_state = newState; // Record state for next pulse interpretation
enc_speed++;
active_delay(1);
}
//if (result)
// Serial.println(result);
return(result);
}
*/
void ritToggle(struct Button *b){
if (ritOn == 0){
ritEnable(frequency);
}
else
ritDisable();
btnDraw(b);
displayRIT();
}
void splitToggle(struct Button *b){
if (splitOn)
splitOn = 0;
else
splitOn = 1;
btnDraw(b);
//disable rit as well
ritDisable();
struct Button b2;
getButton("RIT", &b2);
btnDraw(&b2);
displayRIT();
memset(vfoDisplay, 0, sizeof(vfoDisplay));
displayVFO(VFO_A);
memset(vfoDisplay, 0, sizeof(vfoDisplay));
displayVFO(VFO_B);
}
void vfoReset(){
Button b;
if (vfoActive = VFO_A)
vfoB = vfoA;
else
vfoA = vfoB;
if (splitOn){
getButton("SPL", &b);
splitToggle(&b);
}
if (ritOn){
getButton("RIT", &b);
ritToggle(&b);
}
memset(vfoDisplay, 0, sizeof(vfoDisplay));
displayVFO(VFO_A);
memset(vfoDisplay, 0, sizeof(vfoDisplay));
displayVFO(VFO_B);
saveVFOs();
}
void cwToggle(struct Button *b){
if (cwMode == 0){
cwMode = 1;
}
else
cwMode = 0;
setFrequency(frequency);
btnDraw(b);
}
void sidebandToggle(struct Button *b){
if (!strcmp(b->text, "LSB"))
isUSB = 0;
else
isUSB = 1;
struct Button e;
getButton("USB", &e);
btnDraw(&e);
getButton("LSB", &e);
btnDraw(&e);
saveVFOs();
}
void redrawVFOs(){
struct Button b;
ritDisable();
getButton("RIT", &b);
btnDraw(&b);
displayRIT();
memset(vfoDisplay, 0, sizeof(vfoDisplay));
displayVFO(VFO_A);
memset(vfoDisplay, 0, sizeof(vfoDisplay));
displayVFO(VFO_B);
//draw the lsb/usb buttons, the sidebands might have changed
getButton("LSB", &b);
btnDraw(&b);
getButton("USB", &b);
btnDraw(&b);
}
void switchBand(long bandfreq){
long offset;
// Serial.println(frequency);
// Serial.println(bandfreq);
if (3500000l <= frequency && frequency <= 4000000l)
offset = frequency - 3500000l;
else if (24800000l <= frequency && frequency <= 25000000l)
offset = frequency - 24800000l;
else
offset = frequency % 1000000l;
// Serial.println(offset);
setFrequency(bandfreq + offset);
updateDisplay();
saveVFOs();
}
int setCwSpeed(){
int knob = 0;
int wpm;
wpm = 1200/cwSpeed;
wpm = getValueByKnob(1, 100, 1, wpm, "CW: ", " WPM");
cwSpeed = 1200/wpm;
EEPROM.put(CW_SPEED, cwSpeed);
active_delay(500);
drawStatusbar();
// printLine2("");
// updateDisplay();
}
void setCwTone(){
int knob = 0;
int prev_sideTone;
tone(CW_TONE, sideTone);
//disable all clock 1 and clock 2
while (digitalRead(PTT) == HIGH && !btnDown())
{
knob = enc_read();
if (knob > 0 && sideTone < 2000)
sideTone += 10;
else if (knob < 0 && sideTone > 100 )
sideTone -= 10;
else
continue; //don't update the frequency or the display
tone(CW_TONE, sideTone);
itoa(sideTone, c, 10);
strcpy(b, "CW Tone: ");
strcat(b, c);
strcat(b, " Hz");
drawCommandbar(b);
//printLine2(b);
checkCAT();
active_delay(20);
}
noTone(CW_TONE);
//save the setting
EEPROM.put(CW_SIDETONE, sideTone);
displayFillrect(30,41,280, 32, DISPLAY_NAVY);
drawStatusbar();
// printLine2("");
// updateDisplay();
}
void doCommand(struct Button *b){
if (!strcmp(b->text, "RIT"))
ritToggle(b);
else if (!strcmp(b->text, "LSB"))
sidebandToggle(b);
else if (!strcmp(b->text, "USB"))
sidebandToggle(b);
else if (!strcmp(b->text, "CW"))
cwToggle(b);
else if (!strcmp(b->text, "SPL"))
splitToggle(b);
else if (!strcmp(b->text, "VFOA")){
if (vfoActive == VFO_A)
fastTune();
else
switchVFO(VFO_A);
}
else if (!strcmp(b->text, "VFOB")){
if (vfoActive == VFO_B)
fastTune();
else
switchVFO(VFO_B);
}
else if (!strcmp(b->text, "A=B"))
vfoReset();
else if (!strcmp(b->text, "80"))
switchBand(3500000l);
else if (!strcmp(b->text, "40"))
switchBand(7000000l);
else if (!strcmp(b->text, "30"))
switchBand(10000000l);
else if (!strcmp(b->text, "20"))
switchBand(14000000l);
else if (!strcmp(b->text, "17"))
switchBand(18000000l);
else if (!strcmp(b->text, "15"))
switchBand(21000000l);
else if (!strcmp(b->text, "13"))
switchBand(24800000l);
else if (!strcmp(b->text, "10"))
switchBand(28000000l);
else if (!strcmp(b->text, "FRQ"))
enterFreq();
else if (!strcmp(b->text, "WPM"))
setCwSpeed();
else if (!strcmp(b->text, "TON"))
setCwTone();
}
void checkTouch(){
if (!readTouch())
return;
while(readTouch())
checkCAT();
scaleTouch(&ts_point);
/* //debug code
Serial.print(ts_point.x); Serial.print(' ');Serial.println(ts_point.y);
*/
int total = sizeof(btn_set)/sizeof(struct Button);
for (int i = 0; i < MAX_BUTTONS; i++){
struct Button b;
memcpy_P(&b, btn_set + i, sizeof(struct Button));
int x2 = b.x + b.w;
int y2 = b.y + b.h;
if (b.x < ts_point.x && ts_point.x < x2 &&
b.y < ts_point.y && ts_point.y < y2)
doCommand(&b);
}
}
//returns true if the button is pressed
int btnDown(){
if (digitalRead(FBUTTON) == HIGH)
return 0;
else
return 1;
}
void drawFocus(int ibtn, int color){
struct Button b;
memcpy_P(&b, btn_set + ibtn, sizeof(struct Button));
displayRect(b.x, b.y, b.w, b.h, color);
}
void doCommands(){
int select=0, i, prevButton, btnState;
//wait for the button to be raised up
while(btnDown())
active_delay(50);
active_delay(50); //debounce
menuOn = 2;
while (menuOn){
//check if the knob's button was pressed
btnState = btnDown();
if (btnState){
struct Button b;
memcpy_P(&b, btn_set + select/10, sizeof(struct Button));
doCommand(&b);
//unfocus the buttons
drawFocus(select, DISPLAY_BLUE);
if (vfoActive == VFO_A)
drawFocus(0, DISPLAY_WHITE);
else
drawFocus(1, DISPLAY_WHITE);
//wait for the button to be up and debounce
while(btnDown())
active_delay(100);
active_delay(500);
return;
}
i = enc_read();
if (i == 0){
active_delay(50);
continue;
}
if (i > 0){
if (select + i < MAX_BUTTONS * 10)
select += i;
}
if (i < 0 && select + i >= 0)
select += i; //caught ya, i is already -ve here, so you add it
if (prevButton == select / 10)
continue;
//we are on a new button
drawFocus(prevButton, DISPLAY_BLUE);
drawFocus(select/10, DISPLAY_WHITE);
prevButton = select/10;
}
// guiUpdate();
//debounce the button
while(btnDown())
active_delay(50);
active_delay(50);
checkCAT();
}