Creation

Committer:
UsmanKhan
Date:
Wed Jun 06 16:12:05 2018 +0500
Revision:
2:df51c015ac67
added drivers si7034, on board humidity temperature sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UsmanKhan 2:df51c015ac67 1 /*******************************************************************************
UsmanKhan 2:df51c015ac67 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
UsmanKhan 2:df51c015ac67 3 *
UsmanKhan 2:df51c015ac67 4 * Permission is hereby granted, free of charge, to any person obtaining a
UsmanKhan 2:df51c015ac67 5 * copy of this software and associated documentation files (the "Software"),
UsmanKhan 2:df51c015ac67 6 * to deal in the Software without restriction, including without limitation
UsmanKhan 2:df51c015ac67 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
UsmanKhan 2:df51c015ac67 8 * and/or sell copies of the Software, and to permit persons to whom the
UsmanKhan 2:df51c015ac67 9 * Software is furnished to do so, subject to the following conditions:
UsmanKhan 2:df51c015ac67 10 *
UsmanKhan 2:df51c015ac67 11 * The above copyright notice and this permission notice shall be included
UsmanKhan 2:df51c015ac67 12 * in all copies or substantial portions of the Software.
UsmanKhan 2:df51c015ac67 13 *
UsmanKhan 2:df51c015ac67 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
UsmanKhan 2:df51c015ac67 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
UsmanKhan 2:df51c015ac67 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
UsmanKhan 2:df51c015ac67 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
UsmanKhan 2:df51c015ac67 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
UsmanKhan 2:df51c015ac67 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
UsmanKhan 2:df51c015ac67 20 * OTHER DEALINGS IN THE SOFTWARE.
UsmanKhan 2:df51c015ac67 21 *
UsmanKhan 2:df51c015ac67 22 * Except as contained in this notice, the name of Maxim Integrated
UsmanKhan 2:df51c015ac67 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
UsmanKhan 2:df51c015ac67 24 * Products, Inc. Branding Policy.
UsmanKhan 2:df51c015ac67 25 *
UsmanKhan 2:df51c015ac67 26 * The mere transfer of this software does not imply any licenses
UsmanKhan 2:df51c015ac67 27 * of trade secrets, proprietary technology, copyrights, patents,
UsmanKhan 2:df51c015ac67 28 * trademarks, maskwork rights, or any other form of intellectual
UsmanKhan 2:df51c015ac67 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
UsmanKhan 2:df51c015ac67 30 * ownership rights.
UsmanKhan 2:df51c015ac67 31 *******************************************************************************
UsmanKhan 2:df51c015ac67 32 */
UsmanKhan 2:df51c015ac67 33
UsmanKhan 2:df51c015ac67 34 #include "Si7034.h"
UsmanKhan 2:df51c015ac67 35
UsmanKhan 2:df51c015ac67 36 /***** Definitions *****/
UsmanKhan 2:df51c015ac67 37 #define I2C_ADDR (0xE0) // 1000000x previously 0x80
UsmanKhan 2:df51c015ac67 38 #define POLYVAL (0x131)
UsmanKhan 2:df51c015ac67 39
UsmanKhan 2:df51c015ac67 40 /***** File Scope Data *****/
UsmanKhan 2:df51c015ac67 41 static const char cmd_meas_humid[] = { 0xA2 }; //works
UsmanKhan 2:df51c015ac67 42 static const char cmd_meas_humid_no_hold[] = { 0x66 }; //works
UsmanKhan 2:df51c015ac67 43 static const char cmd_meas_temp[] = { 0x7C }; //works
UsmanKhan 2:df51c015ac67 44 static const char cmd_meas_temp_no_hold[] = { 0x78 }; //works
UsmanKhan 2:df51c015ac67 45 static const char cmd_meas_prev_temp[] = { 0xE0 }; // works but have to start temperature before using it
UsmanKhan 2:df51c015ac67 46 static const char cmd_rst[] = { 0xFE }; //correct
UsmanKhan 2:df51c015ac67 47 static const char cmd_write_user1[] = { 0xE6 }; //heater correct
UsmanKhan 2:df51c015ac67 48 static const char cmd_read_user1[] = { 0xE7 }; //heater correct
UsmanKhan 2:df51c015ac67 49 static const char cmd_id_1[] = { 0xFA, 0x0F }; // correct
UsmanKhan 2:df51c015ac67 50 static const char cmd_id_2[] = { 0xFC, 0xC9 }; // correct
UsmanKhan 2:df51c015ac67 51 static const char cmd_fw_ver[] = { 0x84, 0xF1 }; // correct
UsmanKhan 2:df51c015ac67 52
UsmanKhan 2:df51c015ac67 53 //******************************************************************************
UsmanKhan 2:df51c015ac67 54 Si7034::Si7034(PinName sda, PinName scl)
UsmanKhan 2:df51c015ac67 55 {
UsmanKhan 2:df51c015ac67 56 i2c_ = new I2C(sda, scl);
UsmanKhan 2:df51c015ac67 57 i2c_owner = true;
UsmanKhan 2:df51c015ac67 58
UsmanKhan 2:df51c015ac67 59 // 400KHz, as specified by the datasheet.
UsmanKhan 2:df51c015ac67 60 i2c_->frequency(400000);
UsmanKhan 2:df51c015ac67 61 }
UsmanKhan 2:df51c015ac67 62
UsmanKhan 2:df51c015ac67 63 //******************************************************************************
UsmanKhan 2:df51c015ac67 64 Si7034::Si7034(I2C *i2c) :
UsmanKhan 2:df51c015ac67 65 i2c_(i2c)
UsmanKhan 2:df51c015ac67 66 {
UsmanKhan 2:df51c015ac67 67 i2c_owner = false;
UsmanKhan 2:df51c015ac67 68 }
UsmanKhan 2:df51c015ac67 69
UsmanKhan 2:df51c015ac67 70 //******************************************************************************
UsmanKhan 2:df51c015ac67 71 Si7034::~Si7034()
UsmanKhan 2:df51c015ac67 72 {
UsmanKhan 2:df51c015ac67 73 if(i2c_owner) {
UsmanKhan 2:df51c015ac67 74 delete i2c_;
UsmanKhan 2:df51c015ac67 75 }
UsmanKhan 2:df51c015ac67 76 }
UsmanKhan 2:df51c015ac67 77
UsmanKhan 2:df51c015ac67 78 //******************************************************************************
UsmanKhan 2:df51c015ac67 79 int Si7034::reset(void)
UsmanKhan 2:df51c015ac67 80 {
UsmanKhan 2:df51c015ac67 81 if (i2c_->write(I2C_ADDR, cmd_rst, sizeof(cmd_rst)) != 0) {
UsmanKhan 2:df51c015ac67 82 return -1;
UsmanKhan 2:df51c015ac67 83 }
UsmanKhan 2:df51c015ac67 84
UsmanKhan 2:df51c015ac67 85 return 0;
UsmanKhan 2:df51c015ac67 86 }
UsmanKhan 2:df51c015ac67 87
UsmanKhan 2:df51c015ac67 88 //******************************************************************************
UsmanKhan 2:df51c015ac67 89 int Si7034::getElectronicId(char* id)
UsmanKhan 2:df51c015ac67 90 {
UsmanKhan 2:df51c015ac67 91 // Send cmd with repeated start
UsmanKhan 2:df51c015ac67 92 if (i2c_->write(I2C_ADDR, cmd_id_1, sizeof(cmd_id_1), true) != 0) {
UsmanKhan 2:df51c015ac67 93 return -1;
UsmanKhan 2:df51c015ac67 94 }
UsmanKhan 2:df51c015ac67 95
UsmanKhan 2:df51c015ac67 96 // Read first portion of ID
UsmanKhan 2:df51c015ac67 97 char temp[8];
UsmanKhan 2:df51c015ac67 98 if (i2c_->read(I2C_ADDR, temp, 8, false) != 0) {
UsmanKhan 2:df51c015ac67 99 return -1;
UsmanKhan 2:df51c015ac67 100 }
UsmanKhan 2:df51c015ac67 101
UsmanKhan 2:df51c015ac67 102 // Check the CRC
UsmanKhan 2:df51c015ac67 103 char crc = 0;
UsmanKhan 2:df51c015ac67 104 int i;
UsmanKhan 2:df51c015ac67 105 for(i = 0; i < 4; i++) {
UsmanKhan 2:df51c015ac67 106 crc = crc8(temp[2*i], crc);
UsmanKhan 2:df51c015ac67 107 if(crc != temp[2*i +1]) {
UsmanKhan 2:df51c015ac67 108 return -1;
UsmanKhan 2:df51c015ac67 109 }
UsmanKhan 2:df51c015ac67 110 }
UsmanKhan 2:df51c015ac67 111
UsmanKhan 2:df51c015ac67 112 // Save top portion of ID
UsmanKhan 2:df51c015ac67 113 id[7] = temp [0];
UsmanKhan 2:df51c015ac67 114 id[6] = temp [2];
UsmanKhan 2:df51c015ac67 115 id[5] = temp [4];
UsmanKhan 2:df51c015ac67 116 id[4] = temp [6];
UsmanKhan 2:df51c015ac67 117
UsmanKhan 2:df51c015ac67 118 // Send cmd with repeated start
UsmanKhan 2:df51c015ac67 119 if (i2c_->write(I2C_ADDR, cmd_id_2, sizeof(cmd_id_2), true) != 0) {
UsmanKhan 2:df51c015ac67 120 return -1;
UsmanKhan 2:df51c015ac67 121 }
UsmanKhan 2:df51c015ac67 122
UsmanKhan 2:df51c015ac67 123 // Read rest of ID
UsmanKhan 2:df51c015ac67 124 if (i2c_->read(I2C_ADDR, temp, 6, false) != 0) {
UsmanKhan 2:df51c015ac67 125 return -1;
UsmanKhan 2:df51c015ac67 126 }
UsmanKhan 2:df51c015ac67 127
UsmanKhan 2:df51c015ac67 128 // Check the CRC
UsmanKhan 2:df51c015ac67 129 crc = 0;
UsmanKhan 2:df51c015ac67 130 for(i = 0; i < 2; i++) {
UsmanKhan 2:df51c015ac67 131 crc = crc8(temp[3*i], crc);
UsmanKhan 2:df51c015ac67 132 crc = crc8(temp[3*i + 1], crc);
UsmanKhan 2:df51c015ac67 133 if(crc != temp[3*i + 2]) {
UsmanKhan 2:df51c015ac67 134 return -1;
UsmanKhan 2:df51c015ac67 135 }
UsmanKhan 2:df51c015ac67 136 }
UsmanKhan 2:df51c015ac67 137
UsmanKhan 2:df51c015ac67 138 // Save bottom portion of ID
UsmanKhan 2:df51c015ac67 139 id[3] = temp [0];
UsmanKhan 2:df51c015ac67 140 id[2] = temp [1];
UsmanKhan 2:df51c015ac67 141 id[1] = temp [3];
UsmanKhan 2:df51c015ac67 142 id[0] = temp [4];
UsmanKhan 2:df51c015ac67 143
UsmanKhan 2:df51c015ac67 144 return 0;
UsmanKhan 2:df51c015ac67 145 }
UsmanKhan 2:df51c015ac67 146
UsmanKhan 2:df51c015ac67 147 //******************************************************************************
UsmanKhan 2:df51c015ac67 148 int Si7034::configResolution(Si7034::resolution_t resolution)
UsmanKhan 2:df51c015ac67 149 {
UsmanKhan 2:df51c015ac67 150 char data[2];
UsmanKhan 2:df51c015ac67 151
UsmanKhan 2:df51c015ac67 152 if (i2c_->write(I2C_ADDR, cmd_read_user1, sizeof(cmd_read_user1)) != 0) {
UsmanKhan 2:df51c015ac67 153 return -1;
UsmanKhan 2:df51c015ac67 154 }
UsmanKhan 2:df51c015ac67 155
UsmanKhan 2:df51c015ac67 156 if (i2c_->read(I2C_ADDR, &data[1], 1) != 0) {
UsmanKhan 2:df51c015ac67 157 return -1;
UsmanKhan 2:df51c015ac67 158 }
UsmanKhan 2:df51c015ac67 159
UsmanKhan 2:df51c015ac67 160 switch (resolution) {
UsmanKhan 2:df51c015ac67 161 case RH_12b_TEMP_14b:
UsmanKhan 2:df51c015ac67 162 data[1] &= ~0x81;
UsmanKhan 2:df51c015ac67 163 break;
UsmanKhan 2:df51c015ac67 164 case RH_8b_TEMP_12b:
UsmanKhan 2:df51c015ac67 165 data[1] = (data[1] & ~0x80) | 0x01;
UsmanKhan 2:df51c015ac67 166 break;
UsmanKhan 2:df51c015ac67 167 case RH_10b_TEMP_13b:
UsmanKhan 2:df51c015ac67 168 data[1] = (data[1] & ~0x01) | 0x80;
UsmanKhan 2:df51c015ac67 169 break;
UsmanKhan 2:df51c015ac67 170 case RH_11b_TEMP_11b:
UsmanKhan 2:df51c015ac67 171 data[1] |= 0x81;
UsmanKhan 2:df51c015ac67 172 break;
UsmanKhan 2:df51c015ac67 173 default:
UsmanKhan 2:df51c015ac67 174 return -1;
UsmanKhan 2:df51c015ac67 175 }
UsmanKhan 2:df51c015ac67 176
UsmanKhan 2:df51c015ac67 177 data[0] = cmd_write_user1[0];
UsmanKhan 2:df51c015ac67 178
UsmanKhan 2:df51c015ac67 179 if (i2c_->write(I2C_ADDR, data, 2) != 0) {
UsmanKhan 2:df51c015ac67 180 return -1;
UsmanKhan 2:df51c015ac67 181 }
UsmanKhan 2:df51c015ac67 182
UsmanKhan 2:df51c015ac67 183 return 0;
UsmanKhan 2:df51c015ac67 184 }
UsmanKhan 2:df51c015ac67 185
UsmanKhan 2:df51c015ac67 186 //******************************************************************************
UsmanKhan 2:df51c015ac67 187 int Si7034::getTemperature(int16_t *tempCx10)
UsmanKhan 2:df51c015ac67 188 {
UsmanKhan 2:df51c015ac67 189 if (i2c_->write(I2C_ADDR, cmd_meas_temp, sizeof(cmd_meas_temp)) != 0) {
UsmanKhan 2:df51c015ac67 190 return -1;
UsmanKhan 2:df51c015ac67 191 }
UsmanKhan 2:df51c015ac67 192
UsmanKhan 2:df51c015ac67 193 return checkTemperature(tempCx10);
UsmanKhan 2:df51c015ac67 194 }
UsmanKhan 2:df51c015ac67 195
UsmanKhan 2:df51c015ac67 196 //******************************************************************************
UsmanKhan 2:df51c015ac67 197 int Si7034::getTemperature(float *tempC)
UsmanKhan 2:df51c015ac67 198 {
UsmanKhan 2:df51c015ac67 199 if (i2c_->write(I2C_ADDR, cmd_meas_temp, sizeof(cmd_meas_temp)) != 0) {
UsmanKhan 2:df51c015ac67 200 return -1;
UsmanKhan 2:df51c015ac67 201 }
UsmanKhan 2:df51c015ac67 202
UsmanKhan 2:df51c015ac67 203 return checkTemperature(tempC);
UsmanKhan 2:df51c015ac67 204 }
UsmanKhan 2:df51c015ac67 205
UsmanKhan 2:df51c015ac67 206 //******************************************************************************
UsmanKhan 2:df51c015ac67 207 int Si7034::startTemperature(void)
UsmanKhan 2:df51c015ac67 208 {
UsmanKhan 2:df51c015ac67 209 if (i2c_->write(I2C_ADDR, cmd_meas_temp_no_hold, sizeof(cmd_meas_temp_no_hold)) != 0) {
UsmanKhan 2:df51c015ac67 210 return -1;
UsmanKhan 2:df51c015ac67 211 }
UsmanKhan 2:df51c015ac67 212
UsmanKhan 2:df51c015ac67 213 return 0;
UsmanKhan 2:df51c015ac67 214 }
UsmanKhan 2:df51c015ac67 215
UsmanKhan 2:df51c015ac67 216 //******************************************************************************
UsmanKhan 2:df51c015ac67 217 int Si7034::checkTemperature(int16_t *tempCx10)
UsmanKhan 2:df51c015ac67 218 {
UsmanKhan 2:df51c015ac67 219 char data[3];
UsmanKhan 2:df51c015ac67 220 uint16_t code;
UsmanKhan 2:df51c015ac67 221 int temp;
UsmanKhan 2:df51c015ac67 222
UsmanKhan 2:df51c015ac67 223 if (i2c_->read(I2C_ADDR, data, 3) != 0) {
UsmanKhan 2:df51c015ac67 224 return -1;
UsmanKhan 2:df51c015ac67 225 }
UsmanKhan 2:df51c015ac67 226
UsmanKhan 2:df51c015ac67 227 // Get 16-bit value from bytes read
UsmanKhan 2:df51c015ac67 228 code = ((uint16_t)data[0] << 8) + data[1];
UsmanKhan 2:df51c015ac67 229
UsmanKhan 2:df51c015ac67 230 // Calculate the temperature using the formula from the datasheet
UsmanKhan 2:df51c015ac67 231 // Scaled by 100
UsmanKhan 2:df51c015ac67 232 temp = ((((int)17500 * code) + 0x8000) >> 16) - 4500;
UsmanKhan 2:df51c015ac67 233
UsmanKhan 2:df51c015ac67 234 // Return value is to be scaled by 10
UsmanKhan 2:df51c015ac67 235 *tempCx10 = (temp + 5) / 10;
UsmanKhan 2:df51c015ac67 236
UsmanKhan 2:df51c015ac67 237 return 0;
UsmanKhan 2:df51c015ac67 238 }
UsmanKhan 2:df51c015ac67 239
UsmanKhan 2:df51c015ac67 240 //******************************************************************************
UsmanKhan 2:df51c015ac67 241 int Si7034::checkTemperature(float *tempC)
UsmanKhan 2:df51c015ac67 242 {
UsmanKhan 2:df51c015ac67 243 char data[3];
UsmanKhan 2:df51c015ac67 244 uint16_t code;
UsmanKhan 2:df51c015ac67 245
UsmanKhan 2:df51c015ac67 246 if (i2c_->read(I2C_ADDR, data, 3) != 0) {
UsmanKhan 2:df51c015ac67 247 return -1;
UsmanKhan 2:df51c015ac67 248 }
UsmanKhan 2:df51c015ac67 249
UsmanKhan 2:df51c015ac67 250 // Get 16-bit value from bytes read
UsmanKhan 2:df51c015ac67 251 code = ((uint16_t)data[0] << 8) + data[1];
UsmanKhan 2:df51c015ac67 252
UsmanKhan 2:df51c015ac67 253 // Calculate the temperature using the formula from the datasheet
UsmanKhan 2:df51c015ac67 254 // Scaled by 100
UsmanKhan 2:df51c015ac67 255 *tempC = (((float)175 * code) / 65536) - 45;
UsmanKhan 2:df51c015ac67 256
UsmanKhan 2:df51c015ac67 257 return 0;
UsmanKhan 2:df51c015ac67 258 }
UsmanKhan 2:df51c015ac67 259
UsmanKhan 2:df51c015ac67 260 //******************************************************************************
UsmanKhan 2:df51c015ac67 261 int Si7034::getHumidity(int16_t *humidx10)
UsmanKhan 2:df51c015ac67 262 {
UsmanKhan 2:df51c015ac67 263 if (i2c_->write(I2C_ADDR, cmd_meas_humid, sizeof(cmd_meas_humid), true) != 0) {
UsmanKhan 2:df51c015ac67 264 return -1;
UsmanKhan 2:df51c015ac67 265 }
UsmanKhan 2:df51c015ac67 266
UsmanKhan 2:df51c015ac67 267 return checkHumidity(humidx10);
UsmanKhan 2:df51c015ac67 268 }
UsmanKhan 2:df51c015ac67 269
UsmanKhan 2:df51c015ac67 270 //******************************************************************************
UsmanKhan 2:df51c015ac67 271 int Si7034::getHumidity(float *humid)
UsmanKhan 2:df51c015ac67 272 {
UsmanKhan 2:df51c015ac67 273 if (i2c_->write(I2C_ADDR, cmd_meas_humid, sizeof(cmd_meas_humid)) != 0) {
UsmanKhan 2:df51c015ac67 274 return -1;
UsmanKhan 2:df51c015ac67 275 }
UsmanKhan 2:df51c015ac67 276
UsmanKhan 2:df51c015ac67 277 return checkHumidity(humid);
UsmanKhan 2:df51c015ac67 278 }
UsmanKhan 2:df51c015ac67 279
UsmanKhan 2:df51c015ac67 280 //******************************************************************************
UsmanKhan 2:df51c015ac67 281 int Si7034::startHumidity(void)
UsmanKhan 2:df51c015ac67 282 {
UsmanKhan 2:df51c015ac67 283 if (i2c_->write(I2C_ADDR, cmd_meas_humid_no_hold, sizeof(cmd_meas_humid_no_hold)) != 0) {
UsmanKhan 2:df51c015ac67 284 return -1;
UsmanKhan 2:df51c015ac67 285 }
UsmanKhan 2:df51c015ac67 286
UsmanKhan 2:df51c015ac67 287 return 0;
UsmanKhan 2:df51c015ac67 288 }
UsmanKhan 2:df51c015ac67 289
UsmanKhan 2:df51c015ac67 290 //******************************************************************************
UsmanKhan 2:df51c015ac67 291 int Si7034::checkHumidity(int16_t *humidx10)
UsmanKhan 2:df51c015ac67 292 {
UsmanKhan 2:df51c015ac67 293 char data[3];
UsmanKhan 2:df51c015ac67 294 uint16_t code;
UsmanKhan 2:df51c015ac67 295
UsmanKhan 2:df51c015ac67 296 if (i2c_->read(I2C_ADDR, data, 3) != 0) {
UsmanKhan 2:df51c015ac67 297 return -1;
UsmanKhan 2:df51c015ac67 298 }
UsmanKhan 2:df51c015ac67 299
UsmanKhan 2:df51c015ac67 300 // Get 16-bit value from bytes read
UsmanKhan 2:df51c015ac67 301 code = ((uint16_t)data[0] << 8) + data[1];
UsmanKhan 2:df51c015ac67 302
UsmanKhan 2:df51c015ac67 303 // Calculate the humidity using the formula from the datasheet
UsmanKhan 2:df51c015ac67 304 // Scaled by 10
UsmanKhan 2:df51c015ac67 305 *humidx10 = ((((int)1000 * code) + 0x8000) >> 16) ;
UsmanKhan 2:df51c015ac67 306
UsmanKhan 2:df51c015ac67 307 // Check the crc
UsmanKhan 2:df51c015ac67 308 char crc = crc8(data[0], 0x00);
UsmanKhan 2:df51c015ac67 309 crc = crc8(data[1], crc);
UsmanKhan 2:df51c015ac67 310 if(crc != data[2]) {
UsmanKhan 2:df51c015ac67 311 return -1;
UsmanKhan 2:df51c015ac67 312 }
UsmanKhan 2:df51c015ac67 313
UsmanKhan 2:df51c015ac67 314 return 0;
UsmanKhan 2:df51c015ac67 315 }
UsmanKhan 2:df51c015ac67 316
UsmanKhan 2:df51c015ac67 317 //******************************************************************************
UsmanKhan 2:df51c015ac67 318 int Si7034::checkHumidity(float *humid)
UsmanKhan 2:df51c015ac67 319 {
UsmanKhan 2:df51c015ac67 320 char data[3];
UsmanKhan 2:df51c015ac67 321 uint16_t code;
UsmanKhan 2:df51c015ac67 322
UsmanKhan 2:df51c015ac67 323 if (i2c_->read(I2C_ADDR, data, 3) != 0) {
UsmanKhan 2:df51c015ac67 324 return -1;
UsmanKhan 2:df51c015ac67 325 }
UsmanKhan 2:df51c015ac67 326
UsmanKhan 2:df51c015ac67 327 // Get 16-bit value from bytes read
UsmanKhan 2:df51c015ac67 328 code = ((uint16_t)data[0] << 8) + data[1];
UsmanKhan 2:df51c015ac67 329
UsmanKhan 2:df51c015ac67 330 // Calculate the humidity using the formula from the datasheet
UsmanKhan 2:df51c015ac67 331 *humid = (((float)100 * code) / 65536);
UsmanKhan 2:df51c015ac67 332
UsmanKhan 2:df51c015ac67 333 return 0;
UsmanKhan 2:df51c015ac67 334 }
UsmanKhan 2:df51c015ac67 335
UsmanKhan 2:df51c015ac67 336 //******************************************************************************
UsmanKhan 2:df51c015ac67 337 int Si7034::getPrevTemperature(float *tempC) {
UsmanKhan 2:df51c015ac67 338
UsmanKhan 2:df51c015ac67 339 if (i2c_->write(I2C_ADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) {
UsmanKhan 2:df51c015ac67 340 return -1;
UsmanKhan 2:df51c015ac67 341 }
UsmanKhan 2:df51c015ac67 342
UsmanKhan 2:df51c015ac67 343 if(checkTemperature(tempC) != 0) {
UsmanKhan 2:df51c015ac67 344 return -1;
UsmanKhan 2:df51c015ac67 345 }
UsmanKhan 2:df51c015ac67 346
UsmanKhan 2:df51c015ac67 347 return 0;
UsmanKhan 2:df51c015ac67 348 }
UsmanKhan 2:df51c015ac67 349
UsmanKhan 2:df51c015ac67 350 //******************************************************************************
UsmanKhan 2:df51c015ac67 351 int Si7034::getPrevTemperature(int16_t *tempCx10) {
UsmanKhan 2:df51c015ac67 352
UsmanKhan 2:df51c015ac67 353 if (i2c_->write(I2C_ADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) {
UsmanKhan 2:df51c015ac67 354 return -1;
UsmanKhan 2:df51c015ac67 355 }
UsmanKhan 2:df51c015ac67 356
UsmanKhan 2:df51c015ac67 357 if(checkTemperature(tempCx10) != 0) {
UsmanKhan 2:df51c015ac67 358 return -1;
UsmanKhan 2:df51c015ac67 359 }
UsmanKhan 2:df51c015ac67 360
UsmanKhan 2:df51c015ac67 361 return 0;
UsmanKhan 2:df51c015ac67 362 }
UsmanKhan 2:df51c015ac67 363
UsmanKhan 2:df51c015ac67 364 //******************************************************************************
UsmanKhan 2:df51c015ac67 365 int Si7034::getRev(char *rev)
UsmanKhan 2:df51c015ac67 366 {
UsmanKhan 2:df51c015ac67 367 if (i2c_->write(I2C_ADDR, cmd_fw_ver, sizeof(cmd_fw_ver)) != 0) {
UsmanKhan 2:df51c015ac67 368 return -1;
UsmanKhan 2:df51c015ac67 369 }
UsmanKhan 2:df51c015ac67 370
UsmanKhan 2:df51c015ac67 371 if (i2c_->read(I2C_ADDR, rev, 1) != 0) {
UsmanKhan 2:df51c015ac67 372 return -1;
UsmanKhan 2:df51c015ac67 373 }
UsmanKhan 2:df51c015ac67 374
UsmanKhan 2:df51c015ac67 375 return 0;
UsmanKhan 2:df51c015ac67 376 }
UsmanKhan 2:df51c015ac67 377
UsmanKhan 2:df51c015ac67 378 //******************************************************************************
UsmanKhan 2:df51c015ac67 379 int Si7034::heater(bool enable)
UsmanKhan 2:df51c015ac67 380 {
UsmanKhan 2:df51c015ac67 381 char data[2];
UsmanKhan 2:df51c015ac67 382
UsmanKhan 2:df51c015ac67 383 if (i2c_->write(I2C_ADDR, cmd_read_user1, sizeof(cmd_read_user1)) != 0) {
UsmanKhan 2:df51c015ac67 384 return -1;
UsmanKhan 2:df51c015ac67 385 }
UsmanKhan 2:df51c015ac67 386
UsmanKhan 2:df51c015ac67 387 if (i2c_->read(I2C_ADDR, &data[1], 1) != 0) {
UsmanKhan 2:df51c015ac67 388 return -1;
UsmanKhan 2:df51c015ac67 389 }
UsmanKhan 2:df51c015ac67 390
UsmanKhan 2:df51c015ac67 391 if (enable) {
UsmanKhan 2:df51c015ac67 392 data[1] |= 0x04;
UsmanKhan 2:df51c015ac67 393 } else {
UsmanKhan 2:df51c015ac67 394 data[1] &= ~0x04;
UsmanKhan 2:df51c015ac67 395 }
UsmanKhan 2:df51c015ac67 396
UsmanKhan 2:df51c015ac67 397 data[0] = cmd_write_user1[0];
UsmanKhan 2:df51c015ac67 398
UsmanKhan 2:df51c015ac67 399 if (i2c_->write(I2C_ADDR, data, 2) != 0) {
UsmanKhan 2:df51c015ac67 400 return -1;
UsmanKhan 2:df51c015ac67 401 }
UsmanKhan 2:df51c015ac67 402
UsmanKhan 2:df51c015ac67 403 return 0;
UsmanKhan 2:df51c015ac67 404 }
UsmanKhan 2:df51c015ac67 405
UsmanKhan 2:df51c015ac67 406 //******************************************************************************
UsmanKhan 2:df51c015ac67 407 char Si7034::crc8(char value, char seed)
UsmanKhan 2:df51c015ac67 408 {
UsmanKhan 2:df51c015ac67 409 int i;
UsmanKhan 2:df51c015ac67 410
UsmanKhan 2:df51c015ac67 411 for (i = 0; i < 8; i++) {
UsmanKhan 2:df51c015ac67 412
UsmanKhan 2:df51c015ac67 413 if ((value ^ seed) & 0x80) {
UsmanKhan 2:df51c015ac67 414 seed <<= 1;
UsmanKhan 2:df51c015ac67 415 seed ^= POLYVAL;
UsmanKhan 2:df51c015ac67 416 } else {
UsmanKhan 2:df51c015ac67 417 seed <<= 1;
UsmanKhan 2:df51c015ac67 418 }
UsmanKhan 2:df51c015ac67 419
UsmanKhan 2:df51c015ac67 420 value <<= 1;
UsmanKhan 2:df51c015ac67 421 }
UsmanKhan 2:df51c015ac67 422
UsmanKhan 2:df51c015ac67 423 return seed;
UsmanKhan 2:df51c015ac67 424 }