-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
200 lines (164 loc) · 5.6 KB
/
main.cpp
File metadata and controls
200 lines (164 loc) · 5.6 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
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
#include <memory>
#include "science/scipp/status.h"
#include "science/synapse/channel.h"
#include "science/synapse/data.h"
#include "science/synapse/device.h"
#include "science/synapse/nodes/broadband_source.h"
#include "science/synapse/nodes/stream_out.h"
using synapse::BinnedSpiketrainData;
using synapse::Ch;
using synapse::Config;
using synapse::Device;
using synapse::DeviceInfo;
using synapse::Electrodes;
using synapse::ElectricalBroadbandData;
using synapse::NodeType;
using synapse::Signal;
using synapse::StreamOut;
using synapse::SynapseData;
auto stream_new(Device& device, std::shared_ptr<StreamOut>* stream_out_ptr) -> science::Status {
if (stream_out_ptr == nullptr) {
return { science::StatusCode::kInvalidArgument, "stream out pointer is null" };
}
std::string group = "224.0.0.10";
science::Status s;
DeviceInfo info;
s = device.info(&info);
if (!s.ok()) return s;
Signal signal{
Electrodes{
.channels = {},
.low_cutoff_hz = 500,
.high_cutoff_hz = 6000
}
};
auto& electrodes = std::get<Electrodes>(signal.signal);
electrodes.channels.reserve(19);
for (unsigned int i = 0; i < 19; i++) {
electrodes.channels.push_back(Ch{
.id = i,
.electrode_id = i * 2,
.reference_id = i * 2 + 1
});
}
Config config;
auto broadband_source = std::make_shared<synapse::BroadbandSource>(100, 16, 30000, 20.0, signal);
*stream_out_ptr = std::make_shared<synapse::StreamOut>("out", group);
s = config.add_node(broadband_source);
if (!s.ok()) return s;
s = config.add_node(*stream_out_ptr);
if (!s.ok()) return s;
s = config.connect(broadband_source, *stream_out_ptr);
if (!s.ok()) return s;
s = device.configure(&config);
if (!s.ok()) return s;
s = device.start();
return s;
}
auto stream_existing(Device& device, std::shared_ptr<StreamOut>* stream_out_ptr) -> science::Status {
if (stream_out_ptr == nullptr) {
return { science::StatusCode::kInvalidArgument, "stream out pointer is null" };
}
science::Status s;
DeviceInfo info;
s = device.info(&info);
if (!s.ok()) return s;
uint32_t stream_out_id = 0; // default id
std::string group;
const auto& nodes = info.configuration().nodes();
for (const auto& node : nodes) {
if (node.type() == NodeType::kStreamOut) {
stream_out_id = node.id();
group = node.stream_out().multicast_group();
break;
}
}
if (stream_out_id == 0) {
return { science::StatusCode::kNotFound, "no stream out node found" };
}
std::cout << "found stream out node with id " << stream_out_id << " and group " << group << std::endl;
*stream_out_ptr = std::make_shared<synapse::StreamOut>("out", group);
Config config;
s = config.add_node(*stream_out_ptr, stream_out_id);
if (!s.ok()) return s;
s = config.set_device(&device);
if (!s.ok()) return s;
return s;
}
auto stream(const std::string& uri, bool configure) -> int {
synapse::Device device(uri);
science::Status s;
std::shared_ptr<synapse::StreamOut> stream_out;
if (configure) {
s = stream_new(device, &stream_out);
if (!s.ok()) {
std::cout << "error configuring stream out node: ("
<< static_cast<int>(s.code()) << ") " << s.message() << std::endl;
return 1;
}
} else {
s = stream_existing(device, &stream_out);
if (!s.ok()) {
std::cout << "error getting existing stream out node: ("
<< static_cast<int>(s.code()) << ") " << s.message() << std::endl;
return 1;
}
}
if (stream_out == nullptr) {
std::cout << "stream out node not initialized" << std::endl;
return 1;
}
while (true) {
SynapseData out;
s = stream_out->read(&out);
if (s.code() == science::StatusCode::kUnavailable) {
continue;
}
if (!s.ok()) {
std::cout << "error reading from stream out node: ("
<< static_cast<int>(s.code()) << ") " << s.message() << std::endl;
continue;
}
if (std::holds_alternative<ElectricalBroadbandData>(out)) {
auto data = std::get<ElectricalBroadbandData>(out);
std::cout << "recv electrical broadband data" << std::endl;
std::cout << " - t0: " << data.t0 << std::endl;
std::cout << " - bit_width: " << data.bit_width << std::endl;
std::cout << " - is_signed: " << data.is_signed << std::endl;
std::cout << " - sample_rate: " << data.sample_rate << std::endl;
std::cout << " - n_channels: " << data.channels.size() << std::endl;
for (const auto& c : data.channels) {
size_t n_samples = c.channel_data.size();
std::cout << " - channel [" << c.channel_id << "]" << std::endl;
for (size_t i = 0; i < std::min(n_samples, size_t(10)); i++) {
std::cout << " - sample [" << i << "]: " << c.channel_data[i] << std::endl;
}
}
} else if (std::holds_alternative<BinnedSpiketrainData>(out)) {
auto data = std::get<BinnedSpiketrainData>(out);
size_t n_spiking_channels = 0;
std::stringstream ss;
for (size_t i = 0; i < data.spike_counts.size(); i++) {
if (data.spike_counts[i] > 0) {
if (n_spiking_channels > 0) {
ss << ", ";
}
ss << "[" << i << "]: " << static_cast<unsigned int>(data.spike_counts[i]);
n_spiking_channels++;
}
}
if (n_spiking_channels > 0) {
std::cout << " - received " << n_spiking_channels << " spiking channels:" << std::endl;
std::cout << ss.str() << std::endl;
}
} else {
std::cout << "data type unknown" << std::endl;
}
}
return 1;
}
int main(int argc, char** argv) {
std::string uri = "192.168.0.1:647";
stream(uri, false);
return 0;
}