0 Star 0 Fork 1

ianaxe/vrpn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vrpn_Button_USB.cpp 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
#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(&current_time, NULL);
// Send reports. Stays the same in a real server.
report_changes();
}
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ianaxe/vrpn.git
git@gitee.com:ianaxe/vrpn.git
ianaxe
vrpn
vrpn
master

搜索帮助