Direct copy of the library, but apparently I made a change?
Fork of SHTx by
sht15.cpp@1:8465801be23f, 2010-11-19 (annotated)
- Committer:
- NegativeBlack
- Date:
- Fri Nov 19 16:24:46 2010 +0000
- Revision:
- 1:8465801be23f
- Parent:
- 0:d55659b0c4a0
Added: control over OTP reload
Added: connection reset.
Fixed: invalid humidity conversion when using the lower resolution
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NegativeBlack | 1:8465801be23f | 1 | /** |
NegativeBlack | 1:8465801be23f | 2 | * Copyright (c) 2010 Roy van Dam <roy@negative-black.org> |
NegativeBlack | 1:8465801be23f | 3 | * All rights reserved. |
NegativeBlack | 1:8465801be23f | 4 | * |
NegativeBlack | 1:8465801be23f | 5 | * Redistribution and use in source and binary forms, with or without |
NegativeBlack | 1:8465801be23f | 6 | * modification, are permitted provided that the following conditions |
NegativeBlack | 1:8465801be23f | 7 | * are met: |
NegativeBlack | 1:8465801be23f | 8 | * 1. Redistributions of source code must retain the above copyright |
NegativeBlack | 1:8465801be23f | 9 | * notice, this list of conditions and the following disclaimer. |
NegativeBlack | 1:8465801be23f | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
NegativeBlack | 1:8465801be23f | 11 | * notice, this list of conditions and the following disclaimer in the |
NegativeBlack | 1:8465801be23f | 12 | * documentation and/or other materials provided with the distribution. |
NegativeBlack | 1:8465801be23f | 13 | * |
NegativeBlack | 1:8465801be23f | 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
NegativeBlack | 1:8465801be23f | 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
NegativeBlack | 1:8465801be23f | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
NegativeBlack | 1:8465801be23f | 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
NegativeBlack | 1:8465801be23f | 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
NegativeBlack | 1:8465801be23f | 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
NegativeBlack | 1:8465801be23f | 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
NegativeBlack | 1:8465801be23f | 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
NegativeBlack | 1:8465801be23f | 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
NegativeBlack | 1:8465801be23f | 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
NegativeBlack | 1:8465801be23f | 24 | * SUCH DAMAGE. |
NegativeBlack | 1:8465801be23f | 25 | */ |
NegativeBlack | 1:8465801be23f | 26 | |
NegativeBlack | 1:8465801be23f | 27 | #include "sht15.hpp" |
NegativeBlack | 1:8465801be23f | 28 | |
NegativeBlack | 1:8465801be23f | 29 | namespace SHTx { |
NegativeBlack | 1:8465801be23f | 30 | SHT15::SHT15(PinName sda, PinName scl): i2c(sda, scl) { |
NegativeBlack | 1:8465801be23f | 31 | this->ready = true; |
NegativeBlack | 1:8465801be23f | 32 | wait_ms(11); |
NegativeBlack | 1:8465801be23f | 33 | } |
NegativeBlack | 1:8465801be23f | 34 | |
NegativeBlack | 1:8465801be23f | 35 | float |
NegativeBlack | 1:8465801be23f | 36 | SHT15::getTemperature(void) { |
NegativeBlack | 1:8465801be23f | 37 | return this->convertTemperature( |
NegativeBlack | 1:8465801be23f | 38 | this->temperature, |
NegativeBlack | 1:8465801be23f | 39 | this->getFlag(flag_resolution), |
NegativeBlack | 1:8465801be23f | 40 | this->scale |
NegativeBlack | 1:8465801be23f | 41 | ); |
NegativeBlack | 1:8465801be23f | 42 | } |
NegativeBlack | 1:8465801be23f | 43 | |
NegativeBlack | 1:8465801be23f | 44 | float |
NegativeBlack | 1:8465801be23f | 45 | SHT15::getHumidity(void) { |
NegativeBlack | 1:8465801be23f | 46 | return this->convertHumidity( |
NegativeBlack | 1:8465801be23f | 47 | this->humidity, |
NegativeBlack | 1:8465801be23f | 48 | this->temperature, |
NegativeBlack | 1:8465801be23f | 49 | this->getFlag(flag_resolution) |
NegativeBlack | 1:8465801be23f | 50 | ); |
NegativeBlack | 1:8465801be23f | 51 | } |
NegativeBlack | 1:8465801be23f | 52 | |
NegativeBlack | 1:8465801be23f | 53 | bool |
NegativeBlack | 1:8465801be23f | 54 | SHT15::checkBattery(void) { |
NegativeBlack | 1:8465801be23f | 55 | this->readRegister(cmd_read_register); |
NegativeBlack | 1:8465801be23f | 56 | return this->getFlag(flag_battery); |
NegativeBlack | 1:8465801be23f | 57 | } |
NegativeBlack | 1:8465801be23f | 58 | |
NegativeBlack | 1:8465801be23f | 59 | bool |
NegativeBlack | 1:8465801be23f | 60 | SHT15::setHeater(bool value) { |
NegativeBlack | 1:8465801be23f | 61 | this->setFlag(flag_heater, value); |
NegativeBlack | 1:8465801be23f | 62 | return this->writeRegister(); |
NegativeBlack | 1:8465801be23f | 63 | } |
NegativeBlack | 1:8465801be23f | 64 | |
NegativeBlack | 1:8465801be23f | 65 | bool |
NegativeBlack | 1:8465801be23f | 66 | SHT15::setResolution(bool value) { |
NegativeBlack | 1:8465801be23f | 67 | this->setFlag(flag_resolution, value); |
NegativeBlack | 1:8465801be23f | 68 | return this->writeRegister(); |
NegativeBlack | 1:8465801be23f | 69 | } |
NegativeBlack | 1:8465801be23f | 70 | |
NegativeBlack | 1:8465801be23f | 71 | bool |
NegativeBlack | 1:8465801be23f | 72 | SHT15::setOTPReload(bool value) { |
NegativeBlack | 1:8465801be23f | 73 | this->setFlag(flag_otp_reload, !value); |
NegativeBlack | 1:8465801be23f | 74 | return this->writeRegister(); |
NegativeBlack | 1:8465801be23f | 75 | } |
NegativeBlack | 1:8465801be23f | 76 | |
NegativeBlack | 1:8465801be23f | 77 | void |
NegativeBlack | 1:8465801be23f | 78 | SHT15::setScale(bool value) { |
NegativeBlack | 1:8465801be23f | 79 | this->scale = value; |
NegativeBlack | 1:8465801be23f | 80 | } |
NegativeBlack | 1:8465801be23f | 81 | |
NegativeBlack | 1:8465801be23f | 82 | bool |
NegativeBlack | 1:8465801be23f | 83 | SHT15::update(void) { |
NegativeBlack | 1:8465801be23f | 84 | if ((this->ready == false) || |
NegativeBlack | 1:8465801be23f | 85 | !this->readRegister(cmd_read_temperature) || |
NegativeBlack | 1:8465801be23f | 86 | !this->readRegister(cmd_read_humidity)) { |
NegativeBlack | 1:8465801be23f | 87 | return false; |
NegativeBlack | 1:8465801be23f | 88 | } |
NegativeBlack | 1:8465801be23f | 89 | |
NegativeBlack | 1:8465801be23f | 90 | return true; |
NegativeBlack | 1:8465801be23f | 91 | } |
NegativeBlack | 1:8465801be23f | 92 | |
NegativeBlack | 1:8465801be23f | 93 | bool |
NegativeBlack | 1:8465801be23f | 94 | SHT15::reset(void) { |
NegativeBlack | 1:8465801be23f | 95 | while (this->ready == false) { |
NegativeBlack | 1:8465801be23f | 96 | continue; |
NegativeBlack | 1:8465801be23f | 97 | } |
NegativeBlack | 1:8465801be23f | 98 | |
NegativeBlack | 1:8465801be23f | 99 | this->ready = false; |
NegativeBlack | 1:8465801be23f | 100 | this->i2c.start(); |
NegativeBlack | 1:8465801be23f | 101 | bool ack = this->i2c.write(cmd_reset_device); |
NegativeBlack | 1:8465801be23f | 102 | this->i2c.stop(); |
NegativeBlack | 1:8465801be23f | 103 | this->ready = true; |
NegativeBlack | 1:8465801be23f | 104 | |
NegativeBlack | 1:8465801be23f | 105 | if (ack) { |
NegativeBlack | 1:8465801be23f | 106 | this->status_register = 0; |
NegativeBlack | 1:8465801be23f | 107 | this->humidity = 0; |
NegativeBlack | 1:8465801be23f | 108 | this->temperature = 0; |
NegativeBlack | 1:8465801be23f | 109 | wait_ms(11); |
NegativeBlack | 1:8465801be23f | 110 | return true; |
NegativeBlack | 1:8465801be23f | 111 | } |
NegativeBlack | 1:8465801be23f | 112 | |
NegativeBlack | 1:8465801be23f | 113 | return false; |
NegativeBlack | 1:8465801be23f | 114 | } |
NegativeBlack | 1:8465801be23f | 115 | |
NegativeBlack | 1:8465801be23f | 116 | void |
NegativeBlack | 1:8465801be23f | 117 | SHT15::connectionReset(void) { |
NegativeBlack | 1:8465801be23f | 118 | this->i2c.reset(); |
NegativeBlack | 1:8465801be23f | 119 | } |
NegativeBlack | 1:8465801be23f | 120 | |
NegativeBlack | 1:8465801be23f | 121 | float |
NegativeBlack | 1:8465801be23f | 122 | SHT15::convertTemperature(uint16_t sot, bool res, bool scale) { |
NegativeBlack | 1:8465801be23f | 123 | // Temperature conversion coefficients |
NegativeBlack | 1:8465801be23f | 124 | float d1 = this->coefficient.dv[scale]; |
NegativeBlack | 1:8465801be23f | 125 | float d2 = ((scale) ? this->coefficient.df[res] |
NegativeBlack | 1:8465801be23f | 126 | : this->coefficient.dc[res]); |
NegativeBlack | 1:8465801be23f | 127 | |
NegativeBlack | 1:8465801be23f | 128 | // Temperature data conversion |
NegativeBlack | 1:8465801be23f | 129 | return d1 + (d2 * (float)(sot)); |
NegativeBlack | 1:8465801be23f | 130 | } |
NegativeBlack | 1:8465801be23f | 131 | |
NegativeBlack | 1:8465801be23f | 132 | float |
NegativeBlack | 1:8465801be23f | 133 | SHT15::convertHumidity(uint16_t sohr, uint16_t sot, bool res) { |
NegativeBlack | 1:8465801be23f | 134 | // Humidity conversion coefficients |
NegativeBlack | 1:8465801be23f | 135 | float c1 = this->coefficient.c1[res]; |
NegativeBlack | 1:8465801be23f | 136 | float c2 = this->coefficient.c2[res]; |
NegativeBlack | 1:8465801be23f | 137 | float c3 = this->coefficient.c3[res]; |
NegativeBlack | 1:8465801be23f | 138 | |
NegativeBlack | 1:8465801be23f | 139 | // Temperature compensation coefficients |
NegativeBlack | 1:8465801be23f | 140 | float t1 = this->coefficient.t1[res]; |
NegativeBlack | 1:8465801be23f | 141 | float t2 = this->coefficient.t2[res]; |
NegativeBlack | 1:8465801be23f | 142 | |
NegativeBlack | 1:8465801be23f | 143 | // Temperature data conversion to celcius |
NegativeBlack | 1:8465801be23f | 144 | float temp = this->convertTemperature(sot, res, false); |
NegativeBlack | 1:8465801be23f | 145 | |
NegativeBlack | 1:8465801be23f | 146 | // Humidity data conversion to relative humidity |
NegativeBlack | 1:8465801be23f | 147 | float humid = c1 + (c2 * (float)(sohr)) + (c3 * (float)(sohr * sohr)); |
NegativeBlack | 1:8465801be23f | 148 | |
NegativeBlack | 1:8465801be23f | 149 | // Calculate temperature compensation |
NegativeBlack | 1:8465801be23f | 150 | return (temp - 25) + (t1 + (t2 * (float)(sohr)) + humid); |
NegativeBlack | 1:8465801be23f | 151 | } |
NegativeBlack | 1:8465801be23f | 152 | |
NegativeBlack | 1:8465801be23f | 153 | bool |
NegativeBlack | 1:8465801be23f | 154 | SHT15::writeRegister(void) { |
NegativeBlack | 1:8465801be23f | 155 | while (this->ready == false) { |
NegativeBlack | 1:8465801be23f | 156 | continue; |
NegativeBlack | 1:8465801be23f | 157 | } |
NegativeBlack | 1:8465801be23f | 158 | |
NegativeBlack | 1:8465801be23f | 159 | this->ready = false; |
NegativeBlack | 1:8465801be23f | 160 | this->i2c.start(); |
NegativeBlack | 1:8465801be23f | 161 | |
NegativeBlack | 1:8465801be23f | 162 | if (this->i2c.write(cmd_write_register)) { |
NegativeBlack | 1:8465801be23f | 163 | this->i2c.write(this->status_register); |
NegativeBlack | 1:8465801be23f | 164 | } |
NegativeBlack | 1:8465801be23f | 165 | |
NegativeBlack | 1:8465801be23f | 166 | this->i2c.stop(); |
NegativeBlack | 1:8465801be23f | 167 | this->ready = true; |
NegativeBlack | 1:8465801be23f | 168 | |
NegativeBlack | 1:8465801be23f | 169 | return true; |
NegativeBlack | 1:8465801be23f | 170 | } |
NegativeBlack | 1:8465801be23f | 171 | |
NegativeBlack | 1:8465801be23f | 172 | bool |
NegativeBlack | 1:8465801be23f | 173 | SHT15::readRegister(cmd_list command) { |
NegativeBlack | 1:8465801be23f | 174 | while (this->ready == false) { |
NegativeBlack | 1:8465801be23f | 175 | continue; |
NegativeBlack | 1:8465801be23f | 176 | } |
NegativeBlack | 1:8465801be23f | 177 | |
NegativeBlack | 1:8465801be23f | 178 | this->ready = false; |
NegativeBlack | 1:8465801be23f | 179 | this->i2c.start(); |
NegativeBlack | 1:8465801be23f | 180 | |
NegativeBlack | 1:8465801be23f | 181 | if (!this->i2c.write(command)) { |
NegativeBlack | 1:8465801be23f | 182 | this->i2c.stop(); |
NegativeBlack | 1:8465801be23f | 183 | return false; |
NegativeBlack | 1:8465801be23f | 184 | } |
NegativeBlack | 1:8465801be23f | 185 | |
NegativeBlack | 1:8465801be23f | 186 | switch (command) { |
NegativeBlack | 1:8465801be23f | 187 | case cmd_read_temperature: { |
NegativeBlack | 1:8465801be23f | 188 | if (!this->i2c.wait()) { |
NegativeBlack | 1:8465801be23f | 189 | this->i2c.stop(); |
NegativeBlack | 1:8465801be23f | 190 | return false; |
NegativeBlack | 1:8465801be23f | 191 | } |
NegativeBlack | 1:8465801be23f | 192 | |
NegativeBlack | 1:8465801be23f | 193 | this->temperature = this->i2c.read(1) << 8; |
NegativeBlack | 1:8465801be23f | 194 | this->temperature |= this->i2c.read(0); |
NegativeBlack | 1:8465801be23f | 195 | } break; |
NegativeBlack | 1:8465801be23f | 196 | |
NegativeBlack | 1:8465801be23f | 197 | case cmd_read_humidity: { |
NegativeBlack | 1:8465801be23f | 198 | if (!this->i2c.wait()) { |
NegativeBlack | 1:8465801be23f | 199 | this->i2c.stop(); |
NegativeBlack | 1:8465801be23f | 200 | return false; |
NegativeBlack | 1:8465801be23f | 201 | } |
NegativeBlack | 1:8465801be23f | 202 | |
NegativeBlack | 1:8465801be23f | 203 | this->humidity = this->i2c.read(1) << 8; |
NegativeBlack | 1:8465801be23f | 204 | this->humidity |= this->i2c.read(0); |
NegativeBlack | 1:8465801be23f | 205 | } break; |
NegativeBlack | 1:8465801be23f | 206 | |
NegativeBlack | 1:8465801be23f | 207 | case cmd_read_register: { |
NegativeBlack | 1:8465801be23f | 208 | this->status_register = this->i2c.read(0); |
NegativeBlack | 1:8465801be23f | 209 | } break; |
NegativeBlack | 1:8465801be23f | 210 | } |
NegativeBlack | 1:8465801be23f | 211 | |
NegativeBlack | 1:8465801be23f | 212 | this->i2c.stop(); |
NegativeBlack | 1:8465801be23f | 213 | this->ready = true; |
NegativeBlack | 1:8465801be23f | 214 | |
NegativeBlack | 1:8465801be23f | 215 | return true; |
NegativeBlack | 1:8465801be23f | 216 | } |
NegativeBlack | 1:8465801be23f | 217 | |
NegativeBlack | 1:8465801be23f | 218 | bool |
NegativeBlack | 1:8465801be23f | 219 | SHT15::getFlag(SHT15::flag_list flag) { |
NegativeBlack | 1:8465801be23f | 220 | return (this->status_register & flag) ? true : false; |
NegativeBlack | 1:8465801be23f | 221 | } |
NegativeBlack | 1:8465801be23f | 222 | |
NegativeBlack | 1:8465801be23f | 223 | void |
NegativeBlack | 1:8465801be23f | 224 | SHT15::setFlag(SHT15::flag_list flag, bool value) { |
NegativeBlack | 1:8465801be23f | 225 | if (value) { |
NegativeBlack | 1:8465801be23f | 226 | this->status_register |= flag; |
NegativeBlack | 1:8465801be23f | 227 | } else { |
NegativeBlack | 1:8465801be23f | 228 | this->status_register &= ~flag; |
NegativeBlack | 1:8465801be23f | 229 | } |
NegativeBlack | 1:8465801be23f | 230 | } |
NegativeBlack | 1:8465801be23f | 231 | } |