Espruino/libs/js/BME280.min.js
2023-06-13 09:22:21 +01:00

5 lines
2.0 KiB
JavaScript

function f(a,b,c){this.read=b;this.write=c;this.write(242,1);this.write(244,39);this.write(245,160);this.readCoefficients()}function e(a,b){a=(a[b+1]<<8)+a[b];a&32768&&(a-=65536);return a}f.prototype.setPower=function(a){var b=this.read(244,1)[0];this.write(244,a?b|3:b&-4)};f.prototype.readCoefficients=function(){var a=new Uint8Array(32);a.set(this.read(136,24),0);a.set(this.read(161,1),24);a.set(this.read(225,7),25);this.dT=[,a[1]<<8|a[0],e(a,2),e(a,4)];this.dP=[,a[7]<<8|
a[6],e(a,8),e(a,10),e(a,12),e(a,14),e(a,16),e(a,18),e(a,20),e(a,22)];this.dH=[,a[24],e(a,25),a[27],a[28]<<4|15&a[29],a[30]<<4|a[29]>>4&15,a[31]]};f.prototype.readRawData=function(){var a=this.read(247,8);this.pres_raw=a[0]<<12|a[1]<<4|a[2]>>4;this.temp_raw=a[3]<<12|a[4]<<4|a[5]>>4;this.hum_raw=a[6]<<8|a[7]};f.prototype.calibration_T=function(a){var b=this.dT;var c=(a/16384-b[1]/1024)*b[2];a=(a/131072-b[1]/8192)*(a/131072-b[1]/8192)*b[3];this.t_fine=c+a;return(c+a)/5120*100};f.prototype.calibration_P=
function(a){var b=this.dP;var c=this.t_fine/2-64E3;var d=c*c*b[6]/32768;d+=c*b[5]*2;d=d/4+65536*b[4];c=(b[3]*c*c/524288+b[2]*c)/524288;c=(1+c/32768)*b[1];if(0===c)return 0;a=6250*(1048576-a-d/4096)/c;c=b[9]*a*a/2147483648;d=a*b[8]/32768;return a+=(c+d+b[7])/16};f.prototype.calibration_H=function(a){var b=this.dH;var c=this.t_fine-76800;c=((a<<14)-(b[4]<<20)-b[5]*c+16384>>15)*((((c*b[6]>>10)*((c*b[3]>>11)+32768)>>10)+2097152)*b[2]+8192>>14);c-=((c>>15)*(c>>15)>>7)*b[1]>>4;c=E.clip(c,0,419430400);return c>>
12};f.prototype.getData=function(){this.readRawData();return{temp:this.calibration_T(this.temp_raw)/100,pressure:this.calibration_P(this.pres_raw)/100,humidity:this.calibration_H(this.hum_raw)/1024}};exports.connect=function(a,b){var c=b&&b.addr||118;return new f(b,function(d,g){a.writeTo(c,d);return a.readFrom(c,g)},function(d,g){a.writeTo(c,[d,g])})};exports.connectSPI=function(a,b,c){return new f(c,function(d,g){return a.send([d|128,new Uint8Array(g)],b).slice(1)},function(d,g){a.write(d&127,g,
b)})}