Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CANParser: direct capnp to vector[CanData] conversion #1452

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions opendbc/can/parser_pyx.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from libcpp.pair cimport pair
from libcpp.string cimport string
from libcpp.vector cimport vector
from libc.stdint cimport uint32_t
from libc.stdint cimport uint32_t, uintptr_t

from .common cimport CANParser as cpp_CANParser
from .common cimport dbc_lookup, Msg, DBC, CanData
Expand Down Expand Up @@ -67,13 +67,20 @@ cdef class CANParser:
if self.can:
del self.can

def update_strings(self, strings, sendcan=False):
def update_strings(self, data, sendcan=False):
# If data is a Python nested list, update using the legacy method
if not hasattr(data, "get_data_pointer"):
return self.update_from_list(data, sendcan)

# If data is a ParsedCanData object, directly use the underlying C++ vector[CanData]
cdef uintptr_t pointer = data.get_data_pointer()
can_data = <vector[CanData]*> pointer
return self._update(can_data[0], sendcan)

def update_from_list(self, strings, sendcan=False):
# input format:
# [nanos, [[address, data, src], ...]]
# [[nanos, [[address, data, src], ...], ...]]
for address in self.addresses:
self.vl_all[address].clear()

cdef vector[CanData] can_data_array

try:
Expand All @@ -95,6 +102,12 @@ cdef class CANParser:
except TypeError:
raise RuntimeError("invalid parameter")

return self._update(can_data_array, sendcan)

cdef _update(self, vector[CanData] &can_data_array, sendcan):
for address in self.addresses:
self.vl_all[address].clear()

updated_addrs = self.can.update(can_data_array)
for addr in updated_addrs:
vl = self.vl[addr]
Expand Down
Loading