Skip to content

Commit

Permalink
Merge pull request #18 from chirimen-oh/resolve-issue-7
Browse files Browse the repository at this point in the history
リファレンスエラー修正
  • Loading branch information
kou029w authored Jan 25, 2020
2 parents 47c52c3 + 3292080 commit 7958b9d
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 142 deletions.
99 changes: 48 additions & 51 deletions packages/ads1015/ads1015.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down
99 changes: 48 additions & 51 deletions packages/ads1015/ads1015.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
};

Expand Down
7 changes: 5 additions & 2 deletions packages/ads1x15/ads1x15.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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){
Expand Down
7 changes: 5 additions & 2 deletions packages/ads1x15/ads1x15.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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){
Expand Down
3 changes: 3 additions & 0 deletions packages/ak8963/ak8963.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions packages/ak8963/ak8963.mjs
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
5 changes: 4 additions & 1 deletion packages/bh1750/bh1750.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion packages/bh1750/bh1750.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions packages/grove-oled-display/grove-oled-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
},
Expand Down
5 changes: 3 additions & 2 deletions packages/grove-oled-display/grove-oled-display.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
// Source : http://wiki.seeedstudio.com/wiki/Grove_-_OLED_Display_128*64

const OLED_CONST = {
Expand Down Expand Up @@ -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);
},
Expand Down
5 changes: 4 additions & 1 deletion packages/ina219/ina219.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
5 changes: 4 additions & 1 deletion packages/ina219/ina219.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
Loading

0 comments on commit 7958b9d

Please sign in to comment.