Skip to content

Commit dcf87ce

Browse files
felixdivohardbyte
authored andcommitted
Turn BusState into an enum (hardbyte#533)
1 parent cfb5721 commit dcf87ce

6 files changed

Lines changed: 24 additions & 19 deletions

File tree

can/bus.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@
1111
import threading
1212
from time import time
1313
from collections import namedtuple
14+
from aenum import Enum, auto
1415

1516
from .broadcastmanager import ThreadBasedCyclicSendTask
1617

1718
LOG = logging.getLogger(__name__)
1819

19-
BusState = namedtuple('BusState', 'ACTIVE, PASSIVE, ERROR')
20+
21+
class BusState(Enum):
22+
"""The state in which a :class:`can.BusABC` can be."""
23+
24+
ACTIVE = auto()
25+
PASSIVE = auto()
26+
ERROR = auto()
2027

2128

2229
class BusABC(object):
@@ -152,7 +159,7 @@ def send(self, msg, timeout=None):
152159
for transmit queue to be ready depending on driver implementation.
153160
If timeout is exceeded, an exception will be raised.
154161
Might not be supported by all interfaces.
155-
None blocks indefinitly.
162+
None blocks indefinitely.
156163
157164
:raises can.CanError:
158165
if the message could not be sent
@@ -369,8 +376,7 @@ def state(self):
369376
"""
370377
Return the current state of the hardware
371378
372-
:return: ACTIVE, PASSIVE or ERROR
373-
:rtype: NamedTuple
379+
:type: can.BusState
374380
"""
375381
return BusState.ACTIVE
376382

@@ -379,7 +385,7 @@ def state(self, new_state):
379385
"""
380386
Set the new state of the hardware
381387
382-
:param new_state: BusState.ACTIVE, BusState.PASSIVE or BusState.ERROR
388+
:type: can.BusState
383389
"""
384390
raise NotImplementedError("Property is not implemented.")
385391

can/interfaces/pcan/pcan.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def __init__(self, channel='PCAN_USBBUS1', state=BusState.ACTIVE, bitrate=500000
9797
self.m_objPCANBasic = PCANBasic()
9898
self.m_PcanHandle = globals()[channel]
9999

100-
if state is BusState.ACTIVE or BusState.PASSIVE:
100+
if state is BusState.ACTIVE or state is BusState.PASSIVE:
101101
self.state = state
102102
else:
103103
raise ArgumentError("BusState must be Active or Passive")
@@ -280,7 +280,7 @@ def state(self, new_state):
280280
if new_state is BusState.ACTIVE:
281281
self.m_objPCANBasic.SetValue(self.m_PcanHandle, PCAN_LISTEN_ONLY, PCAN_PARAMETER_OFF)
282282

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

290290
class PcanError(CanError):
291291
"""
292-
TODO: add docs
292+
A generic error on a PCAN bus.
293293
"""
294294
pass

can/interfaces/systec/ucanbus.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(self, channel, can_filters=None, **kwargs):
104104
raise ValueError("Invalid bitrate {}".format(bitrate))
105105

106106
state = kwargs.get('state', BusState.ACTIVE)
107-
if state is BusState.ACTIVE or BusState.PASSIVE:
107+
if state is BusState.ACTIVE or state is BusState.PASSIVE:
108108
self._state = state
109109
else:
110110
raise ValueError("BusState must be Active or Passive")
@@ -247,11 +247,11 @@ def state(self):
247247

248248
@state.setter
249249
def state(self, new_state):
250-
if self._state != BusState.ERROR and (new_state == BusState.ACTIVE or new_state == BusState.PASSIVE):
251-
# deinitialize CAN channel
250+
if self._state is not BusState.ERROR and (new_state is BusState.ACTIVE or new_state is BusState.PASSIVE):
251+
# close the CAN channel
252252
self._ucan.shutdown(self.channel, False)
253253
# set mode
254-
if new_state == BusState.ACTIVE:
254+
if new_state is BusState.ACTIVE:
255255
self._params["mode"] &= ~Mode.MODE_LISTEN_ONLY
256256
else:
257257
self._params["mode"] |= Mode.MODE_LISTEN_ONLY

can/logger.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def main():
5757
parser.add_argument('-b', '--bitrate', type=int,
5858
help='''Bitrate to use for the CAN bus.''')
5959

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

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

9999
if results.active:
100100
bus.state = BusState.ACTIVE
101-
102-
if results.passive:
101+
elif results.passive:
103102
bus.state = BusState.PASSIVE
104103

105104
print('Connected to {}: {}'.format(bus.__class__.__name__, bus.channel_info))

examples/receive_all.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ def receive_all():
1212
#bus = can.interface.Bus(bustype='ixxat', channel=0, bitrate=250000)
1313
#bus = can.interface.Bus(bustype='vector', app_name='CANalyzer', channel=0, bitrate=250000)
1414

15-
bus.state = BusState.ACTIVE
16-
#bus.state = BusState.PASSIVE
15+
bus.state = BusState.ACTIVE # or BusState.PASSIVE
1716

1817
try:
1918
while True:

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
python_requires=">=2.7",
101101
install_requires=[
102102
'wrapt~=1.10',
103+
'aenum',
103104
'typing;python_version<"3.5"',
104105
'windows-curses;platform_system=="Windows"',
105106
],

0 commit comments

Comments
 (0)