forked from vrpn/vrpn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vrpn_Button_USB.cpp
98 lines (83 loc) · 2.21 KB
/
vrpn_Button_USB.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
#include "vrpn_Button_USB.h"
#ifdef _WIN32
vrpn_Button_USB::vrpn_Button_USB(const char *name, const char *deviceName,vrpn_Connection *c)
: vrpn_Button_Filter(name, c)
{
num_buttons = 16;
//setup of usb device
m_hDevice = INVALID_HANDLE_VALUE;
if ( m_hDevice != INVALID_HANDLE_VALUE )
{
fprintf(stderr, "USB device has already been opened." );
return;
}
//--- define the device code
char PortName[20];
strcpy( PortName, "\\\\.\\" );
strcat( PortName, deviceName );
//--- open the serial port
m_hDevice = CreateFile( PortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
if ( m_hDevice == INVALID_HANDLE_VALUE )
{
fprintf(stderr, "Could not open USB device." );
return;
}
return;
}
vrpn_Button_USB::~vrpn_Button_USB(void)
{
if ( m_hDevice != INVALID_HANDLE_VALUE )
CloseHandle( m_hDevice );
}
void vrpn_Button_USB::read(void)
{
unsigned long dat=85;
unsigned long lIn = 65536 * dat;
USBWrite(lIn);
bool ret=USBRead(dat,0);
buttons[0]=!(dat & 1);
buttons[1]=!(dat & 16);
}
//! writes data to the device
bool vrpn_Button_USB::USBWrite(const unsigned long &data)
{
unsigned long lOut;
return USB_IO(data,3,lOut,1);
}
//! reads data from the device
bool vrpn_Button_USB::USBRead(unsigned long &data, int port)
{
bool res;
unsigned long lOut;
unsigned long lIn = port * 256 + 20;
res=USB_IO(lIn,2,lOut,2);
data=(lOut / 256) & 255;
return res;
}
bool vrpn_Button_USB::USB_IO(unsigned long lIn, int lInSize, unsigned long &lOut, int lOutSize)
{
unsigned long lSize;
LPOVERLAPPED gOverlapped=0;
if(!DeviceIoControl(m_hDevice, 0x4, &lIn, lInSize, &lOut, lOutSize, &lSize, gOverlapped))
{
LPVOID lpMsgBuf;
if (!FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, ::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language*/ (LPTSTR) &lpMsgBuf, 0, NULL ))
{
// Handle the error.
return false;
}
}
return true;
}
void vrpn_Button_USB::mainloop()
{
struct timeval current_time;
// Call the generic server mainloop, since we are a server
server_mainloop();
read();
vrpn_gettimeofday(¤t_time, NULL);
// Send reports. Stays the same in a real server.
report_changes();
}
#endif