From a9b417a704ac067b3c162c4ff574d6ae150d3cf2 Mon Sep 17 00:00:00 2001 From: Michael Ruppe <73618042+CoreProduction@users.noreply.github.com> Date: Wed, 5 May 2021 15:44:14 +1000 Subject: [PATCH 01/17] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 067fb70..0ce2157 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ This is the firmware repo for the [Core Electronics PiicoDev® Distance Sensor VL53L1X](https://core-electronics.com.au) +This module depends on the [PiicoDev Unified Library](https://github.com/CoreElectronics/CE-PiicoDev-Unified). Place `PiicoDev_Unified.py` in the same directory. + -This is the firmware repo for the [Core Electronics PiicoDev® Distance Sensor VL53L1X](https://core-electronics.com.au) +This is the firmware repo for the [Core Electronics PiicoDev® Distance Sensor VL53L1X] https://core-electronics.com.au/catalog/product/view/sku/CE07741) This module depends on the [PiicoDev Unified Library](https://github.com/CoreElectronics/CE-PiicoDev-Unified). Place `PiicoDev_Unified.py` in the same directory. - + - Raspberry Pi SBC # License This project is open source - please review the LICENSE.md file for further licensing information. From 4ec278ab89a37a023d7edd43cd084c5f6ae513ba Mon Sep 17 00:00:00 2001 From: CoreProduction2 Date: Thu, 15 Jul 2021 15:45:04 +1000 Subject: [PATCH 07/17] Update for i2c-options unified library --- PiicoDev_VL53L1X.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/PiicoDev_VL53L1X.py b/PiicoDev_VL53L1X.py index c62d651..be012ec 100644 --- a/PiicoDev_VL53L1X.py +++ b/PiicoDev_VL53L1X.py @@ -1,5 +1,4 @@ from PiicoDev_Unified import * -i2c = PiicoDev_Unified_I2C() VL51L1X_DEFAULT_CONFIGURATION = bytes([ 0x00, # 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch */ @@ -97,8 +96,8 @@ class PiicoDev_VL53L1X: - def __init__(self, address=0x29, i2c=i2c): - self.i2c = i2c + def __init__(self, bus=None, freq=None, sda=None, scl=None, address=0x29): + self.i2c = create_unified_i2c(bus=bus, freq=freq, sda=sda, scl=scl) self.address = address self.reset() sleep_ms(1) From 754356eb1ac85e3c0b7f2eacaae4e4700f2f92ba Mon Sep 17 00:00:00 2001 From: CoreProduction2 Date: Thu, 29 Jul 2021 15:29:23 +1000 Subject: [PATCH 08/17] Add Compatibility Indication & I2C Error Handling Also change self.address to self.addr --- PiicoDev_VL53L1X.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/PiicoDev_VL53L1X.py b/PiicoDev_VL53L1X.py index be012ec..c528ed4 100644 --- a/PiicoDev_VL53L1X.py +++ b/PiicoDev_VL53L1X.py @@ -1,5 +1,7 @@ from PiicoDev_Unified import * +compat_str = '\nUnified PiicoDev library out of date. Get the latest module: https://piico.dev/unified \n' + VL51L1X_DEFAULT_CONFIGURATION = bytes([ 0x00, # 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch */ 0x00, # 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */ @@ -97,14 +99,21 @@ class PiicoDev_VL53L1X: def __init__(self, bus=None, freq=None, sda=None, scl=None, address=0x29): + try: + if compat_ind >= 1: + pass + else: + print(compat_str) + except: + print(compat_str) self.i2c = create_unified_i2c(bus=bus, freq=freq, sda=sda, scl=scl) - self.address = address + self.addr = address self.reset() sleep_ms(1) if self.read_model_id() != 0xEACC: raise RuntimeError('Failed to find expected ID register values. Check wiring!') # write default configuration - self.i2c.writeto_mem(self.address, 0x2D, VL51L1X_DEFAULT_CONFIGURATION, addrsize=16) + self.i2c.writeto_mem(self.addr, 0x2D, VL51L1X_DEFAULT_CONFIGURATION, addrsize=16) sleep_ms(100) # the API triggers this change in VL53L1_init_and_start_range() once a # measurement is started; assumes MM1 and MM2 are disabled @@ -112,13 +121,13 @@ def __init__(self, bus=None, freq=None, sda=None, scl=None, address=0x29): sleep_ms(200) def writeReg(self, reg, value): - return self.i2c.writeto_mem(self.address, reg, bytes([value]), addrsize=16) + return self.i2c.writeto_mem(self.addr, reg, bytes([value]), addrsize=16) def writeReg16Bit(self, reg, value): - return self.i2c.writeto_mem(self.address, reg, bytes([(value >> 8) & 0xFF, value & 0xFF]), addrsize=16) + return self.i2c.writeto_mem(self.addr, reg, bytes([(value >> 8) & 0xFF, value & 0xFF]), addrsize=16) def readReg(self, reg): - return self.i2c.readfrom_mem(self.address, reg, 1, addrsize=16)[0] + return self.i2c.readfrom_mem(self.addr, reg, 1, addrsize=16)[0] def readReg16Bit(self, reg): - data = self.i2c.readfrom_mem(self.address, reg, 2, addrsize=16) + data = self.i2c.readfrom_mem(self.addr, reg, 2, addrsize=16) return (data[0]<<8) + data[1] def read_model_id(self): return self.readReg16Bit(0x010F) @@ -127,7 +136,11 @@ def reset(self): sleep_ms(100) self.writeReg(0x0000, 0x01) def read(self): - data = self.i2c.readfrom_mem(self.address, 0x0089, 17, addrsize=16) # RESULT__RANGE_STATUS + try: + data = self.i2c.readfrom_mem(self.addr, 0x0089, 17, addrsize=16) # RESULT__RANGE_STATUS + except: + print(i2c_err_str.format(self.addr)) + return (float('NaN'), float('NaN'), float('NaN')) range_status = data[0] # report_status = data[1] stream_count = data[2] @@ -167,4 +180,4 @@ def read(self): def change_id(self, new_id): self.writeReg(0x0001, new_id & 0x7F) sleep_ms(50) - self.address = new_id \ No newline at end of file + self.addr = new_id From fa4d36147516f8c477e731cce8603b53ab8fee71 Mon Sep 17 00:00:00 2001 From: CoreProduction2 Date: Thu, 29 Jul 2021 16:18:37 +1000 Subject: [PATCH 09/17] Update PiicoDev_VL53L1X.py --- PiicoDev_VL53L1X.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PiicoDev_VL53L1X.py b/PiicoDev_VL53L1X.py index c528ed4..1268fee 100644 --- a/PiicoDev_VL53L1X.py +++ b/PiicoDev_VL53L1X.py @@ -140,7 +140,7 @@ def read(self): data = self.i2c.readfrom_mem(self.addr, 0x0089, 17, addrsize=16) # RESULT__RANGE_STATUS except: print(i2c_err_str.format(self.addr)) - return (float('NaN'), float('NaN'), float('NaN')) + return float('NaN') range_status = data[0] # report_status = data[1] stream_count = data[2] From 2db5b9ee8fec5f8300a092552eb50f67a49d14be Mon Sep 17 00:00:00 2001 From: CoreProduction2 Date: Thu, 26 Aug 2021 16:06:37 +1000 Subject: [PATCH 10/17] Add minify --- .github/workflows/main.yml | 46 ++++++++++++++++++++++++++++++++++++++ min/README.md | 3 +++ 2 files changed, 49 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 min/README.md diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..acaed9c --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,46 @@ +name: minify + +on: + # Allow manual trigger + workflow_dispatch: + # Triggers the workflow on push request events but only for the main branch + push: + branches: [ main ] + paths: + - 'PiicoDev_VL53L1X.py' + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + - name: setup python + uses: actions/setup-python@v2 + with: + python-version: '3.9' + + - name: install python packages + run: pip install python-minifier + + - name: run minifier + run: pyminify PiicoDev_VL53L1X.py > min/PiicoDev_VL53L1X.py + + - name: commit files + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add -A + git commit -m "Update min/PiicoDev_VL53L1X.py" -a --allow-empty + + - name: push changes + uses: ad-m/github-push-action@v0.6.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: main diff --git a/min/README.md b/min/README.md new file mode 100644 index 0000000..0c00073 --- /dev/null +++ b/min/README.md @@ -0,0 +1,3 @@ +The `min/PiicoDev_VL53L1X.py` is an auto-generated minified version of `PiicoDev_VL53L1X.py` intended for any MicroPython controller that has limited available program storage space. + +Use this if you are using a Microbit. From b8147968124abd26a21d68a07988d96a2005c471 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 26 Aug 2021 06:07:44 +0000 Subject: [PATCH 11/17] Update min/PiicoDev_VL53L1X.py --- min/PiicoDev_VL53L1X.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 min/PiicoDev_VL53L1X.py diff --git a/min/PiicoDev_VL53L1X.py b/min/PiicoDev_VL53L1X.py new file mode 100644 index 0000000..c23896b --- /dev/null +++ b/min/PiicoDev_VL53L1X.py @@ -0,0 +1,24 @@ +_A=None +from PiicoDev_Unified import * +compat_str='\nUnified PiicoDev library out of date. Get the latest module: https://piico.dev/unified \n' +VL51L1X_DEFAULT_CONFIGURATION=bytes([0,0,0,1,2,0,2,8,0,8,16,1,1,0,0,0,0,255,0,15,0,0,0,0,0,32,11,0,0,2,10,33,0,0,5,0,0,0,0,200,0,0,56,255,1,0,8,0,0,1,219,15,1,241,13,1,104,0,128,8,184,0,0,0,0,15,137,0,0,0,0,0,0,0,1,15,13,14,14,0,0,2,199,255,155,0,0,0,1,1,64]) +class PiicoDev_VL53L1X: + def __init__(self,bus=_A,freq=_A,sda=_A,scl=_A,address=41): + try: + if compat_ind>=1:0 + else:print(compat_str) + except:print(compat_str) + self.i2c=create_unified_i2c(bus=bus,freq=freq,sda=sda,scl=scl);self.addr=address;self.reset();sleep_ms(1) + if self.read_model_id()!=60108:raise RuntimeError('Failed to find expected ID register values. Check wiring!') + self.i2c.writeto_mem(self.addr,45,VL51L1X_DEFAULT_CONFIGURATION,addrsize=16);sleep_ms(100);self.writeReg16Bit(30,self.readReg16Bit(34)*4);sleep_ms(200) + def writeReg(self,reg,value):return self.i2c.writeto_mem(self.addr,reg,bytes([value]),addrsize=16) + def writeReg16Bit(self,reg,value):return self.i2c.writeto_mem(self.addr,reg,bytes([value>>8&255,value&255]),addrsize=16) + def readReg(self,reg):return self.i2c.readfrom_mem(self.addr,reg,1,addrsize=16)[0] + def readReg16Bit(self,reg):data=self.i2c.readfrom_mem(self.addr,reg,2,addrsize=16);return(data[0]<<8)+data[1] + def read_model_id(self):return self.readReg16Bit(271) + def reset(self):self.writeReg(0,0);sleep_ms(100);self.writeReg(0,1) + def read(self): + try:data=self.i2c.readfrom_mem(self.addr,137,17,addrsize=16) + except:print(i2c_err_str.format(self.addr));return float('NaN') + range_status=data[0];stream_count=data[2];dss_actual_effective_spads_sd0=(data[3]<<8)+data[4];ambient_count_rate_mcps_sd0=(data[7]<<8)+data[8];final_crosstalk_corrected_range_mm_sd0=(data[13]<<8)+data[14];peak_signal_count_rate_crosstalk_corrected_mcps_sd0=(data[15]<<8)+data[16];return final_crosstalk_corrected_range_mm_sd0 + def change_id(self,new_id):self.writeReg(1,new_id&127);sleep_ms(50);self.addr=new_id \ No newline at end of file From b7bb0ab6b1089f443afd76389463113b2770e229 Mon Sep 17 00:00:00 2001 From: CoreProduction2 Date: Thu, 26 Aug 2021 16:30:40 +1000 Subject: [PATCH 12/17] Update README.md --- README.md | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2828893..535830b 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,42 @@ # PiicoDev® VL53L1X MicroPython Module -This is the firmware repo for the [Core Electronics PiicoDev® Distance Sensor VL53L1X] https://core-electronics.com.au/catalog/product/view/sku/CE07741) +This is the firmware repo for the [Core Electronics PiicoDev® Distance Sensor VL53L1X](https://core-electronics.com.au/catalog/product/view/sku/CE07741) This module depends on the [PiicoDev Unified Library](https://github.com/CoreElectronics/CE-PiicoDev-Unified). Place `PiicoDev_Unified.py` in the same directory. - -See the [Quickstart Guide](https://piicodev/p7) - - -This module has been tested on: - - Micro:bit v2 - - Raspberry Pi Pico - - Raspberry Pi SBC +See the Quickstart Guides for: +- [Micro:bit v2](https://core-electronics.com.au/tutorials/piicodev-distance-sensor-vl53l1x-micro-bit-guide.html) +- [Raspberry Pi Pico](https://core-electronics.com.au/tutorials/piicodev-distance-sensor-vl53l1x-raspberry-pi-pico-guide.html). +- [Raspberry Pi](https://core-electronics.com.au/tutorials/piicodev-raspberrypi/piicodev-distance-sensor-vl53l1x-raspberry-pi-guide.html) + +# Usage +## Example +[main.py](https://github.com/CoreElectronics/CE-PiicoDev-VL53L1X-MicroPython-Module/blob/main/main.py) is a simple example to get started. +``` +from PiicoDev_VL53L1X import PiicoDev_VL53L1X +from time import sleep + +distSensor = PiicoDev_VL53L1X() + +while True: + dist = distSensor.read() # read the distance in millimetres + print(str(dist) + " mm") # convert the number to a string and print + sleep(0.1) +``` +## Details +### PiicoDev_VL53L1X(bus=, freq=, sda=, scl=, address=0x29) +Parameter | Type | Range | Default | Description +--- | --- | --- | --- | --- +bus | int | 0,1 | Raspberry Pi Pico: 0, Raspberry Pi: 1 | I2C Bus. Ignored on Micro:bit +freq | int | 100-1000000 | Device dependent | I2C Bus frequency (Hz). Ignored on Raspberry Pi +sda | Pin | Device Dependent | Device Dependent | I2C SDA Pin. Implemented on Raspberry Pi Pico only +scl | Pin | Device Dependent | Device Dependent | I2C SCL Pin. Implemented on Raspberry Pi Pico only +address | int | 0x29 | 0x29 | The VL53L1X Distance Sensor address can be set using the change_id function + +### PiicoDev_VL53L1X.read() +Parameter | Type | Unit | Description +--- | --- | --- | --- +returned | int | mm | Range # License This project is open source - please review the LICENSE.md file for further licensing information. From 913f25d0c163860121970a52e5013a6f5312adb9 Mon Sep 17 00:00:00 2001 From: James Farragher <105866039+James-Farragher@users.noreply.github.com> Date: Thu, 19 May 2022 23:49:05 +1000 Subject: [PATCH 13/17] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 535830b..6b8c649 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,16 @@ bus | int | 0,1 | Raspberry Pi Pico: 0, Raspberry Pi: 1 | I2C Bus. Ignored on M freq | int | 100-1000000 | Device dependent | I2C Bus frequency (Hz). Ignored on Raspberry Pi sda | Pin | Device Dependent | Device Dependent | I2C SDA Pin. Implemented on Raspberry Pi Pico only scl | Pin | Device Dependent | Device Dependent | I2C SCL Pin. Implemented on Raspberry Pi Pico only -address | int | 0x29 | 0x29 | The VL53L1X Distance Sensor address can be set using the change_id function +address | int | 0x29 | 0x29 | The VL53L1X Distance Sensor address can be set using the change_addr function. ### PiicoDev_VL53L1X.read() Parameter | Type | Unit | Description --- | --- | --- | --- returned | int | mm | Range +### Using Multiple Sensors +Since the VL53L1X defaults to a single address, multiple sensors on the same bus will conflict. You can use the change_addr function to use something other than the default, but it will be lost on power down. You can use any spare GPIO on your microcontroller to drive the SHT (shutdown) pin low to disable all the sensors you are using, then bring them up one at a time to give them a unique address each time your code starts. Be sure to pick addresses that are at least 2 away from each other, as reading from the sensor uses your set address + 1. + # License This project is open source - please review the LICENSE.md file for further licensing information. From 5d7c92fcca44a32831d5f94e7b190740e0b0b560 Mon Sep 17 00:00:00 2001 From: James Farragher <105866039+James-Farragher@users.noreply.github.com> Date: Thu, 19 May 2022 23:53:48 +1000 Subject: [PATCH 14/17] Changed reference to "id" to "addr" I believe this removes confusion regarding the model_ID mentioned on L113. I skimmed past this in the docs and code because I didn't realise it changed the address --- PiicoDev_VL53L1X.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PiicoDev_VL53L1X.py b/PiicoDev_VL53L1X.py index 1268fee..d53e8dd 100644 --- a/PiicoDev_VL53L1X.py +++ b/PiicoDev_VL53L1X.py @@ -177,7 +177,7 @@ def read(self): #status = "OK" return final_crosstalk_corrected_range_mm_sd0 - def change_id(self, new_id): - self.writeReg(0x0001, new_id & 0x7F) + def change_addr(self, new_addr): + self.writeReg(0x0001, new_addr & 0x7F) sleep_ms(50) - self.addr = new_id + self.addr = new_addr From 75e6f56170bd57c07f948f3c62e96b47c07c1ef1 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 8 Jun 2022 04:12:36 +0000 Subject: [PATCH 15/17] Update min/PiicoDev_VL53L1X.py --- min/PiicoDev_VL53L1X.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/min/PiicoDev_VL53L1X.py b/min/PiicoDev_VL53L1X.py index c23896b..263ba52 100644 --- a/min/PiicoDev_VL53L1X.py +++ b/min/PiicoDev_VL53L1X.py @@ -21,4 +21,4 @@ def read(self): try:data=self.i2c.readfrom_mem(self.addr,137,17,addrsize=16) except:print(i2c_err_str.format(self.addr));return float('NaN') range_status=data[0];stream_count=data[2];dss_actual_effective_spads_sd0=(data[3]<<8)+data[4];ambient_count_rate_mcps_sd0=(data[7]<<8)+data[8];final_crosstalk_corrected_range_mm_sd0=(data[13]<<8)+data[14];peak_signal_count_rate_crosstalk_corrected_mcps_sd0=(data[15]<<8)+data[16];return final_crosstalk_corrected_range_mm_sd0 - def change_id(self,new_id):self.writeReg(1,new_id&127);sleep_ms(50);self.addr=new_id \ No newline at end of file + def change_addr(self,new_addr):self.writeReg(1,new_addr&127);sleep_ms(50);self.addr=new_addr \ No newline at end of file From 88a23347b39ab35dd655b490f923c758438b98f9 Mon Sep 17 00:00:00 2001 From: Michael Ruppe <73618042+CoreProduction@users.noreply.github.com> Date: Tue, 4 Jul 2023 09:27:35 +1000 Subject: [PATCH 16/17] populate status property --- PiicoDev_VL53L1X.py | 50 +++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/PiicoDev_VL53L1X.py b/PiicoDev_VL53L1X.py index d53e8dd..2a9baae 100644 --- a/PiicoDev_VL53L1X.py +++ b/PiicoDev_VL53L1X.py @@ -108,6 +108,7 @@ def __init__(self, bus=None, freq=None, sda=None, scl=None, address=0x29): print(compat_str) self.i2c = create_unified_i2c(bus=bus, freq=freq, sda=sda, scl=scl) self.addr = address + self.status = None self.reset() sleep_ms(1) if self.read_model_id() != 0xEACC: @@ -135,6 +136,7 @@ def reset(self): self.writeReg(0x0000, 0x00) sleep_ms(100) self.writeReg(0x0000, 0x01) + def read(self): try: data = self.i2c.readfrom_mem(self.addr, 0x0089, 17, addrsize=16) # RESULT__RANGE_STATUS @@ -151,30 +153,30 @@ def read(self): # phase_sd0 = (data[11]<<8) + data[12] final_crosstalk_corrected_range_mm_sd0 = (data[13]<<8) + data[14] peak_signal_count_rate_crosstalk_corrected_mcps_sd0 = (data[15]<<8) + data[16] - #status = None - #if range_status in (17, 2, 1, 3): - #status = "HardwareFail" - #elif range_status == 13: - #status = "MinRangeFail" - #elif range_status == 18: - #status = "SynchronizationInt" - #elif range_status == 5: - #status = "OutOfBoundsFail" - #elif range_status == 4: - #status = "SignalFail" - #elif range_status == 6: - #status = "SignalFail" - #elif range_status == 7: - #status = "WrapTargetFail" - #elif range_status == 12: - #status = "XtalkSignalFail" - #elif range_status == 8: - #status = "RangeValidMinRangeClipped" - #elif range_status == 9: - #if stream_count == 0: - #status = "RangeValidNoWrapCheckFail" - #else: - #status = "OK" + status = None + if range_status in (17, 2, 1, 3): + self.status = "HardwareFail" + elif range_status == 13: + self.status = "MinRangeFail" + elif range_status == 18: + self.status = "SynchronizationInt" + elif range_status == 5: + self.status = "OutOfBoundsFail" + elif range_status == 4: + self.status = "SignalFail" + elif range_status == 6: + self.status = "SignalFail" + elif range_status == 7: + self.status = "WrapTargetFail" + elif range_status == 12: + self.status = "XtalkSignalFail" + elif range_status == 8: + self.status = "RangeValidMinRangeClipped" + elif range_status == 9: + if stream_count == 0: + self.status = "RangeValidNoWrapCheckFail" + else: + self.status = "OK" return final_crosstalk_corrected_range_mm_sd0 def change_addr(self, new_addr): From 47f0cff02306b80d4e03417d355e3e566409e634 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 3 Jul 2023 23:27:57 +0000 Subject: [PATCH 17/17] Update min/PiicoDev_VL53L1X.py --- min/PiicoDev_VL53L1X.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/min/PiicoDev_VL53L1X.py b/min/PiicoDev_VL53L1X.py index 263ba52..b96cecf 100644 --- a/min/PiicoDev_VL53L1X.py +++ b/min/PiicoDev_VL53L1X.py @@ -1,5 +1,5 @@ _A=None -from PiicoDev_Unified import * +from PiicoDev_Unified import* compat_str='\nUnified PiicoDev library out of date. Get the latest module: https://piico.dev/unified \n' VL51L1X_DEFAULT_CONFIGURATION=bytes([0,0,0,1,2,0,2,8,0,8,16,1,1,0,0,0,0,255,0,15,0,0,0,0,0,32,11,0,0,2,10,33,0,0,5,0,0,0,0,200,0,0,56,255,1,0,8,0,0,1,219,15,1,241,13,1,104,0,128,8,184,0,0,0,0,15,137,0,0,0,0,0,0,0,1,15,13,14,14,0,0,2,199,255,155,0,0,0,1,1,64]) class PiicoDev_VL53L1X: @@ -8,7 +8,7 @@ def __init__(self,bus=_A,freq=_A,sda=_A,scl=_A,address=41): if compat_ind>=1:0 else:print(compat_str) except:print(compat_str) - self.i2c=create_unified_i2c(bus=bus,freq=freq,sda=sda,scl=scl);self.addr=address;self.reset();sleep_ms(1) + self.i2c=create_unified_i2c(bus=bus,freq=freq,sda=sda,scl=scl);self.addr=address;self.status=_A;self.reset();sleep_ms(1) if self.read_model_id()!=60108:raise RuntimeError('Failed to find expected ID register values. Check wiring!') self.i2c.writeto_mem(self.addr,45,VL51L1X_DEFAULT_CONFIGURATION,addrsize=16);sleep_ms(100);self.writeReg16Bit(30,self.readReg16Bit(34)*4);sleep_ms(200) def writeReg(self,reg,value):return self.i2c.writeto_mem(self.addr,reg,bytes([value]),addrsize=16) @@ -18,7 +18,21 @@ def readReg16Bit(self,reg):data=self.i2c.readfrom_mem(self.addr,reg,2,addrsize=1 def read_model_id(self):return self.readReg16Bit(271) def reset(self):self.writeReg(0,0);sleep_ms(100);self.writeReg(0,1) def read(self): + A='SignalFail' try:data=self.i2c.readfrom_mem(self.addr,137,17,addrsize=16) except:print(i2c_err_str.format(self.addr));return float('NaN') - range_status=data[0];stream_count=data[2];dss_actual_effective_spads_sd0=(data[3]<<8)+data[4];ambient_count_rate_mcps_sd0=(data[7]<<8)+data[8];final_crosstalk_corrected_range_mm_sd0=(data[13]<<8)+data[14];peak_signal_count_rate_crosstalk_corrected_mcps_sd0=(data[15]<<8)+data[16];return final_crosstalk_corrected_range_mm_sd0 + range_status=data[0];stream_count=data[2];dss_actual_effective_spads_sd0=(data[3]<<8)+data[4];ambient_count_rate_mcps_sd0=(data[7]<<8)+data[8];final_crosstalk_corrected_range_mm_sd0=(data[13]<<8)+data[14];peak_signal_count_rate_crosstalk_corrected_mcps_sd0=(data[15]<<8)+data[16];status=_A + if range_status in(17,2,1,3):self.status='HardwareFail' + elif range_status==13:self.status='MinRangeFail' + elif range_status==18:self.status='SynchronizationInt' + elif range_status==5:self.status='OutOfBoundsFail' + elif range_status==4:self.status=A + elif range_status==6:self.status=A + elif range_status==7:self.status='WrapTargetFail' + elif range_status==12:self.status='XtalkSignalFail' + elif range_status==8:self.status='RangeValidMinRangeClipped' + elif range_status==9: + if stream_count==0:self.status='RangeValidNoWrapCheckFail' + else:self.status='OK' + return final_crosstalk_corrected_range_mm_sd0 def change_addr(self,new_addr):self.writeReg(1,new_addr&127);sleep_ms(50);self.addr=new_addr \ No newline at end of file