1 |
|
|
#include "Blink1Device.hpp" |
2 |
|
|
|
3 |
|
|
#include <thread> |
4 |
|
|
|
5 |
|
|
namespace blink1_lib { |
6 |
|
70 |
Blink1Device::Blink1Device() noexcept : device(blink1_open(), Blink1Device::destroyBlinkDevice) {} |
7 |
|
|
|
8 |
|
|
Blink1Device::Blink1Device(const std::uint32_t id) noexcept : device(blink1_openById(id), Blink1Device::destroyBlinkDevice) {} |
9 |
|
|
|
10 |
|
|
Blink1Device::Blink1Device(const std::string& stringInitializer, STRING_INIT_TYPE initType) noexcept : Blink1Device(stringInitializer.c_str(), initType) {} |
11 |
|
|
|
12 |
|
|
Blink1Device::Blink1Device(const char* stringInitializer, STRING_INIT_TYPE initType) noexcept { |
13 |
|
|
switch (initType) { |
14 |
|
|
case STRING_INIT_TYPE::PATH: |
15 |
|
|
device = std::unique_ptr<blink1_device, std::function<void(blink1_device*)>>(blink1_openByPath(stringInitializer), Blink1Device::destroyBlinkDevice); |
16 |
|
|
break; |
17 |
|
|
case STRING_INIT_TYPE::SERIAL: |
18 |
|
|
device = std::unique_ptr<blink1_device, std::function<void(blink1_device*)>>(blink1_openBySerial(stringInitializer), Blink1Device::destroyBlinkDevice); |
19 |
|
|
break; |
20 |
|
|
default: |
21 |
|
|
device = nullptr; |
22 |
|
|
break; |
23 |
|
|
} |
24 |
|
|
} |
25 |
|
|
|
26 |
|
70 |
Blink1Device::~Blink1Device() { |
27 |
✗✓ |
70 |
if (clearOnExit) { |
28 |
|
|
setRGB(clearColor); |
29 |
|
|
} |
30 |
|
70 |
} |
31 |
|
|
|
32 |
|
48 |
void Blink1Device::destroyBlinkDevice(blink1_device* device) noexcept { |
33 |
|
48 |
blink1_close(device); |
34 |
|
48 |
} |
35 |
|
|
|
36 |
|
78 |
bool Blink1Device::good() const noexcept { |
37 |
|
78 |
return device != nullptr; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
3 |
Blink1Device::operator bool() const noexcept { |
41 |
|
3 |
return good(); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
3 |
std::optional<int> Blink1Device::getVersion() const noexcept { |
45 |
✓✓ |
3 |
if (good()) { |
46 |
|
2 |
return blink1_getVersion(device.get()); |
47 |
|
|
} |
48 |
|
1 |
return std::nullopt; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
6 |
bool Blink1Device::fadeToRGB(const std::uint16_t fadeMillis, const RGB& rgb) noexcept { |
52 |
✓✓ |
6 |
if (good()) { |
53 |
|
5 |
auto retVal = blink1_fadeToRGB(device.get(), fadeMillis, rgb.r, rgb.g, rgb.b); |
54 |
✓✓✓✓
|
5 |
if (blocking && 0 <= retVal) { |
55 |
|
1 |
std::this_thread::sleep_for(std::chrono::milliseconds(fadeMillis)); |
56 |
|
|
} |
57 |
|
5 |
return 0 <= retVal; |
58 |
|
|
} |
59 |
|
1 |
return false; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
9 |
bool Blink1Device::fadeToRGBN(const std::uint16_t fadeMillis, const RGBN& rgbn) noexcept { |
63 |
✓✓ |
9 |
if (good()) { |
64 |
|
7 |
auto retVal = blink1_fadeToRGBN(device.get(), fadeMillis, rgbn.r, rgbn.g, rgbn.b, rgbn.n); |
65 |
✓✓✓✓
|
7 |
if (blocking && 0 <= retVal) { |
66 |
|
1 |
std::this_thread::sleep_for(std::chrono::milliseconds(fadeMillis)); |
67 |
|
|
} |
68 |
|
7 |
return 0 <= retVal; |
69 |
|
|
} |
70 |
|
2 |
return false; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
3 |
bool Blink1Device::setRGB(const RGB& rgb) noexcept { |
74 |
✓✓ |
3 |
if (good()) { |
75 |
|
2 |
return 0 <= blink1_setRGB(device.get(), rgb.r, rgb.g, rgb.b); |
76 |
|
|
} |
77 |
|
1 |
return false; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
3 |
bool Blink1Device::setRGBN(const RGBN& rgbn) noexcept { |
81 |
|
3 |
return fadeToRGBN(0, rgbn); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
6 |
std::optional<PatternLine> Blink1Device::readRGBWithFade(const std::uint8_t ledn) const noexcept { |
85 |
✓✓ |
6 |
if (good()) { |
86 |
|
4 |
PatternLine line; |
87 |
|
4 |
const auto retVal = blink1_readRGB(device.get(), &line.fadeMillis, &line.rgb.r, &line.rgb.g, &line.rgb.b, ledn); |
88 |
✓✓ |
4 |
if (retVal >= 0) { |
89 |
|
2 |
return line; |
90 |
|
|
} |
91 |
|
|
} |
92 |
|
4 |
return std::nullopt; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
3 |
std::optional<RGB> Blink1Device::readRGB(const std::uint8_t ledn) const noexcept { |
96 |
|
3 |
const auto pLine = readRGBWithFade(ledn); |
97 |
✓✓ |
3 |
if (pLine) { |
98 |
|
1 |
return pLine->rgb; |
99 |
|
|
} |
100 |
|
2 |
return std::nullopt; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
3 |
bool Blink1Device::play(const std::uint8_t pos) noexcept { |
104 |
✓✓ |
3 |
if (good()) { |
105 |
|
2 |
return 0 <= blink1_play(device.get(), 1, pos); |
106 |
|
|
} |
107 |
|
1 |
return false; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
3 |
bool Blink1Device::playLoop(std::uint8_t startpos, std::uint8_t endpos, std::uint8_t count) noexcept { |
111 |
✓✓ |
3 |
if (good()) { |
112 |
|
2 |
return 0 <= blink1_playloop(device.get(), 1, startpos, endpos, count); |
113 |
|
|
} |
114 |
|
1 |
return false; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
3 |
bool Blink1Device::stop() noexcept { |
118 |
✓✓ |
3 |
if (good()) { |
119 |
|
2 |
return 0 <= blink1_play(device.get(), 0, 0); |
120 |
|
|
} |
121 |
|
1 |
return false; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
3 |
std::optional<PlayState> Blink1Device::readPlayState() const noexcept { |
125 |
✓✓ |
3 |
if (good()) { |
126 |
|
2 |
PlayState state; |
127 |
|
2 |
std::uint8_t playing = 0; |
128 |
|
2 |
const auto retVal = blink1_readPlayState(device.get(), &playing, &state.playStart, &state.playEnd, &state.playCount, &state.playPos); |
129 |
|
2 |
state.playing = (playing == 1); |
130 |
✓✓ |
2 |
if (retVal >= 0) { |
131 |
|
1 |
return state; |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
2 |
return std::nullopt; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
3 |
bool Blink1Device::writePatternLine(const PatternLine& line, const std::uint8_t pos) noexcept { |
138 |
✓✓ |
3 |
if (good()) { |
139 |
|
2 |
return 0 <= blink1_writePatternLine(device.get(), line.fadeMillis, line.rgb.r, line.rgb.g, line.rgb.b, pos); |
140 |
|
|
} |
141 |
|
1 |
return false; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
3 |
bool Blink1Device::writePatternLineN(const PatternLineN& line, const std::uint8_t pos) noexcept { |
145 |
✓✓ |
3 |
if (good()) { |
146 |
|
2 |
const auto retVal1 = blink1_setLEDN(device.get(), line.rgbn.n); |
147 |
|
2 |
const auto retVal2 = blink1_writePatternLine(device.get(), line.fadeMillis, line.rgbn.r, line.rgbn.g, line.rgbn.b, pos); |
148 |
✓✓✓✗
|
2 |
return retVal1 >= 0 && retVal2 >= 0; |
149 |
|
|
} |
150 |
|
1 |
return false; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
3 |
std::optional<PatternLine> Blink1Device::readPatternLine(const std::uint8_t pos) const noexcept { |
154 |
✓✓ |
3 |
if (good()) { |
155 |
|
2 |
PatternLine line; |
156 |
|
2 |
int retVal = blink1_readPatternLine(device.get(), &line.fadeMillis, &line.rgb.r, &line.rgb.g, &line.rgb.b, pos); |
157 |
✓✓ |
2 |
if (retVal >= 0) { |
158 |
|
1 |
return line; |
159 |
|
|
} |
160 |
|
|
} |
161 |
|
2 |
return std::nullopt; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
3 |
std::optional<PatternLineN> Blink1Device::readPatternLineN(const std::uint8_t pos) const noexcept { |
165 |
✓✓ |
3 |
if (good()) { |
166 |
|
2 |
PatternLineN line; |
167 |
|
2 |
int retVal = blink1_readPatternLineN(device.get(), &line.fadeMillis, &line.rgbn.r, &line.rgbn.g, &line.rgbn.b, &line.rgbn.n, pos); |
168 |
✓✓ |
2 |
if (retVal >= 0) { |
169 |
|
1 |
return line; |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
2 |
return std::nullopt; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
3 |
bool Blink1Device::savePattern() noexcept { |
176 |
✓✓ |
3 |
if (good()) { |
177 |
|
2 |
return 0 <= blink1_savePattern(device.get()); |
178 |
|
|
} |
179 |
|
1 |
return false; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
6 |
void Blink1Device::enableDegamma() noexcept { |
183 |
|
6 |
blink1_enableDegamma(); |
184 |
|
6 |
} |
185 |
|
|
|
186 |
|
3 |
void Blink1Device::disableDegamma() noexcept { |
187 |
|
3 |
blink1_disableDegamma(); |
188 |
|
3 |
} |
189 |
|
|
|
190 |
|
3 |
int Blink1Device::vid() noexcept { |
191 |
|
3 |
return blink1_vid(); |
192 |
|
|
} |
193 |
|
|
|
194 |
|
3 |
int Blink1Device::pid() noexcept { |
195 |
|
3 |
return blink1_pid(); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
3 |
std::optional<int> Blink1Device::getCacheIndex() const noexcept { |
199 |
✓✓ |
3 |
if (good()) { |
200 |
|
2 |
int cacheIndex = blink1_getCacheIndexByDev(device.get()); |
201 |
✓✓ |
2 |
if (cacheIndex != -1) { |
202 |
|
1 |
return cacheIndex; |
203 |
|
|
} |
204 |
|
|
} |
205 |
|
2 |
return std::nullopt; |
206 |
|
|
} |
207 |
|
|
|
208 |
|
3 |
std::optional<int> Blink1Device::clearCache() noexcept { |
209 |
✓✓ |
3 |
if (good()) { |
210 |
|
2 |
int cacheIndex = blink1_clearCacheDev(device.get()); |
211 |
✓✓ |
2 |
if (cacheIndex != -1) { |
212 |
|
1 |
return cacheIndex; |
213 |
|
|
} |
214 |
|
|
} |
215 |
|
2 |
return std::nullopt; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
3 |
std::optional<std::string_view> Blink1Device::getSerial() const noexcept { |
219 |
✓✓ |
3 |
if (good()) { |
220 |
|
2 |
const char* serial = blink1_getSerialForDev(device.get()); |
221 |
✓✗ |
2 |
if (serial != nullptr) { |
222 |
|
2 |
return serial; |
223 |
|
|
} |
224 |
|
|
} |
225 |
|
1 |
return std::nullopt; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
9 |
std::optional<bool> Blink1Device::isMk2() const noexcept { |
229 |
✓✓ |
9 |
if (good()) { |
230 |
|
6 |
return 1 == blink1_isMk2(device.get()); |
231 |
|
|
} |
232 |
|
3 |
return std::nullopt; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
7 |
void Blink1Device::setBlocking(bool _blocking) noexcept { |
236 |
|
7 |
blocking = _blocking; |
237 |
|
7 |
} |
238 |
|
|
|
239 |
|
3 |
void Blink1Device::setBlocking() noexcept { |
240 |
|
3 |
setBlocking(true); |
241 |
|
3 |
} |
242 |
|
|
|
243 |
|
2 |
void Blink1Device::setNonBlocking() noexcept { |
244 |
|
2 |
setBlocking(false); |
245 |
|
2 |
} |
246 |
|
|
|
247 |
|
5 |
bool Blink1Device::isBlocking() const noexcept { |
248 |
|
5 |
return blocking; |
249 |
|
|
} |
250 |
|
|
} |