-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate class HidIoReport into dedicated files
- Loading branch information
1 parent
f35c6db
commit 8d65947
Showing
4 changed files
with
86 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |