first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 14 Aug 2019 18:32:35 +0200
branchv_0
changeset 1 29cbe171cd43
parent 0 6ff501639c23
child 2 abeba77ec581
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
src/cadMousePro.cpp
--- a/src/cadMousePro.cpp	Wed Aug 14 16:16:58 2019 +0200
+++ b/src/cadMousePro.cpp	Wed Aug 14 18:32:35 2019 +0200
@@ -1,4 +1,6 @@
 #include <iostream>
+#include <array>
+#include <vector>
 
 #include <hidapi/hidapi.h>
 
@@ -12,7 +14,7 @@
 
 	virtual ~HIDException() {
 	}
-	
+
 	const std::wstring getMessage() const {
 		return message;
 	}
@@ -25,14 +27,42 @@
 public:
 
 	HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
+		int initError = hid_init(); // TODO: move to HIDContext class?
+		if (initError) throw HIDException(L"Unable to init HID API.");
 		handle = hid_open(vendorId, productId, serialNumber);
 		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?");
 	}
 
 	virtual ~HIDDevice() {
 		hid_close(handle);
+		hid_exit(); // TODO: move to HIDContext class?
+	}
+
+	const std::wstring getManufacturerName() const {
+		std::array<wchar_t, 200 > buffer;
+		int error = hid_get_manufacturer_string(handle, buffer.data(), buffer.size());
+		if (error) throw HIDException(L"Unable to get manufacturer name.");
+		return buffer.data();
 	}
 
+	const std::wstring getProductName() const {
+		std::array<wchar_t, 200 > buffer;
+		int error = hid_get_product_string(handle, buffer.data(), buffer.size());
+		if (error) throw HIDException(L"Unable to get product name.");
+		return buffer.data();
+	}
+
+	const std::wstring getSerialNumber() const {
+		std::array<wchar_t, 200 > buffer;
+		int error = hid_get_serial_number_string(handle, buffer.data(), buffer.size());
+		if (error) throw HIDException(L"Unable to get serial number.");
+		return buffer.data();
+	}
+
+	void sendFeatureReport(std::vector<unsigned char> data) {
+		int written = hid_send_feature_report(handle, data.data(), data.size());
+		if (written < 0) throw HIDException(L"Unable to send feature report.");
+	}
 
 };
 
@@ -42,8 +72,22 @@
 		std::wcout << L"cadMousePro" << std::endl;
 		HIDDevice mouse(0x256f, 0xc652, nullptr);
 		std::wcout << L"mouse opened" << std::endl;
+		std::wcout << L"manufacturer:  " << mouse.getManufacturerName() << std::endl;
+		std::wcout << L"product:       " << mouse.getProductName() << std::endl;
+		// std::wcout << L"serial number: " << mouse.getSerialNumber() << std::endl; // throws exception
+
+		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};
+		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};
+		// mouse.sendFeatureReport(enableFreeWheel);
+		mouse.sendFeatureReport(disableFreeWheel);
+
+		// TODO: enable acceleration
+		// TODO: enable lift-off detection
+		// TODO: sensof polling rate – frequency
+		// TODO: wheel scrolling size – lines, page
+
 	} catch (const HIDException& e) {
 		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
-	
+
 	}
 }
\ No newline at end of file