src/cadMousePro.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 18 Aug 2019 00:10:30 +0200
branchv_0
changeset 3 1197b42e8b2e
parent 2 abeba77ec581
child 4 405aa9de65d2
permissions -rw-r--r--
use a smart pointer with custom deleter inside HIDDevice, so instances can be copied

#include <iostream>
#include <array>
#include <vector>

#include <hidapi/hidapi.h>
#include <memory>

class HIDException {
private:
	std::wstring message;
public:

	HIDException(std::wstring message) : message(message) {
	}

	virtual ~HIDException() {
	}

	const std::wstring getMessage() const {
		return message;
	}

};

class HIDDevice {
private:
	std::shared_ptr<hid_device> handle;

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.reset(hid_open(vendorId, productId, serialNumber), hid_close);
		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?");
	}

	virtual ~HIDDevice() {
		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.get(), 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.get(), 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.get(), 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.get(), data.data(), data.size());
		if (written < 0) throw HIDException(L"Unable to send feature report.");
	}

};

int main(int argc, char** argv) {
	try {

		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;

	}
}