Skip to content

Turn BusState into an enum #533

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

Merged
merged 9 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
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
16 changes: 11 additions & 5 deletions can/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
import threading
from time import time
from collections import namedtuple
from aenum import Enum, auto

from .broadcastmanager import ThreadBasedCyclicSendTask

LOG = logging.getLogger(__name__)

BusState = namedtuple('BusState', 'ACTIVE, PASSIVE, ERROR')

class BusState(Enum):
"""The state in which a :class:`can.BusABC` can be."""

ACTIVE = auto()
PASSIVE = auto()
ERROR = auto()


class BusABC(object):
Expand Down Expand Up @@ -152,7 +159,7 @@ def send(self, msg, timeout=None):
for transmit queue to be ready depending on driver implementation.
If timeout is exceeded, an exception will be raised.
Might not be supported by all interfaces.
None blocks indefinitly.
None blocks indefinitely.

:raises can.CanError:
if the message could not be sent
Expand Down Expand Up @@ -369,8 +376,7 @@ def state(self):
"""
Return the current state of the hardware

:return: ACTIVE, PASSIVE or ERROR
:rtype: NamedTuple
:type: can.BusState
"""
return BusState.ACTIVE

Expand All @@ -379,7 +385,7 @@ def state(self, new_state):
"""
Set the new state of the hardware

:param new_state: BusState.ACTIVE, BusState.PASSIVE or BusState.ERROR
:type: can.BusState
"""
raise NotImplementedError("Property is not implemented.")

Expand Down
6 changes: 3 additions & 3 deletions can/interfaces/pcan/pcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, channel='PCAN_USBBUS1', state=BusState.ACTIVE, bitrate=500000
self.m_objPCANBasic = PCANBasic()
self.m_PcanHandle = globals()[channel]

if state is BusState.ACTIVE or BusState.PASSIVE:
if state is BusState.ACTIVE or state is BusState.PASSIVE:
self.state = state
else:
raise ArgumentError("BusState must be Active or Passive")
Expand Down Expand Up @@ -280,7 +280,7 @@ def state(self, new_state):
if new_state is BusState.ACTIVE:
self.m_objPCANBasic.SetValue(self.m_PcanHandle, PCAN_LISTEN_ONLY, PCAN_PARAMETER_OFF)

if new_state is BusState.PASSIVE:
elif new_state is BusState.PASSIVE:
# When this mode is set, the CAN controller does not take part on active events (eg. transmit CAN messages)
# but stays in a passive mode (CAN monitor), in which it can analyse the traffic on the CAN bus used by a
# PCAN channel. See also the Philips Data Sheet "SJA1000 Stand-alone CAN controller".
Expand All @@ -289,6 +289,6 @@ def state(self, new_state):

class PcanError(CanError):
"""
TODO: add docs
A generic error on a PCAN bus.
"""
pass
8 changes: 4 additions & 4 deletions can/interfaces/systec/ucanbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(self, channel, can_filters=None, **kwargs):
raise ValueError("Invalid bitrate {}".format(bitrate))

state = kwargs.get('state', BusState.ACTIVE)
if state is BusState.ACTIVE or BusState.PASSIVE:
if state is BusState.ACTIVE or state is BusState.PASSIVE:
self._state = state
else:
raise ValueError("BusState must be Active or Passive")
Expand Down Expand Up @@ -247,11 +247,11 @@ def state(self):

@state.setter
def state(self, new_state):
if self._state != BusState.ERROR and (new_state == BusState.ACTIVE or new_state == BusState.PASSIVE):
# deinitialize CAN channel
if self._state is not BusState.ERROR and (new_state is BusState.ACTIVE or new_state is BusState.PASSIVE):
# close the CAN channel
self._ucan.shutdown(self.channel, False)
# set mode
if new_state == BusState.ACTIVE:
if new_state is BusState.ACTIVE:
self._params["mode"] &= ~Mode.MODE_LISTEN_ONLY
else:
self._params["mode"] |= Mode.MODE_LISTEN_ONLY
Expand Down
9 changes: 4 additions & 5 deletions can/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def main():
parser.add_argument('-b', '--bitrate', type=int,
help='''Bitrate to use for the CAN bus.''')

group = parser.add_mutually_exclusive_group(required=False)
group.add_argument('--active', help="Start the bus as active, this is applied the default.",
state_group = parser.add_mutually_exclusive_group(required=False)
state_group.add_argument('--active', help="Start the bus as active, this is applied by default.",
action='store_true')
group.add_argument('--passive', help="Start the bus as passive.",
state_group.add_argument('--passive', help="Start the bus as passive.",
action='store_true')

# print help message when no arguments wre given
Expand Down Expand Up @@ -98,8 +98,7 @@ def main():

if results.active:
bus.state = BusState.ACTIVE

if results.passive:
elif results.passive:
bus.state = BusState.PASSIVE

print('Connected to {}: {}'.format(bus.__class__.__name__, bus.channel_info))
Expand Down
3 changes: 1 addition & 2 deletions examples/receive_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def receive_all():
#bus = can.interface.Bus(bustype='ixxat', channel=0, bitrate=250000)
#bus = can.interface.Bus(bustype='vector', app_name='CANalyzer', channel=0, bitrate=250000)

bus.state = BusState.ACTIVE
#bus.state = BusState.PASSIVE
bus.state = BusState.ACTIVE # or BusState.PASSIVE

try:
while True:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
python_requires=">=2.7",
install_requires=[
'wrapt~=1.10',
'aenum',
'typing;python_version<"3.5"',
'windows-curses;platform_system=="Windows"',
],
Expand Down