From 3ad580107292c00a4ed680a7dc69b160aec4b18b Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Thu, 29 Mar 2018 15:38:41 +0100 Subject: [PATCH] Thingy52: Add 9 axis MPU support --- ChangeLog | 1 + boards/THINGY52.py | 1 + libs/nordic_thingy/MPU9250.min.js | 4 ++++ libs/nordic_thingy/Thingy.js | 37 +++++++++++++++++++++++++++++-- libs/nordic_thingy/Thingy.min.js | 11 ++++----- libs/nordic_thingy/update_js.sh | 1 + 6 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 libs/nordic_thingy/MPU9250.min.js diff --git a/ChangeLog b/ChangeLog index 1cd4b8e8a..f790f1fff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,7 @@ Fix corrupted timer channels returned by Pin.getInfo Add command history to debugger Remove process.env.EXPORTS (EXPTR does the same but takes less space) + Thingy52: Add 9 axis MPU support 1v96 : ESP8266: no callback if SSID is not available (fix #1297) ESP8266: esp8266 wifi getStatus doesn't show savedMode (fix #752) diff --git a/boards/THINGY52.py b/boards/THINGY52.py index 6f91283ef..355058065 100644 --- a/boards/THINGY52.py +++ b/boards/THINGY52.py @@ -43,6 +43,7 @@ info = { 'INCLUDE += -I$(ROOT)/libs/nordic_thingy', 'WRAPPERSOURCES += libs/nordic_thingy/jswrap_thingy.c', 'JSMODULESOURCES+=libs/nordic_thingy/LIS2DH12.min.js', + 'JSMODULESOURCES+=libs/nordic_thingy/MPU9250.min.js', 'JSMODULESOURCES+=libs/nordic_thingy/LPS22HB.min.js', 'JSMODULESOURCES+=libs/nordic_thingy/HTS221.min.js', 'JSMODULESOURCES+=libs/nordic_thingy/CCS811.min.js', diff --git a/libs/nordic_thingy/MPU9250.min.js b/libs/nordic_thingy/MPU9250.min.js new file mode 100644 index 000000000..9b6222aed --- /dev/null +++ b/libs/nordic_thingy/MPU9250.min.js @@ -0,0 +1,4 @@ +function c(a,b,d,e,c){this.r=a;this.w=b;this.rmag=d;this.wmag=e;this.Gscale=this.Ascale=0;this.gyrosensitivity=131;this.accelsensitivity=16384;this.samplerate=200}c.prototype.calibrateMPU9250=function(){return new Promise(function(a){a("calibrateMPU9250 not working at the moment")})};c.prototype.initMPU9250=function(){if(113!=this.r(117,1)[0])throw"MPU9250 WHO_AM_I check failed";var a=this;return(new Promise(function(b){a.w(107,0);setTimeout(b,100)})).then(function(){a.w(107,1);return new Promise(function(a){setTimeout(a, +200)})}).then(function(){a.w(26,3);a.w(25,Math.clip(Math.round(1E3/a.samplerate)-1,0,255));var b=a.r(27,1)[0];b=b&-27|a.Gscale<<3;a.w(27,b);b=a.r(28,1)[0];b=b&-25|a.Ascale<<3;a.w(28,b);b=a.r(29,1)[0];a.w(29,b&-16|3);a.w(55,34);a.w(56,1);a.wmag(10,18);return new Promise(function(a){setTimeout(a,100)})})};c.prototype.dataReady=function(){return this.r(58,1)&1};c.prototype.readAccel=function(){var a=new DataView((new Uint8Array(this.r(59,6))).buffer);return{x:a.getInt16(0,0)/this.accelsensitivity,y:a.getInt16(2, +0)/this.accelsensitivity,z:a.getInt16(4,0)/this.accelsensitivity}};c.prototype.readGyro=function(){var a=new DataView((new Uint8Array(this.r(67,6))).buffer);return{x:a.getInt16(0,0)/this.gyrosensitivity,y:a.getInt16(2,0)/this.gyrosensitivity,z:a.getInt16(4,0)/this.gyrosensitivity}};c.prototype.readMag=function(){var a=new DataView((new Uint8Array(this.rmag(3,7))).buffer),b=49120/32760;return{x:a.getInt16(0,1)*b,y:a.getInt16(2,1)*b,z:a.getInt16(4,1)*b}};c.prototype.read=function(){return{accel:this.readAccel(), +gyro:this.readGyro(),mag:this.readMag(),"new":this.dataReady()}};exports.connectI2C=function(a,b){return new c(function(b,c){a.writeTo(104,b);return a.readFrom(104,c)},function(b,c){a.writeTo(104,b,c)},function(b,c){a.writeTo(12,b);return a.readFrom(12,c)},function(b,c){a.writeTo(12,b,c)},b)} \ No newline at end of file diff --git a/libs/nordic_thingy/Thingy.js b/libs/nordic_thingy/Thingy.js index c96906abe..4227790fa 100644 --- a/libs/nordic_thingy/Thingy.js +++ b/libs/nordic_thingy/Thingy.js @@ -54,7 +54,7 @@ exports.onAcceleration = function(callback) { this.accel = undefined; } }; -// Get one callback with a new acceleration value +// Get one callback with a new acceleration value ({x,y,z}) exports.getAcceleration = function(callback) { if (!this.accel) { require("LIS2DH12").connectI2C(i2ce/*, { int:LIS_INT } - not used */).readXYZ(callback); @@ -62,7 +62,40 @@ exports.getAcceleration = function(callback) { this.accel.readXYZ(callback); } } - +// ------------------------------------------------------------------------------------------- MPU9250 +// Get repeated callbacks with {accel,gyro,mag} from the MPU. Call with no argument to disable +exports.onMPU = function(callback) { + if (callback) { + if (!this.mpu) { + MPU_PWR_CTRL.set(); // MPU on + this.mpu = require("MPU9250").connectI2C(i2c); + this.mpu.samplerate = 10; // Hz + this.mpu.callback = callback; + this.mpu.watch = setWatch(function(){ + this.mpu.callback(this.mpu.read()); + }.bind(this),MPU_INT,{repeat:1,edge:"rising"}); + setTimeout(this.mpu.initMPU9250.bind(this.mpu), 10); + } else { + this.mpu.callback = callback; + } + } else { + if (this.mpu) + clearWatch(this.mpu.watch); + MPU_PWR_CTRL.reset(); // MPU off + this.mpu = undefined; + } +}; +// Get one callback with a {accel,gyro,mag} value from the MPU +exports.getMPU = function(callback) { + if (!this.mpu) { + this.onMPU(function(d) { + this.onMPU(); + callback(d); + }.bind(this)); + } else { + callback(this.mpu.read()); + } +} // ------------------------------------------------------------------------------------------- LPS22HB // Get repeated callbacks with {pressure,temperature}. Call with no argument to disable exports.onPressure = function(callback) { diff --git a/libs/nordic_thingy/Thingy.min.js b/libs/nordic_thingy/Thingy.min.js index eaabb0e60..f33832c96 100644 --- a/libs/nordic_thingy/Thingy.min.js +++ b/libs/nordic_thingy/Thingy.min.js @@ -1,8 +1,9 @@ -var l=V9,m=D25,n=D26,k=V10,p=V11,u=V12,v=D22,q=[V13,V14,V15],r=D23,w=D24,x=D17,y=D28,t=V4,d=D27,f=D29,c=new I2C;c.setup({sda:7,scl:8,bitrate:4E5});exports.I2C=c;var e=new I2C;e.setup({sda:14,scl:15,bitrate:4E5});exports.I2CE=e;exports.onAcceleration=function(a){a?(this.accel||(this.accel=require("LIS2DH12").connectI2C(e)),this.accel.callback=a,this.accel.setPowerMode("low")):(this.accel&&this.accel.setPowerMode("powerdown"),this.accel=void 0)};exports.getAcceleration=function(a){this.accel? -this.accel.readXYZ(a):require("LIS2DH12").connectI2C(e).readXYZ(a)};exports.onPressure=function(a){a?(this.pressure||(this.pressure=require("LPS22HB").connectI2C(c,{"int":r}),this.pressure.on("data",function(a){this.pressureCallback(a)}.bind(this))),this.pressureCallback=a):(this.pressure&&this.pressure.stop(),this.pressureCallback=this.pressure=void 0)};exports.getPressure=function(a){if(this.pressure)this.pressure.read(a);else{var b=require("LPS22HB").connectI2C(c,{"int":r});b.read(function(c){b.stop(); -a(c)})}};exports.onHumidity=function(a){a?(this.humidity||(this.humidity=require("HTS221").connect(c,{"int":w}),this.humidity.on("data",function(a){this.humidityCallback(a)}.bind(this))),this.humidityCallback=a):(this.humidity&&this.humidity.stop(),this.humidityCallback=this.humidity=void 0)};exports.getHumidity=function(a){if(this.humidity)this.humidity.read(a);else this.onHumidity(function(b){this.onHumidity();a(b)}.bind(this))};exports.onGas=function(a){a?(this.gas||(k.set(),p.set(),u.reset(), -this.gas=require("CCS811").connectI2C(c,{"int":v}),this.gas.on("data",function(a){this.gasCallback(a)}.bind(this))),this.gasCallback=a):(this.gas&&(this.gas.stop(),p.reset(),k.reset()),this.gasCallback=this.gas=void 0)};exports.getGas=function(a){if(this.gas)a(this.gas.get());else this.onGas(function(b){this.onGas();a(b)}.bind(this))};exports.onColor=function(a){a?(this.color||(digitalWrite(q,0),this.color=require("BH1745").connectI2C(c),this.colorInt=setInterval(function(){this.colorCallback(this.color.read())}.bind(this), -200)),this.colorCallback=a):(this.color&&(clearInterval(this.colorInt),this.color.stop(),digitalWrite(q,7)),this.colorCallback=this.colorInt=this.color=void 0)};exports.getColor=function(a){if(this.color)a(this.color.read());else this.onColor(function(b){this.onColor();a(b)}.bind(this))};exports.getBattery=function(a){t.set();var b={charging:x.read(),voltage:E.getAnalogVRef()*analogRead(y)*1500/180};t.reset();a&&a(b);return b};exports.sound=function(a,b,c){this.sounds||(this.sounds=0);if(2=h.length&&c())},5);return h};E.on("init",function(){if(exports.accel&&exports.accel.callback){var a=exports.accel.callback;exports.accel=void 0;exports.onAcceleration(a)}exports.pressureCallback&& diff --git a/libs/nordic_thingy/update_js.sh b/libs/nordic_thingy/update_js.sh index aa9a318a2..8be28b411 100755 --- a/libs/nordic_thingy/update_js.sh +++ b/libs/nordic_thingy/update_js.sh @@ -1,6 +1,7 @@ #!/bin/bash node ../../../EspruinoDocs/bin/minify.js Thingy.js Thingy.min.js wget https://www.espruino.com/modules/LIS2DH12.min.js -O LIS2DH12.min.js +wget https://www.espruino.com/modules/MPU9250.min.js -O MPU9250.min.js wget https://www.espruino.com/modules/LPS22HB.min.js -O LPS22HB.min.js wget https://www.espruino.com/modules/HTS221.min.js -O HTS221.min.js wget https://www.espruino.com/modules/CCS811.min.js -O CCS811.min.js