1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2020
3 //              David Freese, W1HKJ
4 //
5 // This file is part of flrig.
6 //
7 // flrig is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // flrig is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
20 #include "gpio.h"
21 #include "gpio_ptt.h"
22 
23 static const char *gpio_label[] = {
24 " 17   00   11",
25 " 18   01   12",
26 " 27   02   13",
27 " 22   03   15",
28 " 23   04   16",
29 " 24   05   18",
30 " 25   06   22",
31 "  4   07    7",
32 "  5   21   29",
33 "  6   22   31",
34 " 13   23   33",
35 " 19   24   35",
36 " 26   25   37",
37 " 12   26   32",
38 " 16   27   36",
39 " 20   28   38",
40 " 21   29   40"
41 };
42 
show_pins()43 static void show_pins()
44 {
45 	printf("%05x | %05x\n", progStatus.enable_gpio, progStatus.gpio_on);
46 }
47 
cb_btn_enable_gpio(Fl_Check_Button * btn,void * val)48 void cb_btn_enable_gpio(Fl_Check_Button* btn, void *val)
49 {
50 	size_t n = (size_t)(val);
51 	if (btn->value()) {
52 		progStatus.enable_gpio |= (1 << n);
53 	} else {
54 		progStatus.enable_gpio &= ~(1 << n);
55 	}
56 	show_pins();
57 }
58 
cb_btn_gpio_on(Fl_Check_Button * btn,void * val)59 static void cb_btn_gpio_on(Fl_Check_Button* btn, void *val)
60 {
61 	size_t n = (size_t)(val);
62 	if (btn->value()) {
63 		progStatus.gpio_on |= (1 << n);
64 	} else {
65 		progStatus.gpio_on &= ~(1 << n);
66 	}
67 	show_pins();
68 }
69 
cb_cnt_gpio_pulse_width(Fl_Counter * o,void *)70 static void cb_cnt_gpio_pulse_width(Fl_Counter* o, void*)
71 {
72 	progStatus.gpio_pulse_width = (int)o->value();
73 }
74 
cb_btn_use_gpio(Fl_Check_Button * btn,void *)75 static void cb_btn_use_gpio(Fl_Check_Button* btn, void *)
76 {
77 	if (btn->value()) {
78 		progStatus.gpio_ptt = true;
79 		open_gpio();
80 	} else {
81 		progStatus.gpio_ptt = false;
82 		close_gpio();
83 	}
84 }
85 
createGPIO(int X,int Y,int W,int H,const char * label)86 Fl_Group *createGPIO(int X, int Y, int W, int H, const char *label)
87 {
88 	tabGPIO = new Fl_Group(X, Y, W, H, label);
89 
90 	size_t w = (W - 20)/4;
91 	size_t h = 18;
92 	size_t hd = (H - 4)/ 11;
93 	size_t y = Y + 2 + hd;
94 	size_t col1 = X + 20;
95 	size_t col2 = col1 + w;
96 	size_t col3 = col2 + w;
97 	size_t col4 = col3 + w;
98 
99 	Fl_Check_Button * btn_use_gpio = new Fl_Check_Button (col2 + w/2, Y + 2, w, h, "Use GPIO PTT");
100 	btn_use_gpio->down_box(FL_DOWN_BOX);
101 	btn_use_gpio->labelfont(FL_COURIER);
102 	btn_use_gpio->labelsize(12);
103 	btn_use_gpio->value(progStatus.gpio_ptt);
104 	btn_use_gpio->callback((Fl_Callback*)cb_btn_use_gpio);
105 
106 	Fl_Box* bx1 = new Fl_Box(col1, y, w, h, "  BCM  GPIO Pin ON");
107 	bx1->labelfont(FL_COURIER);
108 	bx1->labelsize(12);
109 	bx1->align(Fl_Align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE));
110 
111 	Fl_Box* bx3 = new Fl_Box(col3, y, w, h, "  BCM  GPIO Pin ON");
112 	bx3->labelfont(FL_COURIER);
113 	bx3->labelsize(12);
114 	bx3->align(Fl_Align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE));
115 
116 	for (size_t n = 0; n < 8; n++) {
117 		btn_enable_gpio[n] = new Fl_Check_Button(col1, y + hd * (n + 1), w, h, gpio_label[n]);
118 		btn_enable_gpio[n]->tooltip(_("Select pin number"));
119 		btn_enable_gpio[n]->down_box(FL_DOWN_BOX);
120 		btn_enable_gpio[n]->labelfont(FL_COURIER);
121 		btn_enable_gpio[n]->labelsize(12);
122 		btn_enable_gpio[n]->callback((Fl_Callback*)cb_btn_enable_gpio, (void *)n);
123 		btn_enable_gpio[n]->value((progStatus.enable_gpio >> n)& 0x01);
124 	}
125 
126 	for (size_t n = 8; n < 17; n++) {
127 		btn_enable_gpio[n] = new Fl_Check_Button(col3, y + hd * (n - 7), w, h, gpio_label[n]);
128 		btn_enable_gpio[n]->tooltip(_("Select pin number"));
129 		btn_enable_gpio[n]->down_box(FL_DOWN_BOX);
130 		btn_enable_gpio[n]->labelfont(FL_COURIER);
131 		btn_enable_gpio[n]->labelsize(12);
132 		btn_enable_gpio[n]->callback((Fl_Callback*)cb_btn_enable_gpio, (void *)n);
133 		btn_enable_gpio[n]->value((progStatus.enable_gpio >> n)& 0x01);
134 	}
135 
136 	for (size_t n = 0; n < 8; n++) {
137 		btn_gpio_on[n] = new Fl_Check_Button(col2, y + hd * (n + 1), w, h, "");
138 		btn_gpio_on[n]->tooltip(_("Select PTT on state"));
139 		btn_gpio_on[n]->down_box(FL_DOWN_BOX);
140 		btn_gpio_on[n]->labelfont(FL_COURIER);
141 		btn_gpio_on[n]->labelsize(12);
142 		btn_gpio_on[n]->callback((Fl_Callback*)cb_btn_gpio_on, (void *)n);
143 		btn_gpio_on[n]->value((progStatus.gpio_on >> n) & 0x01);
144 	}
145 
146 	for (size_t n = 8; n < 17; n++) {
147 		btn_gpio_on[n] = new Fl_Check_Button(col4, y + hd * (n - 7), w, h, "");
148 		btn_gpio_on[n]->tooltip(_("Select PTT on state"));
149 		btn_gpio_on[n]->down_box(FL_DOWN_BOX);
150 		btn_gpio_on[n]->labelfont(FL_COURIER);
151 		btn_gpio_on[n]->labelsize(12);
152 		btn_gpio_on[n]->callback((Fl_Callback*)cb_btn_gpio_on, (void *)n);
153 		btn_gpio_on[n]->value((progStatus.gpio_on >> n) & 0x01);
154 	}
155 
156 	cnt_gpio_pulse_width = new Fl_Counter(col1, y + hd * 9, 80, 20, "Pulse width (msec)");
157 	cnt_gpio_pulse_width->tooltip(_("Set >0 if pulsed PTT used"));
158 	cnt_gpio_pulse_width->type(1);
159 	cnt_gpio_pulse_width->labelfont(FL_COURIER);
160 	cnt_gpio_pulse_width->labelsize(12);
161 	cnt_gpio_pulse_width->minimum(0);
162 	cnt_gpio_pulse_width->maximum(50);
163 	cnt_gpio_pulse_width->step(1);
164 	cnt_gpio_pulse_width->callback((Fl_Callback*)cb_cnt_gpio_pulse_width);
165 	cnt_gpio_pulse_width->align(Fl_Align(FL_ALIGN_RIGHT));
166 	cnt_gpio_pulse_width->value(progStatus.gpio_pulse_width);
167 
168 	tabGPIO->end();
169 
170   return tabGPIO;
171 }
172