Initial commit
This commit is contained in:
55
src/libusbdevice.cpp
Normal file
55
src/libusbdevice.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "libusbdevice.h"
|
||||
|
||||
#include <libusb-1.0/libusb.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace noolitelib
|
||||
{
|
||||
|
||||
LibUsbDevice::LibUsbDevice()
|
||||
{
|
||||
auto status = libusb_init(&m_context);
|
||||
if (status) {
|
||||
std::cout << "Error initializing context: " << libusb_strerror(status) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
LibUsbDevice::~LibUsbDevice()
|
||||
{
|
||||
if (m_context) {
|
||||
libusb_exit(m_context);
|
||||
}
|
||||
}
|
||||
|
||||
void LibUsbDevice::openDevice(uint16_t vendorId, uint16_t productId)
|
||||
{
|
||||
m_device = libusb_open_device_with_vid_pid(m_context, vendorId, productId);
|
||||
}
|
||||
|
||||
void LibUsbDevice::close()
|
||||
{
|
||||
if (m_device) {
|
||||
libusb_close(m_device);
|
||||
}
|
||||
}
|
||||
|
||||
bool LibUsbDevice::sendDataToDevice(unsigned char *data, uint16_t length, std::chrono::milliseconds timeout)
|
||||
{
|
||||
if (!m_device) {
|
||||
std::cout << "Device not opened" << std::endl;
|
||||
}
|
||||
|
||||
auto requestType = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE;
|
||||
auto request = LIBUSB_REQUEST_SET_CONFIGURATION;
|
||||
|
||||
auto status = libusb_control_transfer(m_device, requestType, request, 0, 0, data, length, timeout.count());
|
||||
|
||||
if (status) {
|
||||
std::cout << "Sending data error: " << libusb_strerror(status) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
29
src/libusbdevice.h
Normal file
29
src/libusbdevice.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef LIBUSBDEVICE_H
|
||||
#define LIBUSBDEVICE_H
|
||||
|
||||
#include "interfaces/iusbdevice.h"
|
||||
|
||||
struct libusb_context;
|
||||
struct libusb_device_handle;
|
||||
|
||||
namespace noolitelib
|
||||
{
|
||||
|
||||
class LibUsbDevice : public IUsbDevice
|
||||
{
|
||||
public:
|
||||
LibUsbDevice();
|
||||
~LibUsbDevice() override;
|
||||
|
||||
void openDevice(uint16_t vendorId, uint16_t productId) override;
|
||||
void close() override;
|
||||
bool sendDataToDevice(unsigned char *data, uint16_t length, std::chrono::milliseconds timeout = std::chrono::seconds(1)) override;
|
||||
|
||||
private:
|
||||
libusb_context *m_context = nullptr;
|
||||
libusb_device_handle *m_device = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // LIBUSBDEVICE_H
|
||||
29
src/noolite.cpp
Normal file
29
src/noolite.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "noolite.h"
|
||||
|
||||
#include "src/libusbdevice.h"
|
||||
|
||||
namespace noolitelib
|
||||
{
|
||||
|
||||
namespace Device
|
||||
{
|
||||
|
||||
constexpr auto VID = 0x16c0;
|
||||
constexpr auto PID = 0x05df;
|
||||
|
||||
}
|
||||
|
||||
Noolite::Noolite(IUsbDevice *device) :
|
||||
m_device(device ? device : new LibUsbDevice())
|
||||
{
|
||||
m_device->openDevice(Device::VID, Device::PID);
|
||||
}
|
||||
|
||||
Noolite::~Noolite()
|
||||
{
|
||||
m_device->close();
|
||||
delete m_device;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
42
src/noolite.h
Normal file
42
src/noolite.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef NOOLITE_H
|
||||
#define NOOLITE_H
|
||||
|
||||
#include "interfaces/iusbdevice.h"
|
||||
|
||||
namespace noolitelib
|
||||
{
|
||||
|
||||
enum Command
|
||||
{
|
||||
Off = 0,
|
||||
DecraseBrightnes,
|
||||
On,
|
||||
IncreaseBrightnes,
|
||||
Switch,
|
||||
InvertBrightnes,
|
||||
Set,
|
||||
CallScenario,
|
||||
SaveScenario,
|
||||
Unbind,
|
||||
StopColorSelection,
|
||||
Bind = 15,
|
||||
// Commands for SD111-180 only
|
||||
ColorSelection,
|
||||
ColorSwitch,
|
||||
ModeSwitch,
|
||||
EffectSpeed
|
||||
};
|
||||
|
||||
class Noolite
|
||||
{
|
||||
public:
|
||||
Noolite(IUsbDevice *device = nullptr);
|
||||
~Noolite();
|
||||
|
||||
private:
|
||||
IUsbDevice *m_device;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // NOOLITE_H
|
||||
Reference in New Issue
Block a user