Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Adding multiple channels support to neoVI
  • Loading branch information
pierreluctg committed Sep 13, 2018
commit 3f8e23914dc26a72ebf6ab2231b0249971b6a626
61 changes: 40 additions & 21 deletions can/interfaces/ics_neovi/neovi_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ class NeoViBus(BusABC):

def __init__(self, channel, can_filters=None, **config):
"""

:param int channel:
The Channel id or name to create this bus with.
:param list channel:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The channel ids to create this bus with.
Can also be a single integer, netid name or a comma separated
string.
:param list can_filters:
See :meth:`can.BusABC.set_filters` for details.
:param use_system_timestamp:
Expand All @@ -98,18 +99,14 @@ def __init__(self, channel, can_filters=None, **config):
logger.info("CAN Filters: {}".format(can_filters))
logger.info("Got configuration of: {}".format(config))

self._use_system_timestamp = bool(
config.get('use_system_timestamp', False)
)
try:
channel = int(channel)
except ValueError:
channel = getattr(ics, "NETID_{}".format(channel.upper()), -1)
if channel == -1:
raise ValueError(
'channel must be an integer or '
'a valid ICS channel name'
)
if isinstance(channel, (list, tuple)):
Comment thread
felixdivo marked this conversation as resolved.
self.channels = channel
elif isinstance(channel, int):
self.channels = [channel]
else:
# Assume comma separated string of channels
self.channels = [ch.strip() for ch in channel.split(',')]
self.channels = [NeoViBus.channel_to_netid(ch) for ch in self.channels]

type_filter = config.get('type_filter')
serial = config.get('serial')
Expand All @@ -125,15 +122,33 @@ def __init__(self, channel, can_filters=None, **config):
ics.set_fd_bit_rate(
self.dev, config.get('data_bitrate'), channel)

self._use_system_timestamp = bool(
config.get('use_system_timestamp', False)
)

self.channel_info = '%s %s CH:%s' % (
self.dev.Name,
self.get_serial_number(self.dev),
channel
self.channels
)
logger.info("Using device: {}".format(self.channel_info))

self.rx_buffer = deque()
self.network = channel if channel is not None else None

@staticmethod
def channel_to_netid(channel_name_or_id):
try:
channel = int(channel_name_or_id)
except ValueError:
netid = "NETID_{}".format(channel_name_or_id.upper())
if hasattr(ics, netid):
channel = getattr(ics, netid)
else:
raise ValueError(
'channel must be an integer or '
'a valid ICS channel name'
)
return channel

@staticmethod
def get_serial_number(device):
Expand Down Expand Up @@ -203,7 +218,7 @@ def _process_msg_queue(self, timeout=0.1):
except ics.RuntimeError:
return
for ics_msg in messages:
if ics_msg.NetworkID != self.network:
if ics_msg.NetworkID not in self.channels:
continue
self.rx_buffer.append(ics_msg)
if errors:
Expand Down Expand Up @@ -258,7 +273,7 @@ def _ics_msg_to_message(self, ics_msg):
bitrate_switch=bool(
ics_msg.StatusBitField3 & ics.SPY_STATUS3_CANFD_BRS
),
channel=ics_msg.NetworkID
channel=int(ics_msg.NetworkID)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this already an int? If not, wouldn't line 221 fail?

)
else:
return Message(
Expand All @@ -273,7 +288,7 @@ def _ics_msg_to_message(self, ics_msg):
is_remote_frame=bool(
ics_msg.StatusBitField & ics.SPY_STATUS_REMOTE_FRAME
),
channel=ics_msg.NetworkID
channel=int(ics_msg.NetworkID)
)

def _recv_internal(self, timeout=0.1):
Expand Down Expand Up @@ -314,7 +329,11 @@ def send(self, msg, timeout=None):
message.StatusBitField = flag0
message.StatusBitField2 = 0
message.StatusBitField3 = flag3
message.NetworkID = self.network
if msg.channel is not None:
message.NetworkID = msg.channel
else:
# defaults to the first channel in channels
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be surprising! We should at least document this clearly, or maybe even raise an Exception? Another option would be to send it to all, but I guess that is unexpected as well?

message.NetworkID = self.channels[0]

try:
ics.transmit_messages(self.dev, message)
Expand Down