Skip to content

Commit da0e584

Browse files
authored
Merge pull request #9 from caternuson/iss7_max_device
Limit max devices for scan.
2 parents 225c815 + 2e798a8 commit da0e584

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

adafruit_onewire/bus.py

+23
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
_SEARCH_ROM = const(0xF0)
3838
_MATCH_ROM = const(0x55)
3939
_SKIP_ROM = const(0xCC)
40+
_MAX_DEV = const(10)
4041

4142
class OneWireError(Exception):
4243
"""A class to represent a 1-Wire exception."""
@@ -76,6 +77,22 @@ def __init__(self, pin):
7677
self._ow = busio.OneWire(pin)
7778
self._readbit = self._ow.read_bit
7879
self._writebit = self._ow.write_bit
80+
self._maximum_devices = _MAX_DEV
81+
82+
@property
83+
def maximum_devices(self):
84+
"""The maximum number of devices the bus will scan for. Valid range is 1 to 255.
85+
It is an error to have more devices on the bus than this number. Having less is OK.
86+
"""
87+
return self._maximum_devices
88+
89+
@maximum_devices.setter
90+
def maximum_devices(self, count):
91+
if not isinstance(count, int):
92+
raise ValueError("Maximum must be an integer value 1 - 255.")
93+
if count < 1 or count > 0xff:
94+
raise ValueError("Maximum must be an integer value 1 - 255.")
95+
self._maximum_devices = count
7996

8097
def reset(self, required=False):
8198
"""
@@ -128,9 +145,15 @@ def scan(self):
128145
devices = []
129146
diff = 65
130147
rom = False
148+
count = 0
131149
for _ in range(0xff):
132150
rom, diff = self._search_rom(rom, diff)
133151
if rom:
152+
count += 1
153+
if count > self.maximum_devices:
154+
raise RuntimeError(
155+
"Maximum device count of {} exceeded."\
156+
.format(self.maximum_devices))
134157
devices.append(OneWireAddress(rom))
135158
if diff == 0:
136159
break

0 commit comments

Comments
 (0)