wfview/shuttle.cpp

199 wiersze
5.6 KiB
C++
Czysty Zwykły widok Historia

2021-05-27 07:49:35 +00:00
#include "shuttle.h"
#include <QDebug>
2021-06-04 10:14:01 +00:00
shuttle::shuttle()
2021-05-27 07:49:35 +00:00
{
2021-06-08 07:04:41 +00:00
qInfo() << "Starting HID USB device detection";
2021-05-27 07:49:35 +00:00
}
shuttle::~shuttle()
{
2021-06-04 10:14:01 +00:00
qDebug() << "************ Ending HID";
hid_close(handle);
hid_exit();
2021-05-27 07:49:35 +00:00
}
2021-06-01 16:48:19 +00:00
void shuttle::init()
2021-05-27 07:49:35 +00:00
{
2021-06-01 16:48:19 +00:00
2021-05-27 07:49:35 +00:00
}
2021-06-04 10:14:01 +00:00
int shuttle::hidApiWrite(unsigned char* data, unsigned char length)
{
Q_UNUSED(data);
Q_UNUSED(length);
2021-06-04 10:14:01 +00:00
/* int res;
unsigned char realData[length + 1];
realData[0] = length;
int i;
for (i = 0; i < length; i++)
{
realData[i + 1] = data[i];
2021-05-27 07:49:35 +00:00
}
2021-06-04 10:14:01 +00:00
res = hid_write(handle, realData, length + 1);
if (res < 0) {
printf("Unable to write()\n");
printf("Error: %ls\n", hid_error(handle));
return -1;
}
2021-05-27 07:49:35 +00:00
2021-06-04 10:14:01 +00:00
printf("write success\n");
*/
return 0;
2021-05-27 07:49:35 +00:00
}
2021-06-04 10:14:01 +00:00
void shuttle::run()
2021-05-27 07:49:35 +00:00
{
2021-06-04 10:14:01 +00:00
handle = hid_open(0x0b33, 0x0020, NULL);
if (!handle) {
handle = hid_open(0x0b33, 0x0030, NULL);
if (!handle) {
handle = hid_open(0x0C26, 0x001E, NULL);
if (!handle) {
// No devices found, schedule another check in 1000ms
QTimer::singleShot(1000, this, SLOT(run()));
}
else {
usbDevice = RC28;
}
}
else {
usbDevice = shuttlePro2;
}
}
else {
usbDevice = shuttleXpress;
}
if (handle)
{
int res;
wchar_t manufacturer[MAX_STR];
wchar_t product[MAX_STR];
res = hid_get_manufacturer_string(handle, manufacturer, MAX_STR);
res = hid_get_product_string(handle, product, MAX_STR);
2021-06-05 00:10:31 +00:00
qInfo() << QString("Found Device: %0 from %1").arg(QString::fromWCharArray(product)).arg(QString::fromWCharArray(manufacturer));
hid_set_nonblocking(handle, 1);
QTimer::singleShot(0, this, SLOT(runTimer()));
2021-05-27 07:49:35 +00:00
}
}
2021-06-04 10:14:01 +00:00
void shuttle::runTimer()
2021-06-01 16:48:19 +00:00
{
2021-06-04 10:14:01 +00:00
int res;
QByteArray data(HIDDATALENGTH,0x0);
res = hid_read(handle, (unsigned char *)data.data(), HIDDATALENGTH);
if (res == 0)
;//printf("waiting...\n");
else if (res < 0)
{
2021-06-05 00:10:31 +00:00
qInfo() << "USB Device disconnected?";
hid_close(handle);
QTimer::singleShot(1000, this, SLOT(run()));
2021-06-04 10:14:01 +00:00
return;
}
2021-06-07 13:05:57 +00:00
else if (res == 5 && (usbDevice == shuttleXpress || usbDevice == shuttlePro2))
2021-06-04 10:14:01 +00:00
{
data.resize(res);
2021-06-04 12:08:14 +00:00
qDebug() << "Shuttle Data received: " << hex << (unsigned char)data[0] << ":"
<< hex << (unsigned char)data[1] << ":"
<< hex << (unsigned char)data[2] << ":"
<< hex << (unsigned char)data[3] << ":"
<< hex << (unsigned char)data[4];
2021-06-05 00:10:31 +00:00
unsigned int tempButtons = (unsigned int)((unsigned char)data[3] | (unsigned char)data[4] << 8);
2021-06-04 12:08:14 +00:00
unsigned char tempJogpos = (unsigned char)data[1];
unsigned char tempShutpos = (unsigned char)data[0];
if (tempJogpos == jogpos +1 || (tempJogpos == 0 && jogpos == 0xff))
{
qDebug() << "JOG PLUS";
emit jogPlus();
}
else if (tempJogpos != jogpos) {
qDebug() << "JOG MINUS";
emit jogMinus();
2021-06-04 12:08:14 +00:00
}
/* Button matrix:
1000000000000000 = button15
0100000000000000 = button14
0010000000000000 = button13
0001000000000000 = button12
0000100000000000 = button11
0000010000000000 = button10
0000001000000000 = button9
2021-06-08 07:04:41 +00:00
0000000100000000 = button8 - xpress0
0000000010000000 = button7 - xpress1
0000000001000000 = button6 - xpress2
0000000000100000 = button5 - xpress3
0000000000010000 = button4 - xpress4
0000000000001000 = button3
0000000000000100 = button2
0000000000000010 = button1
0000000000000001 = button0
*/
if (buttons != tempButtons)
{
qDebug() << "BUTTON: " << qSetFieldWidth(16) << bin << tempButtons;
// Step size down
if ((tempButtons >> 5 & 1) && !(buttons >> 5 & 1))
emit button5(true);
// PTT on and off
if ((tempButtons >> 6 & 1) && !(buttons >> 6 & 1))
emit button6(true);
else if ((buttons >> 6 & 1) && !(tempButtons >> 6 & 1))
emit button6(false);
// Step size up
if ((tempButtons >> 7 & 1) && !(buttons >> 7 & 1))
emit button7(true);
}
2021-06-08 07:04:41 +00:00
buttons = tempButtons;
jogpos = tempJogpos;
shutpos = tempShutpos;
2021-06-04 10:14:01 +00:00
2021-06-01 16:48:19 +00:00
}
2021-06-07 13:05:57 +00:00
else if (res == 64 && usbDevice == RC28)
{
// This is a response from the Icom RC28
data.resize(8); // Might as well get rid of the unused data.
qDebug() << "RC28 Data received: "
<< hex << (unsigned char)data[0] << ":"
<< hex << (unsigned char)data[1] << ":"
<< hex << (unsigned char)data[2] << ":"
<< hex << (unsigned char)data[3] << ":"
<< hex << (unsigned char)data[4] << ":"
<< hex << (unsigned char)data[5] << ":"
<< hex << (unsigned char)data[6] << ":"
<< hex << (unsigned char)data[7];
if ((unsigned char)data[0] == 0x01) {
}
}
2021-06-08 07:04:41 +00:00
if (shutpos > 0 && shutpos < 8)
{
shutMult = shutpos;
emit doShuttle(true,shutMult);
qDebug() << "SHUTTLE PLUS" << shutMult;
}
else if (shutpos <= 0xff && shutpos >= 0xf0) {
shutMult = abs(shutpos - 0xff)+1;
emit doShuttle(false,shutMult);
qDebug() << "SHUTTLE MINUS" << shutMult;
}
// Run every 25ms
QTimer::singleShot(25, this, SLOT(runTimer()));
2021-06-01 16:48:19 +00:00
}
2021-06-04 10:14:01 +00:00