mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
5 lines
2.1 KiB
JavaScript
5 lines
2.1 KiB
JavaScript
function f(c,a,d){this.r=c;this.w=a;this.options=d||{};this.callback=this.options.callback;if(this.r(b.WHO_AM_I,1)[0]!=b.I_AM_MASK)throw"LIS2DH12 WHO_AM_I check failed";this.g_scale=b.SCALE2G;this.mode="powerdown";this.interval=void 0}var b={WHO_AM_I:15,CTRL_REG1:32,CTRL_REG2:33,CTRL_REG3:34,CTRL_REG4:35,STATUS_REG:39,OUT_X_L:40,I_AM_MASK:51,ODR_MASK_1HZ:16,ODR_MASK_10HZ:32,ODR_MASK_25HZ:48,ODR_MASK_50HZ:64,ODR_MASK_100HZ:80,ODR_MASK_200HZ:96,ODR_MASK_400HZ:112,ODR_MASK_1620HZ:128,ODR_MASK_HIGH_RES:144,
|
|
LPEN_MASK:8,XYZ_EN_MASK:7,HR_MASK:8,SCALE2G:0,SCALE4G:1,SCALE8G:2,SCALE16G:3};f.prototype.setPowerMode=function(c){var a=b.XYZ_EN_MASK,d=this.g_scale<<4,e=0;switch(c){case "normal":a|=b.ODR_MASK_100HZ;this.g_mgpb=4<<this.g_scale;e=10;break;case "low":a|=b.ODR_MASK_1HZ;this.g_mgpb=4<<this.g_scale;e=1E3;break;case "fast":a|=b.ODR_MASK_1620HZ|b.LPEN_MASK;this.g_mgpb=16<<this.g_scale;e=1;break;case "highres":a|=b.ODR_MASK_HIGH_RES;d|=b.HR_MASK;this.g_mgpb=1<<this.g_scale;e=1;break;case "powerdown":e=
|
|
a=0;break;default:throw"Unknown power mode "+JSON.stringify(c);}this.w(b.CTRL_REG1,a);this.w(b.CTRL_REG4,d);this.mode=c;this.interval&&clearInterval(this.interval);this.interval=void 0;e&&this.callback&&(this.interval=setInterval(function(a){a.callback(a.getXYZ())},e,this))};f.prototype.getXYZ=function(){var c=new DataView((new Uint8Array(this.r(b.STATUS_REG,7))).buffer),a=1/(16<<this.g_scale)*1E3/1024;return{x:c.getInt16(1,!0)*a,y:c.getInt16(3,!0)*a,z:c.getInt16(5,!0)*a,"new":0!=(c.getUint8(0)&8)}};
|
|
f.prototype.readXYZ=function(c){var a="powerdown"==this.mode,b=this.callback;this.callback=function(e){a&&this.setPowerMode("powerdown");this.callback=b;c(e)};a&&this.setPowerMode("normal")};exports.connectSPI=function(c,a,b){if("function"==typeof b)throw Error("Use require(LIS2DH12).connectSPI(..., {callback:function() { ... }} instead");return new f(function(b,d){return c.send([b|192,new Uint8Array(d)],a).slice(1)},function(b,d){return c.write(b,d,a)},b)};exports.connectI2C=function(b,a){var c=
|
|
a&&a.addr||25;return new f(function(a,d){b.writeTo(c,a|128);return b.readFrom(c,d)},function(a,d){b.writeTo(c,a,d)},a)} |