Skip to content

Commit 33022c5

Browse files
authored
Merge pull request #1 from naisy/naisy
update for smbus/smbus2, i2c_address change, make
2 parents dbbf884 + 3162e5e commit 33022c5

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CC = $(CROSS_COMPILE)gcc
22
RM = rm
33

44
#CFLAGS = -O0 -g -Wall -c
5-
CFLAGS = -O2 -Wall -c
5+
CFLAGS = -O2 -Wall -c -fPIC
66

77
OUTPUT_DIR = bin
88
OBJ_DIR = obj

python/VL53L0X.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424
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
2638
import site
2739

2840

@@ -87,6 +99,12 @@ def __init__(self, i2c_bus=1, i2c_address=0x29, tca9548a_num=255, tca9548a_addr=
8799
self._tca9548a_addr = tca9548a_addr
88100
self._i2c = smbus.SMBus()
89101
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
90108

91109
def open(self):
92110
self._i2c.open(bus=self._i2c_bus)
@@ -184,3 +202,22 @@ def clear_interrupt(self):
184202
status = _TOF_LIBRARY.VL53L0X_ClearInterruptMask(self._dev, mask)
185203
if status != 0:
186204
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

python/VL53L0X_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import VL53L0X
2727

2828
# Create a VL53L0X object
29-
tof = VL53L0X.VL53L0X()
29+
tof = VL53L0X.VL53L0X(i2c_bus=1,i2c_address=0x29)
3030
tof.open()
3131
# Start ranging
3232
tof.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)

python_lib/vl53l0x_python.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ VL53L0X_Dev_t *initialise(uint8_t i2c_address, uint8_t TCA9548A_Device, uint8_t
154154
if (dev != NULL)
155155
{
156156
// Initialize Comms to the default address to start
157-
dev->I2cDevAddr = VL53L0X_DEFAULT_ADDRESS;
157+
dev->I2cDevAddr = i2c_address;
158158
dev->TCA9548A_Device = TCA9548A_Device;
159159
dev->TCA9548A_Address = TCA9548A_Address;
160160

0 commit comments

Comments
 (0)