diff --git a/packages/ads1015/ads1015.mjs b/packages/ads1015/ads1015.mjs index a385229..be767a7 100755 --- a/packages/ads1015/ads1015.mjs +++ b/packages/ads1015/ads1015.mjs @@ -11,12 +11,7 @@ var ADS1015 = function (i2cPort, slaveAddress) { ADS1015.prototype = { init: async function () { - try { - this.i2cSlave = await this.i2cPort.open(this.slaveAddress) - } catch (error) { - console.error("ADS1015.init() Error: " + error.message); - throw error; - } + this.i2cSlave = await this.i2cPort.open(this.slaveAddress); }, read: async function (channel) { if (this.i2cSlave == null) { @@ -36,19 +31,9 @@ ADS1015.prototype = { 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 this.i2cSlave.write16(0x01, data); 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 v = await this.i2cSlave.read16(0); var vH = (v & 0x00ff) << 8; var vL = (v >> 8) & 0x00ffff; var value = (vH | vL) >> 4; diff --git a/packages/ads1x15/ads1x15.mjs b/packages/ads1x15/ads1x15.mjs index b5620f5..800efc5 100755 --- a/packages/ads1x15/ads1x15.mjs +++ b/packages/ads1x15/ads1x15.mjs @@ -17,26 +17,18 @@ var ADS1x15 = function(i2cPort,slaveAddress){ ADS1x15.prototype = { init: async function(isAds1115, amplifierSelection){ - try{ - if ( isAds1115 ){ - this.convertDelay = 80; // 測定が安定するには・・・ - this.bitShift = 0; - console.log("ADS1115.init (16bitADC) OK"); - } else { - this.convertDelay = 10; // 本来は1ms? - this.bitShift = 4; - console.log("ADS1015.init (12bitADC) OK"); - } - if ( amplifierSelection != undefined && amplifierSelection>=0 && amplifierSelection < 8){ - this.amplifierConf = amplifierSelection; - } - console.log("set amplifier to +-" + this.amplifierTable[this.amplifierConf]); - var i2cSlave = await this.i2cPort.open(this.slaveAddress); - this.i2cSlave = i2cSlave; - } catch(error){ - console.log("ADS1015.init() Error: "+error.message); - throw Error(error); + if (isAds1115) { + this.convertDelay = 80; // 測定が安定するには・・・ + this.bitShift = 0; + } else { + this.convertDelay = 10; // 本来は1ms? + this.bitShift = 4; + } + if (amplifierSelection != undefined && amplifierSelection >= 0 && amplifierSelection < 8) { + this.amplifierConf = amplifierSelection; } + var i2cSlave = await this.i2cPort.open(this.slaveAddress); + this.i2cSlave = i2cSlave; }, read: async function(channel){ var config; @@ -50,25 +42,23 @@ ADS1x15.prototype = { config= 0x3000; } else{ channel = Number(channel); - if((channel > 3)||(channel < 0)){ - console.log("ADS1x15.read: channel error "+channel); - throw Error("ADS1x15.read: channel error "+channel); + if ((channel < 0) || (3 < channel)) { + throw new Error("ADS1x15.read: channel error " + channel); } else { channel = Number(channel); config = 0x4000 + (channel * 0x1000); // ADC channel } } - if(!this.i2cSlave){ - console.log("i2cSlave is gone....."); - throw Error("i2cSlave is gone....."); + + if (this.i2cSlave == null) { + throw new Error("i2cSlave is gone....."); } -// console.log((config).toString(16)); + config |= 0x8000; // Set 'start single-conversion' bit config |= 0x0003; // Disable the comparator (default val) config |= 0x0080; // 1600SPS(samples per second)(ADS1015),128SPS(ADS1115) (default) config |= 0x0100; // Power-down single-shot mode (default) config |= (this.amplifierConf << 9); // 0x0200; // +/-4.096V range = Gain 1 -// console.log((this.amplifierConf << 9).toString(16)); var confL = config >> 8; var confH = config & 0x00ff; var data = confH | confL; @@ -84,22 +74,10 @@ ADS1x15.prototype = { var vL = (v >> 8)& 0x00ffff; var value = (vH | vL) >> this.bitShift; this.prevCh = channel; - return ( value ); + return value; }, - getSignedVal: function( w ){ - // Convert 16bit two's complement data to signed integer while maintaining endianness - // console.log("getSignedVal:",w.toString(16)," :" , w); - // console.log("getSignedVal:",w); - // var l = w >>> 8; - // var b = w & 0xff; - // var v = l + ( b << 8 ); - var v = w; -// console.log("Val:",w.toString(16),b.toString(16),l.toString(16),v.toString(16),b,l,v); - if ( v >= 0x8000 ){ - return ( - ((65535 - v ) + 1 ) ); - } else { - return(v); - } + getSignedVal: function (val) { + return new Int16Array([val])[0]; }, getVoltage: function(rawVal){ return ( this.amplifierTable[this.amplifierConf] * 2 * this.getSignedVal(rawVal) / ( this.bitShift == 4 ? 0x0fff : 0xffff ) ); diff --git a/packages/adt7410/adt7410.mjs b/packages/adt7410/adt7410.mjs index c0f38fe..cc99f68 100755 --- a/packages/adt7410/adt7410.mjs +++ b/packages/adt7410/adt7410.mjs @@ -1,32 +1,22 @@ -var ADT7410 = function(i2cPort,slaveAddress){ +var ADT7410 = function(i2cPort, slaveAddress) { this.i2cPort = i2cPort; this.i2cSlave = null; this.slaveAddress = slaveAddress; }; ADT7410.prototype = { - init: function(){ - return new Promise((resolve, reject)=>{ - this.i2cPort.open(this.slaveAddress).then((i2cSlave)=>{ - this.i2cSlave = i2cSlave; - console.log("init ok:"+this.i2cSlave); - resolve(); - },(err)=>{ - reject(err); - }); - }); + init: async function() { + this.i2cSlave = await this.i2cPort.open(this.slaveAddress); }, - read: function(){ - return new Promise(async (resolve, reject)=>{ - if(this.i2cSlave == null){ - reject("i2cSlave Address does'nt yet open!"); - }else{ - var MSB = await this.i2cSlave.read8(0x00); - var LSB = await this.i2cSlave.read8(0x01); - var data = ((MSB << 8) + LSB)/128.0; - resolve(data); - } - }); + read: async function() { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + var MSB = await this.i2cSlave.read8(0x00); + var LSB = await this.i2cSlave.read8(0x01); + var data = ((MSB << 8) + LSB) / 128.0; + return data; } }; diff --git a/packages/ak8963/ak8963.mjs b/packages/ak8963/ak8963.mjs index f54138e..2953ed0 100755 --- a/packages/ak8963/ak8963.mjs +++ b/packages/ak8963/ak8963.mjs @@ -61,11 +61,9 @@ AK8963.prototype = { this.i2cSlave = await this.i2cPort.open(this.slaveAddress); var whoamiVal = await this.i2cSlave.read8(cs._WIA); - if ( whoamiVal ==0x48){ - console.log("This device is AK8963 magnetometer"); - } else { - console.log("This device is NOT Supported..."); - return(null); + if (whoamiVal !== 0x48) { + console.error("This device is NOT Supported..."); + return null; } // Sensitivity adjustement values @@ -90,8 +88,6 @@ AK8963.prototype = { } else { this._so = cs._SO_14BIT; } - console.log("init AK8963 ok: this._adjustement :",this._adjustement ," this._so:",this._so ," this.cs:",cs); - }, readData: async function(){ var cs = this.devConst; @@ -99,7 +95,6 @@ AK8963.prototype = { var mx = this.getVal(await this.i2cSlave.read16(cs._HXL)); // read16 is little endian var my = this.getVal(await this.i2cSlave.read16(cs._HYL)); var mz = this.getVal(await this.i2cSlave.read16(cs._HZL)); - console.log(mx,my,mz); await this.i2cSlave.read8(cs._ST2) // Enable updating readings again //Apply factory axial sensitivy adjustements @@ -129,28 +124,15 @@ AK8963.prototype = { z: mz } }, - getVal: function( w ){ - // Convert to signed integer while maintaining endianness - // console.log("getVal:",w.toString(16)," :" , w); - // console.log("getVal:",w); - // var l = w >>> 8; - // var b = w & 0xff; - // var v = l + ( b << 8 ); - var v = w; -// console.log("Val:",w.toString(16),b.toString(16),l.toString(16),v.toString(16),b,l,v); - if ( v >= 0x8000 ){ - return ( - ((65535 - v ) + 1 ) ); - } else { - return(v); - } + getVal: function (val) { + return new Int16Array([val])[0]; }, calibrate: async function (count, delay){ - console.log("start calibrate: "); - if ( !count){ - count=128; + if (!count) { + count = 128; } - if ( !delay){ - delay=100; + if (!delay) { + delay = 100; } this._offset = [0, 0, 0]; @@ -174,7 +156,6 @@ AK8963.prototype = { minz = Math.min(minz, reading.z); maxz = Math.max(maxz, reading.z); count -= 1; - console.log(count," : " , reading); } // Hard iron correction @@ -196,11 +177,7 @@ AK8963.prototype = { var scale_z = avg_delta / avg_delta_z; this._scale = [scale_x, scale_y, scale_z]; - - console.log("end calibrate: offset:",this._offset," scale:",this._scale ); -// return self._offset, self._scale } - }; export default AK8963; diff --git a/packages/amg8833/amg8833.mjs b/packages/amg8833/amg8833.mjs index d59d6bd..7ef5e69 100755 --- a/packages/amg8833/amg8833.mjs +++ b/packages/amg8833/amg8833.mjs @@ -19,15 +19,10 @@ var AMG8833 = function(i2cPort,slaveAddress){ AMG8833.prototype = { init: async function(){ this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - console.log("init ok:"+this.i2cSlave); await this.setup(); }, - getSVal: function(val){ - if ( val & 0x8000 ){ - return (( -val ^ 0xFFFF) + 1); - } else { - return ( val ); - } + getSVal: function (val) { + return new Int16Array([val])[0]; }, setup: async function(){ @@ -44,9 +39,7 @@ AMG8833.prototype = { await this.i2cSlave.write8(0x1F,0x00); // get sensorTemperature - var sensorTemp = await this.i2cSlave.read16(0x0E); - - console.log("sensorTemperature[deg]:",sensorTemp*0.0625); + await this.i2cSlave.read16(0x0E); }, readData: async function(){ var tdata = []; @@ -74,7 +67,6 @@ AMG8833.prototype = { ansR.push(tdata[i*8+j]); } ans.push(ansR); -// console.log(msg); } return ( ans ); diff --git a/packages/arduino-stepping-motor/arduino-stepping-motor.mjs b/packages/arduino-stepping-motor/arduino-stepping-motor.mjs index f86b385..3642ce3 100644 --- a/packages/arduino-stepping-motor/arduino-stepping-motor.mjs +++ b/packages/arduino-stepping-motor/arduino-stepping-motor.mjs @@ -1,105 +1,80 @@ -var SteppingMotor = function(i2cPort,slaveAddress){ +// @ts-check + +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + +var SteppingMotor = function(i2cPort, slaveAddress) { this.i2cPort = i2cPort; this.i2cSlave = null; this.slaveAddress = slaveAddress; }; SteppingMotor.prototype = { - init: function(){ - return new Promise((resolve, reject)=>{ - this.i2cPort.open(this.slaveAddress).then(async (i2cSlave)=>{ - this.i2cSlave = i2cSlave; - await this.i2cSlave.write16(0x03,0); - console.log("init ok:"+this.i2cSlave); - resolve(); - },(err)=>{ - reject(err); - }); - }); - }, - readStatus: function(){ - return new Promise(async (resolve, reject)=>{ - if(this.i2cSlave == null){ - reject("i2cSlave Address does'nt yet open!"); - }else{ - let data = await this.i2cSlave.read8(0x00); - resolve(data); - } - }); + init: async function() { + this.i2cSlave = await this.i2cPort.open(this.slaveAddress); + await this.i2cSlave.write16(0x03, 0); }, - move: function(step){ - return new Promise(async (resolve, reject)=>{ - if(this.i2cSlave == null){ - reject("i2cSlave Address does'nt yet open!"); - }else{ - if(step>0) - await this.i2cSlave.write16(0x01,(step|0)); - if(step<0) - await this.i2cSlave.write16(0x02,(-step|0)); - let shortSleep=()=>{ - return new Promise(resolve => setTimeout(resolve, 100)); - } - for(;;){ - let busy = await this.i2cSlave.read8(0x00); - if(busy==0){ - resolve(); - break; - } - await shortSleep(); - }; - } - }); + readStatus: async function() { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + const data = await this.i2cSlave.read8(0x00); + return data; }, - abort: function(){ - return new Promise(async (resolve, reject)=>{ - if(this.i2cSlave == null){ - reject("i2cSlave Address does'nt yet open!"); - }else{ - await this.i2cSlave.write16(0x03,0); - resolve(); - } - }); + move: async function(step) { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + if (step > 0) { + await this.i2cSlave.write16(0x01, step | 0); + } + if (step < 0) { + await this.i2cSlave.write16(0x02, -step | 0); + } + + for (;;) { + const busy = await this.i2cSlave.read8(0x00); + if (busy == 0) return; + await sleep(100); + } }, - setSpeed: function(speed){ - return new Promise(async (resolve, reject)=>{ - if(this.i2cSlave == null){ - reject("i2cSlave Address does'nt yet open!"); - }else{ - await this.i2cSlave.write16(0x04,speed|0); - resolve(); - } - }); + abort: async function() { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + await this.i2cSlave.write16(0x03, 0); }, - setMinSpeed: function(speed){ - return new Promise(async (resolve, reject)=>{ - if(this.i2cSlave == null){ - reject("i2cSlave Address does'nt yet open!"); - }else{ - await this.i2cSlave.write16(0x05,speed); - resolve(); - } - }); + setSpeed: async function(speed) { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + await this.i2cSlave.write16(0x04, speed | 0); }, - setAccelRate: function(rate){ - return new Promise(async (resolve, reject)=>{ - if(this.i2cSlave == null){ - reject("i2cSlave Address does'nt yet open!"); - }else{ - await this.i2cSlave.write16(0x06,rate); - resolve(); - } - }); + setMinSpeed: async function(speed) { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + await this.i2cSlave.write16(0x05, speed); }, - enable: function(en){ - return new Promise(async (resolve, reject)=>{ - if(this.i2cSlave == null){ - reject("i2cSlave Address does'nt yet open!"); - }else{ - await this.i2cSlave.write16(0x07,en?1:0); - resolve(); - } - }); + setAccelRate: async function(rate) { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + await this.i2cSlave.write16(0x06, rate); }, + enable: async function(en) { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is not open yet."); + } + + await this.i2cSlave.write16(0x07, en ? 1 : 0); + } }; export default SteppingMotor; diff --git a/packages/bh1750/bh1750.mjs b/packages/bh1750/bh1750.mjs index bbc3f5e..a48dfe2 100755 --- a/packages/bh1750/bh1750.mjs +++ b/packages/bh1750/bh1750.mjs @@ -30,7 +30,6 @@ var BH1750 = function (i2cPort, slaveAddress) { BH1750.prototype = { init: async function(){ this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - console.log("init ok:"+this.i2cSlave); await this.power_down(); await this.set_sensitivity(); }, @@ -81,18 +80,15 @@ BH1750.prototype = { await this._set_mode(0x40 | (this.mtreg >> 5)); await this._set_mode(0x60 | (this.mtreg & 0x1f)); await this.power_down(); -// console.log("mtreg:",this.mtreg); }, get_result: async function(){ // Return current measurement result in lx. var data = await this.i2cSlave.readBytes(2); var count = data[1] | data[0]<<8; -// console.log("count:",count); var mode2coeff = 1; if (this.mode == this.ONE_TIME_HIGH_RES_MODE_2 || this.mode == this.CONTINUOUS_HIGH_RES_MODE_2){ mode2coeff = 2; } -// console.log("mode2coeff:",mode2coeff); var ratio = 1/(1.2 * (this.mtreg/69.0) * mode2coeff); return (ratio*count); }, @@ -107,7 +103,6 @@ BH1750.prototype = { basetime= 0.03; } -// console.log("delay:",basetime * (this.mtreg/69.0) + additional, ": mode:",(this.mode).toString(16)); await sleep(1000*(basetime * (this.mtreg/69.0) + additional)); }, do_measurement: async function(mode, additional_delay){ diff --git a/packages/bme280/bme280.mjs b/packages/bme280/bme280.mjs index 236075a..a4c47f8 100755 --- a/packages/bme280/bme280.mjs +++ b/packages/bme280/bme280.mjs @@ -19,7 +19,6 @@ var BME280 = function(i2cPort,slaveAddress){ BME280.prototype = { init: async function(){ this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - console.log("init ok:"+this.i2cSlave); await this.setup(); await this.getCalibParam(); }, @@ -43,7 +42,6 @@ BME280.prototype = { dat = await this.i2cSlave.read8(ra); calib.push(dat); } -// console.log(calib); var digT = this.digT; var digP = this.digP; var digH = this.digH; @@ -66,15 +64,9 @@ BME280.prototype = { digH.push(getSVal((calib[28]<< 4) | (0x0F & calib[29]))); digH.push(getSVal((calib[30]<< 4) | ((calib[29] >> 4) & 0x0F))); digH.push(getSVal( calib[31] )); - console.log(digT,digP,digH); -// console.log(getSVal(digT[1]),getSVal(digT[2]),getSVal(digP[1]),getSVal(digP[2]),getSVal(digP[3]),getSVal(digP[4]),getSVal(digP[5]),getSVal(digP[6]),getSVal(digP[7]),getSVal(digP[8])); }, - getSVal: function(val){ - if ( val & 0x8000 ){ - return (( -val ^ 0xFFFF) + 1); - } else { - return ( val ); - } + getSVal: function (val) { + return new Int16Array([val])[0]; }, setup: async function(){ var osrs_t = 1; //Temperature oversampling x 1 @@ -117,8 +109,6 @@ BME280.prototype = { v1 = (digP[8] * (((pressure / 8.0) * (pressure / 8.0)) / 8192.0)) / 4096; v2 = ((pressure / 4.0) * digP[7]) / 8192.0; pressure = pressure + ((v1 + v2 + digP[6]) / 16.0) ; - -// console.log( "pressure : ", (pressure/100)," hPa"); return (pressure); }, compensate_T: function(adc_T){ @@ -128,7 +118,6 @@ BME280.prototype = { var v2 = (adc_T / 131072.0 - digT[0] / 8192.0) * (adc_T / 131072.0 - digT[0] / 8192.0) * digT[2]; t_fine = v1 + v2; var temperature = t_fine / 5120.0; -// console.log( "temperature : ",(temperature)," degrees celsius" ); return (temperature); }, compensate_H: function(adc_H){ @@ -146,7 +135,6 @@ BME280.prototype = { } else if ( var_h < 0.0){ var_h = 0.0; } -// console.log( "humidity : ", (var_h)," %"); return ( var_h ); }, readData: async function(){ diff --git a/packages/bmp180/bmp180.mjs b/packages/bmp180/bmp180.mjs index 4b92970..d32a1f9 100755 --- a/packages/bmp180/bmp180.mjs +++ b/packages/bmp180/bmp180.mjs @@ -14,13 +14,11 @@ var BMP180 = function(i2cPort,slaveAddress){ } else { this.slaveAddress = 0x77; } - console.log("this.slaveAddress:",this.slaveAddress); }; BMP180.prototype = { init: async function(){ this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - console.log("init ok:"+this.i2cSlave); await this.getCalibParam(); }, getSVal: function(val){ @@ -34,7 +32,6 @@ BMP180.prototype = { var calib = []; var dat; var getSVal = this.getSVal; - console.log("getCalibParam"); for ( var ra = 0xAA ; ra <= 0xAA+21 ; ra++ ){ dat = await this.i2cSlave.read8(ra); calib.push(dat); @@ -53,7 +50,6 @@ BMP180.prototype = { M.push(getSVal((calib[16] << 8) | calib[17])); // MB M[0] M.push(getSVal((calib[18] << 8) | calib[19])); // MC M.push(getSVal((calib[20] << 8) | calib[21])); // MD - console.log(AC,B,M); }, readAndCalcB5: async function(){ var AC = this.AC; @@ -91,7 +87,6 @@ BMP180.prototype = { dat = await this.i2cSlave.read8(ra); data.push(dat); } - console.log("data:",data); var UP = (( (data[0]<<16 ) | (data[1] << 8 ) | data[2]) >> (8 - mode) ) ; // Calibration for Pressure diff --git a/packages/bmp280/bmp280.mjs b/packages/bmp280/bmp280.mjs index e594978..69e1547 100755 --- a/packages/bmp280/bmp280.mjs +++ b/packages/bmp280/bmp280.mjs @@ -19,7 +19,6 @@ var BMP280 = function(i2cPort,slaveAddress){ BMP280.prototype = { init: async function(){ this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - console.log("init ok:"+this.i2cSlave); await this.setup(); await this.getCalibParam(); }, @@ -39,7 +38,6 @@ BMP280.prototype = { dat = await this.i2cSlave.read8(ra); calib.push(dat); } -// console.log(calib); var digT = this.digT; var digP = this.digP; var getSVal = this.getSVal; @@ -55,15 +53,9 @@ BMP280.prototype = { digP.push(getSVal((calib[19]<< 8) | calib[18])); digP.push(getSVal((calib[21]<< 8) | calib[20])); digP.push(getSVal((calib[23]<< 8) | calib[22])); - console.log(digT,digP); -// console.log(getSVal(digT[1]),getSVal(digT[2]),getSVal(digP[1]),getSVal(digP[2]),getSVal(digP[3]),getSVal(digP[4]),getSVal(digP[5]),getSVal(digP[6]),getSVal(digP[7]),getSVal(digP[8])); }, - getSVal: function(val){ - if ( val & 0x8000 ){ - return (( -val ^ 0xFFFF) + 1); - } else { - return ( val ); - } + getSVal: function (val) { + return new Int16Array([val])[0]; }, setup: async function(){ var osrs_t = 1; //Temperature oversampling x 1 @@ -104,7 +96,6 @@ BMP280.prototype = { v2 = ((pressure / 4.0) * digP[7]) / 8192.0; pressure = pressure + ((v1 + v2 + digP[6]) / 16.0) ; -// console.log( "pressure : ", (pressure/100)," hPa"); return (pressure); }, compensate_T: function(adc_T){ @@ -113,8 +104,6 @@ BMP280.prototype = { var v1 = (adc_T / 16384.0 - digT[0] / 1024.0) * digT[1]; var v2 = (adc_T / 131072.0 - digT[0] / 8192.0) * (adc_T / 131072.0 - digT[0] / 8192.0) * digT[2]; t_fine = v1 + v2; - var temperature = t_fine / 5120.0; -// console.log( "temperature : ",(temperature)," degrees celsius" ); return (temperature); }, readData: async function(){ diff --git a/packages/canzasi/canzasi.mjs b/packages/canzasi/canzasi.mjs index 73ab087..d110b03 100755 --- a/packages/canzasi/canzasi.mjs +++ b/packages/canzasi/canzasi.mjs @@ -1,31 +1,20 @@ -var canzasi = function(i2cPort){ +// @ts-check + +var canzasi = function (i2cPort) { this.i2cPort = i2cPort; this.slaveAddress = 0x30; this.i2cSlave = null; }; canzasi.prototype = { - init: function(){ - return new Promise((resolve, reject) => { - this.i2cPort.open(this.slaveAddress).then((i2cSlave) => { - this.i2cSlave = i2cSlave; - console.log("i2cPort.open"); - resolve(); - }); - }); + init: async function () { + this.i2cSlave = await this.i2cPort.open(this.slaveAddress); }, - set : function(value){ - return new Promise((resolve, reject) => { - if(this.i2cSlave){ - this.i2cSlave.writeByte(value).then(() => { - resolve(); - },(error) => { - console.log("error"+error); - }).catch(reject); - }else{ - console.log("i2cSlave is gone....."); - } - }); + set: async function (value) { + if (this.i2cSlave == null) { + throw new Error("i2cSlave is gone....."); + } + await this.i2cSlave.writeByte(value); } }; diff --git a/packages/grove-gesture/grove-gesture.mjs b/packages/grove-gesture/grove-gesture.mjs index 374dd72..75e274a 100755 --- a/packages/grove-gesture/grove-gesture.mjs +++ b/packages/grove-gesture/grove-gesture.mjs @@ -68,22 +68,18 @@ PAJ7620.prototype = { return new Promise((resolve, reject)=>{ this.i2cPort.open(this.slaveAddress).then(async (i2cSlave)=>{ this.i2cSlave = i2cSlave; - console.log("i2cPort.open"); await this.i2cSlave.write8(0xef,0); // BankSelect=0 await this.i2cSlave.write8(0xef,0); // BankSelect=0 var v0 = await this.i2cSlave.read8(0,true); var v1 = await this.i2cSlave.read8(1,true); if((v0 != 0x20)||(v1 != 0x76)){ - console.log("read error!"); reject("init error :v0["+v0+"] v1["+v1+"]"); }else{ for(var cnt = 0;cnt { this.i2cPort.open(OLED_CONST.address).then((i2cSlave) =>{ this.i2cSlave = i2cSlave; - console.log("i2cPort.open"); this.initQ(); this.playSequence().then(() => {resolve()}); }); @@ -227,7 +226,7 @@ OledDisplay.prototype = { reject("i2cSlave Address does'nt yet open!"); }else{    if(this.sequence != null){ -   console.log("OledDisplay.playSequence(): error! (multiple call)"); +   console.error("OledDisplay.playSequence(): error! (multiple call)");     reject();   } this.sequence = setInterval(() => { @@ -256,7 +255,6 @@ OledDisplay.prototype = { if(this.i2cSlave == null){ reject("i2cSlave Address does'nt yet open!"); }else{ - console.log("start clearDisplay"); this.clearDisplayQ(); this.playSequence().then(() => {resolve()}); } @@ -267,7 +265,6 @@ OledDisplay.prototype = { if(this.i2cSlave == null){ reject("i2cSlave Address does'nt yet open!"); }else{ - console.log("start drawString"); this.drawStringQ(row,col); this.playSequence().then(() => {resolve()}); } diff --git a/packages/grove-touch/grove-touch.mjs b/packages/grove-touch/grove-touch.mjs index 87266e3..30f1ef0 100755 --- a/packages/grove-touch/grove-touch.mjs +++ b/packages/grove-touch/grove-touch.mjs @@ -1,4 +1,6 @@ -function GroveTouch(i2cPort,address){ +// @ts-check + +function GroveTouch(i2cPort, address) { this.i2cPort=i2cPort; this.i2cSlave= null; this.slaveAddress= address; @@ -18,9 +20,7 @@ GroveTouch.prototype = { await this.i2cSlave.write8(0x31,0xff); await this.i2cSlave.write8(0x32,0x02); for(var i=0;i<12*2;i+=2){ -// console.log(i); var address = 0x41+i; -// console.log(address); await this.i2cSlave.write8(address,0x0f); await this.i2cSlave.write8(address+1,0x0a); } @@ -36,7 +36,6 @@ GroveTouch.prototype = { reject("i2cSlave Address does'nt yet open!"); }else{ this.i2cSlave.read16(0x00).then((v)=>{ -// console.log(v); var array = []; for(var cnt = 0;cnt < 12;cnt ++){ array.push(((v & (1 << cnt))!=0)?true:false); diff --git a/packages/ina219/ina219.mjs b/packages/ina219/ina219.mjs index d34c999..30a898d 100644 --- a/packages/ina219/ina219.mjs +++ b/packages/ina219/ina219.mjs @@ -10,7 +10,6 @@ var INA219 = function(i2cPort, slaveAddress) { if (!slaveAddress) { slaveAddress = 0x40; } - console.log("slaveAddress:", slaveAddress); this.i2cPort = i2cPort; this.i2cSlave = null; this.slaveAddress = slaveAddress; @@ -93,7 +92,6 @@ var INA219 = function(i2cPort, slaveAddress) { INA219.prototype = { init: async function(shunt_ohms, max_expected_amps) { this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - console.log("i2cSlave:", this.i2cSlave); if (shunt_ohms) { this._shunt_ohms = shunt_ohms; } else { @@ -139,13 +137,6 @@ INA219.prototype = { } } - console.log( - "gain set to ", - this.__GAIN_VOLTS[this._gain], - " auto_gain_enabled:", - this._auto_gain_enabled - ); - await this._calibrate( this.__BUS_RANGE[voltage_range], this.__GAIN_VOLTS[this._gain], @@ -202,7 +193,6 @@ INA219.prototype = { }, _determine_gain: function(max_expected_amps) { var shunt_v = max_expected_amps * this._shunt_ohms; - console.log("_determine_gain:", shunt_v, this.__GAIN_VOLTS[3]); if (shunt_v > this.__GAIN_VOLTS[3]) { throw " ValueError: " + this.__RNG_ERR_MSG + "," + max_expected_amps; } @@ -210,11 +200,9 @@ INA219.prototype = { while (this.__GAIN_VOLTS[gainIndex] > shunt_v) { --gainIndex; } - // console.log("gain:",gainIndex+1); return gainIndex + 1; // 多分これで良いと思うんだけど・・・ }, _increase_gain: async function() { - console.log(this.__LOG_MSG_3); var gain = await this._read_gain(); if (gain < this.__GAIN_VOLTS.length - 1) { gain = gain + 1; @@ -225,8 +213,7 @@ INA219.prototype = { await this._configure_gain(gain); await sleep(1); } else { - console.log("Device limit reach, gain cannot be increased"); - throw "DeviceRangeError: " + this.__GAIN_VOLTS[gain]; + throw new Error("DeviceRangeError: " + this.__GAIN_VOLTS[gain]); } }, _configure: async function(voltage_range, gain, bus_adc, shunt_adc) { @@ -243,51 +230,33 @@ INA219.prototype = { shunt_volts_max, max_expected_amps ) { - console.log( - this.__LOG_MSG_2, - bus_volts_max, - shunt_volts_max, - max_expected_amps - ); var max_possible_amps = shunt_volts_max / this._shunt_ohms; - console.log("max possible current:", max_possible_amps); this._current_lsb = this._determine_current_lsb( max_expected_amps, max_possible_amps ); - console.log("current LSB: [A/bit]:", this._current_lsb); this._power_lsb = this._current_lsb * 20; - console.log("power LSB: [W/bit]", this._power_lsb); - var max_current = this._current_lsb * 32767; - console.log("max current before overflow: ", max_current); var max_shunt_voltage = max_current * this._shunt_ohms; - console.log( - "max shunt voltage before overflow: mV ;", - max_shunt_voltage * 1000 - ); var calibration = Math.trunc( this.__CALIBRATION_FACTOR / (this._current_lsb * this._shunt_ohms) ); - console.log("calibration: ", calibration); await this._calibration_register(calibration); }, _determine_current_lsb: function(max_expected_amps, max_possible_amps) { - // console.log("_determine_current_lsb:",max_expected_amps, max_possible_amps); var current_lsb; if (max_expected_amps) { if (max_expected_amps > max_possible_amps) { - throw "ValueError: " + + throw new Error("ValueError: " + this.__AMP_ERR_MSG + " : " + max_expected_amps + " : " + - max_possible_amps; - console.log("max expected current: ", max_expected_amps); + max_possible_amps); } if (max_expected_amps < max_possible_amps) { current_lsb = max_expected_amps / this.__CURRENT_LSB_FACTOR; @@ -304,7 +273,6 @@ INA219.prototype = { return current_lsb; }, _configuration_register: async function(register_value) { - console.log("configuration: ", register_value); await this.__write_register(this.__REG_CONFIG, register_value); }, _read_configuration: async function() { @@ -319,7 +287,6 @@ INA219.prototype = { _read_gain: async function() { var configuration = await this._read_configuration(); var gain = (configuration & 0x1800) >> this.__PG0; - console.log("gain is currently: V", this.__GAIN_VOLTS[gain]); return gain; }, _configure_gain: async function(gain) { @@ -327,10 +294,8 @@ INA219.prototype = { configuration = configuration & 0xe7ff; await this._configuration_register(configuration | (gain << this.__PG0)); this._gain = gain; - console.log("gain set to: V:", this.__GAIN_VOLTS[gain]); }, _calibration_register: async function(register_value) { - console.log("calibration: ", register_value); await this.__write_register(this.__REG_CALIBRATION, register_value); }, _has_current_overflow: async function() { @@ -359,13 +324,6 @@ INA219.prototype = { } }, __write_register: async function(register, register_value) { - var register_bytes = this.__to_bytes(register_value); - console.log( - "write register :", - register, - register_value, - register_value.toString(2) - ); // write16 is little endian var bl = register_value & 0xff; var bh = register_value >>> 8; @@ -386,7 +344,6 @@ INA219.prototype = { } } else { } - // console.log("read register :", register, register_value,register_value.toString(2)); return register_value; }, __to_bytes: function(register_value) { diff --git a/packages/mpu6050/mpu6050.mjs b/packages/mpu6050/mpu6050.mjs index 6e01e92..e8859e2 100755 --- a/packages/mpu6050/mpu6050.mjs +++ b/packages/mpu6050/mpu6050.mjs @@ -14,16 +14,13 @@ var MPU6050 = function(i2cPort,slaveAddress){ MPU6050.prototype = { init: async function(){ this.i2cSlave = await this.i2cPort.open(this.slaveAddress); -// console.log("init ok:"+this.i2cSlave); await this.i2cSlave.write8(0x6b,0x00); - console.log("init ok:"+this.i2cSlave); }, readTemp: async function(){ if(this.i2cSlave == null){ throw Error("i2cSlave Address does'nt yet open!"); } var ans = await this.i2cSlave.read16(0x41); - console.log(ans); // var data = ((MSB << 8) + LSB)/128.0; var temp = ans / 340 + 36.53; return(temp); @@ -33,7 +30,6 @@ MPU6050.prototype = { throw Error("i2cSlave Address does'nt yet open!"); } var ans = await this.i2cSlave.read16(0x41); -// console.log("tmpW:",ans); var temp = this.getVal(ans) / 340 + 36.53; ans = await this.i2cSlave.read16(0x43); var rx = this.getVal(ans) / 131; @@ -47,7 +43,6 @@ MPU6050.prototype = { var gy = this.getVal(ans) / 16384; ans = await this.i2cSlave.read16(0x3f); var gz = this.getVal(ans) / 16384; - console.log("t:",temp," rxyz:",rz,ry,rz," gxyz:",gx,gy,gz); return({ temperature: temp, gx: gx, @@ -73,13 +68,8 @@ MPU6050.prototype = { getVal: function( w ){ var l = w >>> 8; var b = w & 0xff; - var v = l + ( b << 8 ); -// console.log("Val:",w.toString(16),b.toString(16),l.toString(16),v.toString(16),b,l,v); - if ( v >= 0x8000 ){ - return ( - ((65535 - v ) + 1 ) ); - } else { - return(v); - } + var v = l + (b << 8); + return new Int16Array([v])[0]; } }; diff --git a/packages/mpu6500/mpu6500.mjs b/packages/mpu6500/mpu6500.mjs index 2e0bfc1..cf8fce7 100755 --- a/packages/mpu6500/mpu6500.mjs +++ b/packages/mpu6500/mpu6500.mjs @@ -1,3 +1,4 @@ +// @ts-check // MPU 6500 driver // Ported from // https://raw.githubusercontent.com/tuupola/micropython-mpu9250/master/mpu6500.py @@ -91,44 +92,35 @@ MPU6500.prototype = { this.i2cSlave = await this.i2cPort.open(this.slaveAddress); var whoamiVal = await this.i2cSlave.read8(cs._WHO_AM_I); - if ( whoamiVal ==0x70){ - console.log("This device is MPU6500"); - } else if ( whoamiVal ==0x71){ - console.log("This device is MPU9250"); - } else if ( whoamiVal ==0x73){ + if (whoamiVal == 0x70) { + // This device is MPU6500 + } else if (whoamiVal == 0x71) { + // This device is MPU9250 + } else if (whoamiVal == 0x73) { // https://github.com/kriswiner/MPU9250/issues/47 - console.log("This device is MPU9255"); + // This device is MPU9255 } else { - console.log("This device is NOT Supported..."); - return(null); + console.error("This device is NOT Supported..."); + return null; } cs._accel_so = await this._accel_fs(accel_fs); cs._gyro_so = await this._gyro_fs(gyro_fs); cs._accel_sf = accel_sf; cs._gyro_sf = gyro_sf; - console.log("init ok:"+this.i2cSlave+ " constant:",cs); - -// await this.i2cSlave.write8(0x6B, 0x00); // clear power management // # Enable I2C bypass to access for MPU9250 magnetometer access. var chr = await this.i2cSlave.read8(cs._INT_PIN_CFG); - console.log("bypass:",chr); chr &= ~cs._I2C_BYPASS_MASK; //# clear I2C bits chr |= cs._I2C_BYPASS_EN; - console.log("bypass_:",chr); await this.i2cSlave.write8(cs._INT_PIN_CFG, chr); - console.log("init2 ok:"); - }, _accel_fs: async function(value){ var cs = this.devConst; await this.i2cSlave.write8(cs._ACCEL_CONFIG, value); - console.log("_accel_fs:",value); //# Return the sensitivity divider if (cs.ACCEL_FS_SEL_2G == value){ - console.log("ACCEL_FS_SEL_2G::sel"); return cs._ACCEL_SO_2G; } else if (cs.ACCEL_FS_SEL_4G == value){ return cs._ACCEL_SO_4G; @@ -169,7 +161,6 @@ MPU6500.prototype = { var x = this.getVal(await this.i2cSlave.read16(cs._ACCEL_XOUT_H)); var y = this.getVal(await this.i2cSlave.read16(cs._ACCEL_YOUT_H)); var z = this.getVal(await this.i2cSlave.read16(cs._ACCEL_ZOUT_H)); - console.log("acc:",x,y,z," addr:",cs._ACCEL_XOUT_H," sosf:",so,sf); return { x: x / so * sf, y: y / so * sf, @@ -188,7 +179,6 @@ MPU6500.prototype = { var x = this.getVal(await this.i2cSlave.read16(cs._GYRO_XOUT_H)); var y = this.getVal(await this.i2cSlave.read16(cs._GYRO_YOUT_H)); var z = this.getVal(await this.i2cSlave.read16(cs._GYRO_ZOUT_H)); - console.log("gyr:",x,y,z," addr:",cs._GYRO_XOUT_H," sosf:",so,sf); return { x: x / so * sf, y: y / so * sf, @@ -199,12 +189,7 @@ MPU6500.prototype = { var l = w >>> 8; var b = w & 0xff; var v = l + ( b << 8 ); -// console.log("Val:",w.toString(16),b.toString(16),l.toString(16),v.toString(16),b,l,v); - if ( v >= 0x8000 ){ - return ( - ((65535 - v ) + 1 ) ); - } else { - return(v); - } + return new Int16Array([v])[0]; } }; diff --git a/packages/neopixel-i2c/neopixel-i2c.mjs b/packages/neopixel-i2c/neopixel-i2c.mjs index de69135..51fec1a 100755 --- a/packages/neopixel-i2c/neopixel-i2c.mjs +++ b/packages/neopixel-i2c/neopixel-i2c.mjs @@ -1,3 +1,4 @@ +// @ts-check // Driver for https://github.com/usedbytes/neopixel_i2c // Programmed by Satoru Takagi diff --git a/packages/pca9685-pwm/pca9685-pwm.mjs b/packages/pca9685-pwm/pca9685-pwm.mjs index 96af4f3..b9425a9 100755 --- a/packages/pca9685-pwm/pca9685-pwm.mjs +++ b/packages/pca9685-pwm/pca9685-pwm.mjs @@ -20,7 +20,7 @@ PCA9685_PWM.prototype = { // angleRange : -angleRange to +angleRange degrees if (this.frequency) { - console.log("alredy initialised"); + console.error("alredy initialised"); } if (frequency) { this.frequency = frequency; @@ -30,7 +30,7 @@ PCA9685_PWM.prototype = { var freq = Math.floor(25000000 / (4096 * this.frequency) - 1); if (freq < 0x03 || freq > 0xff) { - throw Error("Frequency should be between 24Hz to 1526Hz"); + throw new Error("Frequency should be between 24Hz to 1526Hz"); } var i2cSlave = await this.i2cPort.open(this.slaveAddress); @@ -52,17 +52,16 @@ PCA9685_PWM.prototype = { }, setPWM: async function(pwmPort, dutyRatio) { // dutyRatio : 0.0(OFF) .. 1.0(ON) - // console.log(pwmPort,angle) const portStart = 8; const portInterval = 4; if (this.i2cSlave == null) { - throw Error("i2cSlave Address does'nt yet open!"); + throw new Error("i2cSlave Address does'nt yet open!"); } if (pwmPort < 0 || pwmPort > 15) { - throw Error("PWM Port should be between 0 to 15"); + throw new Error("PWM Port should be between 0 to 15"); } if (dutyRatio < 0 || dutyRatio > 1) { - throw Error("dutyRatio should be between 0.0 to 1.0"); + throw new Error("dutyRatio should be between 0.0 to 1.0"); } var ticks = Math.floor(4095 * dutyRatio); var tickH = (ticks >> 8) & 0x0f; diff --git a/packages/pca9685/pca9685.mjs b/packages/pca9685/pca9685.mjs index e39a97b..07048d2 100755 --- a/packages/pca9685/pca9685.mjs +++ b/packages/pca9685/pca9685.mjs @@ -12,21 +12,19 @@ PCA9685.prototype = { return new Promise((resolve)=>{setTimeout(resolve,ms);}); }, init: function(minPulse,maxPulse,angleRange,noSetZero){ - // minPulse,maxPulse: in sec - // angleRange : -angleRange to +angleRange degrees + // minPulse,maxPulse: in sec + // angleRange : -angleRange to +angleRange degrees if(this.minPulse && this.maxPulse && this.angleRange){ - console.log("alredy set param"); + console.error("alredy set param"); } if(minPulse && maxPulse && angleRange){ this.minPulse = minPulse; this.maxPulse = maxPulse; this.angleRange = angleRange; -// console.log("set servo setting."); - }else{ + } else { this.minPulse = 0.0011; this.maxPulse = 0.0019; this.angleRange = 30.0; -// console.log("set defaul servo setting."); } return new Promise((resolve, reject)=>{ @@ -51,7 +49,6 @@ PCA9685.prototype = { }); }, setServo: function(servoPort,angle){ -// console.log(servoPort,angle) const portStart = 8; const portInterval = 4; const freq = 61; // Hz @@ -63,13 +60,12 @@ PCA9685.prototype = { maxPulse = this.maxPulse; pulseRange = maxPulse - minPulse; angleRange = this.angleRange; -// console.log(minPulse,maxPulse,angleRange,pulseRange); }else{ - console.log("wrong param."); + throw new Error("wrong param."); } - if ( angle < -angleRange){ + if (angle < -angleRange) { angle = -angleRange; - } else if ( angle > angleRange ){ + } else if (angle > angleRange) { angle = angleRange; } if ( servoPort < 0){ @@ -79,7 +75,6 @@ PCA9685.prototype = { } var pulse = ((minPulse + maxPulse) + angle / angleRange * pulseRange ) / 2.0; - console.log("pulse:",pulse*1000," msec"); var ticks = Math.round(pulse / tickSec); var tickH = (( ticks >> 8 ) & 0x0f); diff --git a/packages/pcf8591/pcf8591.mjs b/packages/pcf8591/pcf8591.mjs index e22174c..1b76098 100755 --- a/packages/pcf8591/pcf8591.mjs +++ b/packages/pcf8591/pcf8591.mjs @@ -1,3 +1,4 @@ +// @ts-check // PCF8591 driver for CHIRIMEN raspberry pi3 // 4ch 8bit ADC, 1ch 8bit DAC // CDS, Thermister, VR and a LED @@ -28,9 +29,8 @@ PCF8591.prototype = { } }, setDAC: async function(vl){ - var dav = Math.floor( 0xff * vl / this.refV ) & 0xff; - console.log("setDAC:",dav); - await this.i2cSlave.write8( 0x40, dav ); + var dav = Math.floor(0xff * vl / this.refV) & 0xff; + await this.i2cSlave.write8(0x40, dav); } }; diff --git a/packages/s11059/s11059.mjs b/packages/s11059/s11059.mjs index b95900f..15e9325 100755 --- a/packages/s11059/s11059.mjs +++ b/packages/s11059/s11059.mjs @@ -1,4 +1,6 @@ -var S11059 = function(i2cPort,slaveAddress) { +// @ts-check + +var S11059 = function (i2cPort, slaveAddress) { this.i2cPort = i2cPort; this.i2cSlave = null; this.slaveAddress = slaveAddress; diff --git a/packages/sht30/sht30.mjs b/packages/sht30/sht30.mjs index efd63d2..b4d5aaa 100755 --- a/packages/sht30/sht30.mjs +++ b/packages/sht30/sht30.mjs @@ -1,8 +1,12 @@ +// @ts-check // SHT30 driver for CHIRIMEN raspberry pi3 // Temperature and Humidity I2C Sensor // based on https://github.com/ControlEverythingCommunity/SHT30/blob/master/Python/SHT30.py // Programmed by Satoru Takagi +/** @param {number} ms Delay for a number of milliseconds. */ +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + var SHT30 = function(i2cPort,slaveAddress){ if (!slaveAddress){ slaveAddress = 0x44; @@ -15,18 +19,15 @@ var SHT30 = function(i2cPort,slaveAddress){ SHT30.prototype = { init: async function(){ this.i2cSlave = await this.i2cPort.open(this.slaveAddress); - }, readData: async function(){ await this.i2cSlave.write8(0x2C, 0x06); // High repeatability measurement await sleep(100); // wait for measurement? var mdata = await this.i2cSlave.readBytes(6); // prev data.. - console.log("rawData:",mdata); // cTemp MSB, cTemp LSB, cTemp CRC, Humididty MSB, Humidity LSB, Humidity CRC - cTemp = ((((mdata[0] * 256.0) + mdata[1]) * 175) / 65535.0) - 45; // celsius - fTemp = cTemp * 1.8 + 32; // f - humidity = 100 * (mdata[3] * 256 + mdata[4]) / 65535.0; -// console.log("t:",cTemp," h:",humidity); + var cTemp = ((((mdata[0] * 256.0) + mdata[1]) * 175) / 65535.0) - 45; // celsius + var fTemp = cTemp * 1.8 + 32; // f + var humidity = 100 * (mdata[3] * 256 + mdata[4]) / 65535.0; return { humidity: humidity, temperature: cTemp diff --git a/packages/tcs34725/tcs34725.mjs b/packages/tcs34725/tcs34725.mjs index a8975f0..1223b55 100755 --- a/packages/tcs34725/tcs34725.mjs +++ b/packages/tcs34725/tcs34725.mjs @@ -44,11 +44,9 @@ TCS34725.prototype = { this._active = false; this.integration_time(2.4); var sensor_id = await this.sensor_id(); - console.log("TCS34725 sensor_id: 0x",sensor_id.toString(16)); }, active: async function(value){ -// console.log("active:",value); if (value == undefined ){ return this._active; } @@ -93,11 +91,11 @@ TCS34725.prototype = { break; } } - if ( index == -1 ){ - console.log("gain must be 1, 4, 16 or 60"); - return ( false ); + if (index == -1) { + console.error("gain must be 1, 4, 16 or 60"); + return false; } - this.i2cSlave.write8(this._REGISTER_CONTROL | this._COMMAND_BIT, index); + await this.i2cSlave.write8(this._REGISTER_CONTROL | this._COMMAND_BIT, index); }, _valid: async function(){ var v = await this.i2cSlave.read8(this._REGISTER_STATUS | this._COMMAND_BIT); @@ -139,24 +137,6 @@ TCS34725.prototype = { lux: y }; }, - /** - readData: async function(){ - await this.i2cSlave.write8(0x2C, 0x06); // High repeatability measurement - await sleep(100); // wait for measurement? - var mdata = await this.i2cSlave.readBytes(6); // prev data.. - console.log("rawData:",mdata); - // cTemp MSB, cTemp LSB, cTemp CRC, Humididty MSB, Humidity LSB, Humidity CRC - cTemp = ((((mdata[0] * 256.0) + mdata[1]) * 175) / 65535.0) - 45; // celsius - fTemp = cTemp * 1.8 + 32; // f - humidity = 100 * (mdata[3] * 256 + mdata[4]) / 65535.0; -// console.log("t:",cTemp," h:",humidity); - return { - humidity: humidity, - temperature: cTemp - } - - } - **/ }; export default TCS34725; diff --git a/packages/vl53l0x/vl53l0x.mjs b/packages/vl53l0x/vl53l0x.mjs index 7b06f2a..04deafb 100755 --- a/packages/vl53l0x/vl53l0x.mjs +++ b/packages/vl53l0x/vl53l0x.mjs @@ -585,12 +585,8 @@ VL53L0X.prototype = { var data = ((MSB << 8) + LSB) / 128.0; return data; }, - getSVal: function(val) { - if (val & 0x8000) { - return (-val ^ 0xffff) + 1; - } else { - return val; - } + getSVal: function (val) { + return new Int16Array([val])[0]; }, setVcselPulsePeriod: async function(vcselPeriodType, period_pclks) { // for longRangeMode init() diff --git a/packages/vl53l1x/vl53l1x.mjs b/packages/vl53l1x/vl53l1x.mjs index 6a75055..a11d459 100755 --- a/packages/vl53l1x/vl53l1x.mjs +++ b/packages/vl53l1x/vl53l1x.mjs @@ -1218,9 +1218,7 @@ VL53L1X.prototype = { this.i2cSlave = await this.i2cPort.open(this.slaveAddress); await sleep(10); if ((await this.readReg16Bit(this.IDENTIFICATION__MODEL_ID)) != 0xeacc) { - throw "Failed to find expected ID register values. Check wiring!"; - } else { - console.log("OK! Is VL53L1X"); + throw new Error("Failed to find expected ID register values. Check wiring!"); } // VL53L1_software_reset() begin @@ -1259,7 +1257,6 @@ VL53L1X.prototype = { this.osc_calibrate_val = await this.readReg16Bit( this.RESULT__OSC_CALIBRATE_VAL ); - // console.log(this.fast_osc_frequency,this.osc_calibrate_val); // VL53L1_DataInit() end @@ -1359,8 +1356,6 @@ VL53L1X.prototype = { (await this.readReg16Bit(this.MM_CONFIG__OUTER_OFFSET_MM)) * 4 ); - console.log("VL53L1X Init completed : "); - return true; }, @@ -1423,7 +1418,6 @@ VL53L1X.prototype = { } **/ await this.readResults(); - // console.log("readResults end:",this.results); if (!this.calibrated) { this.setupManualCalibration(); this.calibrated = true; @@ -1434,7 +1428,6 @@ VL53L1X.prototype = { await this.getRangingData(); await this.writeReg(this.SYSTEM__INTERRUPT_CLEAR, 0x01); // sys_interrupt_clear_range - //console.log("STAT:",this.ranging_data.range_status); return this.ranging_data.range_mm; }, @@ -1447,7 +1440,6 @@ VL53L1X.prototype = { switch (mode) { case "short": - console.log("ShortMode"); // from VL53L1_preset_mode_standard_ranging_short_range() // timing config await this.writeReg(this.RANGE_CONFIG__VCSEL_PERIOD_A, 0x07); @@ -1460,7 +1452,6 @@ VL53L1X.prototype = { await this.writeReg(this.SD_CONFIG__INITIAL_PHASE_SD1, 6); // tuning parm default break; case "medium": - console.log("MediumMode"); // from VL53L1_preset_mode_standard_ranging() // timing config await this.writeReg(this.RANGE_CONFIG__VCSEL_PERIOD_A, 0x0b); @@ -1473,7 +1464,6 @@ VL53L1X.prototype = { await this.writeReg(this.SD_CONFIG__INITIAL_PHASE_SD1, 10); // tuning parm default break; case "long": // long - console.log("LongMode"); // from VL53L1_preset_mode_standard_ranging_long_range() // timing config await this.writeReg(this.RANGE_CONFIG__VCSEL_PERIOD_A, 0x0f); @@ -1491,7 +1481,6 @@ VL53L1X.prototype = { } // reapply timing budget await this.setMeasurementTimingBudget(budget_us); - // console.log("budget_us:",budget_us); // save mode so it can be returned by getDistanceMode() this.distance_mode = mode; return true; @@ -1618,15 +1607,12 @@ VL53L1X.prototype = { checkForDataReady: async function() { var IntPol = await this.getInterruptPolarity(); var Temp = await this.readReg(this.GPIO__TIO_HV_STATUS); - // console.log("IntPol,GPIO__TIO_HV_STATUS : ",IntPol,Temp); var isDataReady = (Temp & 1) == IntPol ? 1 : 0; return isDataReady; }, getInterruptPolarity: async function() { var Temp = await this.readReg(this.GPIO_HV_MUX__CTRL); Temp = Temp & 0x10; - // console.log("getInterruptPolarity:",Temp); - // return( !(Temp>>4)); var ans; if (Temp >> 4 == 0) { ans = 1; @@ -1669,15 +1655,12 @@ VL53L1X.prototype = { }, // read measurement results into buffer readResults: async function() { - //console.log("readResults:",(this.RESULT__RANGE_STATUS >>> 8).toString(16), (this.RESULT__RANGE_STATUS & 0xFF).toString(16)); await this.i2cSlave.writeBytes([ this.RESULT__RANGE_STATUS >>> 8, this.RESULT__RANGE_STATUS & 0xff ]); var readData = await this.i2cSlave.readBytes(17); - //console.log("readData:",readData); - // last_status = Wire.endTransmission(); this.results.range_status = readData[0]; @@ -1886,22 +1869,18 @@ VL53L1X.prototype = { readReg: async function(addr16) { await this.i2cSlave.writeBytes([addr16 >>> 8, addr16 & 0xff]); var dat = await this.i2cSlave.readBytes(1); - // console.log("readReg16Bit:",addr16.toString(16),ans.toString(16),":",(addr16 >>> 8).toString(16), (addr16 & 0xFF).toString(16), (dat[0]).toString(16), (dat[1]).toString(16)); return dat; }, readReg16Bit: async function(addr16) { await this.i2cSlave.writeBytes([addr16 >>> 8, addr16 & 0xff]); var dat = await this.i2cSlave.readBytes(2); var ans = (dat[0] << 8) + dat[1]; - // console.log("readReg16Bit:",addr16.toString(16),ans.toString(16),":",(addr16 >>> 8).toString(16), (addr16 & 0xFF).toString(16), (dat[0]).toString(16), (dat[1]).toString(16)); return ans; }, writeReg: async function(addr16, dat8) { - // console.log("writeReg8Bit:",addr16.toString(16),dat16.toString(16),":",(addr16 >>> 8).toString(16), (addr16 & 0xFF).toString(16), (dat16>>>8).toString(16), (dat16 & 0xFF).toString(16)); await this.i2cSlave.writeBytes([addr16 >>> 8, addr16 & 0xff, dat8 & 0xff]); }, writeReg16Bit: async function(addr16, dat16) { - // console.log("writeReg16Bit:",addr16.toString(16),dat16.toString(16),":",(addr16 >>> 8).toString(16), (addr16 & 0xFF).toString(16), (dat16>>>8).toString(16), (dat16 & 0xFF).toString(16)); await this.i2cSlave.writeBytes([ addr16 >>> 8, addr16 & 0xff, @@ -1910,7 +1889,6 @@ VL53L1X.prototype = { ]); }, writeReg32Bit: async function(addr16, dat32) { - // console.log("writeReg32Bit:",addr16.toString(16),dat32.toString(16),":",(addr16 >>> 8).toString(16), (addr16 & 0xFF).toString(16), (dat32>>>24).toString(16), (dat32>>>16 & 0xFF).toString(16) , (dat32>>>8 & 0xFF).toString(16), (dat32 & 0xFF).toString(16)); await this.i2cSlave.writeBytes([ addr16 >>> 8, addr16 & 0xff,