Skip to content

Commit 2222ece

Browse files
committed
fixing docstring
1 parent c339050 commit 2222ece

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

adafruit_mcp2515/__init__.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,20 @@ def __init__(
223223
debug: bool = False
224224
):
225225
"""A common shared-bus protocol.
226+
226227
:param ~busio.SPI spi: The SPI bus used to communicate with the MCP2515
227228
:param ~digitalio.DigitalInOut cs_pin: SPI bus enable pin
228-
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on the bus must agree on this value. Defaults to 250000.
229-
:param bool loopback: Receive only packets sent from this device, and send only to this device. Requires that `silent` is also set to `False`, but only prevents transmission to other devices. Otherwise the send/receive behavior is normal.
230-
:param bool silent: When `True` the controller does not transmit and all messages are received, ignoring errors and filters. This mode can be used to “sniff” a CAN bus without interfering. Defaults to `False`.
231-
:param bool auto_restart: **Not supported by hardware. An `AttributeError` will be raised if `auto_restart` is set to `True`**
232-
233-
If `True`, will restart\ communications after entering bus-off state. Defaults to `False`.
229+
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on\
230+
the bus must agree on this value. Defaults to 250000.
231+
:param bool loopback: Receive only packets sent from this device, and send only to this\
232+
device. Requires that `silent` is also set to `True`, but only prevents transmission to\
233+
other devices. Otherwise the send/receive behavior is normal.
234+
:param bool silent: When `True` the controller does not transmit and all messages are\
235+
received, ignoring errors and filters. This mode can be used to “sniff” a CAN bus without\
236+
interfering. Defaults to `False`.
237+
:param bool auto_restart: **Not supported by hardware. An `AttributeError` will be raised\
238+
if `auto_restart` is set to `True`** If `True`, will restart communications after entering\
239+
bus-off state. Defaults to `False`.
234240
235241
:param bool debug: If `True`, will enable printing debug information. Defaults to `False`.
236242
"""
@@ -244,7 +250,7 @@ def __init__(
244250
self._debug = debug
245251
self._bus_device_obj = spi_device.SPIDevice(spi_bus, cs_pin)
246252
self._cs_pin = cs_pin
247-
self._buffer = bytearray(255)
253+
self._buffer = bytearray(20)
248254
self._id_buffer = bytearray(4)
249255
self._unread_message_queue = []
250256
self._timer = Timer()
@@ -362,6 +368,9 @@ def read_message(self):
362368
return self._unread_message_queue.pop(0)
363369

364370
def _read_rx_buffer(self, read_command):
371+
for i in len(self._buffer):
372+
self._buffer[i] = 0
373+
365374
# read from buffer
366375
with self._bus_device_obj as spi:
367376
self._buffer[0] = read_command
@@ -394,7 +403,6 @@ def _read_rx_buffer(self, read_command):
394403
data=bytes(self._buffer[5 : 5 + message_length]),
395404
extended=extended,
396405
)
397-
398406
self._unread_message_queue.append(frame_obj)
399407

400408
def _read_from_rx_buffers(self):

examples/mcp2515_loopback_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
def bus():
2121
cs = DigitalInOut(CS_PIN)
2222
cs.switch_to_output()
23-
return CAN(SPI(), cs, loopback=True)
23+
return CAN(SPI(), cs, loopback=True, silent=True)
2424

2525

2626
mb1 = [0xDE, 0xAD, 0xBE, 0xEF]

examples/mcp2515_receive.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
cs = digitalio.DigitalInOut(board.D5)
1313
cs.switch_to_output()
1414
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
15-
mcp = CAN(spi, cs)
15+
mcp = CAN(spi, cs, silent=True)
1616

1717
t = Timer(timeout=5)
18-
18+
next_message = None
19+
message_num = 0
1920
while True:
2021
# print occationally to show we're alive
2122
if t.expired:
@@ -26,15 +27,20 @@
2627

2728
if message_count == 0:
2829
continue
29-
for _i in range(message_count):
30-
print(message_count, "messages available")
31-
msg = listener.receive()
32-
print("Message received from ", hex(msg.id))
30+
31+
next_message = listener.receive()
32+
message_num = 0
33+
while not next_message is None:
34+
message_num += 1
35+
36+
msg = next_message
37+
print("ID:", hex(msg.id), end=",")
3338
if isinstance(msg, Message):
34-
print("Message data:", msg.data)
35-
message_str = "::".join(["0x{:02X}".format(i) for i in msg.data])
36-
print(message_str)
39+
if len(msg.data) > 0:
40+
print("Data:", end="")
41+
message_str = ",".join(["0x{:02X}".format(i) for i in msg.data])
42+
print(message_str)
3743

3844
if isinstance(msg, RemoteTransmissionRequest):
39-
print("RTR length:", msg.length)
40-
print("")
45+
print("RTR_LEN:", msg.length)
46+
next_message = listener.receive()

0 commit comments

Comments
 (0)