From c4aec144256e3f27fedd8f8de03e10cc08eecab8 Mon Sep 17 00:00:00 2001 From: Mateusz Zalega Date: Thu, 22 Oct 2015 22:41:18 +0200 Subject: Initial --- device.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 device.cc (limited to 'device.cc') diff --git a/device.cc b/device.cc new file mode 100644 index 0000000..c954cb7 --- /dev/null +++ b/device.cc @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include "device.h" + +using namespace device; + +Device::Device() +: m_vid(0), m_pid(0), + m_retry_count(4), + m_retry_timeout(50), + mp_devhandle(NULL) {} + +bool Device::connect() { + hid_init(); + + mp_devhandle = hid_open(m_vid, m_pid, NULL); + return mp_devhandle != NULL; +} + +CommError Device::send(const void *packet) { + return (CommError)( + hid_send_feature_report(mp_devhandle, (const unsigned char *)(packet), + HID_REPORT_SIZE)); +} + +CommError Device::recv(void *packet) { + CommError status; + int retry_count = 0; + + for(;;) { + status = (CommError)( + hid_get_feature_report(mp_devhandle, (unsigned char *)(packet), + HID_REPORT_SIZE)); + if (status == CommError::ERR_NO_ERROR || retry_count++ >= m_retry_count) + break; + std::this_thread::sleep_for(m_retry_timeout); + } + + return status; +} + +Stick10::Stick10() { + m_vid = 0x20a0; + m_pid = 0x4108; +} + +Stick20::Stick20() { + m_vid = 0x20a0; + m_pid = 0x4109; +} -- cgit v1.2.1