Skip to content

Commit

Permalink
Separate class HidIoReport into dedicated files
Browse files Browse the repository at this point in the history
  • Loading branch information
JoergAtGithub committed Jan 5, 2022
1 parent f35c6db commit 8d65947
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 71 deletions.
55 changes: 0 additions & 55 deletions src/controllers/hid/hidio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,6 @@
#include "util/time.h"
#include "util/trace.h"

namespace {
constexpr int kReportIdSize = 1;
constexpr int kMaxHidErrorMessageSize = 512;
QString loggingCategoryPrefix(const QString& deviceName) {
return QStringLiteral("controller.") +
RuntimeLoggingCategory::removeInvalidCharsFromCategory(deviceName.toLower());
}
} // namespace

HidIoReport::HidIoReport(const unsigned char& reportId,
hid_device* device,
const mixxx::hid::DeviceInfo&& deviceInfo)
: m_reportId(reportId),
m_logOutput(loggingCategoryPrefix(deviceInfo.formatName()) + QStringLiteral(".output")),
m_pHidDevice(device),
m_deviceInfo(std::move(deviceInfo)),
m_lastSentOutputReport() {
}

void HidIoReport::sendOutputReport(QByteArray data) {
auto startOfHidWrite = mixxx::Time::elapsed();
if (!m_lastSentOutputReport.compare(data)) {
qCDebug(m_logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit()
<< " Skipped identical Output Report for" << m_deviceInfo.formatName()
<< "serial #" << m_deviceInfo.serialNumberRaw()
<< "(Report ID" << m_reportId << ")";
return; // Same data sent last time
}

// hid_write requires the first byte to be the Report ID, followed by the data[] to be send
QByteArray outputReport;
outputReport.reserve(data.size() + kReportIdSize);
outputReport.append(m_reportId);
outputReport.append(data);

// hid_write can take several milliseconds, because hidapi synchronizes the asyncron HID communication from the OS
int result = hid_write(m_pHidDevice,
(unsigned char*)outputReport.constData(),
outputReport.size());
if (result == -1) {
qCWarning(m_logOutput) << "Unable to send data to" << m_deviceInfo.formatName() << ":"
<< mixxx::convertWCStringToQString(
hid_error(m_pHidDevice),
kMaxHidErrorMessageSize);
} else {
qCDebug(m_logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit() << " "
<< result << "bytes sent to" << m_deviceInfo.formatName()
<< "serial #" << m_deviceInfo.serialNumberRaw()
<< "(including report ID of" << m_reportId << ") - Needed: "
<< (mixxx::Time::elapsed() - startOfHidWrite).formatMicrosWithUnit();

m_lastSentOutputReport = std::move(data);
}
}

HidIoThread::HidIoThread(
hid_device* device, const mixxx::hid::DeviceInfo&& deviceInfo)
: QThread(),
Expand Down
17 changes: 1 addition & 16 deletions src/controllers/hid/hidio.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,11 @@

#include "controllers/controller.h"
#include "controllers/hid/hiddevice.h"
#include "controllers/hid/hidioreport.h"
#include "util/compatibility/qatomic.h"
#include "util/compatibility/qmutex.h"
#include "util/duration.h"

class HidIoReport {
public:
HidIoReport(const unsigned char& reportId,
hid_device* device,
const mixxx::hid::DeviceInfo&& deviceInfo);
void sendOutputReport(QByteArray data);

private:
const unsigned char m_reportId;
const RuntimeLoggingCategory m_logOutput;
hid_device* const
m_pHidDevice; // const pointer to the C data structure, which hidapi uses for communication between functions
const mixxx::hid::DeviceInfo m_deviceInfo;
QByteArray m_lastSentOutputReport;
};

class HidIoThread : public QThread {
Q_OBJECT
public:
Expand Down
55 changes: 55 additions & 0 deletions src/controllers/hid/hidioreport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "controllers/hid/hidioreport.h"

#include <hidapi.h>

#include "controllers/defs_controllers.h"
#include "controllers/hid/legacyhidcontrollermappingfilehandler.h"
#include "util/string.h"
#include "util/time.h"
#include "util/trace.h"

HidIoReport::HidIoReport(const unsigned char& reportId,
hid_device* device,
const mixxx::hid::DeviceInfo&& deviceInfo)
: m_reportId(reportId),
m_logOutput(loggingCategoryPrefix(deviceInfo.formatName()) + QStringLiteral(".output")),
m_pHidDevice(device),
m_deviceInfo(std::move(deviceInfo)),
m_lastSentOutputReport() {
}

void HidIoReport::sendOutputReport(QByteArray data) {
auto startOfHidWrite = mixxx::Time::elapsed();
if (!m_lastSentOutputReport.compare(data)) {
qCDebug(m_logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit()
<< " Skipped identical Output Report for" << m_deviceInfo.formatName()
<< "serial #" << m_deviceInfo.serialNumberRaw()
<< "(Report ID" << m_reportId << ")";
return; // Same data sent last time
}

// hid_write requires the first byte to be the Report ID, followed by the data[] to be send
QByteArray outputReport;
outputReport.reserve(data.size() + kReportIdSize);
outputReport.append(m_reportId);
outputReport.append(data);

// hid_write can take several milliseconds, because hidapi synchronizes the asyncron HID communication from the OS
int result = hid_write(m_pHidDevice,
(unsigned char*)outputReport.constData(),
outputReport.size());
if (result == -1) {
qCWarning(m_logOutput) << "Unable to send data to" << m_deviceInfo.formatName() << ":"
<< mixxx::convertWCStringToQString(
hid_error(m_pHidDevice),
kMaxHidErrorMessageSize);
} else {
qCDebug(m_logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit() << " "
<< result << "bytes sent to" << m_deviceInfo.formatName()
<< "serial #" << m_deviceInfo.serialNumberRaw()
<< "(including report ID of" << m_reportId << ") - Needed: "
<< (mixxx::Time::elapsed() - startOfHidWrite).formatMicrosWithUnit();

m_lastSentOutputReport = std::move(data);
}
}
30 changes: 30 additions & 0 deletions src/controllers/hid/hidioreport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "controllers/controller.h"
#include "controllers/hid/hiddevice.h"
#include "util/duration.h"

namespace {
constexpr int kReportIdSize = 1;
constexpr int kMaxHidErrorMessageSize = 512;
QString loggingCategoryPrefix(const QString& deviceName) {
return QStringLiteral("controller.") +
RuntimeLoggingCategory::removeInvalidCharsFromCategory(deviceName.toLower());
}
} // namespace

class HidIoReport {
public:
HidIoReport(const unsigned char& reportId,
hid_device* device,
const mixxx::hid::DeviceInfo&& deviceInfo);
void sendOutputReport(QByteArray data);

private:
const unsigned char m_reportId;
const RuntimeLoggingCategory m_logOutput;
hid_device* const
m_pHidDevice; // const pointer to the C data structure, which hidapi uses for communication between functions
const mixxx::hid::DeviceInfo m_deviceInfo;
QByteArray m_lastSentOutputReport;
};

0 comments on commit 8d65947

Please sign in to comment.