-
Notifications
You must be signed in to change notification settings - Fork 2
/
ite-device.cpp
137 lines (108 loc) · 3.73 KB
/
ite-device.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <gsl/gsl>
#include <unordered_map>
#include "ite-device.h"
const static std::unordered_map<ITEBrightness, uint8_t> brightnessMap {
{ITEBrightness::OFF, 0x00},
{ITEBrightness::VERY_DIM, 0x08},
{ITEBrightness::DIM, 0x16},
{ITEBrightness::BRIGHT, 0x24},
{ITEBrightness::VERY_BRIGHT, 0x32}
};
enum class ITEStyle
{
STATIC,
BREATHE,
WAVE,
FLASH
};
const static std::unordered_map <ITEStyle, uint8_t> styleMap {
{ITEStyle::STATIC, 0x01},
{ITEStyle::BREATHE, 0x02},
{ITEStyle::WAVE, 0x03},
{ITEStyle::FLASH, 0x12}
};
ITEDevice::ITEDevice(libusb_context *ctx)
: handle(nullptr)
{
int ret;
libusb_device **devices;
auto count = libusb_get_device_list(ctx, &devices);
auto const freeList = gsl::finally([&devices] {
libusb_free_device_list(devices, 1);
});
for (int i = 0; i < count; i++)
{
libusb_device *dev = devices[i];
libusb_device_descriptor desc;
ret = libusb_get_device_descriptor(dev, &desc);
if (ret != 0)
throw std::runtime_error(std::string("could not get USB device descriptor: ") +
libusb_strerror((libusb_error)ret));
if (desc.idVendor == 0x048d && desc.idProduct == 0xce00) {
ret = libusb_open(dev, &handle);
if (ret != 0)
throw std::runtime_error(std::string("could not open USB device: ")
+ libusb_strerror((libusb_error)ret));
}
}
if (!handle)
throw std::runtime_error("could not find backlight controller\n");
libusb_detach_kernel_driver(handle, 1);
}
ITEDevice::~ITEDevice()
{
libusb_close(handle);
}
void ITEDevice::setBreatheStyle(std::array<Colour, 7> palette, Speed speed,
ITEBrightness brightness)
{
setPalette(palette);
transferMsg({0x08, 0x02, styleMap.at(ITEStyle::BREATHE),
speed.getDeviceSpeed(), brightnessMap.at(brightness),
0x08, 0x00, 0x01});
}
void ITEDevice::setWaveStyle(std::array<Colour, 7> palette, Speed speed,
ITEBrightness brightness)
{
setPalette(palette);
transferMsg({0x08, 0x02, styleMap.at(ITEStyle::WAVE),
speed.getDeviceSpeed(), brightnessMap.at(brightness),
0x08, 0x00, 0x01});
}
void ITEDevice::setFlashStyle(std::array<Colour, 7> palette, Speed speed,
ITEBrightness brightness)
{
setPalette(palette);
transferMsg({0x08, 0x02, styleMap.at(ITEStyle::FLASH),
speed.getDeviceSpeed(), brightnessMap.at(brightness),
0x08, 0x00, 0x01});
}
void ITEDevice::setStaticStyle(std::array<Colour, 4> palette, ITEBrightness brightness)
{
setPalette(palette);
transferMsg({0x08, 0x02, styleMap.at(ITEStyle::STATIC),
0x00, brightnessMap.at(brightness), 0x08, 0x00, 0x01});
}
template <std::size_t N>
void ITEDevice::setPalette(const std::array<Colour, N> &palette)
{
size_t i = 1;
for (const auto &colour : palette)
transferColour(colour, i++);
}
void ITEDevice::setMonoColour(Colour colour, ITEBrightness brightness)
{
setStaticStyle({colour, colour, colour, colour}, brightness);
}
void ITEDevice::transferColour(Colour c, uint8_t idx)
{
transferMsg({0x14, 0x00, idx, c.red, c.green, c.blue, 0x00, 0x00});
}
void ITEDevice::transferMsg(std::array<uint8_t, 8> msg)
{
auto ret = libusb_control_transfer(handle, 0x21, 9, 0x0300, 1,
&msg[0], msg.size(), 1000);
if (ret < 0)
throw std::runtime_error(std::string("transfer failed: ") +
libusb_strerror((libusb_error)ret));
}