mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Thingy52: Add 9 axis MPU support
This commit is contained in:
parent
9d0afe9792
commit
3ad5801072
@ -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)
|
||||
|
||||
@ -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',
|
||||
|
||||
4
libs/nordic_thingy/MPU9250.min.js
vendored
Normal file
4
libs/nordic_thingy/MPU9250.min.js
vendored
Normal file
@ -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)}
|
||||
@ -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) {
|
||||
|
||||
11
libs/nordic_thingy/Thingy.min.js
vendored
11
libs/nordic_thingy/Thingy.min.js
vendored
@ -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<this.sounds)throw Error("Too many sounds playing at once");
|
||||
var k=V8,v=D6,l=V9,m=D25,n=D26,p=V10,q=V11,w=V12,x=D22,r=[V13,V14,V15],t=D23,y=D24,z=D17,A=D28,u=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.onMPU=function(a){a?this.mpu?this.mpu.callback=a:(k.set(),this.mpu=require("MPU9250").connectI2C(c),this.mpu.samplerate=10,this.mpu.callback=a,this.mpu.watch=setWatch(function(){this.mpu.callback(this.mpu.read())}.bind(this),v,{repeat:1,edge:"rising"}),setTimeout(this.mpu.initMPU9250.bind(this.mpu),10)):(this.mpu&&clearWatch(this.mpu.watch),k.reset(),this.mpu=void 0)};exports.getMPU=function(a){if(this.mpu)a(this.mpu.read());
|
||||
else this.onMPU(function(b){this.onMPU();a(b)}.bind(this))};exports.onPressure=function(a){a?(this.pressure||(this.pressure=require("LPS22HB").connectI2C(c,{"int":t}),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":t});b.read(function(c){b.stop();
|
||||
a(c)})}};exports.onHumidity=function(a){a?(this.humidity||(this.humidity=require("HTS221").connect(c,{"int":y}),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||(p.set(),q.set(),w.reset(),
|
||||
this.gas=require("CCS811").connectI2C(c,{"int":x}),this.gas.on("data",function(a){this.gasCallback(a)}.bind(this))),this.gasCallback=a):(this.gas&&(this.gas.stop(),q.reset(),p.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(r,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(r,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){u.set();var b={charging:z.read(),voltage:E.getAnalogVRef()*analogRead(A)*1500/180};u.reset();a&&a(b);return b};exports.sound=function(a,b,c){this.sounds||(this.sounds=0);if(2<this.sounds)throw Error("Too many sounds playing at once");
|
||||
var g=new Waveform(a.length);g.buffer.set(a);g.on("finish",function(a){this.sounds--;this.sounds||(f.reset(),digitalWrite(d,0));c&&c()}.bind(this));this.sounds||(analogWrite(d,.5,{freq:4E4}),f.set());this.sounds++;g.startOutput(d,b)};exports.beep=function(a,b){b=0<b?b:250;analogWrite(d,.5,{freq:0<a?a:500});f.set();this.beepTimeout&&clearTimeout(this.beepTimeout);this.beepTimeout=setTimeout(function(){delete this.beepTimeout;f.reset();digitalWrite(d,0)}.bind(this),b)};exports.record=function(a,b){function c(){clearInterval(k);
|
||||
poke8(1073860612,1);poke8(1073861888,0);poke32(1073861952,4294967295);poke32(1073861956,4294967295);l.reset();E.mapInPlace(h,h,function(a){return a+128});b&&setTimeout(b,0,h)}var g=new ArrayBuffer(2049),d=E.getAddressOf(g,!0);if(!d)throw Error("Unable to create a buffer");var h=new Uint8Array(a),f=0,e=0;l.set();n.mode("output");m.mode("input");poke32(1073861892,138412032);poke32(1073861896,1);poke32(1073861912,72);poke32(1073861916,72);poke32(1073861952,n.getInfo().num);poke32(1073861956,m.getInfo().num);
|
||||
poke32(1073861984,d);poke32(1073861988,512);poke32(1073861888,1);poke8(1073860864,0);poke8(1073860868,0);poke8(1073860872,0);poke8(1073860608,1);poke32(1073861984,d+1024);var k=setInterval(function(){peek8(1073860872)&&(poke8(1073860872,0),poke32(1073861984,d+1024*e),h.set(new Uint32Array(g,1+1024*e,256),f),e=1-e,f+=256,f>=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&&
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user