Skip to content

Commit

Permalink
add I2CScan operation (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 authored Aug 7, 2024
1 parent fe280c6 commit 21d4fcc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 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
16 changes: 13 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,18 @@ def __init__(
super().__init__()

def scan(self) -> List[int]:
"""Returns an empty list.
Use :py:meth:`pytest.MonkeyPatch.setattr()` to change this output."""
return []
"""Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of
those that respond.
.. 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
6 changes: 5 additions & 1 deletion tests/test_i2c_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
I2CRead,
I2CWrite,
I2CTransfer,
I2CScan,
)

pytest_plugins = ["circuitpython_mocks.fixtures"]
Expand All @@ -15,7 +16,10 @@ def test_i2c(mock_blinka_imports):
address = 0x42
# do setup
with I2C(board.SCL, board.SDA) as i2c_bus:
assert i2c_bus.scan() == []
i2c_bus.expectations.append(I2CScan([address]))
assert i2c_bus.try_lock()
assert address in i2c_bus.scan()
i2c_bus.unlock()

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

0 comments on commit 21d4fcc

Please sign in to comment.