Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
* console.logの削除
* Int16Arrayを使う
* 処理の整理
  • Loading branch information
kou029w committed Jan 25, 2020
1 parent 7958b9d commit 2dedfa8
Show file tree
Hide file tree
Showing 27 changed files with 180 additions and 453 deletions.
21 changes: 3 additions & 18 deletions packages/ads1015/ads1015.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
62 changes: 20 additions & 42 deletions packages/ads1x15/ads1x15.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 ) );
Expand Down
34 changes: 12 additions & 22 deletions packages/adt7410/adt7410.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
};

Expand Down
41 changes: 9 additions & 32 deletions packages/ak8963/ak8963.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -90,16 +88,13 @@ 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;
// X, Y, Z axis micro-Tesla (uT) as floats.
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
Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand All @@ -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;
14 changes: 3 additions & 11 deletions packages/amg8833/amg8833.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(){

Expand All @@ -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 = [];
Expand Down Expand Up @@ -74,7 +67,6 @@ AMG8833.prototype = {
ansR.push(tdata[i*8+j]);
}
ans.push(ansR);
// console.log(msg);
}

return ( ans );
Expand Down
Loading

0 comments on commit 2dedfa8

Please sign in to comment.