:)

Dependencies:   Servo Cayenne-LPP

Committer:
ragas
Date:
Tue Feb 11 08:40:26 2020 +0000
Revision:
61:c608b27d793d
Parent:
58:81c66fac6476
htzbnx

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