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() {