Skip to content

Commit

Permalink
add I2CScan operation
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Aug 7, 2024
1 parent fe280c6 commit def89d8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions circuitpython_mocks/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
I2CRead,
I2CWrite,
I2CTransfer,
I2CScan,
SPIRead,
SPIWrite,
SPITransfer,
Expand Down Expand Up @@ -72,6 +73,7 @@ def __init__(self, **kwargs) -> None:
I2CRead,
I2CWrite,
I2CTransfer,
I2CScan,
SPIRead,
SPIWrite,
SPITransfer,
Expand Down
15 changes: 12 additions & 3 deletions circuitpython_mocks/busio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
I2CRead,
I2CWrite,
I2CTransfer,
I2CScan,
SPIRead,
SPIWrite,
SPITransfer,
Expand Down Expand Up @@ -82,9 +83,17 @@ def __init__(
super().__init__()

def scan(self) -> List[int]:
"""Returns an empty list.
Use :py:meth:`pytest.MonkeyPatch.setattr()` to change this output."""
return []
"""Returns a list of I2C device addresses.
.. mock-expects::
This function will check against `I2CScan`
:py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`.
"""
assert self.expectations, "no expectation found for I2C.scan()"
op = self.expectations.popleft()
assert isinstance(op, I2CScan), f"I2CScan operation expected, found {repr(op)}"
return op.expected

def readfrom_into(
self,
Expand Down
18 changes: 18 additions & 0 deletions circuitpython_mocks/busio/operations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List
import sys
import circuitpython_typing as cir_py_types

Expand Down Expand Up @@ -140,6 +141,23 @@ def __repr__(self) -> str:
)


class I2CScan:
"""A class to identify a scan operation over a
:py:class:`~circuitpython_mocks.busio.I2C` bus.
The given ``expected`` value will be the result of `I2C.scan()`.
"""

def __init__(self, expected: List[int]) -> None:
for val in expected:
assert val <= 0x7F, f"scan result {val} is not a valid I2C address"
self.expected = expected

def __repr__(self) -> str:
stringify = ", ".join(["%02X" % x for x in self.expected])
return f"<I2CScan expected='[{stringify}]'>"


class SPIRead(_Read):
"""A class to identify a read operation over a
:py:class:`~circuitpython_mocks.busio.SPI` bus."""
Expand Down
1 change: 1 addition & 0 deletions docs/busio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
.. autoclass:: circuitpython_mocks.busio.operations.I2CRead
.. autoclass:: circuitpython_mocks.busio.operations.I2CWrite
.. autoclass:: circuitpython_mocks.busio.operations.I2CTransfer
.. autoclass:: circuitpython_mocks.busio.operations.I2CScan

SPI operations
**************
Expand Down
14 changes: 12 additions & 2 deletions tests/test_i2c.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from collections import deque
from circuitpython_mocks import busio, board
from circuitpython_mocks.busio.operations import I2CTransfer
from circuitpython_mocks.busio.operations import I2CTransfer, I2CScan


@pytest.mark.parametrize(
Expand All @@ -19,8 +19,18 @@ def test_singleton(board_i2c: busio.I2C, busio_i2c: busio.I2C):
assert isinstance(board_i2c.expectations, deque)
assert board_i2c == busio_i2c

board_i2c.expectations.append(I2CTransfer(address, bytearray(1), bytearray(1)))
board_i2c.expectations.extend(
[
I2CScan([address]),
I2CTransfer(address, bytearray(1), bytearray(1)),
]
)
assert busio_i2c.expectations == board_i2c.expectations

assert busio_i2c.try_lock()
assert address in board_i2c.scan()
busio_i2c.unlock()

buffer = bytearray(1)
assert board_i2c.try_lock()
assert not busio_i2c.try_lock()
Expand Down
2 changes: 0 additions & 2 deletions tests/test_i2c_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def test_i2c(mock_blinka_imports):
address = 0x42
# do setup
with I2C(board.SCL, board.SDA) as i2c_bus:
assert i2c_bus.scan() == []

# set expectation for probing performed by I2CDevice.__init__()
i2c_bus.expectations.append(I2CWrite(address, b""))
# set expectations for I2C bus
Expand Down

0 comments on commit def89d8

Please sign in to comment.