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

4 lines
1.9 KiB
JavaScript

function m(a){switch(a){case 0:return g.IDLE;case 1:return g.S1;case 2:return g.S10;case 3:return g.S60;case 4:return g.S025;default:throw"Invalid mode!"+a;}}function l(a,b){return function(c,d){a.write(0);c=b(c,d);a.write(1);return c}}function f(a,b,c){this.r=c&&c.nWake?l(c.nWake,a):a;this.w=c&&c.nWake?l(c.nWake,b):b;this.options=c||{};this.options.mode=this.options.mode||1;this.w(e.SW_R,[17,229,114,138]);var d=this;setTimeout(function(){if(d.r(e.WHOI,1)[0]!=h.WHIV)throw"CCS811 WHO_AM_I check failed";
d.w(h.BLAS,[]);setTimeout(function(){if(!d.r(e.STAT,1)[0]&h.SFM)throw"CCS811 not in FW mode";d.setMode(d.options.mode)},100)},100)}const h={WHIV:129,SDRD:8,SFM:128,MMID:8,BLAS:244},g={IDLE:0,S1:16,S10:32,S60:48,S025:64},e={STAT:0,MEMO:1,ARD:2,ENVD:5,BASE:17,WHOI:32,SW_R:255};f.prototype._setupWatch=function(){var a=this;a.options.int&&!a.watch&&(a.watch=setWatch(function(){a.emit("data",a.get())},a.options.int,{edge:"falling",repeat:!0}))};f.prototype.stop=function(){this.options.mode=0;this.watch&&
(this.watch=clearWatch(this.watch));this.w(e.MEMO,g.IDLE)};f.prototype.available=function(){return 0!=(this.r(e.STAT,1)[0]&h.SDRD)};f.prototype.setMode=function(a){if(0==a)this.stop();else{var b=m(a);this.options.mode=a;this.options.int?(this._setupWatch(),this.w(e.MEMO,b|h.MMID)):this.w(e.MEMO,b)}};f.prototype.readBaseline=function(){return this.r(e.BASE,2)};f.prototype.writeBaseline=function(a){this.w(e.BASE,a)};f.prototype.setEnvData=function(a,b){if(isNaN(a))throw"CCS811 RH NaN!";if(isNaN(b))throw"CCS811 Temp NaN!";
if(!(0<=a&&100>=a))throw"CCS811 RH invalid!";a*=512;b=512*(Math.max(-25,b)+25);this.w(e.ENVD,[a>>8,a&255,b>>8,b&255])};f.prototype.get=function(){var a=this.r(e.ARD,5);return{eCO2:a[0]<<8|a[1],TVOC:a[2]<<8|a[3],new:0!=(a[4]&h.SDRD)}};exports.connectI2C=function(a,b){var c=b&&b.addr||90;return new f(function(d,k){a.writeTo(c,d);return a.readFrom(c,k)},function(d,k){a.writeTo(c,d,k)},b)}