Skip to content

Commit 53d2d90

Browse files
authored
Merge pull request #18 from adafruit/use-core-message-rtr-classes
When available, use core `Message` and `RemoteTransmissionRequest`
2 parents 02b71aa + ed4af30 commit 53d2d90

File tree

3 files changed

+63
-55
lines changed

3 files changed

+63
-55
lines changed

adafruit_mcp2515/__init__.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,8 @@
158158
_SEND_TIMEOUT_MS = const(5) # 500ms
159159
_MAX_CAN_MSG_LEN = 8 # ?!
160160
# perhaps this will be stateful later?
161-
TransmitBuffer = namedtuple(
162-
"TransmitBuffer",
163-
["CTRL_REG", "STD_ID_REG", "INT_FLAG_MASK", "LOAD_CMD", "SEND_CMD"],
164-
)
165-
166-
# perhaps this will be stateful later? #TODO : dedup with above
167-
ReceiveBuffer = namedtuple(
168-
"TransmitBuffer",
161+
_TransmitBuffer = namedtuple(
162+
"_TransmitBuffer",
169163
["CTRL_REG", "STD_ID_REG", "INT_FLAG_MASK", "LOAD_CMD", "SEND_CMD"],
170164
)
171165

@@ -266,25 +260,11 @@ def _tx_buffer_status_decode(status_byte):
266260

267261

268262
class MCP2515: # pylint:disable=too-many-instance-attributes
269-
"""A common shared-bus protocol."""
270-
271-
def __init__(
272-
self,
273-
spi_bus,
274-
cs_pin,
275-
*,
276-
baudrate: int = 250000,
277-
crystal_freq: Literal[8000000, 10000000, 16000000] = 16000000,
278-
loopback: bool = False,
279-
silent: bool = False,
280-
auto_restart: bool = False,
281-
debug: bool = False,
282-
):
283-
"""A common shared-bus protocol.
263+
"""A common shared-bus protocol.
284264
285265
:param ~busio.SPI spi: The SPI bus used to communicate with the MCP2515
286266
:param ~digitalio.DigitalInOut cs_pin: SPI bus enable pin
287-
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on\
267+
:param int baudrate: The bit rate of the bus in Hz. All devices on\
288268
the bus must agree on this value. Defaults to 250000.
289269
:param Literal crystal_freq: MCP2515 crystal frequency. Valid values are:\
290270
16000000, 10000000 and 8000000. Defaults to 16000000 (16MHz).\
@@ -297,10 +277,22 @@ def __init__(
297277
:param bool auto_restart: **Not supported by hardware. An `AttributeError` will be raised\
298278
if `auto_restart` is set to `True`** If `True`, will restart communications after entering\
299279
bus-off state. Defaults to `False`.
300-
301280
:param bool debug: If `True`, will enable printing debug information. Defaults to `False`.
302281
"""
303282

283+
def __init__(
284+
self,
285+
spi_bus,
286+
cs_pin,
287+
*,
288+
baudrate: int = 250000,
289+
crystal_freq: Literal[8000000, 10000000, 16000000] = 16000000,
290+
loopback: bool = False,
291+
silent: bool = False,
292+
auto_restart: bool = False,
293+
debug: bool = False,
294+
):
295+
304296
if loopback and not silent:
305297
raise AttributeError("Loopback mode requires silent to be set")
306298
if auto_restart:
@@ -332,21 +324,21 @@ def __init__(
332324
def _init_buffers(self):
333325

334326
self._tx_buffers = [
335-
TransmitBuffer(
327+
_TransmitBuffer(
336328
CTRL_REG=_TXB0CTRL,
337329
STD_ID_REG=_TXB0SIDH,
338330
INT_FLAG_MASK=_TX0IF,
339331
LOAD_CMD=_LOAD_TX0,
340332
SEND_CMD=_SEND_TX0,
341333
),
342-
TransmitBuffer(
334+
_TransmitBuffer(
343335
CTRL_REG=_TXB1CTRL,
344336
STD_ID_REG=_TXB1SIDH,
345337
INT_FLAG_MASK=_TX1IF,
346338
LOAD_CMD=_LOAD_TX1,
347339
SEND_CMD=_SEND_TX1,
348340
),
349-
TransmitBuffer(
341+
_TransmitBuffer(
350342
CTRL_REG=_TXB2CTRL,
351343
STD_ID_REG=_TXB2SIDH,
352344
INT_FLAG_MASK=_TX2IF,

adafruit_mcp2515/canio/__init__.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,30 @@
88

99

1010
class Message:
11-
"""A class representing a CANbus data frame"""
11+
"""A class representing a CANbus data frame
12+
13+
:param int id: The numeric ID of the message
14+
:param bytes data: The content of the message, from 0 to 8 bytes of data
15+
:param bool extended: True if the message has an extended identifier,
16+
False if it has a standard identifier
17+
"""
1218

1319
# pylint:disable=too-many-arguments,invalid-name,redefined-builtin
1420
def __init__(self, id, data, extended=False):
15-
"""Create a `Message` to send
16-
17-
Args:
18-
id (int): The numeric ID of the message
19-
data (bytes): The content of the message
20-
extended (bool): True if the
21-
Raises:
22-
AttributeError: If `data` of type `bytes` is not provided for a non-RTR message
23-
AttributeError: If `data` is larger than 8 bytes
24-
"""
25-
2621
self._data = None
2722
self.id = id
2823
self.data = data
2924
self.extended = extended
3025

26+
id: int
27+
"""The numeric ID of the message"""
28+
29+
extended: bool
30+
"""Indicates whether the the message has an extended identifier"""
31+
3132
@property
3233
def data(self):
33-
"""The content of the message, or dummy content in the case of an rtr"""
34+
"""The content of the message"""
3435
return self._data
3536

3637
@data.setter
@@ -50,28 +51,41 @@ def data(self, new_data):
5051

5152

5253
class RemoteTransmissionRequest:
53-
"""A class representing a CANbus remote frame"""
54-
55-
def __init__(self, id: int, length: int, *, extended: bool = False):
56-
"""Construct a RemoteTransmissionRequest to send on a CAN bus
54+
"""A class representing a CANbus remote frame
5755
58-
Args:
59-
id (int): The numeric ID of the requested message
60-
length (int): The length of the requested message
61-
extended (bool, optional): True if the message has an extended identifier, False if it\
62-
has a standard identifier. Defaults to False.
56+
:param int id: The numeric ID of the message
57+
:param length int: The length of the requested message
58+
:param bool extended: True if the message has an extended identifier,
59+
False if it has a standard identifier
60+
"""
6361

64-
"""
62+
def __init__(self, id: int, length: int, *, extended: bool = False):
6563
self.id = id
6664
self.length = length
6765
self.extended = extended
6866

67+
id: int
68+
"""The numeric ID of the message"""
69+
70+
extended: bool
71+
"""Indicates whether the the message has an extended identifier"""
72+
73+
length: int
74+
"""The length of the requested message, from 0 to 8"""
75+
76+
77+
# Replace the above implementation with core canio implementation if it is available
78+
try:
79+
from canio import Message, RemoteTransmissionRequest
80+
except ImportError:
81+
pass
82+
6983

7084
class Listener:
7185
"""Listens for a CAN message
7286
73-
canio.Listener is not constructed directly, but instead by calling the `listen` method of a\
74-
canio.CAN object.
87+
canio.Listener is not constructed directly, but instead by calling the
88+
``listen`` method of a canio.CAN object.
7589
"""
7690

7791
def __init__(self, can_bus_obj, timeout=1.0):
@@ -82,8 +96,8 @@ def __init__(self, can_bus_obj, timeout=1.0):
8296

8397
@property
8498
def timeout(self):
85-
"""The maximum amount of time in seconds that `read` or `readinto` will wait before giving\
86-
up"""
99+
"""The maximum amount of time in seconds that ``read`` or ``readinto``
100+
will wait before giving up"""
87101
return self._timeout
88102

89103
@timeout.setter

docs/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
77
.. automodule:: adafruit_mcp2515
88
:members:
9+
.. automodule:: adafruit_mcp2515.canio
10+
:members:

0 commit comments

Comments
 (0)