Skip to content

Commit

Permalink
use hidlib instead of usblib
Browse files Browse the repository at this point in the history
  • Loading branch information
mplatov committed Mar 3, 2021
1 parent 4e330d5 commit f20e587
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ compile_commands.json
CTestTestfile.cmake
.idea
cmake-build-debug
build
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake_modules/)

add_library(b6 STATIC Device.cc Device.hh Packet.cc Packet.hh Error.hh Enum.hh Device.cc Packet.cc)

find_package(libusb-1.0)
if (LIBUSB_1_FOUND)
include_directories(${LIBUSB_1_INCLUDE_DIRS})
target_link_libraries(b6 ${LIBUSB_1_LIBRARIES})
endif(LIBUSB_1_FOUND)
find_package(hidapi REQUIRED)
if (HIDAPI_FOUND)
include_directories(${HIDAPI_INCLUDE_DIRS})
target_link_libraries(b6 ${HIDAPI_LIBRARIES})
endif(HIDAPI_FOUND)

install(TARGETS b6 DESTINATION lib)
install(FILES Device.hh Packet.hh Error.hh Enum.hh DESTINATION include/b6)
30 changes: 7 additions & 23 deletions Device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,23 @@

namespace b6 {
Device::Device() {
int err = libusb_init(&m_libusbCtx);
int err = hid_init();
if (err != 0) {
throw std::runtime_error("libusb err: " + std::to_string(err));
throw std::runtime_error("hid_init err: " + std::to_string(err));
}
m_dev = libusb_open_device_with_vid_pid(m_libusbCtx, B6_VENDOR_ID, B6_PRODUCT_ID);
m_dev = hid_open(B6_VENDOR_ID, B6_PRODUCT_ID, NULL);
if (m_dev == nullptr) {
throw std::runtime_error("cannot find/open b6 device");
}

if (libusb_kernel_driver_active(m_dev, 0) == 1) {
m_hadKernelDriver = true;
err = libusb_detach_kernel_driver(m_dev, 0);
if (err != 0) {
throw std::runtime_error("cannot detach kernel driver, err: " + std::to_string(err));
}
}
err = libusb_claim_interface(m_dev, 0);
if (err != 0) {
throw std::runtime_error("cannot claim interface 0, err: " + std::to_string(err));
}

m_getDevInfo();
}

Device::~Device() {
if (m_dev != nullptr) {
libusb_release_interface(m_dev, 0);
if (m_hadKernelDriver) {
libusb_attach_kernel_driver(m_dev, 0);
}
libusb_close(m_dev);
hid_close(m_dev);
}
libusb_exit(m_libusbCtx);
hid_exit();
}

SysInfo Device::getSysInfo() {
Expand Down Expand Up @@ -109,13 +93,13 @@ namespace b6 {
Packet Device::m_read() {
std::vector<uint8_t> buf(64);
int len = 0;
libusb_interrupt_transfer(m_dev, 0x81, &buf[0], 64, &len, 200);
hid_read(m_dev, &buf[0], 64);
return Packet(buf);
}

void Device::m_write(Packet packet) {
int len = 0;
libusb_interrupt_transfer(m_dev, 0x01, packet.getBuffer(), packet.getSize(), &len, 200);
hid_write(m_dev, packet.getBuffer(), packet.getSize());
}

Packet Device::m_sendCommand(CMD cmd) {
Expand Down
5 changes: 2 additions & 3 deletions Device.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <string>
#include <vector>
#include <libusb.h>
#include "hidapi.h"
#include <stdexcept>
#include "Packet.hh"
#include "Enum.hh"
Expand Down Expand Up @@ -80,8 +80,7 @@ namespace b6 {
static bool isBatteryLi(BATTERY_TYPE type) { return type >= BATTERY_TYPE::LIPO && type <= BATTERY_TYPE::LIHV; };
static bool isBatteryNi(BATTERY_TYPE type) { return type == BATTERY_TYPE::NIMH || type == BATTERY_TYPE::NICD; };
private:
libusb_context *m_libusbCtx{};
libusb_device_handle *m_dev;
hid_device *m_dev;
bool m_hadKernelDriver = false;

int m_cellCount = 6;
Expand Down

0 comments on commit f20e587

Please sign in to comment.