Skip to content

Commit d4bd270

Browse files
committed
breaking change: return None if find_first_sensor_time fails
1 parent 2de798f commit d4bd270

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

include/event_camera_py/decoder.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
#include <event_camera_py/event_ext_trig.h>
2424
#include <pybind11/numpy.h>
2525
#include <pybind11/pybind11.h>
26+
#include <pybind11/stl.h>
2627

2728
#include <string>
2829
#include <tuple>
30+
#include <variant>
2931
#include <vector>
3032

3133
template <class A>
@@ -68,7 +70,7 @@ class Decoder
6870
return (std::tuple<bool, uint64_t>({reachedTimeLimit, nextTime}));
6971
}
7072

71-
std::tuple<bool, uint64_t> find_first_sensor_time(pybind11::object msg)
73+
std::variant<uint64_t, pybind11::none> find_first_sensor_time(pybind11::object msg)
7274
{
7375
auto decoder = initialize_decoder(
7476
get_attr<std::string>(msg, "encoding"), get_attr<uint32_t>(msg, "width"),
@@ -79,11 +81,16 @@ class Decoder
7981
if (PyObject_GetBuffer(eventsObj.ptr(), &view, PyBUF_CONTIG_RO) != 0) {
8082
throw std::runtime_error("cannot convert events to byte buffer");
8183
}
84+
decoder->setTimeBase(get_attr<uint64_t>(msg, "time_base"));
8285
uint64_t firstTime{0};
8386
const bool foundTime = decoder->findFirstSensorTime(
8487
reinterpret_cast<const uint8_t *>(view.buf), view.len, &firstTime);
8588
PyBuffer_Release(&view);
86-
return (std::tuple<bool, uint64_t>({foundTime, firstTime}));
89+
if (foundTime) {
90+
return (firstTime);
91+
} else {
92+
return (pybind11::cast<pybind11::none>(Py_None));
93+
}
8794
}
8895

8996
void decode_bytes(

src/decoder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void declare_decoder(pybind11::module & m, std::string typestr)
9595
:rtype: tuple[boolean, uint64_t]
9696
)pbdoc")
9797
.def("find_first_sensor_time", &MyDecoder::find_first_sensor_time, R"pbdoc(
98-
find_first_sensor_time(msg) -> tuple[Boolean, uint64_t]
98+
find_first_sensor_time(msg) -> uint64|None
9999
100100
Peeks into encoded message to find first event sensor time stamp. The
101101
boolean return flag indicates whether any time stamp was detected.
@@ -105,8 +105,8 @@ void declare_decoder(pybind11::module & m, std::string typestr)
105105
106106
:param msg: event packet message to decode
107107
:type msg: event_camera_msgs/msgs/EventPacket
108-
:return: tuple with flag (true if event sensor time was found) and the sensor time
109-
:rtype: tuple[boolean, uint64_t]
108+
:return: sensor time
109+
:rtype: uint64_t or None if not found
110110
)pbdoc")
111111
.def("get_start_time", &MyDecoder::get_start_time, R"pbdoc(
112112
get_start_time() -> uint64

tests/test_1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def test_find_first_sensor_time(verbose=True):
188188
print('Testing find_first_time_stamp')
189189
decoder = Decoder()
190190
for _, msg, _ in bag.read_messages(topics=['/event_camera/events']):
191-
found_ts, ts = decoder.find_first_sensor_time(msg)
192-
assert found_ts
191+
ts = decoder.find_first_sensor_time(msg)
192+
assert ts is not None
193193
if verbose:
194194
print('first sensor time stamp: ', ts)
195195
assert ts == 7139840

0 commit comments

Comments
 (0)