-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebug.cpp
71 lines (59 loc) · 1.44 KB
/
Debug.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
#include "Debug.h"
#include <umm_malloc/umm_heap_select.h>
std::unique_ptr<DebugManager> DebugManager::instance;
DebugManager& DebugManager::the()
{
HeapSelectIram iram;
if (!DebugManager::instance)
DebugManager::instance = std::make_unique<DebugManager>();
return *DebugManager::instance.get();
}
DebugManager::DebugManager()
{
log_server.begin();
// Disable Nagle to send out all log messages immediately
log_server.setNoDelay(true);
}
size_t DebugManager::write(uint8_t value)
{
#if USE_SERIAL
Serial.write(value);
#endif
size_t last_size = 0;
for (auto& client : log_clients) {
if (client && client.connected())
last_size = client.write(value);
}
return last_size;
}
size_t DebugManager::write(const uint8_t* buffer, size_t size)
{
#if USE_SERIAL
Serial.write(buffer, size);
#endif
size_t last_size = 0;
for (auto& client : log_clients) {
if (client && client.connected())
last_size = client.write(buffer, size);
}
return last_size;
}
void DebugManager::handle()
{
HeapSelectIram iram;
while (log_server.hasClient()) {
log_clients.push_back(log_server.accept());
}
for (size_t i = 0; i < log_clients.size(); ++i) {
auto& client = log_clients[i];
if (!client || !client.connected()) {
log_clients.erase(log_clients.begin() + i);
--i;
continue;
}
client.flush();
// Echo functionality allows clients to check that their connection is working.
while (client.available())
client.write(client.read());
}
}