MacroBinds/qmacrokeysmanager.h
2024-09-17 04:05:32 -04:00

98 lines
2.2 KiB
C++

#ifndef QMACROKEYSMANAGER_H
#define QMACROKEYSMANAGER_H
#include <QObject>
#include <QMap>
#include <QThread>
#include <QTimer>
#include "hidapi.h"
#define MACROKEYS_VID 0x2341
#define MACROKEYS_PID 0x8036
#define NUM_HID_BYTES 5
#define NUM_BUTTON_ROWS 8
#define NUM_BUTTON_COLUMNS 4
#define ENC1_CCW 0b10000000
#define ENC1_CW 0b01000000
#define ENC2_CCW 0b00100000
#define ENC2_CW 0b00010000
#define ENC3_CCW 0b00001000
#define ENC3_CW 0b00000100
#define ENC4_CCW 0b00000010
#define ENC4_CW 0b00000001
enum class EncoderRotation : qint8 {
CCW = -1,
INVALID,
CW
};
Q_DECLARE_METATYPE(EncoderRotation)
struct HIDDevice
{
hid_device *hidDevice = nullptr;
QString sManufacturer;
QString sProduct;
QString sSerialNumber;
QString sPath;
const hid_device_info *hidInfo;
};
typedef QVector<bool> ButtonStateRow;
typedef QVector<ButtonStateRow> ButtonStateVector;
class QMacroKeysManager : public QObject
{
Q_OBJECT
public:
explicit QMacroKeysManager(QObject *parent = nullptr);
void initDevices();
void uninitDevices();
signals:
void newDeviceFound(const QString &devicePath);
void newButtonState(const quint8 &row, const quint8 &column, const bool &state);
void newEncoderRotation(const quint8 &column, const EncoderRotation &direction);
private slots:
void getDeviceInitStatus(const HIDDevice &device);
void handleNewButtonState(const quint8 &row, const quint8 &column, const bool &state);
void handleNewEncoderRotation(const quint8 &column, const EncoderRotation &direction);
private:
QMap<QString, class QDeviceHandler*> mDevices;
};
class QDeviceHandler : public QObject
{
Q_OBJECT
public:
explicit QDeviceHandler(QObject *parent = nullptr);
~QDeviceHandler();
void initDevice(const hid_device_info *di);
void runDevice();
public slots:
void buttonReadLoop();
signals:
void deviceInitialised(const HIDDevice &device);
void buttonStateChanged(const quint8 &row, const quint8 &column, const bool &state);
void encoderRotationChanged(const quint8 &column, const EncoderRotation &direction = EncoderRotation::INVALID);
private:
QThread threadButtonReader;
HIDDevice hidDevice;
ButtonStateVector vCurrentButtonStates;
};
#endif // QMACROKEYSMANAGER_H