Skip to content

Commit 52e911d

Browse files
committed
Reformat and clean up oc_client.
1 parent 331f528 commit 52e911d

File tree

1 file changed

+128
-116
lines changed

1 file changed

+128
-116
lines changed

tools/oculus_client.cpp

Lines changed: 128 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11

2+
#include <fstream>
23
#include <memory>
3-
#include <thread>
44
#include <string>
5-
#include <fstream>
5+
#include <thread>
66

77
using std::string;
88

99
#include <boost/asio.hpp>
1010
#include <boost/bind.hpp>
1111

12-
#include <libg3logger/g3logger.h>
1312
#include <CLI/CLI.hpp>
13+
#include <libg3logger/g3logger.h>
1414

1515
#include "liboculus/DataRx.h"
16-
#include "liboculus/StatusRx.h"
1716
#include "liboculus/IoServiceThread.h"
18-
#include "liboculus/SonarPlayer.h"
1917
#include "liboculus/PingAgreesWithConfig.h"
18+
#include "liboculus/SonarPlayer.h"
19+
#include "liboculus/StatusRx.h"
2020

21-
using std::ofstream;
2221
using std::ios_base;
22+
using std::ofstream;
2323
using std::shared_ptr;
2424

25-
using liboculus::SonarConfiguration;
26-
using liboculus::IoServiceThread;
2725
using liboculus::DataRx;
28-
using liboculus::StatusRx;
26+
using liboculus::IoServiceThread;
2927
using liboculus::SimplePingResult;
30-
using liboculus::SonarStatus;
28+
using liboculus::SonarConfiguration;
3129
using liboculus::SonarPlayerBase;
32-
//using liboculus::SonarPlayer;
30+
using liboculus::SonarStatus;
31+
using liboculus::StatusRx;
32+
// using liboculus::SonarPlayer;
3333

3434
int playbackSonarFile(const std::string &filename, ofstream &output,
3535
int stopAfter = -1);
@@ -40,7 +40,8 @@ bool doStop = false;
4040

4141
// Catch signals
4242
void signalHandler(int signo) {
43-
if (_io_thread) _io_thread->stop();
43+
if (_io_thread)
44+
_io_thread->stop();
4445
doStop = true;
4546
}
4647

@@ -50,21 +51,26 @@ int main(int argc, char **argv) {
5051
CLI::App app{"Simple Oculus Sonar app"};
5152

5253
int verbosity = 0;
53-
app.add_flag("-v,--verbose", verbosity, "Additional output (use -vv for even more!)");
54+
app.add_flag("-v,--verbose", verbosity,
55+
"Additional output (use -vv for even more!)");
5456

5557
string ipAddr("auto");
56-
app.add_option("ip", ipAddr, "IP address of sonar or \"auto\" to automatically detect.");
58+
app.add_option("ip", ipAddr,
59+
"IP address of sonar or \"auto\" to automatically detect.");
5760

5861
string outputFilename("");
59-
app.add_option("-o,--output", outputFilename, "Saves raw sonar data to specified file.");
62+
app.add_option("-o,--output", outputFilename,
63+
"Saves raw sonar data to specified file.");
6064

61-
string inputFilename("");
62-
app.add_option("-i,--input", inputFilename,
63-
"Reads raw sonar data from specified file. Plays file contents rather than contacting \"real\" sonar on network.");
65+
// Playback currently not working
66+
// string inputFilename("");
67+
// app.add_option("-i,--input", inputFilename,
68+
// "Reads raw sonar data from specified file. Plays file "
69+
// "contents rather than contacting \"real\" sonar on
70+
// network.");
6471

6572
int bitDepth(8);
66-
app.add_option("-b,--bits", bitDepth,
67-
"Bit depth oof data (8,16,32)");
73+
app.add_option("-b,--bits", bitDepth, "Bit depth oof data (8,16,32)");
6874

6975
int stopAfter = -1;
7076
app.add_option("-n,--frames", stopAfter, "Stop after (n) frames.");
@@ -98,10 +104,10 @@ int main(int argc, char **argv) {
98104
}
99105

100106
// If playing back an input file, run a different main loop ...
101-
if (!inputFilename.empty()) {
102-
playbackSonarFile(inputFilename, output, stopAfter);
103-
return 0;
104-
}
107+
// if (!inputFilename.empty()) {
108+
// playbackSonarFile(inputFilename, output, stopAfter);
109+
// return 0;
110+
// }
105111

106112
int count = 0;
107113

@@ -118,64 +124,64 @@ int main(int argc, char **argv) {
118124
} else if (bitDepth == 16) {
119125
config.setDataSize(dataSize16Bit);
120126
} else if (bitDepth == 32) {
121-
config.sendGain()
122-
.noGainAssistance()
123-
.setDataSize(dataSize32Bit);
127+
config.sendGain().noGainAssistance().setDataSize(dataSize32Bit);
124128
}
125129

126130
_io_thread.reset(new IoServiceThread);
127131
DataRx _data_rx(_io_thread->context());
128132
StatusRx _status_rx(_io_thread->context());
129133

130134
// Callback for a SimplePingResultV1
131-
_data_rx.setCallback<liboculus::SimplePingResultV1>([&](const liboculus::SimplePingResultV1 &ping) {
132-
// Pings send to the callback are always valid
133-
134-
{
135-
const auto valid = checkPingAgreesWithConfig(ping, config);
136-
if (!valid) {
137-
LOG(WARNING) << "Mismatch between requested config and ping";
138-
}
139-
}
140-
141-
ping.dump();
142-
143-
if (output.is_open()) {
144-
const char *cdata = reinterpret_cast<const char *>(ping.buffer()->data());
145-
output.write(cdata, ping.buffer()->size());
146-
}
147-
148-
count++;
149-
if ((stopAfter > 0) && (count >= stopAfter)) _io_thread->stop();
150-
});
135+
_data_rx.setCallback<liboculus::SimplePingResultV1>(
136+
[&](const liboculus::SimplePingResultV1 &ping) {
137+
// Pings send to the callback are always valid, don't need to check
138+
// again
139+
140+
{
141+
const auto valid = checkPingAgreesWithConfig(ping, config);
142+
if (!valid) {
143+
LOG(WARNING) << "Mismatch between requested config and ping";
144+
}
145+
}
146+
147+
ping.dump();
148+
149+
if (output.is_open()) {
150+
const char *cdata =
151+
reinterpret_cast<const char *>(ping.buffer()->data());
152+
output.write(cdata, ping.buffer()->size());
153+
}
154+
155+
count++;
156+
if ((stopAfter > 0) && (count >= stopAfter))
157+
_io_thread->stop();
158+
});
151159

152160
// Callback for a SimplePingResultV2
153-
_data_rx.setCallback<liboculus::SimplePingResultV2>([&](const liboculus::SimplePingResultV2 &ping) {
154-
// Pings send to the callback are always valid
155-
156-
// {
157-
// const auto valid = checkPingAgreesWithConfig(ping, config);
158-
// if (!valid) {
159-
// LOG(WARNING) << "Mismatch between requested config and ping";
160-
// }
161-
// }
162-
163-
ping.dump();
164-
165-
// const auto gains = ping.gains();
166-
// if (gains.size() > 0) {
167-
// LOG(INFO) << "First five gains " << gains[10] << ", " << gains[11] << ", "
168-
// << gains[12] << ", " << gains[13] << ", " << gains[14];
169-
// }
170-
171-
if (output.is_open()) {
172-
const char *cdata = reinterpret_cast<const char *>(ping.buffer()->data());
173-
output.write(cdata, ping.buffer()->size());
174-
}
175-
176-
count++;
177-
if ((stopAfter > 0) && (count >= stopAfter)) doStop=true;
178-
});
161+
_data_rx.setCallback<liboculus::SimplePingResultV2>(
162+
[&](const liboculus::SimplePingResultV2 &ping) {
163+
// Pings send to the callback are always valid, don't need to check
164+
// again
165+
166+
{
167+
const auto valid = checkPingAgreesWithConfig(ping, config);
168+
if (!valid) {
169+
LOG(WARNING) << "Mismatch between requested config and ping";
170+
}
171+
}
172+
173+
ping.dump();
174+
175+
if (output.is_open()) {
176+
const char *cdata =
177+
reinterpret_cast<const char *>(ping.buffer()->data());
178+
output.write(cdata, ping.buffer()->size());
179+
}
180+
181+
count++;
182+
if ((stopAfter > 0) && (count >= stopAfter))
183+
doStop = true;
184+
});
179185

180186
// Callback when connection to a sonar
181187
_data_rx.setOnConnectCallback([&]() {
@@ -185,13 +191,15 @@ int main(int argc, char **argv) {
185191

186192
// Connect the client
187193
if (ipAddr == "auto") {
188-
// To auto-detect, when the StatusRx connects,
189-
// configure the DataRx
190-
_status_rx.setCallback([&](const SonarStatus &status, bool is_valid){
191-
if (!is_valid || _data_rx.isConnected()) return;
194+
// For auto-deteciont, when the StatusRx connects configure the DataRx
195+
_status_rx.setCallback([&](const SonarStatus &status, bool is_valid) {
196+
if (!is_valid || _data_rx.isConnected())
197+
return;
192198
_data_rx.connect(status.ipAddr());
193199
});
194200
} else {
201+
// Otherwise, just (attempt to) connect the DataRx to the specified IP
202+
// address
195203
_data_rx.connect(ipAddr);
196204
}
197205
_io_thread->start();
@@ -201,7 +209,7 @@ int main(int argc, char **argv) {
201209

202210
// Very rough Hz calculation right now
203211
const auto c = count;
204-
LOG(INFO) << "Received pings at " << c-lastCount << " Hz";
212+
LOG(INFO) << "Received pings at " << c - lastCount << " Hz";
205213

206214
lastCount = c;
207215
sleep(1);
@@ -210,47 +218,51 @@ int main(int argc, char **argv) {
210218
_io_thread->stop();
211219
_io_thread->join();
212220

213-
if (output.is_open()) output.close();
221+
if (output.is_open())
222+
output.close();
214223

215224
LOG(INFO) << "At exit";
216225

217226
return 0;
218227
}
219228

220-
221-
int playbackSonarFile(const std::string &filename, ofstream &output, int stopAfter) {
222-
shared_ptr<SonarPlayerBase> player(SonarPlayerBase::OpenFile(filename));
223-
224-
if( !player ) {
225-
LOG(WARNING) << "Unable to open sonar file";
226-
return -1;
227-
}
228-
229-
if( !player->open(filename) ) {
230-
LOG(INFO) << "Failed to open " << filename;
231-
return -1;
232-
}
233-
234-
int count = 0;
235-
// SimplePingResult ping;
236-
// while( player->nextPing(ping) && !player->eof() ) {
237-
// if (!ping.valid()) {
238-
// LOG(WARNING) << "Invalid ping";
239-
// continue;
240-
// }
241-
242-
// ping.dump();
243-
244-
// if (output.is_open()) {
245-
//const char *cdata = reinterpret_cast<const char *>(ping.buffer().data());
246-
// output.write(cdata, ping.buffer().size());
247-
// }
248-
249-
// count++;
250-
// if( (stopAfter > 0) && (count >= stopAfter) ) break;
251-
// }
252-
253-
LOG(INFO) << count << " sonar packets decoded";
254-
255-
return 0;
256-
}
229+
// Playback not currently working
230+
//
231+
// int playbackSonarFile(const std::string &filename, ofstream &output,
232+
// int stopAfter) {
233+
// shared_ptr<SonarPlayerBase> player(SonarPlayerBase::OpenFile(filename));
234+
235+
// if (!player) {
236+
// LOG(WARNING) << "Unable to open sonar file";
237+
// return -1;
238+
// }
239+
240+
// if (!player->open(filename)) {
241+
// LOG(INFO) << "Failed to open " << filename;
242+
// return -1;
243+
// }
244+
245+
// int count = 0;
246+
// // SimplePingResult ping;
247+
// // while( player->nextPing(ping) && !player->eof() ) {
248+
// // if (!ping.valid()) {
249+
// // LOG(WARNING) << "Invalid ping";
250+
// // continue;
251+
// // }
252+
253+
// // ping.dump();
254+
255+
// // if (output.is_open()) {
256+
// // const char *cdata = reinterpret_cast<const char
257+
// *>(ping.buffer().data());
258+
// // output.write(cdata, ping.buffer().size());
259+
// // }
260+
261+
// // count++;
262+
// // if( (stopAfter > 0) && (count >= stopAfter) ) break;
263+
// // }
264+
265+
// LOG(INFO) << count << " sonar packets decoded";
266+
267+
// return 0;
268+
// }

0 commit comments

Comments
 (0)