-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightpackDevice.cpp
More file actions
115 lines (92 loc) · 3.87 KB
/
LightpackDevice.cpp
File metadata and controls
115 lines (92 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "LightpackDevice.hpp"
#include <cstring>
#include <iostream>
#include <algorithm>
namespace prismatoid {
namespace devices {
enum Cmd{
CmdUpdateLeds = 1,
CmdOffAll,
CmdSetTimerOptions,
CmdPwmOptions, /* deprecated */
CmdSetSmoothness,
CmdSetBrightness,
CmdNoop = 0x0F
};
LightpackDevice *LightpackDevice::this_ = NULL;
LightpackDevice::LightpackDevice(libusb_context * ctx) {
dev_ = NULL;
ctx_ = ctx;
}
LightpackDevice::~LightpackDevice() {
if (LightpackDevice::this_)
delete LightpackDevice::this_;
}
LightpackDevice *LightpackDevice::init(libusb_context * ctx) {
if (!LightpackDevice::this_)
LightpackDevice::this_ = new LightpackDevice(ctx);
return LightpackDevice::this_;
}
LightpackDevice *LightpackDevice::instance() {
return LightpackDevice::this_;
}
bool LightpackDevice::open() {
if (! dev_) {
dev_ = libusb_open_device_with_vid_pid(ctx_, 0x03EB, 0x204F);
if (!dev_)
dev_ = libusb_open_device_with_vid_pid(ctx_, 0x1d50, 0x6022);
if (dev_) {
libusb_detach_kernel_driver(dev_, 0);
libusb_claim_interface(dev_, 0);
}
}
return dev_ != NULL;
}
void LightpackDevice::close() {
if (dev_) {
libusb_release_interface(dev_, 0);
libusb_attach_kernel_driver(dev_, 0);
dev_ = NULL;
}
}
bool LightpackDevice::set_colors(const std::vector<types::Rgb12>& colors) {
buf_[0] = CmdUpdateLeds;
int idx = 1;
for(std::vector<types::Rgb12>::const_iterator it = colors.begin(); it != colors.end(); ++it) {
types::Rgb12 color = (*it);
color.correctGamma(2.0);
buf_[idx++] = color.r8();
buf_[idx++] = color.g8();
buf_[idx++] = color.b8();
buf_[idx++] = color.r() & 0x0f;
buf_[idx++] = color.g() & 0x0f;
buf_[idx++] = color.b() & 0x0f;
}
if (idx < kLightpackDeviceBufferSize) {
memset(buf_ + idx, 0, kLightpackDeviceBufferSize - idx);
}
size_t bytes_to_transfer = kLightpackDeviceBufferSize;
int result = libusb_control_transfer(dev_,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE,
0x09,
(2 << 8),
0x00,
buf_, bytes_to_transfer, 1000);
return result == bytes_to_transfer;
}
bool LightpackDevice::set_smoothness(const int smoothness) {
const char bytes_to_transfer = 2;
buf_[0] = CmdSetSmoothness;
buf_[1] = smoothness;
int result = libusb_control_transfer(dev_,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE,
0x09,
(2 << 8),
0x00,
buf_, bytes_to_transfer, 1000);
return result == bytes_to_transfer;
}
} /* devices */
} /* prismatoid */