wfview/usbcontroller.h

302 wiersze
8.3 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 <QSpinBox>
2022-04-22 10:11:21 +00:00
#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>
#include <QImage>
#include <QPainter>
#include <QImageWriter>
#include <QBuffer>
2023-03-29 22:33:18 +00:00
#include <QSettings>
#include <QMessageBox>
2023-04-13 14:28:46 +00:00
#include <memory>
2023-03-29 22:33:18 +00:00
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
2023-03-28 18:07:54 +00:00
#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
struct USBTYPE {
USBTYPE() {}
2023-04-15 19:13:20 +00:00
USBTYPE(usbDeviceType model,quint32 manufacturerId, quint32 productId , quint32 usage, quint32 usagePage, int buttons, int knobs, int leds, int maxPayload, int iconSize) :
model(model), manufacturerId(manufacturerId), productId(productId), usage(usage), usagePage(usagePage), buttons(buttons), knobs(knobs), leds(leds), maxPayload(maxPayload), iconSize(iconSize) {}
usbDeviceType model = usbNone;
quint32 manufacturerId=0;
quint32 productId=0;
quint32 usage=0;
quint32 usagePage=0;
int buttons=0;
int knobs=0;
2023-04-15 19:13:20 +00:00
int leds=0;
int maxPayload=0;
int iconSize=0;
};
2023-03-28 18:07:54 +00:00
struct KNOBVALUE {
int value=0;
int previous=0;
quint8 send=0;
qint64 lastChanged=0;
QString name="";
};
2023-03-17 23:35:40 +00:00
struct USBDEVICE {
USBDEVICE() {}
USBDEVICE(USBTYPE type) : type(type) {}
USBTYPE type;
bool detected = false;
bool remove = false;
2023-03-17 23:35:40 +00:00
bool connected = false;
bool uiCreated = false;
bool disabled = false;
quint8 speed=2;
quint8 timeout=30;
quint8 brightness=2;
quint8 orientation=0;
QColor color=Qt::darkGray;
cmds lcd=cmdNone;
2023-03-17 23:35:40 +00:00
hid_device* handle = NULL;
QString product = "";
QString manufacturer = "";
QString serial = "<none>";
QString deviceId = "";
QString path = "";
int sensitivity = 1;
unsigned char jogpos=0;
unsigned char shutpos=0;
unsigned char shutMult = 0;
int jogCounter = 0;
quint32 buttons = 0;
quint32 knobs = 0;
2023-03-28 18:07:54 +00:00
QList<KNOBVALUE> knobValues;
2023-04-15 19:13:20 +00:00
2023-03-17 23:35:40 +00:00
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;
int pages=1;
int currentPage=0;
QGraphicsScene* scene = Q_NULLPTR;
QSpinBox* pageSpin = Q_NULLPTR;
QImage image;
2023-04-15 10:25:35 +00:00
quint8 ledStatus=0x07;
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() {}
COMMAND(int index, QString text, usbCommandType cmdType, int command, int value) :
index(index), text(text), cmdType(cmdType), command(command), value(value) {}
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) {}
2023-03-27 11:38:01 +00:00
COMMAND(int index, QString text, usbCommandType cmdType, int command, int getCommand, unsigned char suffix) :
index(index), text(text), cmdType(cmdType), command(command), getCommand(getCommand), suffix(suffix) {}
2023-02-09 13:21:51 +00:00
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;
2023-03-27 11:38:01 +00:00
int getCommand=0;
2023-01-30 12:01:29 +00:00
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() {}
BUTTON(usbDeviceType dev, int num, QRect pos, const QColor textColour, COMMAND* on, COMMAND* off, bool graphics=false, int led=0) :
2023-04-15 19:13:20 +00:00
dev(dev), num(num), name(""), pos(pos), textColour(textColour), onCommand(on), offCommand(off), on(onCommand->text), off(offCommand->text), graphics(graphics), led(led){}
2023-04-09 22:53:51 +00:00
BUTTON(usbDeviceType dev, QString name, QRect pos, const QColor textColour, COMMAND* on, COMMAND* off) :
dev(dev), num(-1), name(name), pos(pos), textColour(textColour), onCommand(on), offCommand(off), on(onCommand->text), off(offCommand->text) {}
2022-04-22 10:11:21 +00:00
2023-02-09 13:21:51 +00:00
usbDeviceType dev;
USBDEVICE* parent = Q_NULLPTR;
int page=1;
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;
2023-04-09 22:53:51 +00:00
QGraphicsRectItem* bgRect = Q_NULLPTR;
QGraphicsTextItem* text = Q_NULLPTR;
2023-03-17 23:35:40 +00:00
QString on;
QString off;
QString path;
QColor backgroundOn = Qt::lightGray;
QColor backgroundOff = Qt::blue;
2023-03-28 18:07:54 +00:00
QString iconName = "";
QImage* icon = Q_NULLPTR;
bool toggle = false;
bool isOn = false;
2023-04-09 22:53:51 +00:00
bool graphics = false;
int led = 0;
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), cmd(command->text) {}
2023-02-09 13:21:51 +00:00
usbDeviceType dev;
USBDEVICE* parent = Q_NULLPTR;
int page=1;
2023-02-09 13:21:51 +00:00
int num;
QString name;
QRect pos;
QColor textColour;
const COMMAND* command = Q_NULLPTR;
QGraphicsTextItem* text = Q_NULLPTR;
2023-03-17 23:35:40 +00:00
QString cmd;
QString path;
2023-03-17 23:35:40 +00:00
};
2023-02-09 13:21:51 +00:00
typedef QMap<QString,USBDEVICE> usbDevMap;
2023-03-17 23:35:40 +00:00
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();
2023-03-28 18:07:54 +00:00
bool hotPlugEvent(const QByteArray & eventType, void * message, long * result);
2022-04-22 10:11:21 +00:00
public slots:
void init(QMutex* mut,usbDevMap* prefs ,QVector<BUTTON>* buts,QVector<KNOB>* knobs);
2022-04-22 10:11:21 +00:00
void run();
void runTimer();
2023-02-12 16:45:21 +00:00
void receivePTTStatus(bool on);
2023-03-27 11:38:01 +00:00
void receiveLevel(cmds cmd, unsigned char level);
void programPages(USBDEVICE* dev, int pages);
void programDisable(USBDEVICE* dev, bool disabled);
2023-04-09 22:53:51 +00:00
void sendRequest(USBDEVICE *dev, usbFeatureType feature, int val=0, QString text="", QImage* img=Q_NULLPTR, QColor* color=Q_NULLPTR);
void sendToLCD(QImage *img);
2023-04-09 22:53:51 +00:00
void backupController(USBDEVICE* dev, QString file);
void restoreController(USBDEVICE* dev, QString file);
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);
void initUI(usbDevMap* devs, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd, QMutex* mut);
void newDevice(USBDEVICE* dev);
2023-03-17 23:35:40 +00:00
void removeDevice(USBDEVICE* dev);
void setConnected(USBDEVICE* dev);
void changePage(USBDEVICE* dev, int page);
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
QVector<BUTTON> defaultButtons;
QVector<KNOB> defaultKnobs;
QVector<USBTYPE> knownDevices;
2023-03-17 23:35:40 +00:00
QVector<COMMAND> commands;
2023-04-09 22:53:51 +00:00
usbDevMap* devices;
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-04-09 22:53:51 +00:00
QTimer* dataTimer = Q_NULLPTR;
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