109 lines
3.3 KiB
C++
109 lines
3.3 KiB
C++
|
#include <unistd.h>
|
||
|
#include <stdint.h>
|
||
|
#include <stdio.h>
|
||
|
#include <libusb-1.0/libusb.h>
|
||
|
|
||
|
static void print_devs(libusb_device **devs)
|
||
|
{
|
||
|
libusb_device *dev;
|
||
|
int i = 0;
|
||
|
|
||
|
while ((dev = devs[i++]) != NULL) {
|
||
|
struct libusb_device_descriptor desc;
|
||
|
int r = libusb_get_device_descriptor(dev, &desc);
|
||
|
if (r < 0) {
|
||
|
fprintf(stderr, "failed to get device descriptor");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
printf("%04x:%04x (bus %d, device %d)\n",
|
||
|
desc.idVendor, desc.idProduct,
|
||
|
libusb_get_bus_number(dev), libusb_get_device_address(dev));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int r;
|
||
|
ssize_t cnt;
|
||
|
libusb_device_handle *dev_handle; //a device handle
|
||
|
libusb_device **devs; //devices
|
||
|
//libusb_context **ctx=NULL;
|
||
|
r=libusb_init(NULL); //init 初始化libusb
|
||
|
if(r<0) {
|
||
|
printf("failed to init libusb\n");
|
||
|
return 1;
|
||
|
}
|
||
|
cnt = libusb_get_device_list(NULL,&devs); //获取设备列表
|
||
|
if (cnt < 0) {
|
||
|
printf("failed to get device list\n");
|
||
|
return 1;
|
||
|
}
|
||
|
//print_devs(devs);
|
||
|
dev_handle = libusb_open_device_with_vid_pid(NULL, 0x2833, 0x0002);
|
||
|
if(dev_handle == NULL){
|
||
|
printf("Cannot open device\n");
|
||
|
return 1;
|
||
|
}else
|
||
|
printf("Device Opened\n");
|
||
|
|
||
|
libusb_free_device_list(devs, 1); //free the list, unref the devices in it
|
||
|
|
||
|
if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached
|
||
|
printf("Kernel Driver Active\n");
|
||
|
if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
|
||
|
printf("Kernel Driver Detached!\n");
|
||
|
}
|
||
|
r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1)
|
||
|
if(r < 0) {
|
||
|
printf("Cannot Claim Interface\n");
|
||
|
return 1;
|
||
|
}
|
||
|
printf("Claimed Interface\n");
|
||
|
sleep(1);
|
||
|
unsigned char data[2];
|
||
|
|
||
|
unsigned char tmp_char[64];
|
||
|
data[0]=0x02;data[1]=0x64;
|
||
|
int transferred;
|
||
|
int actual; //used to find out how many bytes were written
|
||
|
while(1){
|
||
|
//r = libusb_interrupt_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_OUT), data, 2, &actual, 0); //my device's out endpoint was 1, found with trial- the device had 2 endpoints: 2 and 129
|
||
|
//if(r == 0 && actual == 2) //we wrote the 2 bytes successfully
|
||
|
//printf("Writing Successful\n");
|
||
|
//else
|
||
|
// printf("Write Error\n");
|
||
|
|
||
|
//r = libusb_interrupt_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_IN),tmp_char,64,&actual, 1000);//pay attion
|
||
|
|
||
|
r = libusb_bulk_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_IN), tmp_char, 64, &transferred,1000);
|
||
|
if(r == 0 && actual == 64) //we read the 64 bytes successfully
|
||
|
printf("Read Successful\n");
|
||
|
else
|
||
|
printf("Read Error\n");
|
||
|
printf("%i,%i\n",r,actual);
|
||
|
|
||
|
int16_t mx = *(int16_t *)(tmp_char+56);
|
||
|
int16_t my = *(int16_t *)(tmp_char+58);
|
||
|
int16_t mz = *(int16_t *)(tmp_char+60);
|
||
|
printf("mag:%d,%d,%d\n",mx,my,mz);
|
||
|
usleep(1000*500);
|
||
|
//printf("%s","\033[1H\033[2J");//clear display
|
||
|
|
||
|
}
|
||
|
|
||
|
r = libusb_release_interface(dev_handle, 0); //release the claimed interface
|
||
|
if(r!=0) {
|
||
|
printf("Cannot Release Interface\n");
|
||
|
return 1;
|
||
|
}
|
||
|
printf("Released Interface\n");
|
||
|
|
||
|
libusb_close(dev_handle); //close the device we opened
|
||
|
libusb_exit(NULL); //needs to be called to end the
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|