|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Johannes Winkelmann <jw@smts.ch> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of the <organization> nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 20 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +*/ |
| 27 | + |
| 28 | +#include <Wire.h> |
| 29 | +#include <Arduino.h> |
| 30 | + |
| 31 | +#include "shtbase.h" |
| 32 | +#include "shti2chelper.h" |
| 33 | + |
| 34 | +ShtBase::ShtBase(uint8_t i2cAddress, |
| 35 | + uint16_t i2cCommand, |
| 36 | + float a, float b, float c, |
| 37 | + float x, float y) |
| 38 | + : mI2CAddress(i2cAddress), |
| 39 | + mI2CCommand(i2cCommand), |
| 40 | + mHumidity(HUMIDITY_INVALID), |
| 41 | + mTemperature(TEMPERATURE_INVALID), |
| 42 | + mA(a), mB(b), mC(c), |
| 43 | + mX(x), mY(y) |
| 44 | +{ |
| 45 | +} |
| 46 | + |
| 47 | +float ShtBase::getHumidity() const |
| 48 | +{ |
| 49 | + return mHumidity; |
| 50 | +} |
| 51 | + |
| 52 | +float ShtBase::getTemperature() const |
| 53 | +{ |
| 54 | + return mTemperature; |
| 55 | +} |
| 56 | + |
| 57 | +bool ShtBase::readSample() |
| 58 | +{ |
| 59 | + uint8_t data[EXPECTED_DATA_SIZE]; |
| 60 | + uint8_t cmd[CMD_SIZE]; |
| 61 | + |
| 62 | + cmd[0] = mI2CCommand >> 8; |
| 63 | + cmd[1] = mI2CCommand & 0x00ff; |
| 64 | + |
| 65 | + if (!ShtI2CHelper::readFromI2C(mI2CAddress, cmd, CMD_SIZE, data, EXPECTED_DATA_SIZE)) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + // Important: this is assuming 2-byte data, 1-byte |
| 70 | + uint16_t val; |
| 71 | + val = (data[0] << 8) + data[1]; |
| 72 | + mTemperature = mA + mB * (val / mC); |
| 73 | + // TODO: verify CRC: data[2] |
| 74 | + |
| 75 | + val = (data[3] << 8) + data[4]; |
| 76 | + mHumidity = mX * (val / mY); |
| 77 | + // TODO: verify CRC: data[5] |
| 78 | + |
| 79 | + return true; |
| 80 | +} |
0 commit comments