forked from kost/dcled
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice.cc
267 lines (232 loc) · 6.55 KB
/
device.cc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// dcled-hidapi - userland driver for the Dream Cheeky LED Message Board
// Copyright 2018 Jahn Fuchs <github.jahnf@wolke7.net>
// Distributed under the MIT License. See accompanying LICENSE file.
#include "device.h"
#include "animations.h"
#include "output.h"
#include "screen.h"
#include <atomic>
#include <chrono>
#include <codecvt>
#include <iostream>
#include <locale>
#include <sstream>
#include <thread>
#include <utility>
#ifdef __linux__
#include <unistd.h>
#elif defined _WIN32
#include <windows.h>
#include <io.h>
#include <stdio.h>
#define isatty _isatty
#define fileno _fileno
#endif
#include "moodycamel/concurrentqueue.h"
#ifndef WITHOUT_HIDAPI
#include <hidapi/hidapi.h>
HidApi::HidApi() : res(hid_init()) {}
HidApi::~HidApi() { hid_exit(); }
#else
HidApi::HidApi() : res(1) {}
HidApi::~HidApi() {}
namespace {
using hid_device = void;
void* hid_open(...) { return nullptr; }
void* hid_open_path(...) { return nullptr; }
void hid_close(...) {}
}
#endif
namespace {
bool stdoutIsTty() {
return isatty(fileno(stdout));
}
// Screen with modified flag for the internal implementation
struct InternalScreen : public dcled::Screen
{
static constexpr size_t LEDMSGSIZE = sizeof(LedMsg);
InternalScreen() = default;
~InternalScreen() = default;
inline const auto& msgs() const { return msgs_; }
};
std::atomic<bool> threadStop(false);
} // end anonymous namespace
constexpr char dcled::Device::EMULATED_DEV_PATH[];
constexpr char dcled::Device::INVALID_DEV_PATH[];
void dcled::Device::stopThreads(int /*s*/)
{
::threadStop = true;
}
struct dcled::Device::Impl
{
Impl(bool toStdout = false)
: handle(hid_open(VENDOR_ID, PRODUCT_ID, NULL)),
to_stdout(toStdout), stdout_is_tty(stdoutIsTty())
{
std::stringstream ss;
ss << "VENDOR_ID: " << std::hex << VENDOR_ID << ", PRODUCT_ID: " << PRODUCT_ID;
path = ss.str();
if (this->to_stdout && stdout_is_tty) {
for (int i = 0; i < Screen::HEIGHT; ++i) std::cout << std::endl;
}
}
Impl(std::string usb_path, bool toStdout = false)
: path(std::move(usb_path)), to_stdout(toStdout), stdout_is_tty(stdoutIsTty())
{
if (path.compare( INVALID_DEV_PATH ) == 0) {
handle = nullptr;
}
else if (path.compare( EMULATED_DEV_PATH ) != 0) {
handle = hid_open_path(usb_path.c_str());
}
else {
this->to_stdout = emulated_only = true;
}
if (this->to_stdout && stdout_is_tty) {
for (int i = 0; i < Screen::HEIGHT; ++i) std::cout << std::endl;
}
}
~Impl()
{
// TODO check if an animation thread is running -> stop, join
if (handle)
hid_close(handle);
}
uint32_t sleepDuration(uint32_t& ms) const
{
if (ms < 250) { auto res = ms; ms = 0; return res; }
if (ms < 350) { ms -= 175; return 175; }
ms -= 250; return 250;
}
void animationThread()
{
//debug() << "started animation thread " << std::this_thread::get_id();
std::unique_ptr<Animation> a;
uint32_t animDurationMs = 0;
while (!::threadStop && animations.try_dequeue(a))
{
while (!::threadStop && (animDurationMs = a->step(screen)))
{
auto timeUntil = std::chrono::high_resolution_clock::now();
while (!::threadStop && ( animDurationMs > 0 ))
{
screenToDevice();
timeUntil += std::chrono::milliseconds(sleepDuration(animDurationMs));
std::this_thread::sleep_until(timeUntil);
}
}
}
// debug() << "stopping animation thread " << std::this_thread::get_id();
}
void terminalPosReset() {
if (stdout_is_tty) {
#ifndef _WIN32
std::cout << "\x1b[" << char(Screen::HEIGHT + 0x30) << "A";
#else
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
if (GetConsoleScreenBufferInfo(hConsole, &screenBufferInfo)) {
COORD coord;
coord.X = 0;
coord.Y = screenBufferInfo.dwCursorPosition.Y - Screen::HEIGHT;
SetConsoleCursorPosition(hConsole, coord);
}
#endif
}
else
std::cout << "\n";
}
void screenToDevice()
{
#ifndef WITHOUT_HIDAPI
if (handle) {
for (const auto& msg : screen.msgs() ) {
hid_send_feature_report(handle, reinterpret_cast<const uint8_t*>(&msg), screen.LEDMSGSIZE );
}
}
#endif
if (to_stdout) {
terminalPosReset();
screen.print(stdout_is_tty);
}
}
hid_device* handle = nullptr;
std::string path = "/dev/null";
InternalScreen screen;
moodycamel::ConcurrentQueue<std::unique_ptr<dcled::Animation>> animations;
bool to_stdout = false;
bool stdout_is_tty = false;
bool emulated_only = false;
};
dcled::Device::Device(bool toStdout)
: p_(new Impl(toStdout))
{
}
dcled::Device::Device(const char* device_path, bool toStdout)
: p_(new Impl(device_path, toStdout))
{
}
dcled::Device::Device(std::string device_path, bool toStdout)
: p_(new Impl(std::move(device_path), toStdout))
{
}
dcled::Device::Device(Device&& other)
: p_(std::move(other.p_))
{
}
dcled::Device::~Device()
{
}
const std::list<dcled::DeviceInfo> dcled::Device::list()
{
std::list<DeviceInfo> device_list;
#ifndef WITHOUT_HIDAPI
if (HidApi::init())
{
hid_device_info* devs = hid_enumerate(0x0, 0x0);
for (hid_device_info* cur_dev = devs; cur_dev; cur_dev = cur_dev->next) {
if (cur_dev->vendor_id == VENDOR_ID && cur_dev->product_id == PRODUCT_ID) {
if (!cur_dev->manufacturer_string || !cur_dev->product_string || !cur_dev->serial_number) {
error() << "Could not open device at '" << cur_dev->path
<< "'." << "Check your udev rules.";
continue;
}
std::wstring_convert<std::codecvt_utf8<wchar_t>,wchar_t> cv;
device_list.push_back( DeviceInfo{ VENDOR_ID, PRODUCT_ID, cur_dev->path,
cv.to_bytes(cur_dev->serial_number),
cv.to_bytes(cur_dev->manufacturer_string),
cv.to_bytes(cur_dev->product_string) });
}
}
hid_free_enumeration(devs);
}
#endif
return device_list;
}
dcled::Screen& dcled::Device::screen()
{
return p_->screen;
}
bool dcled::Device::isOpen() const
{
if( p_->emulated_only )
return true;
return p_->handle;
}
const std::string& dcled::Device::path() const
{
return p_->path;
}
void dcled::Device::update()
{
p_->screenToDevice();
}
void dcled::Device::enqueue_ptr(std::unique_ptr<Animation> a)
{
if(a) p_->animations.enqueue(std::move(a));
}
void dcled::Device::playAll()
{
auto t = std::thread(&Impl::animationThread, p_.get());
t.join();
}