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

5 lines
2.3 KiB
JavaScript

function d(b,a){this.i2c=b;this.options=a||{};this.w(c.CTRL_REG1_ADDRESS,c.PD_ACTIVE_MODE|c.BDU_NOT_CONTINUOUS_UPDATE|c.ODR_ONE_HZ);this.w(c.CTRL_AV_CONF,c.AVGT_DEFAULT);a=this.r(50,2);b=a[0];a=a[1];var e=this.r(53,1)[0]&15;this.T0_degC=(256*(e&3)+b)/8;this.T1_degC=(64*(e&12)+a)/8;this.T0_OUT=this.r16(60,!0);this.T1_OUT=this.r16(62,!0);a=this.r(48,2);this.H0_RH=a[0]/2;this.H1_RH=a[1]/2;this.H0_T0_OUT=this.r16(54,!0);this.H1_T0_OUT=this.r16(58,!0);this.options.int&&(this.w(c.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 c={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(b,a){return new d(b,a)};d.prototype.stop=function(){this.watch&&clearWatch(this.watch);this.watch=void 0;this.w(c.CTRL_REG1_ADDRESS,c.PD_POWER_DOWN_MODE)};d.prototype.ObtainCalibratedTemperatureCoefficient=function(){print("ObtainCalibratedTemperatureCoefficient deprecated. It's now done automatically.")};d.prototype.getTemperature=function(b){var a=this.r16(c.DATA_TEMP_OUT_L,!0);a=(this.T1_degC-this.T0_degC)*(a-this.T0_OUT)/(this.T1_OUT-this.T0_OUT)+this.T0_degC;
b&&b(a);return a};d.prototype.getHumidity=function(b){var a=this.r16(c.DATA_HUMIDITY_OUT_L,!0);a=(this.H1_RH-this.H0_RH)*(a-this.H0_T0_OUT)/(this.H1_T0_OUT-this.H0_T0_OUT)+this.H0_RH;b&&b(a);return a};d.prototype.get=function(){return{temperature:this.getTemperature(),humidity:this.getHumidity()}};d.prototype.read=function(b){if(this.options.int)b(this.get());else{var a=setInterval(function(){this.r(c.CTRL_STATUS_ADDRESS)&3&&(clearInterval(a),b(this.get()))}.bind(this),100);return this.get()}};d.prototype.r=
function(b,a){this.i2c.writeTo({address:c.HTS221_I2C_SLAVE_ADDRESS,stop:!1},128|b);return this.i2c.readFrom(c.HTS221_I2C_SLAVE_ADDRESS,a)};d.prototype.r16=function(b,a){b=this.r(b,2);b=b[1]<<8|b[0];a&&b&32768&&(b-=65536);return b};d.prototype.w=function(b,a){this.i2c.writeTo(c.HTS221_I2C_SLAVE_ADDRESS,b,a)}