-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
213 lines (176 loc) · 4.32 KB
/
Controller.cpp
File metadata and controls
213 lines (176 loc) · 4.32 KB
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
/*
Controller.cpp: PCTx driver
Copyright (C) 2010 Wade Smith
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "Controller.h"
#define TRANSFER_TIMEOUT 200
#define DEBUG
#ifdef DEBUG
#include <iostream>
using namespace std;
#endif
Controller::Controller()
{
output[0] = 0x00;
output[1] = 0x00;
output[2] = 0x00;
output[3] = 0x00;
output[4] = 0x00;
output[5] = 0x00;
output[6] = 0x00;
output[7] = 0x00;
output[8] = 0x00;
output[9] = 0x00;
output[10] = 0x00;
output[11] = 0x00;
output[12] = 0x00;
output[13] = 0x00;
output[14] = 0x00;
output[15] = 0x00;
output[16] = 0x00;
output[17] = 0x00;
output[18] = 0x00;
output[19] = 0x00;
output[20] = 0x00;
output[21] = 0x00;
output[22] = 0x00;
output[23] = 0x00;
output[24] = 0x00;
output[25] = 0x00;
output[26] = 0x00;
output[27] = 0x44;
output[28] = 0x00;
output[29] = 0x0e;
output[30] = 0x00;
output[31] = 0x28;
connected = false;
}
Controller::~Controller()
{
if (connected)
{
libusb_release_interface(usb_device_handle, 0);
libusb_close(usb_device_handle);
libusb_exit(usb_context);
}
}
// Connect to the PCTx, may need to be run as root if the kernel driver is attached
bool Controller::connect()
{
if (connected)
{
return true;
}
#ifdef DEBUG
cout << "Init libusb" << endl;
#endif
if (libusb_init(&usb_context))
{
#ifdef DEBUG
cout << "Failed to init libusb" << endl;
#endif
return false;
}
#ifdef DEBUG
libusb_set_debug(usb_context, 3);
#endif
#ifdef DEBUG
cout << "Success" << endl << "Opening device handle" << endl;
#endif
if (!(usb_device_handle = libusb_open_device_with_vid_pid(usb_context, vendor_id, product_id)))
{
#ifdef DEBUG
cout << "Failed to open device" << endl;
#endif
return false;
}
if (libusb_kernel_driver_active(usb_device_handle, 0) == 1)
{
#ifdef DEBUG
cout << "Detaching kernel driver" << endl;
#endif
libusb_detach_kernel_driver(usb_device_handle, 0);
}
#ifdef DEBUG
cout << "Setting configuration" << endl;
#endif
libusb_set_configuration(usb_device_handle, 1);
#ifdef DEBUG
cout << "Claiming interface" << endl;
#endif
libusb_claim_interface(usb_device_handle, 0);
#ifdef DEBUG
cout << "Success" << endl;
#endif
connected = true;
return true;
}
bool Controller::isConnected()
{
return connected;
}
// Update data on all channels and send to the PCTx
bool Controller::transmit(int channel1, int channel2, int channel3, int channel4, int channel5,
int channel6, int channel7, int channel8, int channel9)
{
output[0] = channel1%256;
output[1] = channel1/256;
output[2] = channel2%256;
output[3] = channel2/256;
output[4] = channel3%256;
output[5] = channel3/256;
output[6] = channel4%256;
output[7] = channel4/256;
output[8] = channel5%256;
output[9] = channel5/256;
output[10] = channel6%256;
output[11] = channel6/256;
output[12] = channel7%256;
output[13] = channel7/256;
output[14] = channel8%256;
output[15] = channel8/256;
output[16] = channel9%256;
output[17] = channel9/256;
return writeToDevice();
}
// Update data on a single channel and send to the PCTx
bool updateChannel(int channelNum, int value)
{
int refIndex = (channelNum - 1) * 2;
output[refIndex] = value%256;
output[refIndex + 1] = value/256;
return writeToDevice();
}
bool Controller::writeToDevice(void)
{
int transferred;
unsigned char input[10];
libusb_interrupt_transfer(usb_device_handle, 0x01, output, 32, &transferred, TRANSFER_TIMEOUT);
if (transferred < 32)
{
#ifdef DEBUG
cout << "Write failed" << endl;
#endif
return false;
}
libusb_interrupt_transfer(usb_device_handle, 0x81, input, 10, &transferred, TRANSFER_TIMEOUT);
if (transferred < 10)
{
#ifdef DEBUG
cout << "Read failed" << endl;
#endif
return false;
}
return true;
}