Noolite::sendCommand was implemented
This commit is contained in:
@@ -6,11 +6,13 @@
|
||||
namespace noolitelib
|
||||
{
|
||||
|
||||
constexpr auto DefaultTimeout = 1000;
|
||||
|
||||
LibUsbDevice::LibUsbDevice()
|
||||
{
|
||||
auto status = libusb_init(&m_context);
|
||||
if (status) {
|
||||
std::cout << "Error initializing context: " << libusb_strerror(status) << std::endl;
|
||||
std::cerr << "Error initializing context: " << libusb_strerror(status) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,10 +35,10 @@ void LibUsbDevice::close()
|
||||
}
|
||||
}
|
||||
|
||||
bool LibUsbDevice::sendDataToDevice(const Data &data, std::chrono::milliseconds timeout)
|
||||
bool LibUsbDevice::sendDataToDevice(const Data &data)
|
||||
{
|
||||
if (!m_device) {
|
||||
std::cout << "Device not opened" << std::endl;
|
||||
std::cerr << "Device not opened" << std::endl;
|
||||
}
|
||||
|
||||
auto requestType = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE;
|
||||
@@ -45,10 +47,10 @@ bool LibUsbDevice::sendDataToDevice(const Data &data, std::chrono::milliseconds
|
||||
unsigned char package[data.size()];
|
||||
std::copy(data.begin(), data.end(), package);
|
||||
|
||||
auto status = libusb_control_transfer(m_device, requestType, request, 0, 0, package, data.size(), timeout.count());
|
||||
auto status = libusb_control_transfer(m_device, requestType, request, 0, 0, package, data.size(), DefaultTimeout);
|
||||
|
||||
if (status) {
|
||||
std::cout << "Sending data error: " << libusb_strerror(status) << std::endl;
|
||||
std::cerr << "Sending data error: " << libusb_strerror(status) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ public:
|
||||
|
||||
void openDevice(uint16_t vendorId, uint16_t productId) override;
|
||||
void close() override;
|
||||
bool sendDataToDevice(const Data &data, std::chrono::milliseconds timeout = std::chrono::seconds(1)) override;
|
||||
bool sendDataToDevice(const Data &data) override;
|
||||
|
||||
private:
|
||||
libusb_context *m_context = nullptr;
|
||||
|
||||
@@ -13,6 +13,17 @@ constexpr auto PID = 0x05df;
|
||||
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
constexpr char WorkMode = 0;
|
||||
constexpr char Bitrate = 2;
|
||||
constexpr char Repeats = 2;
|
||||
|
||||
constexpr auto Mode = (Repeats << 5 | Bitrate << 3 | WorkMode);
|
||||
|
||||
}
|
||||
|
||||
Noolite::Noolite(IUsbDevice *device) :
|
||||
m_device(device ? device : new LibUsbDevice())
|
||||
{
|
||||
@@ -25,14 +36,32 @@ Noolite::~Noolite()
|
||||
delete m_device;
|
||||
}
|
||||
|
||||
bool Noolite::sendCommand(int channel, Command command, Params params)
|
||||
bool Noolite::sendCommand(unsigned char channel, Command command, const Params ¶ms)
|
||||
{
|
||||
(void) channel;
|
||||
(void) command;
|
||||
(void) params;
|
||||
auto data = composeCommand(channel, command, params);
|
||||
|
||||
if (data) {
|
||||
return m_device->sendDataToDevice(data.value());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::optional<Data> Noolite::composeCommand(unsigned char channel, Command command, const Params ¶ms) const
|
||||
{
|
||||
if (noolitelib::Set == command && !(params.size() == 1 || params.size() == 3)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
unsigned char format = (noolitelib::Set == command) ? params.size() : (command > noolitelib::Bind ? 0x04: 0x00);
|
||||
|
||||
Data res { Mode, static_cast<unsigned char>(command), format, 0x0, channel, 0x0, 0x0, 0x0 };
|
||||
for (int i = 0; i < params.size(); ++i) {
|
||||
res[5 + i] = params[i];
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define NOOLITE_H
|
||||
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
|
||||
#include "interfaces/iusbdevice.h"
|
||||
|
||||
@@ -37,10 +38,12 @@ public:
|
||||
Noolite(IUsbDevice *device = nullptr);
|
||||
~Noolite();
|
||||
|
||||
bool sendCommand(int channel, Command command, Params params = Params());
|
||||
bool sendCommand(unsigned char channel, Command command, const Params ¶ms = Params());
|
||||
|
||||
private:
|
||||
IUsbDevice *m_device;
|
||||
|
||||
std::optional<Data> composeCommand(unsigned char channel, Command command, const Params ¶ms) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user