|
| 1 | +/* |
| 2 | + This file is part of the Arduino_HS300x 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 | +#include "HS300x.h" |
| 22 | + |
| 23 | +#define HS300X_ADR 0x44 |
| 24 | +#define HS300X_TEMP_MULTY 0.010071415 |
| 25 | +#define HS300X_TEMP_MIN 40 |
| 26 | +#define HS300X_HUMD_MULTY 0.006163516 |
| 27 | +#define HS300X_MAX_ITERATION 100 |
| 28 | +#define HS300X_DELAY_MEASUREMENT 35 // typical on datasheet 16.90 ms, rounded up a little (35ms = 1 try) |
| 29 | +#define HS300X_DELAY_ITERATION 1 |
| 30 | + |
| 31 | +#define HS300X_STALE_DATA 2 |
| 32 | +#define HS300X_OK 1 |
| 33 | +#define HS300X_ERROR_SENSOR_BUSY 0 |
| 34 | +#define HS300X_ERROR_COLLISION_I2C -1 |
| 35 | + |
| 36 | +int8_t HS300xClass::_measurementReq(){ |
| 37 | + int8_t _status = 0; |
| 38 | + uint8_t _iteration = 0; |
| 39 | + _wire->beginTransmission(HS300X_ADR); |
| 40 | + _wire->write((uint8_t)0); |
| 41 | + if (_wire->endTransmission(true) != 0) { |
| 42 | + return HS300X_ERROR_COLLISION_I2C; |
| 43 | + } |
| 44 | + delay(HS300X_DELAY_MEASUREMENT); |
| 45 | + do { |
| 46 | + _status = _readSensor(); |
| 47 | + _iteration ++; |
| 48 | + if (_iteration > HS300X_MAX_ITERATION) return HS300X_ERROR_SENSOR_BUSY; |
| 49 | + delay(HS300X_DELAY_ITERATION); |
| 50 | + } while (!_status); |
| 51 | + return _status; |
| 52 | +} |
| 53 | + |
| 54 | +uint8_t HS300xClass::_readSensor(){ |
| 55 | + uint16_t _rawTempMSB; |
| 56 | + uint16_t _rawTemp; |
| 57 | + uint16_t _rawHumMSB; |
| 58 | + uint16_t _rawHum; |
| 59 | + uint8_t _rawStatus; |
| 60 | + |
| 61 | + _wire->requestFrom(HS300X_ADR, 4); //true, stop message after transmission & releas the I2C bus |
| 62 | + if (_wire->available() != 4) { |
| 63 | + return 0; |
| 64 | + } |
| 65 | + _rawHumMSB = _wire->read() << 8; // MSB |
| 66 | + _rawHum = _wire->read(); // LSB |
| 67 | + _rawTempMSB = _wire->read() << 8; |
| 68 | + _rawTemp= _wire->read(); |
| 69 | + |
| 70 | + _rawHum |= _rawHumMSB; |
| 71 | + _rawTemp |= _rawTempMSB; |
| 72 | + |
| 73 | + _rawStatus = _rawTemp >> 14; |
| 74 | + _rawHum = _rawHum & 0x3FFF; // mask 2 bit first |
| 75 | + _rawTemp = _rawTemp >>2; // mask 2 bit last |
| 76 | + if (_rawHum == 0x3FFF) return 0; |
| 77 | + if (_rawTemp == 0x3FFF) return 0; |
| 78 | + _temperature = (_rawTemp* HS300X_TEMP_MULTY) - HS300X_TEMP_MIN; |
| 79 | + _humidity = _rawHum * HS300X_HUMD_MULTY; |
| 80 | + return _rawStatus + 1; |
| 81 | +} |
| 82 | + |
| 83 | +HS300xClass::HS300xClass(TwoWire& wire) : |
| 84 | + _wire(&wire) |
| 85 | +{ |
| 86 | +} |
| 87 | + |
| 88 | +int HS300xClass::begin() |
| 89 | +{ |
| 90 | + _wire->begin(); |
| 91 | + return 1; |
| 92 | +} |
| 93 | + |
| 94 | +void HS300xClass::end() |
| 95 | +{ |
| 96 | + // TODO: add disable HS300x |
| 97 | + |
| 98 | + _wire->end(); |
| 99 | +} |
| 100 | + |
| 101 | +float HS300xClass::readTemperature(int units) |
| 102 | +{ |
| 103 | + if (_measurementReq() <= 0) { |
| 104 | + return NAN; |
| 105 | + } |
| 106 | + |
| 107 | + if (units == FAHRENHEIT) { // Fahrenheit = (Celsius * 9 / 5) + 32 |
| 108 | + return (_temperature * 9.0 / 5.0) + 32.0; |
| 109 | + } else { |
| 110 | + return _temperature; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +float HS300xClass::readHumidity() |
| 115 | +{ |
| 116 | + if (_measurementReq() <= 0) { |
| 117 | + return NAN; |
| 118 | + } |
| 119 | + |
| 120 | + return _humidity; |
| 121 | +} |
| 122 | + |
| 123 | +#ifdef ARDUINO_ARDUINO_NANO33BLE |
| 124 | +HS300xClass HS300x(Wire1); |
| 125 | +#else |
| 126 | +HS300xClass HS300x(Wire); |
| 127 | +#endif |
0 commit comments