@@ -97,6 +97,31 @@ def _get_i2c_brgval(cls, frequency: float) -> int:
97
97
def _get_i2c_frequency (cls , brgval : int ) -> float :
98
98
return 1 / ((brgval + 2 ) / CP .CLOCK_RATE + cls ._SCL_DELAY )
99
99
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
+
100
125
def _start (self , address : int , mode : int ) -> int :
101
126
"""Initiate I2C transfer.
102
127
@@ -442,17 +467,13 @@ def scan(self) -> List[int]:
442
467
addrs : list of int
443
468
List of 7-bit addresses on which slave devices replied.
444
469
"""
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 )
449
471
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
+ )
456
477
457
478
return addrs
458
479
0 commit comments