-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomm.cpp
More file actions
104 lines (88 loc) · 2.25 KB
/
comm.cpp
File metadata and controls
104 lines (88 loc) · 2.25 KB
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
// (C) 2026-2025 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#include <cassert>
#include <cstring>
#include "comm.h"
#if defined(ARDUINO)
#include "comm_arduino.h"
#endif
#if defined(ESP32)
#include "comm_esp32_hardwareserial.h"
#include "comm_esp32_SC16IS752.h"
#endif
#if IS_POSIX
#include "comm_posix_tty.h"
#endif
#if !defined(BUILD_FOR_PICO2W) && !defined(TEENSY4_1)
#include "comm_tcp_socket_client.h"
#include "comm_tcp_socket_server.h"
#endif
#include "log.h"
#if defined(ESP32)
SC16IS752 *comm::ser2_inst_1 { nullptr };
SC16IS752 *comm::ser2_inst_2 { nullptr };
#endif
comm::comm()
{
}
comm::~comm()
{
}
void comm::println(const char *const s)
{
send_data(reinterpret_cast<const uint8_t *>(s), strlen(s));
send_data(reinterpret_cast<const uint8_t *>("\r\n"), 2);
}
void comm::println(const std::string & in)
{
send_data(reinterpret_cast<const uint8_t *>(in.c_str()), in.size());
send_data(reinterpret_cast<const uint8_t *>("\r\n"), 2);
}
#if defined(ESP32)
void comm::set_comm(SC16IS752 *const a, SC16IS752 *const b)
{
ser2_inst_1 = a;
ser2_inst_2 = b;
}
#endif
#if IS_POSIX
comm *comm::deserialize(const JsonVariantConst j, bus *const)
{
std::string type = j["comm-backend-type"];
comm *d = nullptr;
if (false) {
}
#if !defined(BUILD_FOR_PICO2W) && !defined(TEENSY4_1)
else if (type == "tcp-server")
d = comm_tcp_socket_server::deserialize(j);
else if (type == "tcp-client")
d = comm_tcp_socket_client::deserialize(j);
#endif
#if defined(ESP32)
else if (type == "SC16IS752-serial")
d = comm_esp32_SC16IS752::deserialize(j, ser2_inst_1, ser2_inst_2);
else if (type == "hardware-serial")
d = comm_esp32_hardwareserial::deserialize(j);
#endif
#if defined(ARDUINO)
else if (type == "arduino")
d = comm_arduino::deserialize(j);
#endif
#if IS_POSIX
else if (type == "posix")
d = comm_posix_tty::deserialize(j);
#endif
else {
DOLOG(log_ss::LS_COMM, "comm::deserialize: \"%s\" not de-serialized", type.c_str());
return nullptr;
}
assert(d);
if (!d->begin()) {
delete d;
DOLOG(log_ss::LS_COMM, "comm::deserialize: begin() \"%s\" failed", type.c_str());
return nullptr;
}
return d;
}
#endif