wfview/usbcontroller.h

253 wiersze
6.7 KiB
C
Czysty Zwykły widok Historia

2022-04-22 10:11:21 +00:00
#ifndef usbController_H
#define usbController_H
#include <iostream>
#include <QThread>
#include <QCoreApplication>
#include <QTimer>
#include <QDateTime>
#include <QRect>
#include <QGraphicsTextItem>
#include <QColor>
2022-04-25 16:40:41 +00:00
#include <QVector>
2023-02-10 09:52:51 +00:00
#include <QList>
2023-03-17 23:35:40 +00:00
#include <QMap>
2023-02-10 15:23:57 +00:00
#include <QMutex>
2023-02-12 16:45:21 +00:00
#include <QIODevice>
#include <QtEndian>
2023-03-17 23:35:40 +00:00
#include <QUuid>
#include <QLabel>
2023-01-12 19:39:35 +00:00
#if defined(USB_CONTROLLER) && QT_VERSION < QT_VERSION_CHECK(6,0,0)
2022-10-22 19:55:08 +00:00
#include <QGamepad>
2023-01-12 19:39:35 +00:00
#endif
2022-04-22 10:11:21 +00:00
#if defined(USB_CONTROLLER)
#ifndef Q_OS_WIN
#include "hidapi/hidapi.h"
#else
#include "hidapi.h"
#endif
#ifdef HID_API_VERSION_MAJOR
#ifndef HID_API_MAKE_VERSION
#define HID_API_MAKE_VERSION(mj, mn, p) (((mj) << 24) | ((mn) << 8) | (p))
#endif
#ifndef HID_API_VERSION
#define HID_API_VERSION HID_API_MAKE_VERSION(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH)
#endif
#if defined(__APPLE__) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
#include <hidapi/hidapi_darwin.h>
#endif
#if defined(USING_HIDAPI_LIBUSB) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
#include <hidapi_libusb.h>
#endif
#endif
#endif
2022-04-22 10:11:21 +00:00
#ifndef Q_OS_WIN
#include <unistd.h>
#endif
// Include these so we have the various enums
#include "rigidentities.h"
using namespace std;
#define HIDDATALENGTH 64
#define MAX_STR 255
2023-03-17 23:35:40 +00:00
struct USBDEVICE {
usbDeviceType usbDevice = usbNone;
bool remove = false;
2023-03-17 23:35:40 +00:00
bool connected = false;
bool uiCreated = false;
bool disabled = false;
2023-03-17 23:35:40 +00:00
hid_device* handle = NULL;
QString product = "";
QString manufacturer = "";
QString serial = "<none>";
QString deviceId = "";
QString path = "";
quint16 vendorId = 0;
quint16 productId = 0;
int sensitivity = 1;
unsigned char jogpos=0;
unsigned char shutpos=0;
unsigned char shutMult = 0;
int jogCounter = 0;
quint32 buttons = 0;
quint32 knobs = 0;
QList<int> knobValues;
QList<int> knobPrevious;
2023-03-17 23:35:40 +00:00
QList<quint8> knobSend;
QTime lastusbController = QTime::currentTime();
QByteArray lastData = QByteArray(8,0x0);
unsigned char lastDialPos=0;
QUuid uuid;
2023-03-20 15:32:04 +00:00
QLabel *message;
2023-03-17 23:35:40 +00:00
};
2022-04-22 10:11:21 +00:00
struct COMMAND {
2022-04-26 07:41:58 +00:00
COMMAND() {}
2023-02-09 13:21:51 +00:00
COMMAND(int index, QString text, usbCommandType cmdType, int command, unsigned char suffix) :
index(index), text(text), cmdType(cmdType), command(command), suffix(suffix) {}
COMMAND(int index, QString text, usbCommandType cmdType, int command, availableBands band) :
index(index), text(text), cmdType(cmdType), command(command), band(band) {}
COMMAND(int index, QString text, usbCommandType cmdType, int command, mode_kind mode) :
index(index), text(text), cmdType(cmdType), command(command), mode(mode) {}
2022-04-22 10:11:21 +00:00
2023-01-30 12:01:29 +00:00
int index=0;
2022-04-26 07:41:58 +00:00
QString text;
2023-02-12 17:14:26 +00:00
usbCommandType cmdType = commandButton;
2023-01-30 12:01:29 +00:00
int command=0;
unsigned char suffix=0x0;
2023-03-17 23:35:40 +00:00
int value=0;
2023-01-30 12:01:29 +00:00
availableBands band=bandGen;
mode_kind mode=modeLSB;
2022-04-22 10:11:21 +00:00
};
struct BUTTON {
2022-04-26 07:41:58 +00:00
BUTTON() {}
2023-02-09 13:21:51 +00:00
BUTTON(usbDeviceType dev, int num, QRect pos, const QColor textColour, COMMAND* on, COMMAND* off) :
2022-10-23 17:56:31 +00:00
dev(dev), num(num), name(""), pos(pos), textColour(textColour), onCommand(on), offCommand(off) {}
2023-02-09 13:21:51 +00:00
BUTTON(usbDeviceType dev, QString name, QRect pos, const QColor textColour, COMMAND* on, COMMAND* off) :
2022-10-23 17:56:31 +00:00
dev(dev), num(-1), name(name), pos(pos), textColour(textColour), onCommand(on), offCommand(off) {}
2022-04-22 10:11:21 +00:00
2023-02-09 13:21:51 +00:00
usbDeviceType dev;
2022-09-18 20:00:44 +00:00
int num;
2022-10-23 17:54:18 +00:00
QString name;
2022-04-22 10:11:21 +00:00
QRect pos;
2022-09-18 20:00:44 +00:00
QColor textColour;
2023-02-09 13:21:51 +00:00
const COMMAND* onCommand = Q_NULLPTR;
const COMMAND* offCommand = Q_NULLPTR;
2022-04-25 16:40:41 +00:00
QGraphicsTextItem* onText;
QGraphicsTextItem* offText;
2023-03-17 23:35:40 +00:00
QString on;
QString off;
QString devicePath;
2022-04-22 10:11:21 +00:00
};
2023-02-09 13:21:51 +00:00
struct KNOB {
KNOB() {}
KNOB(usbDeviceType dev, int num, QRect pos, const QColor textColour, COMMAND* command) :
dev(dev), num(num), name(""), pos(pos), textColour(textColour), command(command) {}
usbDeviceType dev;
int num;
QString name;
QRect pos;
QColor textColour;
const COMMAND* command = Q_NULLPTR;
QGraphicsTextItem* text;
2023-03-17 23:35:40 +00:00
QString cmd;
QString devicePath;
};
2023-02-09 13:21:51 +00:00
2023-03-17 23:35:40 +00:00
struct CONTROLLER {
bool disabled=false;
2023-03-17 23:35:40 +00:00
int sensitivity=1;
quint8 speed=2;
quint8 timeout=30;
quint8 brightness=2;
quint8 orientation=0;
QColor color=Qt::white;
2023-02-09 13:21:51 +00:00
};
2023-03-17 23:35:40 +00:00
typedef QMap<QString,CONTROLLER> usbMap;
2023-02-09 13:21:51 +00:00
#if defined(USB_CONTROLLER)
2022-04-22 10:11:21 +00:00
class usbController : public QObject
{
Q_OBJECT
public:
usbController();
~usbController();
public slots:
2023-03-17 23:35:40 +00:00
void init(QMutex* mut,usbMap* prefs ,QVector<BUTTON>* buts,QVector<KNOB>* knobs);
2022-04-22 10:11:21 +00:00
void run();
void runTimer();
void ledControl(bool on, unsigned char num);
2023-02-12 16:45:21 +00:00
void receivePTTStatus(bool on);
2023-02-02 00:03:13 +00:00
void getVersion();
2023-03-17 23:35:40 +00:00
void programButton(QString path, quint8 val, QString text);
void programSensitivity(QString path, quint8 val);
void programBrightness(QString path, quint8 val);
void programOrientation(QString path, quint8 val);
void programSpeed(QString path, quint8 val);
void programWheelColour(QString path, quint8 r, quint8 g, quint8 b);
void programOverlay(QString path, quint8 duration, QString text);
void programTimeout(QString path, quint8 val);
void programDisable(QString path, bool disabled);
2022-04-22 10:11:21 +00:00
signals:
void jogPlus();
void jogMinus();
void sendJog(int counter);
void doShuttle(bool plus, quint8 level);
void setBand(int band);
2022-04-27 11:56:54 +00:00
void button(const COMMAND* cmd);
2023-03-17 23:35:40 +00:00
void newDevice(USBDEVICE* dev, CONTROLLER* cntrl, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd, QMutex* mut);
void removeDevice(USBDEVICE* dev);
2022-05-17 07:53:24 +00:00
2022-04-22 10:11:21 +00:00
private:
2023-03-17 23:35:40 +00:00
void loadButtons();
void loadKnobs();
void loadCommands();
int hidStatus = 1;
2022-04-22 10:11:21 +00:00
bool isOpen=false;
2023-03-17 23:35:40 +00:00
int devicesConnected=0;
2022-09-18 20:00:44 +00:00
QVector<BUTTON>* buttonList;
2023-02-09 13:21:51 +00:00
QVector<KNOB>* knobList;
2023-03-17 23:35:40 +00:00
QList<QVector<KNOB>*> deviceKnobs;
QList<QVector<BUTTON>*> deviceButtons;
QVector<BUTTON> defaultButtons;
QVector<KNOB> defaultKnobs;
QVector<COMMAND> commands;
QMap<QString,USBDEVICE> usbDevices;
usbMap *controllers;
2023-01-12 19:39:35 +00:00
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
2022-10-22 19:55:08 +00:00
QGamepad* gamepad=Q_NULLPTR;
2023-01-12 19:39:35 +00:00
#endif
2022-10-23 17:54:18 +00:00
void buttonState(QString but, bool val);
2022-10-23 18:08:31 +00:00
void buttonState(QString but, double val);
2023-03-17 23:35:40 +00:00
QColor currentColour;
QMutex* mutex=Q_NULLPTR;
COMMAND sendCommand;
2023-02-12 16:45:21 +00:00
unsigned short knownUsbDevices[6][5] = {
2023-02-20 22:19:55 +00:00
{shuttleXpress,0x0b33,0x0020,0x0000,0x0000},
{shuttlePro2,0x0b33,0x0030,0x0000,0x0000},
2023-03-17 23:35:40 +00:00
{RC28,0x0c26,0x001e,0x0000,0x0000},
2023-02-12 16:45:21 +00:00
{eCoderPlus, 0x1fc9, 0x0003,0x0000,0x0000},
{QuickKeys, 0x28bd, 0x5202,0x0001,0xff0a},
{QuickKeys, 0x28bd, 0x5203,0x0001,0xff0a}
2023-02-07 20:32:46 +00:00
};
2022-04-22 10:11:21 +00:00
protected:
};
2023-03-17 23:35:40 +00:00
class usbControllerDev : public QObject
{
Q_OBJECT
};
#endif
2022-04-22 10:11:21 +00:00
#endif