-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_buzzergroup.cpp
218 lines (195 loc) · 6.07 KB
/
serial_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
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
#include "serial_buzzergroup.hpp"
#include <exception>
#include <list>
#include "serial_opcode.hpp"
using namespace std;
using namespace std::chrono;
using namespace boost::asio;
serial_buzzergroup::serial_buzzergroup(string device)
: port(io, device),
ping_watchdog(milliseconds(100), bind(&serial_buzzergroup::send_ping, this)),
timeout_watchdog(milliseconds(250), bind(&serial_buzzergroup::on_timeout, this))
{
this->device = device;
this->connected = false;
// Initialize serial port
port.set_option(serial_port_base::baud_rate(9600));
tcflush(port.lowest_layer().native_handle(), TCIOFLUSH);
if (!port.is_open())
{
throw "Couldn't open serial port '" + device + "'";
}
}
serial_buzzergroup::~serial_buzzergroup()
{
stop_thread = true;
port.close();
if (serial_read_thread.get_id() == this_thread::get_id())
serial_read_thread.detach();
else
serial_read_thread.join();
}
void serial_buzzergroup::start_threads()
{
stop_thread = false;
serial_read_thread = thread(&serial_buzzergroup::thread_loop, this);
}
void serial_buzzergroup::thread_loop()
{
try {
try
{
set<unsigned char> buzzers = perform_handshake();
connected = true;
buzzergroup_connected.raise(*this, buzzers);
}
catch (string s)
{
buzzergroup_connect_failed.raise(*this, s);
return;
}
unsigned char opcode;
while (!stop_thread) {
timeout_watchdog.kick();
ping_watchdog.kick();
if (!connected)
{
unique_lock<mutex> write_lock(write_mutex);
reset();
}
read(port, buffer(&opcode, 1));
unsigned char buzzer_id;
switch (static_cast<serial_opcode>(opcode))
{
case serial_opcode::SERIAL_READY:
{
unique_lock<std::mutex> write_lock(write_mutex);
timeout_watchdog.stop();
ping_watchdog.stop();
connected = false;
buzzergroup_disconnected.raise(*this, disconnect_reason::RESET);
perform_handshake(false);
reset();
break;
}
case serial_opcode::PING:
send_pong();
break;
case serial_opcode::PONG:
// Nothing to do
break;
case serial_opcode::BUZZ:
read(port, buffer(&buzzer_id, 1));
if (connected)
buzzer_hit.raise(*this, buzzer_id);
break;
case serial_opcode::CONNECT:
read(port, buffer(&buzzer_id, 1));
if (connected)
buzzer_connected.raise(*this, buzzer_id);
break;
case serial_opcode::DISCONNECT:
read(port, buffer(&buzzer_id, 1));
if (connected)
buzzer_disconnected.raise(*this, buzzer_id);
break;
default:
{
unique_lock<std::mutex> write_lock(write_mutex);
reset();
break;
}
}
}
}
catch (boost::system::system_error &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;
}
connected = false;
buzzergroup_disconnected.raise(*this, disconnect_reason::DISCONNECTED);
}
set<unsigned char> serial_buzzergroup::perform_handshake(bool include_first_byte)
{
// Perform the handshake
unsigned char buf[3];
if (include_first_byte)
{
read(port, buffer(buf, 3));
}
else
{
buf[0] = 0x00;
read(port, buffer(buf + 1, 2));
}
// Check magic number
if (buf[0] != 0x00 || buf[1] != 0x42) {
throw "Couldn't connect to '" + device + "' - invalid handshake received";
}
// Check protocol version
if (buf[2] != 0x00) {
throw "Couldn't connect to '" + device + "' - invalid protocol version '" + to_string(buf[2]) + "'";
}
// Send magic number
buf[0] = 0x13;
buf[1] = 0x37;
write(port, buffer(buf, 2));
// Receive list of connected buzzers
set<unsigned char> buzzers;
read(port, buffer(buf + 1, 1));
for (unsigned char i = 0;i < buf[1];i++)
{
read(port, buffer(buf, 1));
buzzers.insert(buf[0]);
}
return buzzers;
}
void serial_buzzergroup::send_ping()
{
unsigned char buf = static_cast<char>(serial_opcode::PING);
unique_lock<mutex> write_lock(write_mutex);
write(port, buffer(&buf, 1));
}
void serial_buzzergroup::send_pong()
{
unsigned char buf = static_cast<char>(serial_opcode::PONG);
unique_lock<mutex> write_lock(write_mutex);
write(port, buffer(&buf, 1));
}
void serial_buzzergroup::set_color(unsigned char buzzer_id, unsigned char r, unsigned char g, unsigned char b)
{
unsigned char buf[] = {static_cast<unsigned char>(serial_opcode::SET_COLOR), buzzer_id, r, g, b};
unique_lock<mutex> write_lock(write_mutex);
write(port, buffer(buf, sizeof(buf)));
}
void serial_buzzergroup::on_timeout()
{
connected = false;
buzzergroup_disconnected.raise(*this, disconnect_reason::TIMEOUT);
}
string serial_buzzergroup::get_id() const
{
return device;
}
void serial_buzzergroup::reset()
{
connected = false;
port.close();
port.open(device);
tcflush(port.lowest_layer().native_handle(), TCIOFLUSH);
set<unsigned char> buzzers = perform_handshake();
connected = true;
buzzergroup_connected.raise(*this, move(buzzers));
timeout_watchdog.kick();
ping_watchdog.kick();
}