ffff

Dependencies:   Servo pourtibo driver_mbed_TH02

Committer:
paparoms
Date:
Tue Mar 08 11:01:51 2022 +0000
Revision:
64:b57da430b53c
Parent:
58:81c66fac6476
dd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
superphil06 58:81c66fac6476 1 // **********************************************************************************
superphil06 58:81c66fac6476 2 // Driver definition for HopeRF TH02 temperature and humidity sensor
superphil06 58:81c66fac6476 3 // **********************************************************************************
superphil06 58:81c66fac6476 4 // Creative Commons Attrib Share-Alike License
superphil06 58:81c66fac6476 5 // You are free to use/extend this library but please abide with the CC-BY-SA license:
superphil06 58:81c66fac6476 6 // http://creativecommons.org/licenses/by-sa/4.0/
superphil06 58:81c66fac6476 7 //
superphil06 58:81c66fac6476 8 // For any explanation see TH02 sensor information at
superphil06 58:81c66fac6476 9 // http://www.hoperf.com/sensor/app/TH02.htm
superphil06 58:81c66fac6476 10 //
superphil06 58:81c66fac6476 11 // Code based on following datasheet
paparoms 64:b57da430b53c 12 // http://www.hoperf.com/upload/sensor/TH02_V1.1.pdf
superphil06 58:81c66fac6476 13 //
superphil06 58:81c66fac6476 14 // Written by Charles-Henri Hallard (http://hallard.me)
superphil06 58:81c66fac6476 15 //
superphil06 58:81c66fac6476 16 // History : V1.00 2014-07-14 - First release
superphil06 58:81c66fac6476 17 // V1.10 2015-04-13 - changed to Wire library instead of m_I2C
superphil06 58:81c66fac6476 18 //
superphil06 58:81c66fac6476 19 // All text above must be included in any redistribution.
superphil06 58:81c66fac6476 20 //
superphil06 58:81c66fac6476 21 // **********************************************************************************
superphil06 58:81c66fac6476 22 #include "th02.h"
superphil06 58:81c66fac6476 23 #include "mbed.h"
superphil06 58:81c66fac6476 24 #include "math.h"
superphil06 58:81c66fac6476 25
superphil06 58:81c66fac6476 26 // Class Constructor
superphil06 58:81c66fac6476 27
superphil06 58:81c66fac6476 28 TH02::TH02(PinName sda,PinName scl,uint8_t address): m_I2C(sda, scl)
paparoms 64:b57da430b53c 29 {
paparoms 64:b57da430b53c 30 _address = address; // m_I2C Module Address
paparoms 64:b57da430b53c 31 _last_temp = TH02_UNINITIALIZED_TEMP; // Last measured temperature (for linearization)
paparoms 64:b57da430b53c 32 _last_rh = TH02_UNINITIALIZED_RH; // Last measured RH
superphil06 58:81c66fac6476 33 }
superphil06 58:81c66fac6476 34
superphil06 58:81c66fac6476 35 TH02::~TH02()
superphil06 58:81c66fac6476 36 {
superphil06 58:81c66fac6476 37
superphil06 58:81c66fac6476 38 }
superphil06 58:81c66fac6476 39
superphil06 58:81c66fac6476 40
superphil06 58:81c66fac6476 41
superphil06 58:81c66fac6476 42 /* ======================================================================
superphil06 58:81c66fac6476 43 Function: writeCommand
paparoms 64:b57da430b53c 44 Purpose : write the "register address" value on m_I2C bus
superphil06 58:81c66fac6476 45 Input : register address
superphil06 58:81c66fac6476 46 true if we need to release the bus after (default yes)
superphil06 58:81c66fac6476 47 Output : Arduino Wire library return code (0 if ok)
paparoms 64:b57da430b53c 48 Comments:
superphil06 58:81c66fac6476 49 ====================================================================== */
superphil06 58:81c66fac6476 50 uint8_t TH02::writeCommand(uint8_t command, bool release)
paparoms 64:b57da430b53c 51 {int iError;
paparoms 64:b57da430b53c 52 (void) m_I2C.start();
paparoms 64:b57da430b53c 53 //Wire.beginTransmission(_address);
paparoms 64:b57da430b53c 54 iError=m_I2C.write(_address);// send adress of i2c slave
paparoms 64:b57da430b53c 55
paparoms 64:b57da430b53c 56 if (iError==1)
paparoms 64:b57da430b53c 57 {
paparoms 64:b57da430b53c 58 // Wire.write(command) ;
paparoms 64:b57da430b53c 59 iError= m_I2C.write(command);
paparoms 64:b57da430b53c 60 }
paparoms 64:b57da430b53c 61 if (release==true)
paparoms 64:b57da430b53c 62 { m_I2C.stop();// return stop error code
paparoms 64:b57da430b53c 63 }
paparoms 64:b57da430b53c 64 if (iError==1) iError=0;// ack received
paparoms 64:b57da430b53c 65 else iError=1;// no ack
paparoms 64:b57da430b53c 66 return iError;
superphil06 58:81c66fac6476 67 }
superphil06 58:81c66fac6476 68
superphil06 58:81c66fac6476 69 /* ======================================================================
superphil06 58:81c66fac6476 70 Function: writeRegister
paparoms 64:b57da430b53c 71 Purpose : write a value on the designed register address on m_I2C bus
superphil06 58:81c66fac6476 72 Input : register address
superphil06 58:81c66fac6476 73 value to write
superphil06 58:81c66fac6476 74 Output : Arduino Wire library return code (0 if ok)
paparoms 64:b57da430b53c 75 Comments:
superphil06 58:81c66fac6476 76 ====================================================================== */
superphil06 58:81c66fac6476 77 uint8_t TH02::writeRegister(uint8_t reg, uint8_t value)
paparoms 64:b57da430b53c 78 { int iError;
paparoms 64:b57da430b53c 79
paparoms 64:b57da430b53c 80 bool ret = false;
superphil06 58:81c66fac6476 81
paparoms 64:b57da430b53c 82 //Wire.beginTransmission(_address);
paparoms 64:b57da430b53c 83 (void) m_I2C.start();
paparoms 64:b57da430b53c 84 iError=m_I2C.write(_address);// send adress of i2c slave
paparoms 64:b57da430b53c 85 // Wire.write(reg);
paparoms 64:b57da430b53c 86 if (iError==1){
paparoms 64:b57da430b53c 87
paparoms 64:b57da430b53c 88 iError= m_I2C.write(reg);
paparoms 64:b57da430b53c 89
paparoms 64:b57da430b53c 90 // Wire.write(value);
paparoms 64:b57da430b53c 91 (void) m_I2C.write(value);
paparoms 64:b57da430b53c 92 }
paparoms 64:b57da430b53c 93 // return Wire.endTransmission();
superphil06 58:81c66fac6476 94 m_I2C.stop();// return stop error code
paparoms 64:b57da430b53c 95 if (iError==1) iError=0;// ack received
paparoms 64:b57da430b53c 96 else iError=1;// no ack
paparoms 64:b57da430b53c 97 return iError;
superphil06 58:81c66fac6476 98 }
superphil06 58:81c66fac6476 99
superphil06 58:81c66fac6476 100 /* ======================================================================
superphil06 58:81c66fac6476 101 Function: readRegister
paparoms 64:b57da430b53c 102 Purpose : read a register address value on m_I2C bus
superphil06 58:81c66fac6476 103 Input : register address
superphil06 58:81c66fac6476 104 pointer where the return value will be filled
superphil06 58:81c66fac6476 105 Output : Arduino Wire library return code (0 if ok)
paparoms 64:b57da430b53c 106 Comments:
superphil06 58:81c66fac6476 107 ====================================================================== */
superphil06 58:81c66fac6476 108 uint8_t TH02::readRegister(uint8_t reg, uint8_t * value)
superphil06 58:81c66fac6476 109 {
paparoms 64:b57da430b53c 110 uint8_t ret ;
paparoms 64:b57da430b53c 111 int iAck,iRedVal,iError;
paparoms 64:b57da430b53c 112 // Send a register reading command
paparoms 64:b57da430b53c 113 // but DO NOT release the m_I2C bus
paparoms 64:b57da430b53c 114 // (void) m_I2C.start();
paparoms 64:b57da430b53c 115 // iError=m_I2C.write(_address);// send adress of i2c slave
paparoms 64:b57da430b53c 116
paparoms 64:b57da430b53c 117 //if (iError==1)
paparoms 64:b57da430b53c 118
paparoms 64:b57da430b53c 119 ret = writeCommand(reg, false);
superphil06 58:81c66fac6476 120
paparoms 64:b57da430b53c 121 if ( ret == 0) //if command ok
paparoms 64:b57da430b53c 122 {
paparoms 64:b57da430b53c 123 // Wire.requestFrom( (uint8_t) _address, (uint8_t) 1);
paparoms 64:b57da430b53c 124 (void) m_I2C.start();
paparoms 64:b57da430b53c 125 iError=m_I2C.write(_address+0x01);// send adress of i2c slave in read mode
paparoms 64:b57da430b53c 126 iRedVal=m_I2C.read(0);//send non ack
paparoms 64:b57da430b53c 127 // if (Wire.available() != 1)
paparoms 64:b57da430b53c 128 /*if (iAck != 1)
paparoms 64:b57da430b53c 129
paparoms 64:b57da430b53c 130 // Other error as Wire library
paparoms 64:b57da430b53c 131 ret = 4;
paparoms 64:b57da430b53c 132 else
paparoms 64:b57da430b53c 133 // grab the value*/
paparoms 64:b57da430b53c 134 *value = iRedVal; // return Red value by ref
paparoms 64:b57da430b53c 135
paparoms 64:b57da430b53c 136 //}
paparoms 64:b57da430b53c 137
paparoms 64:b57da430b53c 138 // Ok now we have finished
paparoms 64:b57da430b53c 139 // Wire.endTransmission();
paparoms 64:b57da430b53c 140
paparoms 64:b57da430b53c 141 }
paparoms 64:b57da430b53c 142 (void) m_I2C.stop();// return stop error code
paparoms 64:b57da430b53c 143 return ret;
paparoms 64:b57da430b53c 144 }
superphil06 58:81c66fac6476 145
superphil06 58:81c66fac6476 146 /* ======================================================================
superphil06 58:81c66fac6476 147 Function: getId
paparoms 64:b57da430b53c 148 Purpose : Get device ID register
superphil06 58:81c66fac6476 149 Input : pointer where the return value will be filled
superphil06 58:81c66fac6476 150 Output : Arduino Wire library return code (0 if ok)
superphil06 58:81c66fac6476 151 Comments: -
superphil06 58:81c66fac6476 152 ====================================================================== */
superphil06 58:81c66fac6476 153 uint8_t TH02::getId(uint8_t * pvalue)
superphil06 58:81c66fac6476 154 {
paparoms 64:b57da430b53c 155 return (readRegister(TH02_ID, pvalue));
superphil06 58:81c66fac6476 156 }
superphil06 58:81c66fac6476 157
superphil06 58:81c66fac6476 158 /* ======================================================================
superphil06 58:81c66fac6476 159 Function: getStatus
paparoms 64:b57da430b53c 160 Purpose : Get device status register
superphil06 58:81c66fac6476 161 Input : pointer where the return value will be filled
superphil06 58:81c66fac6476 162 Output : Arduino Wire library return code (0 if ok)
paparoms 64:b57da430b53c 163 Comments:
superphil06 58:81c66fac6476 164 ====================================================================== */
superphil06 58:81c66fac6476 165 uint8_t TH02::getStatus(uint8_t * pvalue)
superphil06 58:81c66fac6476 166 {
paparoms 64:b57da430b53c 167 return (readRegister(TH02_STATUS, pvalue));
superphil06 58:81c66fac6476 168 }
superphil06 58:81c66fac6476 169
superphil06 58:81c66fac6476 170 /* ======================================================================
superphil06 58:81c66fac6476 171 Function: isConverting
paparoms 64:b57da430b53c 172 Purpose : Indicate if a temperature or humidity conversion is in progress
superphil06 58:81c66fac6476 173 Input : -
superphil06 58:81c66fac6476 174 Output : true if conversion in progress false otherwise
paparoms 64:b57da430b53c 175 Comments:
superphil06 58:81c66fac6476 176 ====================================================================== */
superphil06 58:81c66fac6476 177 bool TH02::isConverting(void)
superphil06 58:81c66fac6476 178 {
paparoms 64:b57da430b53c 179 uint8_t status;
paparoms 64:b57da430b53c 180 // Get status and check RDY bit
paparoms 64:b57da430b53c 181 if ( getStatus(&status) == 0)
paparoms 64:b57da430b53c 182
paparoms 64:b57da430b53c 183 { //printf("\n lecture status %x",status);
paparoms 64:b57da430b53c 184 if ( (status & TH02_STATUS_RDY) ==1 )
paparoms 64:b57da430b53c 185 return true;
paparoms 64:b57da430b53c 186 }
paparoms 64:b57da430b53c 187 return false;
superphil06 58:81c66fac6476 188 }
superphil06 58:81c66fac6476 189
superphil06 58:81c66fac6476 190 /* ======================================================================
superphil06 58:81c66fac6476 191 Function: getConfig
paparoms 64:b57da430b53c 192 Purpose : Get device configuration register
superphil06 58:81c66fac6476 193 Input : pointer where the return value will be filled
superphil06 58:81c66fac6476 194 Output : Arduino Wire library return code (0 if ok)
paparoms 64:b57da430b53c 195 Comments:
superphil06 58:81c66fac6476 196 ====================================================================== */
superphil06 58:81c66fac6476 197 uint8_t TH02::getConfig(uint8_t * pvalue)
superphil06 58:81c66fac6476 198 {
paparoms 64:b57da430b53c 199 return (readRegister(TH02_CONFIG, pvalue));
superphil06 58:81c66fac6476 200 }
superphil06 58:81c66fac6476 201
superphil06 58:81c66fac6476 202 /* ======================================================================
superphil06 58:81c66fac6476 203 Function: setConfig
paparoms 64:b57da430b53c 204 Purpose : Set device configuration register
superphil06 58:81c66fac6476 205 Input : value to set
superphil06 58:81c66fac6476 206 Output : true if succeded, false otherwise
paparoms 64:b57da430b53c 207 Comments:
superphil06 58:81c66fac6476 208 ====================================================================== */
superphil06 58:81c66fac6476 209 uint8_t TH02::setConfig(uint8_t config)
superphil06 58:81c66fac6476 210 {
paparoms 64:b57da430b53c 211 return (writeRegister(TH02_CONFIG, config));
superphil06 58:81c66fac6476 212 }
superphil06 58:81c66fac6476 213
superphil06 58:81c66fac6476 214 /* ======================================================================
superphil06 58:81c66fac6476 215 Function: startTempConv
paparoms 64:b57da430b53c 216 Purpose : Start a temperature conversion
superphil06 58:81c66fac6476 217 Input : - fastmode true to enable fast conversion
superphil06 58:81c66fac6476 218 - heater true to enable heater
superphil06 58:81c66fac6476 219 Output : true if succeded, false otherwise
superphil06 58:81c66fac6476 220 Comments: if heater enabled, it will not be auto disabled
superphil06 58:81c66fac6476 221 ====================================================================== */
superphil06 58:81c66fac6476 222 uint8_t TH02::startTempConv(bool fastmode, bool heater)
superphil06 58:81c66fac6476 223 {
paparoms 64:b57da430b53c 224 // init configuration register to start and temperature
paparoms 64:b57da430b53c 225 uint8_t config = TH02_CONFIG_START | TH02_CONFIG_TEMP;
paparoms 64:b57da430b53c 226
paparoms 64:b57da430b53c 227 // set fast mode and heater if asked
paparoms 64:b57da430b53c 228 if (fastmode) config |= TH02_CONFIG_FAST;
paparoms 64:b57da430b53c 229 if (heater) config |= TH02_CONFIG_HEAT;
paparoms 64:b57da430b53c 230
paparoms 64:b57da430b53c 231 // write to configuration register
paparoms 64:b57da430b53c 232 return ( setConfig( config ) );
superphil06 58:81c66fac6476 233 }
superphil06 58:81c66fac6476 234
superphil06 58:81c66fac6476 235 /* ======================================================================
superphil06 58:81c66fac6476 236 Function: startRHConv
paparoms 64:b57da430b53c 237 Purpose : Start a relative humidity conversion
superphil06 58:81c66fac6476 238 Input : - fastmode true to enable fast conversion
superphil06 58:81c66fac6476 239 - heater true to enable heater
superphil06 58:81c66fac6476 240 Output : true if succeded, false otherwise
superphil06 58:81c66fac6476 241 Comments: if heater enabled, it will not be auto disabled
superphil06 58:81c66fac6476 242 ====================================================================== */
superphil06 58:81c66fac6476 243 uint8_t TH02::startRHConv(bool fastmode, bool heater)
superphil06 58:81c66fac6476 244 {
paparoms 64:b57da430b53c 245 // init configuration register to start and no temperature (so RH)
paparoms 64:b57da430b53c 246 uint8_t config = TH02_CONFIG_START;
paparoms 64:b57da430b53c 247
paparoms 64:b57da430b53c 248 // set fast mode and heater if asked
paparoms 64:b57da430b53c 249 if (fastmode) config |= TH02_CONFIG_FAST;
paparoms 64:b57da430b53c 250 if (heater) config |= TH02_CONFIG_HEAT;
paparoms 64:b57da430b53c 251
paparoms 64:b57da430b53c 252 // write to configuration register
paparoms 64:b57da430b53c 253 return ( setConfig( config ) );
superphil06 58:81c66fac6476 254 }
superphil06 58:81c66fac6476 255
superphil06 58:81c66fac6476 256 /* ======================================================================
superphil06 58:81c66fac6476 257 Function: waitEndConversion
paparoms 64:b57da430b53c 258 Purpose : wait for a temperature or RH conversion is done
paparoms 64:b57da430b53c 259 Input :
paparoms 64:b57da430b53c 260 Output : delay in ms the process took.
superphil06 58:81c66fac6476 261 Comments: if return >= TH02_CONVERSION_TIME_OUT, time out occured
superphil06 58:81c66fac6476 262 ====================================================================== */
superphil06 58:81c66fac6476 263 uint8_t TH02::waitEndConversion(void)
superphil06 58:81c66fac6476 264 {
paparoms 64:b57da430b53c 265 // okay this is basic approach not so accurate
paparoms 64:b57da430b53c 266 // but avoid using long and millis()
paparoms 64:b57da430b53c 267 uint8_t time_out = 0;
paparoms 64:b57da430b53c 268
paparoms 64:b57da430b53c 269 // loop until conversion done or duration >= time out
paparoms 64:b57da430b53c 270 while (isConverting() && time_out <= TH02_CONVERSION_TIME_OUT )
paparoms 64:b57da430b53c 271 {
paparoms 64:b57da430b53c 272 ++time_out;
paparoms 64:b57da430b53c 273 wait_ms(1);
paparoms 64:b57da430b53c 274 }
paparoms 64:b57da430b53c 275
paparoms 64:b57da430b53c 276 // return approx time of conversion
paparoms 64:b57da430b53c 277 return (time_out);
superphil06 58:81c66fac6476 278 }
superphil06 58:81c66fac6476 279
superphil06 58:81c66fac6476 280 /* ======================================================================
superphil06 58:81c66fac6476 281 Function: roundInt
paparoms 64:b57da430b53c 282 Purpose : round a float value to int
superphil06 58:81c66fac6476 283 Input : float value
paparoms 64:b57da430b53c 284 Output : int value rounded
paparoms 64:b57da430b53c 285 Comments:
superphil06 58:81c66fac6476 286 ====================================================================== */
superphil06 58:81c66fac6476 287 int16_t TH02::roundInt(float value)
paparoms 64:b57da430b53c 288 {
superphil06 58:81c66fac6476 289
paparoms 64:b57da430b53c 290 // check positive number and do round
paparoms 64:b57da430b53c 291 if (value >= 0.0f)
paparoms 64:b57da430b53c 292 value = floor(value + 0.5f);
paparoms 64:b57da430b53c 293 else
paparoms 64:b57da430b53c 294 value = ceil(value - 0.5f);
paparoms 64:b57da430b53c 295
paparoms 64:b57da430b53c 296 // return int value
paparoms 64:b57da430b53c 297 return (static_cast<int16_t>(value));
superphil06 58:81c66fac6476 298 }
superphil06 58:81c66fac6476 299
superphil06 58:81c66fac6476 300 /* to avoid math library may I need to test something
superphil06 58:81c66fac6476 301 like that
paparoms 64:b57da430b53c 302 float TH02::showDecimals(float x, int numDecimals)
superphil06 58:81c66fac6476 303 {
superphil06 58:81c66fac6476 304 int y=x;
superphil06 58:81c66fac6476 305 double z=x-y;
superphil06 58:81c66fac6476 306 double m=pow(10,numDecimals);
superphil06 58:81c66fac6476 307 double q=z*m;
superphil06 58:81c66fac6476 308 double r=round(q);
superphil06 58:81c66fac6476 309 return static_cast<double>(y)+(1.0/m)*r;
superphil06 58:81c66fac6476 310 }
superphil06 58:81c66fac6476 311 */
superphil06 58:81c66fac6476 312
superphil06 58:81c66fac6476 313 /* ======================================================================
superphil06 58:81c66fac6476 314 Function: getConversionValue
paparoms 64:b57da430b53c 315 Purpose : return the last converted value to int * 10 to have 1 digit prec.
superphil06 58:81c66fac6476 316 Input : float value
superphil06 58:81c66fac6476 317 Output : value rounded but multiplied per 10 or TH02_UNDEFINED_VALUE on err
superphil06 58:81c66fac6476 318 Comments: - temperature and rh raw values (*100) are stored for raw purpose
superphil06 58:81c66fac6476 319 - the configuration register is checked to see if last conv was
superphil06 58:81c66fac6476 320 a temperature or humidity conversion
superphil06 58:81c66fac6476 321 ====================================================================== */
superphil06 58:81c66fac6476 322 int16_t TH02::getConversionValue(void)
paparoms 64:b57da430b53c 323 { char cMaChaine[3];
paparoms 64:b57da430b53c 324 int iError;
paparoms 64:b57da430b53c 325 int32_t result=0 ;
paparoms 64:b57da430b53c 326 uint8_t config;
paparoms 64:b57da430b53c 327 int16_t ret = TH02_UNDEFINED_VALUE;
paparoms 64:b57da430b53c 328
paparoms 64:b57da430b53c 329 // Prepare reading address of ADC data result
paparoms 64:b57da430b53c 330 if ( writeCommand(TH02_DATAh, false) == 0 )
paparoms 64:b57da430b53c 331 {
superphil06 58:81c66fac6476 332 // Read 2 bytes adc data result MSB and LSB from TH02
superphil06 58:81c66fac6476 333 //Wire.requestFrom( (uint8_t) _address, (uint8_t) 2);
paparoms 64:b57da430b53c 334
paparoms 64:b57da430b53c 335
paparoms 64:b57da430b53c 336 iError= m_I2C.read (_address,cMaChaine,2,false);
superphil06 58:81c66fac6476 337
paparoms 64:b57da430b53c 338
superphil06 58:81c66fac6476 339
superphil06 58:81c66fac6476 340
paparoms 64:b57da430b53c 341 // we got 2 bytes ?
paparoms 64:b57da430b53c 342 // if (Wire.available() == 2)
paparoms 64:b57da430b53c 343 if (iError == 0 )
paparoms 64:b57da430b53c 344 {
paparoms 64:b57da430b53c 345 // convert number
paparoms 64:b57da430b53c 346 // result = Wire.read() << 8;
paparoms 64:b57da430b53c 347 // result |= Wire.read();
paparoms 64:b57da430b53c 348 result=(cMaChaine[0]<<8 )|cMaChaine[1];
paparoms 64:b57da430b53c 349 // Get configuration to know what was asked last time
paparoms 64:b57da430b53c 350 if (getConfig(&config)==0)
paparoms 64:b57da430b53c 351 {
paparoms 64:b57da430b53c 352 // last conversion was temperature ?
paparoms 64:b57da430b53c 353 if( config & TH02_CONFIG_TEMP)
paparoms 64:b57da430b53c 354 {
paparoms 64:b57da430b53c 355 result >>= 2; // remove 2 unused LSB bits
paparoms 64:b57da430b53c 356 result *= 100; // multiply per 100 to have int value with 2 decimal
paparoms 64:b57da430b53c 357 result /= 32; // now apply datasheet formula
paparoms 64:b57da430b53c 358 if(result >= 5000)
paparoms 64:b57da430b53c 359 {
paparoms 64:b57da430b53c 360 result -= 5000;
paparoms 64:b57da430b53c 361 }
paparoms 64:b57da430b53c 362 else
paparoms 64:b57da430b53c 363 {
paparoms 64:b57da430b53c 364 result -= 5000;
paparoms 64:b57da430b53c 365 result = -result;
paparoms 64:b57da430b53c 366 }
paparoms 64:b57da430b53c 367
paparoms 64:b57da430b53c 368 // now result contain temperature * 100
paparoms 64:b57da430b53c 369 // so 2134 is 21.34 C
paparoms 64:b57da430b53c 370
paparoms 64:b57da430b53c 371 // Save raw value
paparoms 64:b57da430b53c 372 _last_temp = result;
paparoms 64:b57da430b53c 373 }
paparoms 64:b57da430b53c 374 // it was RH conversion
paparoms 64:b57da430b53c 375 else
paparoms 64:b57da430b53c 376 {
paparoms 64:b57da430b53c 377 result >>= 4; // remove 4 unused LSB bits
paparoms 64:b57da430b53c 378 result *= 100; // multiply per 100 to have int value with 2 decimal
paparoms 64:b57da430b53c 379 result /= 16; // now apply datasheet formula
paparoms 64:b57da430b53c 380 result -= 2400;
superphil06 58:81c66fac6476 381
paparoms 64:b57da430b53c 382 // now result contain humidity * 100
paparoms 64:b57da430b53c 383 // so 4567 is 45.67 % RH
paparoms 64:b57da430b53c 384 _last_rh = result;
paparoms 64:b57da430b53c 385 }
superphil06 58:81c66fac6476 386
paparoms 64:b57da430b53c 387 // remember result value is multiplied by 10 to avoid float calculation later
paparoms 64:b57da430b53c 388 // so humidity of 45.6% is 456 and temp of 21.3 C is 213
superphil06 58:81c66fac6476 389 ret = roundInt(result/10.0f);
superphil06 58:81c66fac6476 390 }
paparoms 64:b57da430b53c 391 } // if got 2 bytes from m_I2C
superphil06 58:81c66fac6476 392
paparoms 64:b57da430b53c 393 // Ok now we have finished with m_I2C, release
paparoms 64:b57da430b53c 394 //Wire.endTransmission();
paparoms 64:b57da430b53c 395
superphil06 58:81c66fac6476 396
paparoms 64:b57da430b53c 397 } // if write command TH02_DATAh
paparoms 64:b57da430b53c 398 (void) m_I2C.stop();
paparoms 64:b57da430b53c 399 return ret;
paparoms 64:b57da430b53c 400 }
superphil06 58:81c66fac6476 401
superphil06 58:81c66fac6476 402 /* ======================================================================
superphil06 58:81c66fac6476 403 Function: getConpensatedRH
paparoms 64:b57da430b53c 404 Purpose : return the compensated calulated humidity
superphil06 58:81c66fac6476 405 Input : true if we want to round value to 1 digit precision, else 2
superphil06 58:81c66fac6476 406 Output : the compensed RH value (rounded or not)
paparoms 64:b57da430b53c 407 Comments:
superphil06 58:81c66fac6476 408 ====================================================================== */
superphil06 58:81c66fac6476 409 int16_t TH02::getConpensatedRH(bool round)
superphil06 58:81c66fac6476 410 {
paparoms 64:b57da430b53c 411 float rhvalue ;
paparoms 64:b57da430b53c 412 float rhlinear ;
paparoms 64:b57da430b53c 413 int16_t ret = TH02_UNDEFINED_VALUE;
superphil06 58:81c66fac6476 414
paparoms 64:b57da430b53c 415 // did we had a previous measure RH
paparoms 64:b57da430b53c 416 if (_last_rh != TH02_UNINITIALIZED_RH)
paparoms 64:b57da430b53c 417 {
paparoms 64:b57da430b53c 418 // now we're float restore real value RH value
paparoms 64:b57da430b53c 419 rhvalue = (float) _last_rh / 100.0 ;
paparoms 64:b57da430b53c 420
paparoms 64:b57da430b53c 421 // apply linear compensation
paparoms 64:b57da430b53c 422 rhlinear = rhvalue - ((rhvalue*rhvalue) * TH02_A2 + rhvalue * TH02_A1 + TH02_A0);
superphil06 58:81c66fac6476 423
paparoms 64:b57da430b53c 424 // correct value
paparoms 64:b57da430b53c 425 rhvalue = rhlinear;
paparoms 64:b57da430b53c 426
paparoms 64:b57da430b53c 427 // do we have a initialized temperature value ?
paparoms 64:b57da430b53c 428 if (_last_temp != TH02_UNINITIALIZED_TEMP )
paparoms 64:b57da430b53c 429 {
paparoms 64:b57da430b53c 430 // Apply Temperature compensation
paparoms 64:b57da430b53c 431 // remember last temp was stored * 100
paparoms 64:b57da430b53c 432 rhvalue += ((_last_temp/100.0) - 30.0) * (rhlinear * TH02_Q1 + TH02_Q0);
superphil06 58:81c66fac6476 433 }
paparoms 64:b57da430b53c 434
paparoms 64:b57da430b53c 435 // now get back * 100 to have int with 2 digit precision
paparoms 64:b57da430b53c 436 rhvalue *= 100;
paparoms 64:b57da430b53c 437
paparoms 64:b57da430b53c 438 // do we need to round to 1 digit ?
paparoms 64:b57da430b53c 439 if (round)
paparoms 64:b57da430b53c 440 {
paparoms 64:b57da430b53c 441 // remember result value is multiplied by 10 to avoid float calculation later
paparoms 64:b57da430b53c 442 // so humidity of 45.6% is 456
paparoms 64:b57da430b53c 443 ret = roundInt(rhvalue/10.0f);
paparoms 64:b57da430b53c 444 }
paparoms 64:b57da430b53c 445 else
paparoms 64:b57da430b53c 446 {
paparoms 64:b57da430b53c 447 ret = (int16_t) rhvalue;
paparoms 64:b57da430b53c 448 }
paparoms 64:b57da430b53c 449 }
paparoms 64:b57da430b53c 450
paparoms 64:b57da430b53c 451 return ret;
superphil06 58:81c66fac6476 452 }
superphil06 58:81c66fac6476 453
superphil06 58:81c66fac6476 454 /* ======================================================================
superphil06 58:81c66fac6476 455 Function: getLastRawRH
paparoms 64:b57da430b53c 456 Purpose : return the raw humidity * 100
paparoms 64:b57da430b53c 457 Input :
superphil06 58:81c66fac6476 458 Output : int value (ie 4123 for 41.23%)
paparoms 64:b57da430b53c 459 Comments:
superphil06 58:81c66fac6476 460 ====================================================================== */
superphil06 58:81c66fac6476 461 int32_t TH02::getLastRawRH(void)
superphil06 58:81c66fac6476 462 {
paparoms 64:b57da430b53c 463 return _last_rh;
superphil06 58:81c66fac6476 464 }
superphil06 58:81c66fac6476 465
superphil06 58:81c66fac6476 466 /* ======================================================================
superphil06 58:81c66fac6476 467 Function: getLastRawTemp
paparoms 64:b57da430b53c 468 Purpose : return the raw temperature value * 100
paparoms 64:b57da430b53c 469 Input :
superphil06 58:81c66fac6476 470 Output : int value (ie 2124 for 21.24 C)
paparoms 64:b57da430b53c 471 Comments:
superphil06 58:81c66fac6476 472 ====================================================================== */
paparoms 64:b57da430b53c 473 int32_t TH02::getLastRawTemp(void)
superphil06 58:81c66fac6476 474 {
paparoms 64:b57da430b53c 475 return _last_temp;
superphil06 58:81c66fac6476 476 }