forked from g0orx/pihpsdr
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgpio.c
1441 lines (1244 loc) · 46.7 KB
/
gpio.c
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
/* Copyright (C)
* 2020 - John Melton, G0ORX/N6LYT
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
// Rewrite to use gpiod rather than wiringPi
// Note that all pin numbers are now the Broadcom GPIO
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <poll.h>
#include <sched.h>
#ifdef GPIO
#include <gpiod.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
#include <sys/ioctl.h>
#endif
#include "actions.h"
#include "band.h"
#include "bandstack.h"
#include "channel.h"
#include "discovered.h"
#include "diversity_menu.h"
#include "encoder_menu.h"
#include "ext.h"
#include "filter.h"
#include "gpio.h"
#include "i2c.h"
#include "iambic.h"
#include "main.h"
#include "message.h"
#include "mode.h"
#include "new_menu.h"
#include "new_protocol.h"
#include "property.h"
#include "radio.h"
#include "sliders.h"
#include "toolbar.h"
#include "vfo.h"
#include "zoompan.h"
//
// for controllers which have spare GPIO lines,
// these lines can be associated to certain
// functions, namely
//
// CWL: input: left paddle for internal (iambic) keyer
// CWR: input: right paddle for internal (iambic) keyer
// CWKEY: input: key-down from external keyer
// PTTIN: input: PTT from external keyer or microphone
// PTTOUT: output: PTT output (indicating TX status)
//
// a value < 0 indicates "do not use". All inputs are active-low,
// but PTTOUT is active-high
//
// Avoid using GPIO lines 18, 19, 20, 21 since they are used for I2S
// by some GPIO-connected audio output "hats"
//
//
static int CWL_LINE = -1;
static int CWR_LINE = -1;
static int CWKEY_LINE = -1;
static int PTTIN_LINE = -1;
static int PTTOUT_LINE = -1;
static int CWOUT_LINE = -1;
#ifdef GPIO
static struct gpiod_line *pttout_line = NULL;
static struct gpiod_line *cwout_line = NULL;
#endif
void gpio_set_ptt(int state) {
#ifdef GPIO
if (pttout_line) {
//t_print("%s: state=%d\n", __FUNCTION__, state);
if (gpiod_line_set_value(pttout_line, NOT(state)) < 0) {
t_print("%s failed: %s\n", __FUNCTION__, g_strerror(errno));
}
}
#endif
}
void gpio_set_cw(int state) {
#ifdef GPIO
if (cwout_line) {
//t_print("%s: state=%d\n", __FUNCTION__, state);
if (gpiod_line_set_value(cwout_line, NOT(state)) < 0) {
t_print("%s failed: %s\n", __FUNCTION__, g_strerror(errno));
}
}
#endif
}
enum {
TOP_ENCODER,
BOTTOM_ENCODER
};
enum {
A,
B
};
#define DIR_NONE 0x0
// Clockwise step.
#define DIR_CW 0x10
// Anti-clockwise step.
#define DIR_CCW 0x20
//
// Encoder states for a "full cycle"
//
#define R_START 0x00
#define R_CW_FINAL 0x01
#define R_CW_BEGIN 0x02
#define R_CW_NEXT 0x03
#define R_CCW_BEGIN 0x04
#define R_CCW_FINAL 0x05
#define R_CCW_NEXT 0x06
//
// Encoder states for a "half cycle"
//
#define R_START1 0x07
#define R_START0 0x08
#define R_CW_BEG1 0x09
#define R_CW_BEG0 0x0A
#define R_CCW_BEG1 0x0B
#define R_CCW_BEG0 0x0C
//
// Few general remarks on the state machine:
// - if the levels do not change, the machinestate does not change
// - if there is bouncing on one input line, the machine oscillates
// between two "adjacent" states but generates at most one tick
// - if both input lines change level, move to a suitable new
// starting point but do not generate a tick
// - if one or both of the AB lines are inverted, the same cycles
// are passed but with a different starting point. Therefore,
// it still works.
//
guchar encoder_state_table[13][4] = {
//
// A "full cycle" has the following state changes
// (first line: line levels AB, 1=pressed, 0=released,
// 2nd line: state names
//
// clockwise: 11 --> 10 --> 00 --> 01 --> 11
// Start --> CWbeg --> CWnext --> CWfinal --> Start
//
// ccw: 11 --> 01 --> 00 --> 10 --> 11
// Start --> CCWbeg --> CCWnext --> CCWfinal --> Start
//
// Emit the "tick" when moving from "final" to "start".
//
// 00 10 01 11
// -----------------------------------------------------------------------------
/* R_START */ {R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START},
/* R_CW_FINAL */ {R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW},
/* R_CW_BEGIN */ {R_CW_NEXT, R_CW_BEGIN, R_START, R_START},
/* R_CW_NEXT */ {R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START},
/* R_CCW_BEGIN */ {R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START},
/* R_CCW_FINAL */ {R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},
/* R_CCW_NEXT */ {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
//
// The same sequence can be interpreted as two "half cycles"
//
// clockwise1: 11 --> 10 --> 00
// Start1 --> CWbeg1 --> Start0
//
// clockwise2: 00 --> 01 --> 11
// Start0 --> CWbeg0 --> Start1
//
// ccw1: 11 --> 01 --> 00
// Start1 --> CCWbeg1 --> Start0
//
// ccw2: 00 --> 10 --> 11
// Start0 --> CCWbeg0 --> Start1
//
// If both lines change, this is interpreted as a two-step move
// without changing the orientation and without emitting a "tick".
//
// Emit the "tick" each time when moving from "beg" to "start".
//
// 00 10 01 11
// -----------------------------------------------------------------------------
/* R_START1 */ {R_START0, R_CW_BEG1, R_CCW_BEG1, R_START1},
/* R_START0 */ {R_START0, R_CCW_BEG0, R_CW_BEG0, R_START1},
/* R_CW_BEG1 */ {R_START0 | DIR_CW, R_CW_BEG1, R_CW_BEG0, R_START1},
/* R_CW_BEG0 */ {R_START0, R_CW_BEG1, R_CW_BEG0, R_START1 | DIR_CW},
/* R_CCW_BEG1 */ {R_START0 | DIR_CCW, R_CCW_BEG0, R_CCW_BEG1, R_START1},
/* R_CCW_BEG0 */ {R_START0, R_CCW_BEG0, R_CCW_BEG1, R_START1 | DIR_CCW},
};
#ifdef GPIO
char *consumer = "pihpsdr";
//
// gpio_init() tries several chips, until success.
// gpio_device is then set to the first device sucessfully opened.
//
char *gpio_device = NULL;
static struct gpiod_chip *chip = NULL;
static GMutex encoder_mutex;
static GThread *monitor_thread_id;
#endif
int I2C_INTERRUPT = 15;
#define MAX_LINES 32
unsigned int monitor_lines[MAX_LINES];
int lines = 0;
long settle_time = 50; // ms
//
// The "static const" data is the DEFAULT assignment for encoders,
// and for Controller2 and G2 front panel switches
// These defaults are read-only and copied to my_encoders and my_switches
// when restoring default values
//
// Controller1 has 3 small encoders + VFO, and 8 switches in 6 layers
// Controller2 has 4 small encoders + VFO, and 16 switches
// G2 panel has 4 small encoders + VFO, and 16 switches
//
// The controller1 switches are hard-wired to the toolbar buttons
//
//
// RPI5: GPIO line 20 not available, replace "20" by "14" at four places in the following lines
// and re-wire the controller connection from GPIO20 to GPIO14
//
static const ENCODER encoders_no_controller[MAX_ENCODERS] = {
{FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0L},
{FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0L},
{FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0L},
{FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0L},
{FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, 0, 0L},
};
static const ENCODER encoders_controller1[MAX_ENCODERS] = {
{TRUE, TRUE, 20, 1, 26, 1, 0, AF_GAIN, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, TRUE, TRUE, 25, MENU_BAND, 0L},
{TRUE, TRUE, 16, 1, 19, 1, 0, AGC_GAIN, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, TRUE, TRUE, 8, MENU_BANDSTACK, 0L},
{TRUE, TRUE, 4, 1, 21, 1, 0, DRIVE, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, TRUE, TRUE, 7, MENU_MODE, 0L},
{TRUE, TRUE, 18, 1, 17, 1, 0, VFO, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, NO_ACTION, 0L},
{FALSE, TRUE, 0, 1, 0, 0, 1, NO_ACTION, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, NO_ACTION, 0L},
};
static const ENCODER encoders_controller2_v1[MAX_ENCODERS] = {
{TRUE, TRUE, 20, 1, 26, 1, 0, AF_GAIN, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, TRUE, TRUE, 22, MENU_BAND, 0L},
{TRUE, TRUE, 4, 1, 21, 1, 0, AGC_GAIN, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, TRUE, TRUE, 27, MENU_BANDSTACK, 0L},
{TRUE, TRUE, 16, 1, 19, 1, 0, IF_WIDTH, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, TRUE, TRUE, 23, MENU_MODE, 0L},
{TRUE, TRUE, 25, 1, 8, 1, 0, RIT, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, TRUE, TRUE, 24, MENU_FREQUENCY, 0L},
{TRUE, TRUE, 18, 1, 17, 1, 0, VFO, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, NO_ACTION, 0L},
};
static const ENCODER encoders_controller2_v2[MAX_ENCODERS] = {
{TRUE, TRUE, 5, 1, 6, 1, 0, AGC_GAIN_RX1, R_START1, TRUE, TRUE, 26, 1, 20, 1, 0, AF_GAIN_RX1, R_START1, TRUE, TRUE, 22, RX1, 0L}, //ENC2
{TRUE, TRUE, 9, 1, 7, 1, 0, AGC_GAIN_RX2, R_START1, TRUE, TRUE, 21, 1, 4, 1, 0, AF_GAIN_RX2, R_START1, TRUE, TRUE, 27, RX2, 0L}, //ENC3
{TRUE, TRUE, 11, 1, 10, 1, 0, DIV_GAIN, R_START1, TRUE, TRUE, 19, 1, 16, 1, 0, DIV_PHASE, R_START1, TRUE, TRUE, 23, DIV, 0L}, //ENC4
{TRUE, TRUE, 13, 1, 12, 1, 0, XIT, R_START1, TRUE, TRUE, 8, 1, 25, 1, 0, RIT, R_START1, TRUE, TRUE, 24, MENU_FREQUENCY, 0L}, //ENC5
{TRUE, TRUE, 18, 1, 17, 1, 0, VFO, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, NO_ACTION, R_START, FALSE, TRUE, 0, NO_ACTION, 0L}, //ENC1/VFO
};
static const ENCODER encoders_g2_frontpanel[MAX_ENCODERS] = {
{TRUE, TRUE, 5, 1, 6, 1, 0, DRIVE, R_START1, TRUE, TRUE, 26, 1, 20, 1, 0, MIC_GAIN, R_START1, TRUE, TRUE, 22, PS, 0L}, //ENC1
{TRUE, TRUE, 9, 1, 7, 1, 0, AGC_GAIN, R_START1, TRUE, TRUE, 21, 1, 4, 1, 0, AF_GAIN, R_START1, TRUE, TRUE, 27, MUTE, 0L}, //ENC3
{TRUE, TRUE, 11, 1, 10, 1, 0, DIV_GAIN, R_START1, TRUE, TRUE, 19, 1, 16, 1, 0, DIV_PHASE, R_START1, TRUE, TRUE, 23, DIV, 0L}, //ENC7
{TRUE, TRUE, 13, 1, 12, 1, 0, XIT, R_START1, TRUE, TRUE, 8, 1, 25, 1, 0, RIT, R_START1, TRUE, TRUE, 24, MENU_FREQUENCY, 0L}, //ENC5
{TRUE, TRUE, 18, 1, 17, 1, 0, VFO, R_START, FALSE, TRUE, 0, 0, 0, 0, 0, 0, R_START, FALSE, TRUE, 0, NO_ACTION, 0L}, //VFO
};
static const SWITCH switches_no_controller[MAX_SWITCHES] = {
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L}
};
SWITCH switches_controller1[MAX_FUNCTIONS][MAX_SWITCHES] = {
{ {TRUE, TRUE, 27, MOX, 0L},
{TRUE, TRUE, 13, MENU_BAND, 0L},
{TRUE, TRUE, 12, MENU_BANDSTACK, 0L},
{TRUE, TRUE, 6, MENU_MODE, 0L},
{TRUE, TRUE, 5, MENU_FILTER, 0L},
{TRUE, TRUE, 24, MENU_NOISE, 0L},
{TRUE, TRUE, 23, MENU_AGC, 0L},
{TRUE, TRUE, 22, FUNCTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L}
},
{ {TRUE, TRUE, 27, MOX, 0L},
{TRUE, TRUE, 13, LOCK, 0L},
{TRUE, TRUE, 12, CTUN, 0L},
{TRUE, TRUE, 6, A_TO_B, 0L},
{TRUE, TRUE, 5, B_TO_A, 0L},
{TRUE, TRUE, 24, A_SWAP_B, 0L},
{TRUE, TRUE, 23, SPLIT, 0L},
{TRUE, TRUE, 22, FUNCTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L}
},
{ {TRUE, TRUE, 27, MOX, 0L},
{TRUE, TRUE, 13, MENU_FREQUENCY, 0L},
{TRUE, TRUE, 12, MENU_MEMORY, 0L},
{TRUE, TRUE, 6, RIT_ENABLE, 0L},
{TRUE, TRUE, 5, RIT_PLUS, 0L},
{TRUE, TRUE, 24, RIT_MINUS, 0L},
{TRUE, TRUE, 23, RIT_CLEAR, 0L},
{TRUE, TRUE, 22, FUNCTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L}
},
{ {TRUE, TRUE, 27, MOX, 0L},
{TRUE, TRUE, 13, MENU_FREQUENCY, 0L},
{TRUE, TRUE, 12, MENU_MEMORY, 0L},
{TRUE, TRUE, 6, XIT_ENABLE, 0L},
{TRUE, TRUE, 5, XIT_PLUS, 0L},
{TRUE, TRUE, 24, XIT_MINUS, 0L},
{TRUE, TRUE, 23, XIT_CLEAR, 0L},
{TRUE, TRUE, 22, FUNCTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L}
},
{ {TRUE, TRUE, 27, MOX, 0L},
{TRUE, TRUE, 13, MENU_FREQUENCY, 0L},
{TRUE, TRUE, 12, SPLIT, 0L},
{TRUE, TRUE, 6, DUPLEX, 0L},
{TRUE, TRUE, 5, SAT, 0L},
{TRUE, TRUE, 24, RSAT, 0L},
{TRUE, TRUE, 23, MENU_BAND, 0L},
{TRUE, TRUE, 22, FUNCTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L}
},
{ {TRUE, TRUE, 27, MOX, 0L},
{TRUE, TRUE, 13, TUNE, 0L},
{TRUE, TRUE, 12, TUNE_FULL, 0L},
{TRUE, TRUE, 6, TUNE_MEMORY, 0L},
{TRUE, TRUE, 5, MENU_BAND, 0L},
{TRUE, TRUE, 24, MENU_MODE, 0L},
{TRUE, TRUE, 23, MENU_FILTER, 0L},
{TRUE, TRUE, 22, FUNCTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L},
{FALSE, FALSE, 0, NO_ACTION, 0L}
},
};
static const SWITCH switches_controller2_v1[MAX_SWITCHES] = {
{FALSE, FALSE, 0, MOX, 0L},
{FALSE, FALSE, 0, TUNE, 0L},
{FALSE, FALSE, 0, PS, 0L},
{FALSE, FALSE, 0, TWO_TONE, 0L},
{FALSE, FALSE, 0, NR, 0L},
{FALSE, FALSE, 0, A_TO_B, 0L},
{FALSE, FALSE, 0, B_TO_A, 0L},
{FALSE, FALSE, 0, MODE_MINUS, 0L},
{FALSE, FALSE, 0, BAND_MINUS, 0L},
{FALSE, FALSE, 0, MODE_PLUS, 0L},
{FALSE, FALSE, 0, BAND_PLUS, 0L},
{FALSE, FALSE, 0, XIT_ENABLE, 0L},
{FALSE, FALSE, 0, NB, 0L},
{FALSE, FALSE, 0, SNB, 0L},
{FALSE, FALSE, 0, LOCK, 0L},
{FALSE, FALSE, 0, CTUN, 0L}
};
static const SWITCH switches_controller2_v2[MAX_SWITCHES] = {
{FALSE, FALSE, 0, MOX, 0L}, //GPB7 SW2
{FALSE, FALSE, 0, TUNE, 0L}, //GPB6 SW3
{FALSE, FALSE, 0, PS, 0L}, //GPB5 SW4
{FALSE, FALSE, 0, TWO_TONE, 0L}, //GPB4 SW5
{FALSE, FALSE, 0, NR, 0L}, //GPA3 SW6
{FALSE, FALSE, 0, NB, 0L}, //GPB3 SW14
{FALSE, FALSE, 0, SNB, 0L}, //GPB2 SW15
{FALSE, FALSE, 0, XIT_ENABLE, 0L}, //GPA7 SW13
{FALSE, FALSE, 0, BAND_PLUS, 0L}, //GPA6 SW12
{FALSE, FALSE, 0, MODE_PLUS, 0L}, //GPA5 SW11
{FALSE, FALSE, 0, BAND_MINUS, 0L}, //GPA4 SW10
{FALSE, FALSE, 0, MODE_MINUS, 0L}, //GPA0 SW9
{FALSE, FALSE, 0, A_TO_B, 0L}, //GPA2 SW7
{FALSE, FALSE, 0, B_TO_A, 0L}, //GPA1 SW8
{FALSE, FALSE, 0, LOCK, 0L}, //GPB1 SW16
{FALSE, FALSE, 0, CTUN, 0L} //GPB0 SW17
};
static const SWITCH switches_g2_frontpanel[MAX_SWITCHES] = {
{FALSE, FALSE, 0, XIT_ENABLE, 0L}, //GPB7 SW22
{FALSE, FALSE, 0, RIT_ENABLE, 0L}, //GPB6 SW21
{FALSE, FALSE, 0, FUNCTION, 0L}, //GPB5 SW20
{FALSE, FALSE, 0, SPLIT, 0L}, //GPB4 SW19
{FALSE, FALSE, 0, LOCK, 0L}, //GPA3 SW9
{FALSE, FALSE, 0, B_TO_A, 0L}, //GPB3 SW18
{FALSE, FALSE, 0, A_TO_B, 0L}, //GPB2 SW17
{FALSE, FALSE, 0, MODE_MINUS, 0L}, //GPA7 SW13
{FALSE, FALSE, 0, BAND_PLUS, 0L}, //GPA6 SW12
{FALSE, FALSE, 0, FILTER_PLUS, 0L}, //GPA5 SW11
{FALSE, FALSE, 0, MODE_PLUS, 0L}, //GPA4 SW10
{FALSE, FALSE, 0, MOX, 0L}, //GPA0 SW6
{FALSE, FALSE, 0, CTUN, 0L}, //GPA2 SW8
{FALSE, FALSE, 0, TUNE, 0L}, //GPA1 SW7
{FALSE, FALSE, 0, BAND_MINUS, 0L}, //GPB1 SW16
{FALSE, FALSE, 0, FILTER_MINUS, 0L} //GPB0 SW15
};
ENCODER my_encoders[MAX_ENCODERS];
SWITCH my_switches[MAX_SWITCHES];
ENCODER *encoders = NULL;
SWITCH *switches = NULL;
#ifdef GPIO
static GThread *rotary_encoder_thread_id;
static uint64_t epochMilli;
static void initialiseEpoch() {
struct timespec ts ;
clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
}
static unsigned int millis () {
uint64_t now ;
struct timespec ts ;
clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
now = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
return (uint32_t)(now - epochMilli) ;
}
static gpointer rotary_encoder_thread(gpointer data) {
int i;
enum ACTION action;
enum ACTION_MODE mode;
int val;
usleep(250000);
t_print("%s\n", __FUNCTION__);
while (TRUE) {
g_mutex_lock(&encoder_mutex);
for (i = 0; i < MAX_ENCODERS; i++) {
if (encoders[i].bottom_encoder_enabled && encoders[i].bottom_encoder_pos != 0) {
//t_print("%s: BOTTOM encoder %d pos=%d\n",__FUNCTION__,i,encoders[i].bottom_encoder_pos);
action = encoders[i].bottom_encoder_function;
mode = RELATIVE;
val = encoders[i].bottom_encoder_pos;
encoders[i].bottom_encoder_pos = 0;
schedule_action(action, mode, val);
}
if (encoders[i].top_encoder_enabled && encoders[i].top_encoder_pos != 0) {
//t_print("%s: TOP encoder %d pos=%d\n",__FUNCTION__,i,encoders[i].top_encoder_pos);
action = encoders[i].top_encoder_function;
mode = RELATIVE;
val = encoders[i].top_encoder_pos;
encoders[i].top_encoder_pos = 0;
schedule_action(action, mode, val);
}
}
g_mutex_unlock(&encoder_mutex);
usleep(100000); // sleep for 100ms
}
return NULL;
}
static void process_encoder(int e, int l, int addr, int val) {
guchar pinstate;
//t_print("%s: encoder=%d level=%d addr=0x%02X val=%d\n",__FUNCTION__,e,l,addr,val);
g_mutex_lock(&encoder_mutex);
switch (l) {
case BOTTOM_ENCODER:
switch (addr) {
case A:
encoders[e].bottom_encoder_a_value = val;
pinstate = (encoders[e].bottom_encoder_b_value << 1) | encoders[e].bottom_encoder_a_value;
encoders[e].bottom_encoder_state = encoder_state_table[encoders[e].bottom_encoder_state & 0xf][pinstate];
//t_print("%s: state=%02X\n",__FUNCTION__,encoders[e].bottom_encoder_state);
switch (encoders[e].bottom_encoder_state & 0x30) {
case DIR_NONE:
break;
case DIR_CW:
encoders[e].bottom_encoder_pos++;
break;
case DIR_CCW:
encoders[e].bottom_encoder_pos--;
break;
default:
break;
}
//t_print("%s: %d BOTTOM pos=%d\n",__FUNCTION__,e,encoders[e].bottom_encoder_pos);
break;
case B:
encoders[e].bottom_encoder_b_value = val;
pinstate = (encoders[e].bottom_encoder_b_value << 1) | encoders[e].bottom_encoder_a_value;
encoders[e].bottom_encoder_state = encoder_state_table[encoders[e].bottom_encoder_state & 0xf][pinstate];
//t_print("%s: state=%02X\n",__FUNCTION__,encoders[e].bottom_encoder_state);
switch (encoders[e].bottom_encoder_state & 0x30) {
case DIR_NONE:
break;
case DIR_CW:
encoders[e].bottom_encoder_pos++;
break;
case DIR_CCW:
encoders[e].bottom_encoder_pos--;
break;
default:
break;
}
//t_print("%s: %d BOTTOM pos=%d\n",__FUNCTION__,e,encoders[e].bottom_encoder_pos);
break;
}
break;
case TOP_ENCODER:
switch (addr) {
case A:
encoders[e].top_encoder_a_value = val;
pinstate = (encoders[e].top_encoder_b_value << 1) | encoders[e].top_encoder_a_value;
encoders[e].top_encoder_state = encoder_state_table[encoders[e].top_encoder_state & 0xf][pinstate];
//t_print("%s: state=%02X\n",__FUNCTION__,encoders[e].top_encoder_state);
switch (encoders[e].top_encoder_state & 0x30) {
case DIR_NONE:
break;
case DIR_CW:
encoders[e].top_encoder_pos++;
break;
case DIR_CCW:
encoders[e].top_encoder_pos--;
break;
default:
break;
}
//t_print("%s: %d TOP pos=%d\n",__FUNCTION__,e,encoders[e].top_encoder_pos);
break;
case B:
encoders[e].top_encoder_b_value = val;
pinstate = (encoders[e].top_encoder_b_value << 1) | encoders[e].top_encoder_a_value;
encoders[e].top_encoder_state = encoder_state_table[encoders[e].top_encoder_state & 0xf][pinstate];
//t_print("%s: state=%02X\n",__FUNCTION__,encoders[e].top_encoder_state);
switch (encoders[e].top_encoder_state & 0x30) {
case DIR_NONE:
break;
case DIR_CW:
encoders[e].top_encoder_pos++;
break;
case DIR_CCW:
encoders[e].top_encoder_pos--;
break;
default:
break;
}
//t_print("%s: %d TOP pos=%d\n",__FUNCTION__,e,encoders[e].top_encoder_pos);
break;
}
break;
}
g_mutex_unlock(&encoder_mutex);
}
static void process_edge(int offset, int value) {
int i;
unsigned int t;
gboolean found;
//t_print("%s: offset=%d value=%d\n",__FUNCTION__,offset,value);
found = FALSE;
//
// Priority 1 (highst): check encoder
//
for (i = 0; i < MAX_ENCODERS; i++) {
if (encoders[i].bottom_encoder_enabled && encoders[i].bottom_encoder_address_a == offset) {
//t_print("%s: found %d encoder %d bottom A\n",__FUNCTION__,offset,i);
process_encoder(i, BOTTOM_ENCODER, A, SET(value == PRESSED));
found = TRUE;
break;
} else if (encoders[i].bottom_encoder_enabled && encoders[i].bottom_encoder_address_b == offset) {
//t_print("%s: found %d encoder %d bottom B\n",__FUNCTION__,offset,i);
process_encoder(i, BOTTOM_ENCODER, B, SET(value == PRESSED));
found = TRUE;
break;
} else if (encoders[i].top_encoder_enabled && encoders[i].top_encoder_address_a == offset) {
//t_print("%s: found %d encoder %d top A\n",__FUNCTION__,offset,i);
process_encoder(i, TOP_ENCODER, A, SET(value == PRESSED));
found = TRUE;
break;
} else if (encoders[i].top_encoder_enabled && encoders[i].top_encoder_address_b == offset) {
//t_print("%s: found %d encoder %d top B\n",__FUNCTION__,offset,i);
process_encoder(i, TOP_ENCODER, B, SET(value == PRESSED));
found = TRUE;
break;
} else if (encoders[i].switch_enabled && encoders[i].switch_address == offset) {
//t_print("%s: found %d encoder %d switch\n",__FUNCTION__,offset,i);
t = millis();
//t_print("%s: found %d encoder %d switch value=%d t=%u\n",__FUNCTION__,offset,i,value,t);
if (t < encoders[i].switch_debounce) {
return;
}
encoders[i].switch_debounce = t + settle_time;
schedule_action(encoders[i].switch_function, value, 0);
found = TRUE;
break;
}
}
if (found) { return; }
//
// Priority 2: check "non-controller" inputs
// take care for "external" debouncing!
//
if (offset == CWL_LINE) {
schedule_action(CW_LEFT, value, 0);
found = TRUE;
}
if (offset == CWR_LINE) {
schedule_action(CW_RIGHT, value, 0);
found = TRUE;
}
if (offset == CWKEY_LINE) {
schedule_action(CW_KEYER_KEYDOWN, value, 0);
found = TRUE;
}
if (offset == PTTIN_LINE) {
schedule_action(CW_KEYER_PTT, value, 0);
found = TRUE;
}
if (found) { return; }
//
// Priority 3: handle i2c interrupt and i2c switches
//
if (controller == CONTROLLER2_V1 || controller == CONTROLLER2_V2 || controller == G2_FRONTPANEL) {
if (I2C_INTERRUPT == offset) {
if (value == PRESSED) {
i2c_interrupt();
}
found = TRUE;
}
}
if (found) { return; }
//
// Priority 4: handle "normal" (non-I2C) switches
//
for (i = 0; i < MAX_SWITCHES; i++) {
if (switches[i].switch_enabled && switches[i].switch_address == offset) {
t = millis();
//t_print("%s: found %d switch %d value=%d t=%u\n",__FUNCTION__,offset,i,value,t);
found = TRUE;
if (t < switches[i].switch_debounce) {
return;
}
//t_print("%s: switches=%p function=%d (%s)\n",__FUNCTION__,switches,switches[i].switch_function,sw_string[switches[i].switch_function]);
switches[i].switch_debounce = t + settle_time;
schedule_action(switches[i].switch_function, value, 0);
break;
}
}
if (found) { return; }
t_print("%s: could not find %d\n", __FUNCTION__, offset);
}
// cppcheck-suppress constParameterCallback
static int interrupt_cb(int event_type, unsigned int line, const struct timespec *timeout, void* data) {
//t_print("%s: event=%d line=%d\n",__FUNCTION__,event_type,line);
switch (event_type) {
case GPIOD_CTXLESS_EVENT_CB_TIMEOUT:
// timeout - ignore
//t_print("%s: Ignore timeout\n",__FUNCTION__);
break;
case GPIOD_CTXLESS_EVENT_CB_RISING_EDGE:
//t_print("%s: Ignore RISING EDGE\n",__FUNCTION__);
process_edge(line, RELEASED);
break;
case GPIOD_CTXLESS_EVENT_CB_FALLING_EDGE:
//t_print("%s: Process FALLING EDGE\n",__FUNCTION__);
process_edge(line, PRESSED);
break;
}
return GPIOD_CTXLESS_EVENT_CB_RET_OK;
}
#endif
void gpio_default_encoder_actions(int ctrlr) {
const ENCODER *default_encoders;
switch (ctrlr) {
case NO_CONTROLLER:
case G2_V2:
default:
default_encoders = NULL;
break;
case CONTROLLER1:
default_encoders = encoders_controller1;
break;
case CONTROLLER2_V1:
default_encoders = encoders_controller2_v1;
break;
case CONTROLLER2_V2:
default_encoders = encoders_controller2_v2;
break;
case G2_FRONTPANEL:
default_encoders = encoders_g2_frontpanel;
break;
}
if (default_encoders) {
//
// Copy (only) actions
//
for (int i = 0; i < MAX_ENCODERS; i++) {
my_encoders[i].bottom_encoder_function = default_encoders[i].bottom_encoder_function;
my_encoders[i].top_encoder_function = default_encoders[i].top_encoder_function;
my_encoders[i].switch_function = default_encoders[i].switch_function;
}
}
}
void gpio_default_switch_actions(int ctrlr) {
const SWITCH *default_switches;
switch (ctrlr) {
case NO_CONTROLLER:
case CONTROLLER1:
case G2_V2:
default:
default_switches = NULL;
break;
case CONTROLLER2_V1:
default_switches = switches_controller2_v1;
break;
case CONTROLLER2_V2:
default_switches = switches_controller2_v2;
break;
case G2_FRONTPANEL:
default_switches = switches_g2_frontpanel;
break;
}
if (default_switches) {
//
// Copy (only) actions
//
for (int i = 0; i < MAX_SWITCHES; i++) {
my_switches[i].switch_function = default_switches[i].switch_function;
}
}
}
//
// If there is non-standard hardware at the GPIO lines
// the code below in the NO_CONTROLLER section must
// be adjusted such that "occupied" GPIO lines are not
// used for CW or PTT.
// For CONTROLLER1 and CONTROLLER2_V1, GPIO
// lines 9,10,11,14 are "free" and can be
// used for CW and PTT.
//
// At this place, copy complete data structures to my_encoders
// and my_switches, including GPIO lines etc.
//
void gpio_set_defaults(int ctrlr) {
t_print("%s: %d\n", __FUNCTION__, ctrlr);
switch (ctrlr) {
case CONTROLLER1:
//
// GPIO lines not used by controller: 9, 10, 11, 14, 15
//
CWL_LINE = 9;
CWR_LINE = 11;
CWKEY_LINE = 10;
PTTIN_LINE = 14;
PTTOUT_LINE = 15;
CWOUT_LINE = -1;
memcpy(my_encoders, encoders_controller1, sizeof(my_encoders));
encoders = my_encoders;
switches = switches_controller1[0];
break;
case CONTROLLER2_V1:
//
// GPIO lines not used by controller: 5, 6, 7, 9, 10, 11, 12, 13, 14
//
CWL_LINE = 9;
CWR_LINE = 11;
CWKEY_LINE = 10;
PTTIN_LINE = 14;
PTTOUT_LINE = 13;
CWOUT_LINE = 12;
memcpy(my_encoders, encoders_controller2_v1, sizeof(my_encoders));
memcpy(my_switches, switches_controller2_v1, sizeof(my_switches));
encoders = my_encoders;
switches = my_switches;
break;
case CONTROLLER2_V2:
//
// GPIO lines not used by controller: 14. Assigned to PTTIN by default
//
CWL_LINE = -1;
CWR_LINE = -1;
PTTIN_LINE = 14;
CWKEY_LINE = -1;
PTTOUT_LINE = -1;
memcpy(my_encoders, encoders_controller2_v2, sizeof(my_encoders));
memcpy(my_switches, switches_controller2_v2, sizeof(my_switches));
encoders = my_encoders;
switches = my_switches;
break;
case G2_FRONTPANEL:
//
// Regard all GPIO lines as "used"
//
CWL_LINE = -1;
CWR_LINE = -1;
PTTIN_LINE = -1;
CWKEY_LINE = -1;
PTTOUT_LINE = -1;
memcpy(my_encoders, encoders_g2_frontpanel, sizeof(my_encoders));
memcpy(my_switches, switches_g2_frontpanel, sizeof(my_switches));
encoders = my_encoders;
switches = my_switches;
break;
case G2_V2:
//
// There are no GPIO lines that the user can use
//
memcpy(my_encoders, encoders_no_controller, sizeof(my_encoders));
memcpy(my_switches, switches_no_controller, sizeof(my_switches));
encoders = my_encoders;
switches = my_switches;
break;
case NO_CONTROLLER:
default:
//
// GPIO lines that are not used elsewhere: 5, 6, 12, 16,
// 22, 23, 24, 25, 27
//
CWL_LINE = 5;
CWR_LINE = 6;
CWKEY_LINE = 12;
PTTIN_LINE = 16;
PTTOUT_LINE = 22;
CWOUT_LINE = 23;
if (have_radioberry1) {
CWL_LINE = 14;
CWR_LINE = 15;
CWKEY_LINE = -1;
PTTIN_LINE = -1;
PTTOUT_LINE = -1;
CWOUT_LINE = -1;
}
if (have_radioberry2) {
CWL_LINE = 17;
CWR_LINE = 21;
CWKEY_LINE = -1;
PTTIN_LINE = -1;
PTTOUT_LINE = -1;
CWOUT_LINE = -1;
}
memcpy(my_encoders, encoders_no_controller, sizeof(my_encoders));
memcpy(my_switches, switches_no_controller, sizeof(my_switches));
encoders = my_encoders;