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