Espruino/libs/js/HTS221.min.js
2019-03-13 14:44:34 +00:00

5 lines
2.3 KiB
JavaScript

function d(c,b){this.i2c=c;this.options=b||{};this.w(a.CTRL_REG1_ADDRESS,a.PD_ACTIVE_MODE|a.BDU_NOT_CONTINUOUS_UPDATE|a.ODR_ONE_HZ);this.w(a.CTRL_AV_CONF,a.AVGT_DEFAULT);var e=this.r(50,2),d=e[0];e=e[1];var f=this.r(53,1)[0]&15;this.T0_degC=(256*(f&3)+d)/8;this.T1_degC=(64*(f&12)+e)/8;this.T0_OUT=this.r16(60,!0);this.T1_OUT=this.r16(62,!0);e=this.r(48,2);this.H0_RH=e[0]/2;this.H1_RH=e[1]/2;this.H0_T0_OUT=this.r16(54,!0);this.H1_T0_OUT=this.r16(58,!0);this.options["int"]&&(this.w(a.CTRL_REG3_ADDRESS,
4),this.watch=setWatch(function(){this.emit("data",this.get())}.bind(this),this.options["int"],{edge:"rising",repeat:!0}),this.get())}var a={HTS221_I2C_SLAVE_ADDRESS:95,CTRL_WHO_AM_I_ADDRESS:15,CTRL_AV_CONF:16,CTRL_REG1_ADDRESS:32,CTRL_REG3_ADDRESS:34,CTRL_STATUS_ADDRESS:39,PD_ACTIVE_MODE:128,PD_POWER_DOWN_MODE:0,BDU_CONTINUOUS_UPDATE:0,BDU_NOT_CONTINUOUS_UPDATE:4,ODR_ONE_SHOT:0,ODR_ONE_HZ:1,ODR_SEVEN_HZ:2,ODR_TWELVE_DOT_FIVE_HZ:3,AVGT_DEFAULT:27,DATA_HUMIDITY_OUT_L:40,DATA_HUMIDITY_OUT_H:41,DATA_TEMP_OUT_L:42,
DATA_TEMP_OUT_H:43};exports.connect=function(c,b){return new d(c,b)};d.prototype.stop=function(){this.watch&&clearWatch(this.watch);this.watch=void 0;this.w(a.CTRL_REG1_ADDRESS,a.PD_POWER_DOWN_MODE)};d.prototype.ObtainCalibratedTemperatureCoefficient=function(){print("ObtainCalibratedTemperatureCoefficient deprecated. It's now done automatically.")};d.prototype.getTemperature=function(c){var b=this.r16(a.DATA_TEMP_OUT_L,!0);b=(this.T1_degC-this.T0_degC)*(b-this.T0_OUT)/(this.T1_OUT-this.T0_OUT)+this.T0_degC;
c&&c(b);return b};d.prototype.getHumidity=function(c){var b=this.r16(a.DATA_HUMIDITY_OUT_L,!0);b=(this.H1_RH-this.H0_RH)*(b-this.H0_T0_OUT)/(this.H1_T0_OUT-this.H0_T0_OUT)+this.H0_RH;c&&c(b);return b};d.prototype.get=function(){return{temperature:this.getTemperature(),humidity:this.getHumidity()}};d.prototype.read=function(c){if(this.options["int"])c(this.get());else{var b=setInterval(function(){this.r(a.CTRL_STATUS_ADDRESS)&3&&(clearInterval(b),c(this.get()))}.bind(this),100);return this.get()}};
d.prototype.r=function(c,b){this.i2c.writeTo({address:a.HTS221_I2C_SLAVE_ADDRESS,stop:!1},128|c);return this.i2c.readFrom(a.HTS221_I2C_SLAVE_ADDRESS,b)};d.prototype.r16=function(c,b){var a=this.r(c,2);a=a[1]<<8|a[0];b&&a&32768&&(a-=65536);return a};d.prototype.w=function(c,b){this.i2c.writeTo(a.HTS221_I2C_SLAVE_ADDRESS,c,b)}