File: HID-PTT

package info (click to toggle)
freedv 1.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,440 kB
  • sloc: cpp: 8,723; ansic: 3,564; python: 42; makefile: 10; sh: 7
file content (355 lines) | stat: -rw-r--r-- 13,829 bytes parent folder | download
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
--- a/src/fdmdv2_main.h
+++ b/src/fdmdv2_main.h
@@ -50,6 +50,14 @@
 #include <wx/regex.h>
 #include <wx/socket.h>
 
+#ifdef HAVE_LINUX_HIDRAW_H
+#include <linux/hidraw.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#endif
+
 #include <samplerate.h>
 
 #include <stdint.h>
@@ -189,6 +197,9 @@
 
         bool                m_boolUseSerialPTT;
         wxString            m_strRigCtrlPort;
+        wxString            m_strRigHidPort;
+        wxString            m_strRigHidBit;
+	bool                m_boolUseHIDPTT;
         bool                m_boolUseRTS;
         bool                m_boolRTSPos;
         bool                m_boolUseDTR;
@@ -457,7 +468,12 @@
         txRxThread*             m_txRxThread;
 
         bool                    OpenHamlibRig();
-        void                    OpenSerialPort(void);
+	void                    HIDPTT(int pushed);
+        void                    OpenHIDPort(void);
+	void                    CloseHIDPort(void);
+	int                     m_hidPort;
+	char                    m_hidGPIO;
+	void                    OpenSerialPort(void);
         void                    CloseSerialPort(void);
         void                    SerialPTTRx(void);
 
--- a/src/dlg_ptt.h
+++ b/src/dlg_ptt.h
@@ -49,6 +49,13 @@
     protected:
         wxCheckBox* m_ckLeftChannelVoxTone;
 
+        /* HID PTT settings.*/
+
+	wxCheckBox* m_ckUseHIDPTT;
+	wxListBox* m_listHidPorts;
+	wxTextCtrl *m_txtHidDevicePath;
+	wxTextCtrl *m_txtHidDeviceBit;
+
         /* Hamlib settings.*/
 
         wxCheckBox *m_ckUseHamlibPTT;
@@ -81,6 +88,7 @@
         void populatePortList();
 
         void PTTUseHamLibClicked(wxCommandEvent& event);
+        void PTTUseHIDClicked(wxCommandEvent& event);
         void PTTUseSerialClicked(wxCommandEvent& event);
 
         void OnTest(wxCommandEvent& event);
--- a/src/dlg_ptt.cpp
+++ b/src/dlg_ptt.cpp
@@ -51,6 +51,42 @@
     mainSizer->Add(staticBoxSizer28, 0, wxEXPAND, 5);
 
     //----------------------------------------------------------------------
+    // HID PTT
+    //----------------------------------------------------------------------
+
+    wxStaticBoxSizer* staticBoxSizerHID = new wxStaticBoxSizer( new wxStaticBox(this, wxID_ANY, _("HID PTT Settings")), wxVERTICAL);
+
+    wxBoxSizer* gridSizerHID = new wxBoxSizer(wxHORIZONTAL);
+
+    /* Use HID PTT checkbox. */
+    m_ckUseHIDPTT = new wxCheckBox(this, wxID_ANY, _("Use HID PTT"), wxDefaultPosition, wxDefaultSize, 0);
+    m_ckUseHIDPTT->SetValue(false);
+    gridSizerHID->Add(m_ckUseHIDPTT, 0, wxALIGN_CENTER_VERTICAL, 2);
+
+    /* HID device path listbox. */
+    //wxArrayString m_listHidPortsArr;
+    gridSizerHID->Add(new wxStaticText(this, wxID_ANY, _("Device (/dev/hidraw? :"), wxDefaultPosition, wxDefaultSize, 0),
+                      0, wxALIGN_CENTER_VERTICAL | wxLEFT, 2);
+    //m_listHidPorts = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxSize(250, -1), m_listHidPortsArr, wxLB_SINGLE | wxLB_SORT);
+    //gridSizerHID->Add(m_listHidPorts, 1, wxALIGN_CENTER_VERTICAL, 0);
+
+    m_txtHidDevicePath = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(140,-1), 0);
+    gridSizerHID->Add(m_txtHidDevicePath, 1, wxEXPAND|wxALIGN_CENTER|wxALIGN_RIGHT, 2);
+
+    /* HID GPIO selector. */
+    wxArrayString m_listHidGPIOArr;
+
+    gridSizerHID->Add(new wxStaticText(this, wxID_ANY, _("GPIO PIN # :"), wxDefaultPosition, wxDefaultSize, 0),
+                      0, wxALIGN_CENTER_VERTICAL | wxLEFT, 0);
+    // listHIDGPIO = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxSize(-1, -1), m_listHidGPIOArr, wxLB_SINGLE | wxLB_SORT);
+    // gridSizerHID->Add(listHIDGPIO, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 0);
+    m_txtHidDeviceBit = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(20,-1), 0);
+    gridSizerHID->Add(m_txtHidDeviceBit, 1, wxEXPAND|wxALIGN_CENTER|wxALIGN_RIGHT, 2);
+
+    staticBoxSizerHID->Add(gridSizerHID, 0);
+    mainSizer->Add(staticBoxSizerHID, 1);
+
+    //----------------------------------------------------------------------
     // Hamlib for CAT PTT
     //----------------------------------------------------------------------
 
@@ -68,7 +104,7 @@
     /* Hamlib Rig Type combobox. */
 
     gridSizerhl->Add(new wxStaticText(this, wxID_ANY, _("Rig Model:"), wxDefaultPosition, wxDefaultSize, 0), 
-                      0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT, 20);
+                      0, wxALIGN_CENTER_VERTICAL | wxLEFT, 20);
     m_cbRigName = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(250, -1), 0, NULL, wxCB_DROPDOWN | wxCB_READONLY);
     wxGetApp().m_hamlib->populateComboBox(m_cbRigName);
     m_cbRigName->SetSelection(wxGetApp().m_intHamlibRig);
@@ -193,6 +229,7 @@
     // Connect events
     this->Connect(wxEVT_INIT_DIALOG, wxInitDialogEventHandler(ComPortsDlg::OnInitDialog), NULL, this);
     m_ckUseHamlibPTT->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(ComPortsDlg::PTTUseHamLibClicked), NULL, this);
+    m_ckUseHIDPTT->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(ComPortsDlg::PTTUseHIDClicked), NULL, this);
     m_ckUseSerialPTT->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(ComPortsDlg::PTTUseSerialClicked), NULL, this);
     m_buttonOK->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ComPortsDlg::OnOK), NULL, this);
     m_buttonCancel->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ComPortsDlg::OnCancel), NULL, this);
@@ -208,6 +245,7 @@
     // Disconnect Events
     this->Disconnect(wxEVT_INIT_DIALOG, wxInitDialogEventHandler(ComPortsDlg::OnInitDialog), NULL, this);
     m_ckUseHamlibPTT->Disconnect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(ComPortsDlg::PTTUseHamLibClicked), NULL, this);
+    m_ckUseHIDPTT->Disconnect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(ComPortsDlg::PTTUseHIDClicked), NULL, this);
     m_ckUseSerialPTT->Disconnect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(ComPortsDlg::PTTUseSerialClicked), NULL, this);
     m_buttonOK->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ComPortsDlg::OnOK), NULL, this);
     m_buttonCancel->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ComPortsDlg::OnCancel), NULL, this);
@@ -339,7 +377,7 @@
 void ComPortsDlg::ExchangeData(int inout)
 {
     wxConfigBase *pConfig = wxConfigBase::Get();
-    wxString str;
+    wxString str,hidstr,hidbit;
     
     if(inout == EXCHANGE_DATA_IN) {
         m_ckLeftChannelVoxTone->SetValue(wxGetApp().m_leftChannelVoxTone);
@@ -358,13 +396,21 @@
 
         /* Serial PTT */
 
+	m_ckUseHIDPTT->SetValue(wxGetApp().m_boolUseHIDPTT);
+	hidstr = wxGetApp().m_strRigHidPort;
+	hidbit = wxGetApp().m_strRigHidBit;
+
         m_ckUseSerialPTT->SetValue(wxGetApp().m_boolUseSerialPTT);
         str = wxGetApp().m_strRigCtrlPort;
 #ifdef __WXMSW__
         m_listCtrlPorts->SetStringSelection(str);
+	m_listHidPorts->SetStringSelection(hidstr);
+	m_listHidBit->SetStringSelection(hidbit);
 #endif
 #if defined(__WXOSX__) || defined(__WXGTK__)
         m_cbCtlDevicePath->SetValue(str);
+	m_txtHidDevicePath->SetValue(hidstr);
+	m_txtHidDeviceBit->SetValue(hidbit);
 #endif
         m_rbUseRTS->SetValue(wxGetApp().m_boolUseRTS);
         m_ckRTSPos->SetValue(wxGetApp().m_boolRTSPos);
@@ -397,6 +443,21 @@
         pConfig->Write(wxT("/Hamlib/SerialPort"), wxGetApp().m_strHamlibSerialPort);
         pConfig->Write(wxT("/Hamlib/SerialRate"), wxGetApp().m_intHamlibSerialRate);
 
+        /* HID PTT settings. */
+
+        wxGetApp().m_boolUseHIDPTT = m_ckUseHIDPTT->IsChecked();
+#ifdef __WXMSW__
+        wxGetApp().m_strRigHidPort = m_listHidPorts->GetStringSelection();
+        wxGetApp().m_strRigHidBit  = m_listHidPorts->GetStringSelection();
+#endif
+#ifdef __WXGTK__
+        wxGetApp().m_strRigHidPort             = m_txtHidDevicePath->GetValue();
+        wxGetApp().m_strRigHidBit             = m_txtHidDeviceBit->GetValue();
+#endif
+        pConfig->Write(wxT("/Rig/UseHIDPTT"),    wxGetApp().m_boolUseHIDPTT);
+        pConfig->Write(wxT("/Rig/HIDPort"),    wxGetApp().m_strRigHidPort);
+        pConfig->Write(wxT("/Rig/HIDBit"),    wxGetApp().m_strRigHidBit);
+
         /* Serial settings */
 
         wxGetApp().m_boolUseSerialPTT           = m_ckUseSerialPTT->IsChecked();
@@ -545,6 +606,13 @@
 
 
 //-------------------------------------------------------------------------
+// PTTUseHIDClicked()
+//-------------------------------------------------------------------------
+void ComPortsDlg::PTTUseHIDClicked(wxCommandEvent& event)
+{
+}
+
+//-------------------------------------------------------------------------
 // PTTUseSerialClicked()
 //-------------------------------------------------------------------------
 void ComPortsDlg::PTTUseSerialClicked(wxCommandEvent& event)
--- a/src/fdmdv2_main.cpp
+++ b/src/fdmdv2_main.cpp
@@ -457,6 +457,10 @@
     wxGetApp().m_strHamlibSerialPort = pConfig->Read("/Hamlib/SerialPort", "");
     wxGetApp().m_intHamlibSerialRate = pConfig->ReadLong("/Hamlib/SerialRate", 0);
     
+    wxGetApp().m_boolUseHIDPTT      = pConfig->ReadBool(wxT("/Rig/UseHIDPTT"), false);
+    wxGetApp().m_strRigHidPort      = pConfig->Read(wxT("/Rig/HIDPort"), wxT(""));
+    wxGetApp().m_strRigHidBit      = pConfig->Read(wxT("/Rig/HIDBit"), wxT(""));
+
     wxGetApp().m_boolUseSerialPTT   = pConfig->ReadBool(wxT("/Rig/UseSerialPTT"),   false);
     wxGetApp().m_strRigCtrlPort     = pConfig->Read(wxT("/Rig/Port"),               wxT(""));
     wxGetApp().m_boolUseRTS         = pConfig->ReadBool(wxT("/Rig/UseRTS"),         true);
@@ -1548,6 +1552,14 @@
         wxGetApp().m_serialport->ptt(g_tx);
     }
 
+    if(wxGetApp().m_boolUseHIDPTT && m_hidPort >= 0)
+    {
+        if(g_tx)
+	  HIDPTT(1);
+        else
+	  HIDPTT(0);
+    }
+
     // reset level gauge
 
     m_maxLevel = 0;
@@ -2557,6 +2569,8 @@
         
         if (wxGetApp().m_boolHamlibUseForPTT)
             OpenHamlibRig();
+        if (wxGetApp().m_boolUseHIDPTT)
+            OpenHIDPort();
         if (wxGetApp().m_boolUseSerialPTT) {
             OpenSerialPort();
         }
@@ -2605,6 +2619,9 @@
             }
         }
 
+        if (wxGetApp().m_boolUseHIDPTT)
+            CloseHIDPort();
+
         if (wxGetApp().m_boolUseSerialPTT) {
             CloseSerialPort();
         }
@@ -4337,3 +4354,84 @@
         return;
     }
 }
+
+//----------------------------------------------------------------
+// HIDPTT(int pushed)
+//----------------------------------------------------------------
+void MainFrame::HIDPTT(int pushed)
+{
+#ifdef HAVE_LINUX_HIDRAW_H
+  if (m_hidPort>0) {
+    // Build two packets for CM108 HID.  One turns a GPIO bit on.  The other turns it off.
+    // Packet is 4 bytes, preceded by a 'report number' byte
+    // 0x00 report number
+    // Write data packet (from CM108 documentation)
+    // byte 0: 00xx xxxx     Write GPIO
+    // byte 1: xxxx dcba     GPIO3-0 output values (1=high)
+    // byte 2: xxxx dcba     GPIO3-0 data-direction register (1=output)
+    // byte 3: xxxx xxxx     SPDIF
+
+    char out_rep[] = {
+      0x00, // report number
+      // HID output report
+      0x00,
+      (char)(pushed ? (1 << m_hidGPIO) : 0), // set GPIO
+      (char)(1 << m_hidGPIO), // Data direction register (1=output)
+      0x00
+    };
+
+    // printf("HIDPTT: using gpio=%d\n", m_hidGPIO);
+    // printf("HIDPTT: ptt state=%d\n", pushed ? (1 << m_hidGPIO) : 0);
+    if (write(m_hidPort, out_rep, sizeof(out_rep)) < 0) {
+      printf("HIDPTT: CM108 write error\n");
+    }
+  }
+#endif
+}
+
+//----------------------------------------------------------------
+// OpenHIDPort()
+//----------------------------------------------------------------
+void MainFrame::OpenHIDPort(void)
+{
+#ifdef HAVE_LINUX_HIDRAW_H
+  struct hidraw_devinfo hiddevinfo;
+
+    if(!wxGetApp().m_strRigHidPort.IsEmpty())
+    {
+      m_hidGPIO = 0x0f & wxAtoi(wxGetApp().m_strRigHidBit);
+      m_hidPort = open(wxGetApp().m_strRigHidPort.c_str(),O_RDWR,0);
+      // 0x0d8c is the vendor id of CMedia USB sound devices that may have HID GPIO features
+      if ((m_hidPort >=0)&&(!ioctl(m_hidPort, HIDIOCGRAWINFO, &hiddevinfo) && hiddevinfo.vendor == 0x0d8c))
+        {
+            //  always start with PTT cleared
+	    HIDPTT(0);
+            //m_btnTogPTT->Enable(true);
+            m_btnTogPTT->SetValue(false);
+        }
+        else
+        {
+#endif
+            m_hidPort = -1;
+            m_hidGPIO = -1;
+            //m_btnTogPTT->Disable();
+#ifdef HAVE_LINUX_HIDRAW_H
+        }
+    }
+#endif
+}
+
+//----------------------------------------------------------------
+// CloseHIDPort()
+//----------------------------------------------------------------
+void MainFrame::CloseHIDPort(void)
+{
+    if(m_hidPort != 0)
+    {
+      close(m_hidPort);
+      m_hidPort = 0;
+      m_hidGPIO = -1;
+      //m_btnTogPTT->SetLabel(wxT("PTT"));
+      //m_btnTogPTT->Enable(false);
+    }
+}
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -165,6 +165,7 @@
 check_include_files("inttypes.h"   HAVE_INTTYPES_H)
 check_include_files("sys/stat.h"   HAVE_SYS_STAT_H)
 check_include_files("sys/types.h"  HAVE_SYS_TYPES_H)
+check_include_files("linux/hidraw.h"  HAVE_LINUX_HIDRAW_H)
 
 include(CheckTypeSize)
 check_type_size("int" SIZEOF_INT)
@@ -182,6 +183,10 @@
 include(CheckSymbolExists)
 check_symbol_exists("_fseeki64" "stdio.h" HAVE__FSEEKI64)
 
+if(${HAVE_LINUX_HIDRAW_H})
+ add_definitions(-DHAVE_LINUX_HIDRAW_H)
+endif(${HAVE_HIDRAW_H})
+
 # fdmdv2_main.h requires patching to find config.h as it current looks in the
 # source directory and the generated file goes in the binary directory.
 configure_file ("${PROJECT_SOURCE_DIR}/cmake/config.h.in"