From 38fc23f1f3f1fa9ba3b3d2b66296d8a2aaab6407 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Sat, 25 Jan 2020 14:19:38 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=AA=E3=83=95=E3=82=A1=E3=83=AC?= =?UTF-8?q?=E3=83=B3=E3=82=B9=E3=82=A8=E3=83=A9=E3=83=BC=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=20resolved=20#7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ads1015/ads1015.mjs | 99 +++++++++---------- packages/ads1x15/ads1x15.mjs | 7 +- packages/ak8963/ak8963.mjs | 3 + packages/bh1750/bh1750.mjs | 5 +- .../grove-oled-display/grove-oled-display.mjs | 5 +- packages/ina219/ina219.mjs | 5 +- packages/mlx90614/mlx90614.mjs | 4 + packages/vl53l0x/vl53l0x.mjs | 6 +- packages/vl53l1x/vl53l1x.mjs | 17 ++-- 9 files changed, 80 insertions(+), 71 deletions(-) diff --git a/packages/ads1015/ads1015.mjs b/packages/ads1015/ads1015.mjs index 998c2cd..a385229 100755 --- a/packages/ads1015/ads1015.mjs +++ b/packages/ads1015/ads1015.mjs @@ -1,61 +1,58 @@ -var ADS1015 = function(i2cPort,slaveAddress){ +// @ts-check + +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + +var ADS1015 = function (i2cPort, slaveAddress) { this.i2cPort = i2cPort; this.slaveAddress = slaveAddress; this.i2cSlave = null; }; ADS1015.prototype = { - init: function(){ - return new Promise((resolve, reject) => { - this.i2cPort.open(this.slaveAddress).then((i2cSlave) => { - this.i2cSlave = i2cSlave; - resolve(); - },(err) => { - console.error("ADS1015.init() Error: "+error.message); - reject(err); - }); - }); + init: async function () { + try { + this.i2cSlave = await this.i2cPort.open(this.slaveAddress) + } catch (error) { + console.error("ADS1015.init() Error: " + error.message); + throw error; + } }, - read: function(channel){ - return new Promise((resolve, reject)=>{ - if(this.i2cSlave){ - if((channel > 3)||(channel < 0)){ - console.error("ADS1015.read: channel error"+channel); - err.code = 5; - reject(err.message); - } - var config = 0x4000 + (channel * 0x1000); // ADC channel - config |= 0x8000; // Set 'start single-conversion' bit - config |= 0x0003; // Disable the comparator (default val) - config |= 0x0080; // 1600 samples per second (default) - config |= 0x0100; // Power-down single-shot mode (default) - config |= 0x0200; // +/-4.096V range = Gain 1 - var confL = config >> 8; - var confH = config & 0x00ff; - var data = confH | confL; - this.i2cSlave.write16(0x01, data).then((v) => { - setTimeout(()=>{ - this.i2cSlave.read16(0).then((v) =>{ - var vH = (v & 0x00ff) << 8; - var vL = (v >> 8)& 0x00ffff; - var value = (vH | vL) >> 4; - resolve(value); - },(err) => { - console.error("ADS1015.read: read16(0) error"+err.message); - err.code = 3; - reject(err.message); - }); - },10); - }, (err) => { - console.error("ADS1015.read: write16(0,config) error"+err.message); - err.code = 2; - reject(err.message); - }); - }else{ - err.code = 1; - reject(err.message); - } - }); + read: async function (channel) { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + if ((channel < 0) || (3 < channel)) { + throw new Error("ADS1015.read: channel error" + channel); + } + + var config = 0x4000 + (channel * 0x1000); // ADC channel + config |= 0x8000; // Set 'start single-conversion' bit + config |= 0x0003; // Disable the comparator (default val) + config |= 0x0080; // 1600 samples per second (default) + config |= 0x0100; // Power-down single-shot mode (default) + config |= 0x0200; // +/-4.096V range = Gain 1 + var confL = config >> 8; + var confH = config & 0x00ff; + var data = confH | confL; + + try { + await this.i2cSlave.write16(0x01, data); + } catch (error) { + throw new Error("ADS1015.read: write16(0,config) error" + error.message); + } + await sleep(10); + var v; + try { + v = await this.i2cSlave.read16(0); + } catch (error) { + throw new Error("ADS1015.read: read16(0) error" + error.message); + } + var vH = (v & 0x00ff) << 8; + var vL = (v >> 8) & 0x00ffff; + var value = (vH | vL) >> 4; + return value; } }; diff --git a/packages/ads1x15/ads1x15.mjs b/packages/ads1x15/ads1x15.mjs index ea6e74a..b5620f5 100755 --- a/packages/ads1x15/ads1x15.mjs +++ b/packages/ads1x15/ads1x15.mjs @@ -3,6 +3,9 @@ // 2018/12/22 Satoru Takagi // 2019/10/01 Support Differential Mode +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var ADS1x15 = function(i2cPort,slaveAddress){ this.i2cPort = i2cPort; this.slaveAddress = slaveAddress; @@ -30,9 +33,9 @@ ADS1x15.prototype = { console.log("set amplifier to +-" + this.amplifierTable[this.amplifierConf]); var i2cSlave = await this.i2cPort.open(this.slaveAddress); this.i2cSlave = i2cSlave; - } catch(err){ + } catch(error){ console.log("ADS1015.init() Error: "+error.message); - throw Error( err ); + throw Error(error); } }, read: async function(channel){ diff --git a/packages/ak8963/ak8963.mjs b/packages/ak8963/ak8963.mjs index 6268dad..f54138e 100755 --- a/packages/ak8963/ak8963.mjs +++ b/packages/ak8963/ak8963.mjs @@ -1,6 +1,9 @@ // Ported from https://github.com/tuupola/micropython-mpu9250/blob/master/ak8963.py // by Satoru Takagi +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var AK8963 = function(i2cPort,slaveAddress){ this.devConst={ _WIA : (0x00), diff --git a/packages/bh1750/bh1750.mjs b/packages/bh1750/bh1750.mjs index a0a8154..bbc3f5e 100755 --- a/packages/bh1750/bh1750.mjs +++ b/packages/bh1750/bh1750.mjs @@ -2,7 +2,10 @@ // ported from https://gist.github.com/oskar456/95c66d564c58361ecf9f // by Satoru Takagi -var BH1750 = function(i2cPort,slaveAddress){ +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + +var BH1750 = function (i2cPort, slaveAddress) { this.digP = []; this.i2cPort = i2cPort; this.i2cSlave = null; diff --git a/packages/grove-oled-display/grove-oled-display.mjs b/packages/grove-oled-display/grove-oled-display.mjs index c45dbac..70d6370 100755 --- a/packages/grove-oled-display/grove-oled-display.mjs +++ b/packages/grove-oled-display/grove-oled-display.mjs @@ -1,3 +1,4 @@ +// @ts-check // Source : http://wiki.seeedstudio.com/wiki/Grove_-_OLED_Display_128*64 const OLED_CONST = { @@ -178,12 +179,12 @@ OledDisplay.prototype = { this.registerQueue(OLED_CONST.commandMode,Brightness); }, setHorizontalModeQ: function(){ - this.addressingMode = horizontalMode; + this.addressingMode = OLED_CONST.horizontalMode; this.registerQueue(OLED_CONST.commandMode,0x20); this.registerQueue(OLED_CONST.commandMode,0); }, setPageModeQ: function(){ - this.addressingMode = pageMode; + this.addressingMode = OLED_CONST.pageMode; this.registerQueue(OLED_CONST.commandMode,0x20); this.registerQueue(OLED_CONST.commandMode,0x02); }, diff --git a/packages/ina219/ina219.mjs b/packages/ina219/ina219.mjs index 603e30a..d34c999 100644 --- a/packages/ina219/ina219.mjs +++ b/packages/ina219/ina219.mjs @@ -3,6 +3,9 @@ // Ported from https://github.com/chrisb2/pi_ina219 // Programmed by Satoru Takagi +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var INA219 = function(i2cPort, slaveAddress) { if (!slaveAddress) { slaveAddress = 0x40; @@ -220,7 +223,7 @@ INA219.prototype = { this.__GAIN_VOLTS[gain] ); await this._configure_gain(gain); - sleep(1); + await sleep(1); } else { console.log("Device limit reach, gain cannot be increased"); throw "DeviceRangeError: " + this.__GAIN_VOLTS[gain]; diff --git a/packages/mlx90614/mlx90614.mjs b/packages/mlx90614/mlx90614.mjs index ec905fd..8876e9d 100644 --- a/packages/mlx90614/mlx90614.mjs +++ b/packages/mlx90614/mlx90614.mjs @@ -1,8 +1,12 @@ +// @ts-check // MLX90614 pyro termometer driver for CHIRIMEN raspberry pi3 // Temperature and Humidity I2C Sensor // based on https://github.com/CRImier/python-MLX90614/blob/master/mlx90614.py // Programmed by Satoru Takagi +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var MLX90614 = function(i2cPort, slaveAddress) { if (!slaveAddress) { slaveAddress = 0x5a; diff --git a/packages/vl53l0x/vl53l0x.mjs b/packages/vl53l0x/vl53l0x.mjs index 322405c..7b06f2a 100755 --- a/packages/vl53l0x/vl53l0x.mjs +++ b/packages/vl53l0x/vl53l0x.mjs @@ -84,7 +84,7 @@ var VL53L0X = function(i2cPort, slaveAddress, io_timeout_s) { VL53L0X.prototype = { _decode_timeout: function(val) { - return (val & 0xff) * math.pow(2.0, (val & 0xff00) >> 8) + 1; + return (val & 0xff) * Math.pow(2.0, (val & 0xff00) >> 8) + 1; }, _encode_timeout: function(timeout_mclks) { var timeout_mclks = timeout_mclks & 0xffff; @@ -527,10 +527,6 @@ VL53L0X.prototype = { macro_period_ns ); }, - _decode_timeout: function(val) { - // format: "(LSByte * 2^MSByte) + 1" - return (val & 0xff) * Math.pow(2.0, (val & 0xff00) >> 8) + 1; - }, _get_spad_info: async function() { // # Get reference SPAD count and type, returned as a 2-tuple of // # count and boolean is_aperture. Based on code from: diff --git a/packages/vl53l1x/vl53l1x.mjs b/packages/vl53l1x/vl53l1x.mjs index 220492b..6a75055 100755 --- a/packages/vl53l1x/vl53l1x.mjs +++ b/packages/vl53l1x/vl53l1x.mjs @@ -5,6 +5,9 @@ // Not VL53L0X driver // Programmed by Satoru Takagi +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var VL53L1X = function(i2cPort, slaveAddress) { if (!slaveAddress) { slaveAddress = 0x29; @@ -1213,7 +1216,7 @@ var VL53L1X = function(i2cPort, slaveAddress) { VL53L1X.prototype = { init: async function(mode, io_2v8) { this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - sleep(10); + await sleep(10); if ((await this.readReg16Bit(this.IDENTIFICATION__MODEL_ID)) != 0xeacc) { throw "Failed to find expected ID register values. Check wiring!"; } else { @@ -1244,8 +1247,8 @@ VL53L1X.prototype = { // VL53L1_DataInit() begin if (io_2v8) { await this.writeReg( - PAD_I2C_HV__EXTSUP_CONFIG, - (await this.readReg(PAD_I2C_HV__EXTSUP_CONFIG)) | 0x01 + this.PAD_I2C_HV__EXTSUP_CONFIG, + (await this.readReg(this.PAD_I2C_HV__EXTSUP_CONFIG)) | 0x01 ); } @@ -1490,7 +1493,7 @@ VL53L1X.prototype = { await this.setMeasurementTimingBudget(budget_us); // console.log("budget_us:",budget_us); // save mode so it can be returned by getDistanceMode() - distance_mode = mode; + this.distance_mode = mode; return true; }, @@ -1616,11 +1619,7 @@ VL53L1X.prototype = { var IntPol = await this.getInterruptPolarity(); var Temp = await this.readReg(this.GPIO__TIO_HV_STATUS); // console.log("IntPol,GPIO__TIO_HV_STATUS : ",IntPol,Temp); - if ((Temp & 1) == IntPol) { - isDataReady = 1; - } else { - isDataReady = 0; - } + var isDataReady = (Temp & 1) == IntPol ? 1 : 0; return isDataReady; }, getInterruptPolarity: async function() { From 32920809aa9e9f44dc349bc2ba8f0ccaf39a5055 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Sat, 25 Jan 2020 14:19:53 +0900 Subject: [PATCH 2/2] yarn build --- packages/ads1015/ads1015.js | 99 +++++++++---------- packages/ads1x15/ads1x15.js | 7 +- packages/ak8963/ak8963.js | 3 + packages/bh1750/bh1750.js | 5 +- .../grove-oled-display/grove-oled-display.js | 5 +- packages/ina219/ina219.js | 5 +- packages/mlx90614/mlx90614.js | 4 + packages/vl53l0x/vl53l0x.js | 6 +- packages/vl53l1x/vl53l1x.js | 17 ++-- 9 files changed, 80 insertions(+), 71 deletions(-) diff --git a/packages/ads1015/ads1015.js b/packages/ads1015/ads1015.js index c726698..a000caa 100644 --- a/packages/ads1015/ads1015.js +++ b/packages/ads1015/ads1015.js @@ -4,64 +4,61 @@ (global = global || self, global.ADS1015 = factory()); }(this, (function () { 'use strict'; - var ADS1015 = function(i2cPort,slaveAddress){ + // @ts-check + + /** @param {number} ms Delay for a number of milliseconds. */ + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + + var ADS1015 = function (i2cPort, slaveAddress) { this.i2cPort = i2cPort; this.slaveAddress = slaveAddress; this.i2cSlave = null; }; ADS1015.prototype = { - init: function(){ - return new Promise((resolve, reject) => { - this.i2cPort.open(this.slaveAddress).then((i2cSlave) => { - this.i2cSlave = i2cSlave; - resolve(); - },(err) => { - console.error("ADS1015.init() Error: "+error.message); - reject(err); - }); - }); + init: async function () { + try { + this.i2cSlave = await this.i2cPort.open(this.slaveAddress); + } catch (error) { + console.error("ADS1015.init() Error: " + error.message); + throw error; + } }, - read: function(channel){ - return new Promise((resolve, reject)=>{ - if(this.i2cSlave){ - if((channel > 3)||(channel < 0)){ - console.error("ADS1015.read: channel error"+channel); - err.code = 5; - reject(err.message); - } - var config = 0x4000 + (channel * 0x1000); // ADC channel - config |= 0x8000; // Set 'start single-conversion' bit - config |= 0x0003; // Disable the comparator (default val) - config |= 0x0080; // 1600 samples per second (default) - config |= 0x0100; // Power-down single-shot mode (default) - config |= 0x0200; // +/-4.096V range = Gain 1 - var confL = config >> 8; - var confH = config & 0x00ff; - var data = confH | confL; - this.i2cSlave.write16(0x01, data).then((v) => { - setTimeout(()=>{ - this.i2cSlave.read16(0).then((v) =>{ - var vH = (v & 0x00ff) << 8; - var vL = (v >> 8)& 0x00ffff; - var value = (vH | vL) >> 4; - resolve(value); - },(err) => { - console.error("ADS1015.read: read16(0) error"+err.message); - err.code = 3; - reject(err.message); - }); - },10); - }, (err) => { - console.error("ADS1015.read: write16(0,config) error"+err.message); - err.code = 2; - reject(err.message); - }); - }else{ - err.code = 1; - reject(err.message); - } - }); + read: async function (channel) { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + if ((channel < 0) || (3 < channel)) { + throw new Error("ADS1015.read: channel error" + channel); + } + + var config = 0x4000 + (channel * 0x1000); // ADC channel + config |= 0x8000; // Set 'start single-conversion' bit + config |= 0x0003; // Disable the comparator (default val) + config |= 0x0080; // 1600 samples per second (default) + config |= 0x0100; // Power-down single-shot mode (default) + config |= 0x0200; // +/-4.096V range = Gain 1 + var confL = config >> 8; + var confH = config & 0x00ff; + var data = confH | confL; + + try { + await this.i2cSlave.write16(0x01, data); + } catch (error) { + throw new Error("ADS1015.read: write16(0,config) error" + error.message); + } + await sleep(10); + var v; + try { + v = await this.i2cSlave.read16(0); + } catch (error) { + throw new Error("ADS1015.read: read16(0) error" + error.message); + } + var vH = (v & 0x00ff) << 8; + var vL = (v >> 8) & 0x00ffff; + var value = (vH | vL) >> 4; + return value; } }; diff --git a/packages/ads1x15/ads1x15.js b/packages/ads1x15/ads1x15.js index fa587ef..95b59cf 100644 --- a/packages/ads1x15/ads1x15.js +++ b/packages/ads1x15/ads1x15.js @@ -9,6 +9,9 @@ // 2018/12/22 Satoru Takagi // 2019/10/01 Support Differential Mode + /** @param {number} ms Delay for a number of milliseconds. */ + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var ADS1x15 = function(i2cPort,slaveAddress){ this.i2cPort = i2cPort; this.slaveAddress = slaveAddress; @@ -36,9 +39,9 @@ console.log("set amplifier to +-" + this.amplifierTable[this.amplifierConf]); var i2cSlave = await this.i2cPort.open(this.slaveAddress); this.i2cSlave = i2cSlave; - } catch(err){ + } catch(error){ console.log("ADS1015.init() Error: "+error.message); - throw Error( err ); + throw Error(error); } }, read: async function(channel){ diff --git a/packages/ak8963/ak8963.js b/packages/ak8963/ak8963.js index c06714d..874572e 100644 --- a/packages/ak8963/ak8963.js +++ b/packages/ak8963/ak8963.js @@ -7,6 +7,9 @@ // Ported from https://github.com/tuupola/micropython-mpu9250/blob/master/ak8963.py // by Satoru Takagi + /** @param {number} ms Delay for a number of milliseconds. */ + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var AK8963 = function(i2cPort,slaveAddress){ this.devConst={ _WIA : (0x00), diff --git a/packages/bh1750/bh1750.js b/packages/bh1750/bh1750.js index 09ea392..a0cbae0 100644 --- a/packages/bh1750/bh1750.js +++ b/packages/bh1750/bh1750.js @@ -8,7 +8,10 @@ // ported from https://gist.github.com/oskar456/95c66d564c58361ecf9f // by Satoru Takagi - var BH1750 = function(i2cPort,slaveAddress){ + /** @param {number} ms Delay for a number of milliseconds. */ + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + + var BH1750 = function (i2cPort, slaveAddress) { this.digP = []; this.i2cPort = i2cPort; this.i2cSlave = null; diff --git a/packages/grove-oled-display/grove-oled-display.js b/packages/grove-oled-display/grove-oled-display.js index 9d9a05d..f1fe1de 100644 --- a/packages/grove-oled-display/grove-oled-display.js +++ b/packages/grove-oled-display/grove-oled-display.js @@ -4,6 +4,7 @@ (global = global || self, global.OledDisplay = factory()); }(this, (function () { 'use strict'; + // @ts-check // Source : http://wiki.seeedstudio.com/wiki/Grove_-_OLED_Display_128*64 const OLED_CONST = { @@ -184,12 +185,12 @@ this.registerQueue(OLED_CONST.commandMode,Brightness); }, setHorizontalModeQ: function(){ - this.addressingMode = horizontalMode; + this.addressingMode = OLED_CONST.horizontalMode; this.registerQueue(OLED_CONST.commandMode,0x20); this.registerQueue(OLED_CONST.commandMode,0); }, setPageModeQ: function(){ - this.addressingMode = pageMode; + this.addressingMode = OLED_CONST.pageMode; this.registerQueue(OLED_CONST.commandMode,0x20); this.registerQueue(OLED_CONST.commandMode,0x02); }, diff --git a/packages/ina219/ina219.js b/packages/ina219/ina219.js index 7c2012e..9f187cc 100644 --- a/packages/ina219/ina219.js +++ b/packages/ina219/ina219.js @@ -9,6 +9,9 @@ // Ported from https://github.com/chrisb2/pi_ina219 // Programmed by Satoru Takagi + /** @param {number} ms Delay for a number of milliseconds. */ + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var INA219 = function(i2cPort, slaveAddress) { if (!slaveAddress) { slaveAddress = 0x40; @@ -226,7 +229,7 @@ this.__GAIN_VOLTS[gain] ); await this._configure_gain(gain); - sleep(1); + await sleep(1); } else { console.log("Device limit reach, gain cannot be increased"); throw "DeviceRangeError: " + this.__GAIN_VOLTS[gain]; diff --git a/packages/mlx90614/mlx90614.js b/packages/mlx90614/mlx90614.js index 1b446ab..7fc1e16 100644 --- a/packages/mlx90614/mlx90614.js +++ b/packages/mlx90614/mlx90614.js @@ -4,11 +4,15 @@ (global = global || self, global.MLX90614 = factory()); }(this, (function () { 'use strict'; + // @ts-check // MLX90614 pyro termometer driver for CHIRIMEN raspberry pi3 // Temperature and Humidity I2C Sensor // based on https://github.com/CRImier/python-MLX90614/blob/master/mlx90614.py // Programmed by Satoru Takagi + /** @param {number} ms Delay for a number of milliseconds. */ + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var MLX90614 = function(i2cPort, slaveAddress) { if (!slaveAddress) { slaveAddress = 0x5a; diff --git a/packages/vl53l0x/vl53l0x.js b/packages/vl53l0x/vl53l0x.js index 59baefd..5730242 100644 --- a/packages/vl53l0x/vl53l0x.js +++ b/packages/vl53l0x/vl53l0x.js @@ -90,7 +90,7 @@ VL53L0X.prototype = { _decode_timeout: function(val) { - return (val & 0xff) * math.pow(2.0, (val & 0xff00) >> 8) + 1; + return (val & 0xff) * Math.pow(2.0, (val & 0xff00) >> 8) + 1; }, _encode_timeout: function(timeout_mclks) { var timeout_mclks = timeout_mclks & 0xffff; @@ -529,10 +529,6 @@ macro_period_ns ); }, - _decode_timeout: function(val) { - // format: "(LSByte * 2^MSByte) + 1" - return (val & 0xff) * Math.pow(2.0, (val & 0xff00) >> 8) + 1; - }, _get_spad_info: async function() { // # Get reference SPAD count and type, returned as a 2-tuple of // # count and boolean is_aperture. Based on code from: diff --git a/packages/vl53l1x/vl53l1x.js b/packages/vl53l1x/vl53l1x.js index 895a682..1731390 100644 --- a/packages/vl53l1x/vl53l1x.js +++ b/packages/vl53l1x/vl53l1x.js @@ -11,6 +11,9 @@ // Not VL53L0X driver // Programmed by Satoru Takagi + /** @param {number} ms Delay for a number of milliseconds. */ + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var VL53L1X = function(i2cPort, slaveAddress) { if (!slaveAddress) { slaveAddress = 0x29; @@ -1219,7 +1222,7 @@ VL53L1X.prototype = { init: async function(mode, io_2v8) { this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - sleep(10); + await sleep(10); if ((await this.readReg16Bit(this.IDENTIFICATION__MODEL_ID)) != 0xeacc) { throw "Failed to find expected ID register values. Check wiring!"; } else { @@ -1250,8 +1253,8 @@ // VL53L1_DataInit() begin if (io_2v8) { await this.writeReg( - PAD_I2C_HV__EXTSUP_CONFIG, - (await this.readReg(PAD_I2C_HV__EXTSUP_CONFIG)) | 0x01 + this.PAD_I2C_HV__EXTSUP_CONFIG, + (await this.readReg(this.PAD_I2C_HV__EXTSUP_CONFIG)) | 0x01 ); } @@ -1495,7 +1498,7 @@ await this.setMeasurementTimingBudget(budget_us); // console.log("budget_us:",budget_us); // save mode so it can be returned by getDistanceMode() - distance_mode = mode; + this.distance_mode = mode; return true; }, @@ -1621,11 +1624,7 @@ var IntPol = await this.getInterruptPolarity(); var Temp = await this.readReg(this.GPIO__TIO_HV_STATUS); // console.log("IntPol,GPIO__TIO_HV_STATUS : ",IntPol,Temp); - if ((Temp & 1) == IntPol) { - isDataReady = 1; - } else { - isDataReady = 0; - } + var isDataReady = (Temp & 1) == IntPol ? 1 : 0; return isDataReady; }, getInterruptPolarity: async function() {