|
37 | 37 | _SEARCH_ROM = const(0xF0)
|
38 | 38 | _MATCH_ROM = const(0x55)
|
39 | 39 | _SKIP_ROM = const(0xCC)
|
| 40 | +_MAX_DEV = const(10) |
40 | 41 |
|
41 | 42 | class OneWireError(Exception):
|
42 | 43 | """A class to represent a 1-Wire exception."""
|
@@ -76,6 +77,22 @@ def __init__(self, pin):
|
76 | 77 | self._ow = busio.OneWire(pin)
|
77 | 78 | self._readbit = self._ow.read_bit
|
78 | 79 | 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 |
79 | 96 |
|
80 | 97 | def reset(self, required=False):
|
81 | 98 | """
|
@@ -128,9 +145,15 @@ def scan(self):
|
128 | 145 | devices = []
|
129 | 146 | diff = 65
|
130 | 147 | rom = False
|
| 148 | + count = 0 |
131 | 149 | for _ in range(0xff):
|
132 | 150 | rom, diff = self._search_rom(rom, diff)
|
133 | 151 | 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)) |
134 | 157 | devices.append(OneWireAddress(rom))
|
135 | 158 | if diff == 0:
|
136 | 159 | break
|
|
0 commit comments