Skip to content

Commit dedfb98

Browse files
committed
Minor cpplint fixes before submitting PR.
1 parent 99de2ae commit dedfb98

File tree

6 files changed

+66
-67
lines changed

6 files changed

+66
-67
lines changed

include/liboculus/Constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace liboculus {
6464
const float AzimuthBeamwidthRad = 0.4*M_PI/180.0;
6565

6666
// \todo These shouldn't be fixed, should read from Oculus.h
67+
// But I don't feel like dealing with their data structure
6768
const float MaxRange = 10; // meters
6869
};
6970

include/liboculus/ImageData.h

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,39 @@ class ImageData {
8484
// This function works for either 1- or 2-byte sonar data
8585
// For 1-byte, the 8-bit value is simply cast into the 16-bit return
8686
uint16_t at_uint16(unsigned int beam, unsigned int rangeBin) const {
87-
if ((_data == nullptr) || (beam >= _numBeams) || (rangeBin >= _numRanges)) return 0;
88-
89-
if(_dataSize == 1) {
90-
return at_uint8(beam,rangeBin);
91-
} else if (_dataSize == 2) {
92-
const size_t offset = (rangeBin * _stride) + (beam * _dataSize) + _offset;
93-
CHECK(offset < (_imageSize-1));
94-
return (_data[offset] | _data[offset+1] << 8);
95-
}
87+
if ((_data == nullptr) || (beam >= _numBeams) || (rangeBin >= _numRanges)) return 0;
9688

97-
return 0;
98-
}
89+
if (_dataSize == 1) {
90+
return at_uint8(beam, rangeBin);
91+
} else if (_dataSize == 2) {
92+
const size_t offset = (rangeBin * _stride) + (beam * _dataSize) + _offset;
93+
CHECK(offset < (_imageSize-1));
94+
return (_data[offset] | _data[offset+1] << 8);
95+
}
9996

100-
uint32_t at_uint32(unsigned int beam, unsigned int rangeBin) const {
101-
if ((_data == nullptr) || (beam >= _numBeams)
102-
|| (rangeBin >= _numRanges)) return 0;
103-
104-
if (_dataSize == 1) {
105-
return at_uint8(beam, rangeBin);
106-
} else if (_dataSize == 2) {
107-
return at_uint16(beam, rangeBin);
108-
} else {
109-
const size_t offset = (rangeBin * _stride) + (beam * _dataSize) + _offset;
110-
CHECK(offset < (_imageSize-1));
111-
return (_data[offset] | _data[offset+1] << 8 | _data[offset+2] << 16 | _data[offset+3] << 24);
97+
return 0;
11298
}
11399

114-
return 0;
100+
// This function will work regardless of whether the raw Oculus
101+
// data is 1-, 2- or 4-byte.
102+
uint32_t at_uint32(unsigned int beam, unsigned int rangeBin) const {
103+
if ((_data == nullptr) || (beam >= _numBeams)
104+
|| (rangeBin >= _numRanges)) return 0;
105+
106+
if (_dataSize == 1) {
107+
return at_uint8(beam, rangeBin);
108+
} else if (_dataSize == 2) {
109+
return at_uint16(beam, rangeBin);
110+
} else {
111+
const size_t offset = (rangeBin * _stride) + (beam * _dataSize) + _offset;
112+
CHECK(offset < (_imageSize-1));
113+
114+
// \todo. Come to think of it, this could be done by mapping the 4 bytes into
115+
// a uin32 then maybe using a system call to swap endianness?
116+
return (_data[offset] | _data[offset+1] << 8 | _data[offset+2] << 16 | _data[offset+3] << 24);
117+
}
118+
119+
return 0;
115120
}
116121

117122
private:

include/liboculus/OculusMessageHandler.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ namespace liboculus {
4343

4444
using std::shared_ptr;
4545

46+
// Base class for "things which can produce Oculus SimplePingResults"
47+
// and will call callbacks when it happens.
48+
// Both DataRx and SonarPlay inherit from this class.
4649
class OculusMessageHandler {
4750
public:
51+
OculusMessageHandler()
52+
: _simplePingCallback(),
53+
_simplePing2Callback()
54+
{;}
55+
4856
template <typename T>
4957
using Callback = std::function< void(const T &) >;
5058

@@ -64,4 +72,4 @@ class OculusMessageHandler {
6472

6573
};
6674

67-
};
75+
} // namespace liboculus

include/liboculus/PingAgreesWithConfig.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

3636
namespace liboculus {
3737

38-
// \TODO develop better API for exposing results
38+
// \TODO develop better API for returning more descriptive results, esp
39+
// when the ping and config don't agree.
3940
template<typename PingT>
4041
bool checkPingAgreesWithConfig( const SimplePingResult<PingT> &ping,
4142
const SonarConfiguration &config ) {
@@ -57,10 +58,14 @@ bool checkPingAgreesWithConfig( const SimplePingResult<PingT> &ping,
5758

5859
// Check data size
5960
if (config.getDataSize() != dataSize) {
60-
LOG(WARNING) << "Config expected " << 8*SizeOfDataSize(config.getDataSize()) << " bit data, data is " << 8*SizeOfDataSize(dataSize) << " bit";
61+
LOG(WARNING) << "Config expected "
62+
<< 8*SizeOfDataSize(config.getDataSize())
63+
<< " bit data, data is actually "
64+
<< 8*SizeOfDataSize(dataSize) << " bit";
6165
}
6266

6367
return true;
6468
}
6569

6670
} // namespace liboculus
71+

include/liboculus/SimplePingResult.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ using std::vector;
5757
// then the rest of OculusSimplePingResult
5858

5959
// Conveniently the OculusSimplePingResult and OculusSimplePingResult2
60-
// structs have the same fields, but a different structure
60+
// structs have the same fields, but a different structure.
61+
// If that weren't the case, we'd use two separate classes instead
62+
// of templates...
6163

6264
template <typename Ping_t>
6365
class SimplePingResult : public SimpleFireMessage<typename Ping_t::FireMsg_t> {
@@ -104,15 +106,16 @@ SimplePingResult<Ping_t>::SimplePingResult(const std::shared_ptr<ByteVector> &bu
104106
_image() {
105107
assert(buffer->size() >= sizeof(Ping_t));
106108

107-
// Bearing data is packed into an array of shorts at the end of the
108-
// OculusSimpleFireMessage
109+
// Bearing data is packed into an array of numBeams shorts immediately
110+
// the end of the OculusSimplePing struct
109111
const int16_t *bearingData = reinterpret_cast<const short*>(buffer->data() + sizeof(Ping_t));
110112
_bearings = BearingData(bearingData, this->ping()->nBeams);
111113

112114
const uint8_t *imageData = reinterpret_cast<const uint8_t*>(buffer->data() + ping()->imageOffset);
113115

114-
if (this->flags().getSendGain()) {
115-
// If sent, the gain is included as the first 4 bytes in each "row" of data
116+
if (this->flags().getSendGain()) {
117+
// If sent, the gain is included as the first 4 bytes in
118+
// each row of data
116119
const uint16_t offsetBytes = 4;
117120

118121
// The size of one "row" of data in bytes

lib/DataRx.cpp

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ namespace liboculus {
3838
namespace asio = boost::asio;
3939

4040
DataRx::DataRx(const IoServiceThread::IoContextPtr &iosrv)
41-
: _socket(*iosrv),
41+
: OculusMessageHandler(),
42+
_socket(*iosrv),
4243
_buffer(std::make_shared<ByteVector>()),
43-
// _simplePingCallback([](const SimplePingResult<PingV1> &){}),
44-
// _simplePing2Callback([](const SimplePingResult<PingV2> &){}),
45-
_onConnectCallback([](void){}) {
44+
_onConnectCallback() {
4645
}
4746

4847
DataRx::~DataRx() {
@@ -77,34 +76,9 @@ void DataRx::onConnect(const boost::system::error_code& ec) {
7776

7877
LOG(DEBUG) << "Connected to sonar!";
7978
restartReceiveCycle();
80-
_onConnectCallback();
79+
if (_onConnectCallback) _onConnectCallback();
8180
}
8281

83-
//== Data writers
84-
85-
// void DataRx::sendSimpleFireMessage(const SonarConfiguration &msg) {
86-
// if (!isConnected()) {
87-
// LOG(WARNING) << "Can't send to sonar, not connected";
88-
// return;
89-
// }
90-
91-
// // According to Blueprint, send OculusSimpleFireMessage2
92-
// // for 32 bit data
93-
94-
// std::vector<std::uint8_t> data;
95-
// //if (msg.getDataSize() == dataSize32Bit) {
96-
// data = msg.serializeFireMsg2();
97-
// //} else {
98-
// // data = msg.serializeFireMsg();
99-
// //}
100-
101-
// if (data.size() > 0) {
102-
// auto result = _socket.send(asio::buffer(data));
103-
// LOG(DEBUG) << "Sent " << result << " bytes to sonar";
104-
// haveWritten(data);
105-
// }
106-
// }
107-
10882
//=== Readers
10983
void DataRx::readUpTo(size_t bytes,
11084
StateMachineCallback callback) {
@@ -220,12 +194,12 @@ void DataRx::rxPacket(const boost::system::error_code& ec,
220194
}
221195

222196
if (bytes_transferred < hdr.payloadSize()) {
223-
LOG(WARNING) << "Received short header of " << bytes_transferred << ", expected " << hdr.payloadSize();
197+
LOG(WARNING) << "Received short header of " << bytes_transferred
198+
<< ", expected " << hdr.payloadSize();
224199
goto exit;
225200
}
226201

227202
if (hdr.msgId() == messageSimplePingResult) {
228-
229203
if ((hdr.msgVersion() == 1) || (hdr.msgVersion() == 0)) {
230204
SimplePingResultV1 ping(_buffer);
231205

@@ -253,15 +227,18 @@ void DataRx::rxPacket(const boost::system::error_code& ec,
253227
LOG(WARNING) << "Incoming packet invalid";
254228
}
255229
} else {
256-
LOG(WARNING) << "Unknown message version " << hdr.msgVersion() << " ignoring";
230+
LOG(WARNING) << "Unknown message version " << hdr.msgVersion()
231+
<< " ignoring";
257232
}
258233

259234

260235
} else if (hdr.msgId() == messageLogs) {
261236
LOG(DEBUG) << "Received " << bytes_transferred << " of LogMessage data";
262-
LOG(INFO) << std::string(_buffer->begin()+sizeof(OculusMessageHeader), _buffer->end());
237+
LOG(INFO) << std::string(_buffer->begin()+sizeof(OculusMessageHeader),
238+
_buffer->end());
263239
} else {
264-
LOG(WARNING) << "Ignoring " << MessageTypeToString(hdr.msgId()) << " message, id " <<static_cast<int>(hdr.msgId());
240+
LOG(WARNING) << "Ignoring " << MessageTypeToString(hdr.msgId())
241+
<< " message, id " << static_cast<int>(hdr.msgId());
265242
}
266243

267244
exit:

0 commit comments

Comments
 (0)