|
| 1 | +/* |
| 2 | + This file is part of the Arduino_HTS221 library. |
| 3 | + Copyright (c) 2019 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <Wire.h> |
| 21 | + |
| 22 | +#include "HTS.h" |
| 23 | + |
| 24 | +#define HTS221_ADDRESS 0x5F |
| 25 | + |
| 26 | +#define HTS221_WHO_AM_I_REG 0x0f |
| 27 | +#define HTS221_CTRL1_REG 0x20 |
| 28 | +#define HTS221_CTRL2_REG 0x21 |
| 29 | +#define HTS221_STATUS_REG 0x27 |
| 30 | +#define HTS221_HUMIDITY_OUT_L_REG 0x28 |
| 31 | +#define HTS221_TEMP_OUT_L_REG 0x2a |
| 32 | +#define HTS221_H0_rH_x2_REG 0x30 |
| 33 | +#define HTS221_H1_rH_x2_REG 0x31 |
| 34 | +#define HTS221_T0_degC_x8_REG 0x32 |
| 35 | +#define HTS221_T1_degC_x8_REG 0x33 |
| 36 | +#define HTS221_T1_T0_MSB_REG 0x35 |
| 37 | +#define HTS221_H0_T0_OUT_REG 0x36 |
| 38 | +#define HTS221_H1_T0_OUT_REG 0x3a |
| 39 | +#define HTS221_T0_OUT_REG 0x3c |
| 40 | +#define HTS221_T1_OUT_REG 0x3e |
| 41 | + |
| 42 | +HTS221Class::HTS221Class(TwoWire& wire) : |
| 43 | + _wire(&wire) |
| 44 | +{ |
| 45 | +} |
| 46 | + |
| 47 | +int HTS221Class::begin() |
| 48 | +{ |
| 49 | + _wire->begin(); |
| 50 | + |
| 51 | + if (i2cRead(HTS221_WHO_AM_I_REG) != 0xbc) { |
| 52 | + end(); |
| 53 | + |
| 54 | + return 0; |
| 55 | + } |
| 56 | + |
| 57 | + readHTS221Calibration(); |
| 58 | + |
| 59 | + // enable HTS221 |
| 60 | + i2cWrite(HTS221_CTRL1_REG, 0x80); |
| 61 | + |
| 62 | + return 1; |
| 63 | +} |
| 64 | + |
| 65 | +void HTS221Class::end() |
| 66 | +{ |
| 67 | + // disable HTS221 |
| 68 | + i2cWrite(HTS221_CTRL1_REG, 0x00); |
| 69 | + |
| 70 | + _wire->end(); |
| 71 | +} |
| 72 | + |
| 73 | +float HTS221Class::readTemperature(int units) |
| 74 | +{ |
| 75 | + // trigger one shot |
| 76 | + i2cWrite(HTS221_CTRL2_REG, 0x01); |
| 77 | + |
| 78 | + // wait for completion |
| 79 | + while ((i2cRead(HTS221_STATUS_REG) & 0x01) == 0) { |
| 80 | + yield(); |
| 81 | + } |
| 82 | + |
| 83 | + // read value and convert |
| 84 | + int16_t tout = i2cRead16(HTS221_TEMP_OUT_L_REG); |
| 85 | + float reading = (tout * _hts221TemperatureSlope + _hts221TemperatureZero); |
| 86 | + if (units == FAHRENHEIT) { // Fahrenheit = (Celsius * 9 / 5) + 32 |
| 87 | + return (reading * 9.0 / 5.0) + 32.0; |
| 88 | + } else { |
| 89 | + return reading; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +float HTS221Class::readHumidity() |
| 94 | +{ |
| 95 | + // trigger one shot |
| 96 | + i2cWrite(HTS221_CTRL2_REG, 0x01); |
| 97 | + |
| 98 | + // wait for completion |
| 99 | + while ((i2cRead(HTS221_STATUS_REG) & 0x02) == 0) { |
| 100 | + yield(); |
| 101 | + } |
| 102 | + |
| 103 | + // read value and convert |
| 104 | + int16_t hout = i2cRead16(HTS221_HUMIDITY_OUT_L_REG); |
| 105 | + |
| 106 | + return (hout * _hts221HumiditySlope + _hts221HumidityZero); |
| 107 | +} |
| 108 | + |
| 109 | +int HTS221Class::i2cRead(uint8_t reg) |
| 110 | +{ |
| 111 | + _wire->beginTransmission(HTS221_ADDRESS); |
| 112 | + _wire->write(reg); |
| 113 | + if (_wire->endTransmission(false) != 0) { |
| 114 | + return -1; |
| 115 | + } |
| 116 | + |
| 117 | + if (_wire->requestFrom(HTS221_ADDRESS, 1) != 1) { |
| 118 | + return -1; |
| 119 | + } |
| 120 | + |
| 121 | + return _wire->read(); |
| 122 | +} |
| 123 | + |
| 124 | +int HTS221Class::i2cWrite(uint8_t reg, uint8_t val) |
| 125 | +{ |
| 126 | + _wire->beginTransmission(HTS221_ADDRESS); |
| 127 | + _wire->write(reg); |
| 128 | + _wire->write(val); |
| 129 | + if (_wire->endTransmission() != 0) { |
| 130 | + return 0; |
| 131 | + } |
| 132 | + |
| 133 | + return 1; |
| 134 | +} |
| 135 | + |
| 136 | +void HTS221Class::readHTS221Calibration() |
| 137 | +{ |
| 138 | + uint8_t h0rH = i2cRead(HTS221_H0_rH_x2_REG); |
| 139 | + uint8_t h1rH = i2cRead(HTS221_H1_rH_x2_REG); |
| 140 | + |
| 141 | + uint16_t t0degC = i2cRead(HTS221_T0_degC_x8_REG) | ((i2cRead(HTS221_T1_T0_MSB_REG) & 0x03) << 8); |
| 142 | + uint16_t t1degC = i2cRead(HTS221_T1_degC_x8_REG) | ((i2cRead(HTS221_T1_T0_MSB_REG) & 0x0c) << 6); |
| 143 | + |
| 144 | + int16_t h0t0Out = i2cRead16(HTS221_H0_T0_OUT_REG); |
| 145 | + int16_t h1t0Out = i2cRead16(HTS221_H1_T0_OUT_REG); |
| 146 | + |
| 147 | + int16_t t0Out = i2cRead16(HTS221_T0_OUT_REG); |
| 148 | + int16_t t1Out = i2cRead16(HTS221_T1_OUT_REG); |
| 149 | + |
| 150 | + // calculate slopes and 0 offset from calibration values, |
| 151 | + // for future calculations: value = a * X + b |
| 152 | + |
| 153 | + _hts221HumiditySlope = (h1rH - h0rH) / (2.0 * (h1t0Out - h0t0Out)); |
| 154 | + _hts221HumidityZero = (h0rH / 2.0) - _hts221HumiditySlope * h0t0Out; |
| 155 | + |
| 156 | + _hts221TemperatureSlope = (t1degC - t0degC) / (8.0 * (t1Out - t0Out)); |
| 157 | + _hts221TemperatureZero = (t0degC / 8.0) - _hts221TemperatureSlope * t0Out; |
| 158 | +} |
| 159 | + |
| 160 | +HTS221Class HTS(Wire1); |
0 commit comments