|
22 | 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23 | 23 | # SOFTWARE. |
24 | 24 | from ctypes import CDLL, CFUNCTYPE, POINTER, c_int, c_uint, pointer, c_ubyte, c_uint8, c_uint32 |
25 | | -import smbus2 as smbus |
| 25 | +import pkg_resources |
| 26 | +SMBUS='smbus' |
| 27 | +for dist in pkg_resources.working_set: |
| 28 | + #print(dist.project_name, dist.version) |
| 29 | + if dist.project_name == 'smbus': |
| 30 | + break |
| 31 | + if dist.project_name == 'smbus2': |
| 32 | + SMBUS='smbus2' |
| 33 | + break |
| 34 | +if SMBUS == 'smbus': |
| 35 | + import smbus |
| 36 | +elif SMBUS == 'smbus2': |
| 37 | + import smbus2 as smbus |
26 | 38 | import site |
27 | 39 |
|
28 | 40 |
|
@@ -87,6 +99,12 @@ def __init__(self, i2c_bus=1, i2c_address=0x29, tca9548a_num=255, tca9548a_addr= |
87 | 99 | self._tca9548a_addr = tca9548a_addr |
88 | 100 | self._i2c = smbus.SMBus() |
89 | 101 | self._dev = None |
| 102 | + # Resgiter Address |
| 103 | + self.ADDR_UNIT_ID_HIGH = 0x16 # Serial number high byte |
| 104 | + self.ADDR_UNIT_ID_LOW = 0x17 # Serial number low byte |
| 105 | + self.ADDR_I2C_ID_HIGH = 0x18 # Write serial number high byte for I2C address unlock |
| 106 | + self.ADDR_I2C_ID_LOW = 0x19 # Write serial number low byte for I2C address unlock |
| 107 | + self.ADDR_I2C_SEC_ADDR = 0x8a # Write new I2C address after unlock |
90 | 108 |
|
91 | 109 | def open(self): |
92 | 110 | self._i2c.open(bus=self._i2c_bus) |
@@ -184,3 +202,22 @@ def clear_interrupt(self): |
184 | 202 | status = _TOF_LIBRARY.VL53L0X_ClearInterruptMask(self._dev, mask) |
185 | 203 | if status != 0: |
186 | 204 | raise Vl53l0xError('Error clearing VL53L0X interrupt') |
| 205 | + |
| 206 | + def change_address(self, new_address): |
| 207 | + if new_address == None: |
| 208 | + return |
| 209 | + elif new_address == self.i2c_address: |
| 210 | + return |
| 211 | + else: |
| 212 | + # read value from 0x16,0x17 |
| 213 | + high = self.bus.read_byte_data(self.i2c_address, self.ADDR_UNIT_ID_HIGH) |
| 214 | + low = self.bus.read_byte_data(self.i2c_address, self.ADDR_UNIT_ID_LOW) |
| 215 | + |
| 216 | + # write value to 0x18,0x19 |
| 217 | + self.bus.write_byte_data(self.i2c_address, self.ADDR_I2C_ID_HIGH, high) |
| 218 | + self.bus.write_byte_data(self.i2c_address, self.ADDR_I2C_ID_LOW, low) |
| 219 | + |
| 220 | + # write new_address to 0x1a |
| 221 | + self.bus.write_byte_data(self.i2c_address, self.ADDR_I2C_SEC_ADDR, new_address) |
| 222 | + |
| 223 | + self.i2c_address = new_address |
0 commit comments