src/cadMousePro.cpp
branchv_0
changeset 1 29cbe171cd43
parent 0 6ff501639c23
child 2 abeba77ec581
equal deleted inserted replaced
0:6ff501639c23 1:29cbe171cd43
     1 #include <iostream>
     1 #include <iostream>
       
     2 #include <array>
       
     3 #include <vector>
     2 
     4 
     3 #include <hidapi/hidapi.h>
     5 #include <hidapi/hidapi.h>
     4 
     6 
     5 class HIDException {
     7 class HIDException {
     6 private:
     8 private:
    10 	HIDException(std::wstring message) : message(message) {
    12 	HIDException(std::wstring message) : message(message) {
    11 	}
    13 	}
    12 
    14 
    13 	virtual ~HIDException() {
    15 	virtual ~HIDException() {
    14 	}
    16 	}
    15 	
    17 
    16 	const std::wstring getMessage() const {
    18 	const std::wstring getMessage() const {
    17 		return message;
    19 		return message;
    18 	}
    20 	}
    19 
    21 
    20 };
    22 };
    23 private:
    25 private:
    24 	hid_device* handle;
    26 	hid_device* handle;
    25 public:
    27 public:
    26 
    28 
    27 	HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
    29 	HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
       
    30 		int initError = hid_init(); // TODO: move to HIDContext class?
       
    31 		if (initError) throw HIDException(L"Unable to init HID API.");
    28 		handle = hid_open(vendorId, productId, serialNumber);
    32 		handle = hid_open(vendorId, productId, serialNumber);
    29 		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?");
    33 		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?");
    30 	}
    34 	}
    31 
    35 
    32 	virtual ~HIDDevice() {
    36 	virtual ~HIDDevice() {
    33 		hid_close(handle);
    37 		hid_close(handle);
       
    38 		hid_exit(); // TODO: move to HIDContext class?
    34 	}
    39 	}
    35 
    40 
       
    41 	const std::wstring getManufacturerName() const {
       
    42 		std::array<wchar_t, 200 > buffer;
       
    43 		int error = hid_get_manufacturer_string(handle, buffer.data(), buffer.size());
       
    44 		if (error) throw HIDException(L"Unable to get manufacturer name.");
       
    45 		return buffer.data();
       
    46 	}
       
    47 
       
    48 	const std::wstring getProductName() const {
       
    49 		std::array<wchar_t, 200 > buffer;
       
    50 		int error = hid_get_product_string(handle, buffer.data(), buffer.size());
       
    51 		if (error) throw HIDException(L"Unable to get product name.");
       
    52 		return buffer.data();
       
    53 	}
       
    54 
       
    55 	const std::wstring getSerialNumber() const {
       
    56 		std::array<wchar_t, 200 > buffer;
       
    57 		int error = hid_get_serial_number_string(handle, buffer.data(), buffer.size());
       
    58 		if (error) throw HIDException(L"Unable to get serial number.");
       
    59 		return buffer.data();
       
    60 	}
       
    61 
       
    62 	void sendFeatureReport(std::vector<unsigned char> data) {
       
    63 		int written = hid_send_feature_report(handle, data.data(), data.size());
       
    64 		if (written < 0) throw HIDException(L"Unable to send feature report.");
       
    65 	}
    36 
    66 
    37 };
    67 };
    38 
    68 
    39 int main(int argc, char** argv) {
    69 int main(int argc, char** argv) {
    40 	try {
    70 	try {
    41 
    71 
    42 		std::wcout << L"cadMousePro" << std::endl;
    72 		std::wcout << L"cadMousePro" << std::endl;
    43 		HIDDevice mouse(0x256f, 0xc652, nullptr);
    73 		HIDDevice mouse(0x256f, 0xc652, nullptr);
    44 		std::wcout << L"mouse opened" << std::endl;
    74 		std::wcout << L"mouse opened" << std::endl;
       
    75 		std::wcout << L"manufacturer:  " << mouse.getManufacturerName() << std::endl;
       
    76 		std::wcout << L"product:       " << mouse.getProductName() << std::endl;
       
    77 		// std::wcout << L"serial number: " << mouse.getSerialNumber() << std::endl; // throws exception
       
    78 
       
    79 		std::vector<unsigned char> enableFreeWheel{0x10, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0a, 0x0b, 0x0c, 0x0c, 0x0e, 0x0d, 0x2f, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01};
       
    80 		std::vector<unsigned char> disableFreeWheel{0x10, 0x00, 0x1c, 0x00, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0a, 0x0b, 0x0c, 0x0c, 0x0e, 0x0d, 0x2f, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01};
       
    81 		// mouse.sendFeatureReport(enableFreeWheel);
       
    82 		mouse.sendFeatureReport(disableFreeWheel);
       
    83 
       
    84 		// TODO: enable acceleration
       
    85 		// TODO: enable lift-off detection
       
    86 		// TODO: sensof polling rate – frequency
       
    87 		// TODO: wheel scrolling size – lines, page
       
    88 
    45 	} catch (const HIDException& e) {
    89 	} catch (const HIDException& e) {
    46 		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
    90 		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
    47 	
    91 
    48 	}
    92 	}
    49 }
    93 }