-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard_buzzergroup.cpp
84 lines (76 loc) · 1.98 KB
/
keyboard_buzzergroup.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
#include "keyboard_buzzergroup.hpp"
#ifdef __linux__
#include <linux/input.h>
#endif
using namespace std;
keyboard_buzzergroup::keyboard_buzzergroup(std::string device)
{
this->device = device;
file.open(device, ios::in | ios::binary);
if (!file.is_open())
{
throw "Couldn't open keyboard '" + device + "'";
}
file.exceptions(fstream::failbit | fstream::badbit | fstream::eofbit);
}
keyboard_buzzergroup::~keyboard_buzzergroup()
{
stop_thread = true;
file.close();
if (this_thread::get_id() == input_thread.get_id())
input_thread.detach();
else
input_thread.join();
}
void keyboard_buzzergroup::start_threads()
{
stop_thread = false;
input_thread = thread(bind(&keyboard_buzzergroup::thread_loop, this));
}
void keyboard_buzzergroup::thread_loop()
{
set<unsigned char> buzzers;
for (int i = 0;i < 255;i++)
{
buzzers.insert(i);
}
buzzergroup_connected.raise(*this, buzzers);
buzzers.clear();
#ifdef __linux__
try
{
while (!stop_thread)
{
input_event e;
file.read(reinterpret_cast<char*>(&e), sizeof(e));
if (e.value == 1)
{
buzzer_hit.raise(*this, static_cast<unsigned char>(e.code));
}
}
}
catch (ios_base::failure &e)
{
// If the thread was supposed to stop this exception is thrown by design and can be ignored
if (!stop_thread)
{
buzzergroup_disconnected.raise(*this, disconnect_reason::ERROR);
return;
}
}
catch (...)
{
buzzergroup_disconnected.raise(*this, disconnect_reason::ERROR);
return;
}
#endif
buzzergroup_disconnected.raise(*this, disconnect_reason::DISCONNECTED);
}
string keyboard_buzzergroup::get_id() const
{
return device;
}
void keyboard_buzzergroup::set_color(unsigned char buzzer_id, unsigned char r, unsigned char g, unsigned char b)
{
// Not supported by this device
}