Skip to content

Commit 1b27233

Browse files
nkpro2000srbessman
authored andcommitted
Add I2CPrimitive._scan
1 parent 04780e8 commit 1b27233

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

pslab/bus/busio.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"""
3030
from typing import List, Union
3131

32-
from pslab.bus.i2c import I2CPrimitive, I2CSlave
32+
from pslab.bus.i2c import I2CPrimitive
3333
from pslab.serial_handler import SerialHandler
3434

3535
__all__ = "I2C"
@@ -75,15 +75,7 @@ def scan(self) -> List[int]:
7575
addrs : list of int
7676
List of 7-bit addresses on which slave devices replied.
7777
"""
78-
addrs = []
79-
80-
for address in range(0x08, 0x77):
81-
slave = I2CSlave(address, self._device)
82-
83-
if slave.ping():
84-
addrs.append(address)
85-
86-
return addrs
78+
return self._scan(0x08, 0x77)
8779

8880
def try_lock(self) -> bool: # pylint: disable=no-self-use
8981
"""Just a dummy method."""

pslab/bus/i2c.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,31 @@ def _get_i2c_brgval(cls, frequency: float) -> int:
9797
def _get_i2c_frequency(cls, brgval: int) -> float:
9898
return 1 / ((brgval + 2) / CP.CLOCK_RATE + cls._SCL_DELAY)
9999

100+
def _scan(self, start: int = 1, end: int = 128) -> List[int]:
101+
"""Scan I2C port for connected devices.
102+
103+
Parameters
104+
----------
105+
start : int
106+
Address to start scaning at. Defaults to 1(0 is the general call address).
107+
end : int
108+
Address to scan up to. Defaults to 128.
109+
110+
Returns
111+
-------
112+
addrs : list of int
113+
List of 7-bit addresses on which slave devices replied.
114+
"""
115+
addrs = []
116+
117+
for address in range(start, end):
118+
slave = I2CSlave(address, self._device)
119+
120+
if slave.ping():
121+
addrs.append(address)
122+
123+
return addrs
124+
100125
def _start(self, address: int, mode: int) -> int:
101126
"""Initiate I2C transfer.
102127
@@ -442,17 +467,13 @@ def scan(self) -> List[int]:
442467
addrs : list of int
443468
List of 7-bit addresses on which slave devices replied.
444469
"""
445-
addrs = []
446-
447-
for address in range(1, 128): # 0 is the general call address.
448-
slave = I2CSlave(address, self._device)
470+
addrs = self._scan(1, 128)
449471

450-
if slave.ping():
451-
addrs.append(address)
452-
logger.info(
453-
f"Response from slave on {hex(address)} "
454-
+ f"({sensors.get(address, 'None')})."
455-
)
472+
for address in addrs:
473+
logger.info(
474+
f"Response from slave on {hex(address)} "
475+
+ f"({sensors.get(address, 'None')})."
476+
)
456477

457478
return addrs
458479

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy