upm  1.0.2
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sht1x.h
Go to the documentation of this file.
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include <stdint.h>
28 #include "upm.h"
29 #include "mraa/gpio.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
46  typedef struct _sht1x_context {
47  mraa_gpio_context gpio_clk;
48  mraa_gpio_context gpio_data;
49 
50  // high res? (temp/hum 14/12b vs. 12/8b)
51  bool hires;
52 
53  // our data
54  float temperature;
55  float humidity;
56 
57  // temperature coeff (we only care about C)
58  float coeff_d1;
59  float coeff_d2;
60 
61  // humidity coeff
62  float coeff_c1;
63  float coeff_c2;
64  float coeff_c3;
65 
66  float coeff_t1;
67  float coeff_t2;
68  } *sht1x_context;
69 
70  // SHT1X commands. The first 3 msb's are the address, which are
71  // always 0. The following 5 bits are the actual command.
72  typedef enum {
73  SHT1X_CMD_MEAS_TEMPERATURE = 0x03,
74  SHT1X_CMD_MEAS_HUMIDITY = 0x05,
75  SHT1X_CMD_WRITE_STATUS = 0x06,
76  SHT1X_CMD_READ_STATUS = 0x07,
77  SHT1X_CMD_SOFT_RESET = 0x1e
78  } SHT1X_CMD_T;
79 
80  // status register bits
81  typedef enum {
82  SHT1X_STATUS_RESOLUTION_LOW = 0x01, // 0=12b RH/14b temp (dflt)
83  SHT1X_STATUS_NO_RELOAD_FROM_OTP = 0x02,
84  SHT1X_STATUS_HEATER_EN = 0x04,
85 
86  // 0x08-0x20 reserved
87 
88  SHT1X_STATUS_LOW_VOLT = 0x40 // low battery
89 
90  // 0x80 reserved
91  } SHT1X_STATUS_BITS_T;
92 
93  // The Vdd voltage can affect the temperature coefficients, so we
94  // provide a way to indicate the closest voltage and set up the
95  // compensation accordingly.
96  typedef enum {
97  SHT1X_VOLTS_5 = 0, // 5 volts
98  SHT1X_VOLTS_4 = 1,
99  SHT1X_VOLTS_3_5 = 2, // 3.5v
100  SHT1X_VOLTS_3 = 3,
101  SHT1X_VOLTS_2_5 = 4
102  } SHT1X_VOLTS_T;
103 
111  sht1x_context sht1x_init(unsigned int clk_pin, unsigned int data_pin);
112 
116  void sht1x_close(sht1x_context dev);
117 
124  void sht1x_reset(const sht1x_context dev);
125 
133  upm_result_t sht1x_update(const sht1x_context dev);
134 
142  float sht1x_get_temperature(const sht1x_context dev);
143 
151  float sht1x_get_humidity(const sht1x_context dev);
152 
161  upm_result_t sht1x_read_status(const sht1x_context dev, uint8_t *status);
162 
170  upm_result_t sht1x_write_status(const sht1x_context dev, uint8_t status);
171 
182  void sht1x_set_volts(const sht1x_context dev, SHT1X_VOLTS_T volts);
183 
193  upm_result_t sht1x_send_command(const sht1x_context dev, SHT1X_CMD_T cmd);
194 
203  upm_result_t sht1x_wait_for_response(const sht1x_context dev);
204 
213  void sht1x_start_xmit(const sht1x_context dev);
214 
224  void sht1x_read_8bits(const sht1x_context dev, uint8_t *value);
225 
235  upm_result_t sht1x_write_8bits(const sht1x_context dev, uint8_t byte);
236 
237 #ifdef __cplusplus
238 }
239 #endif
void sht1x_close(sht1x_context dev)
Definition: sht1x.c:119
float sht1x_get_humidity(const sht1x_context dev)
Definition: sht1x.c:336
upm_result_t sht1x_read_status(const sht1x_context dev, uint8_t *status)
Definition: sht1x.c:351
upm_result_t sht1x_write_status(const sht1x_context dev, uint8_t status)
Definition: sht1x.c:367
upm_result_t sht1x_wait_for_response(const sht1x_context dev)
Definition: sht1x.c:258
void sht1x_read_8bits(const sht1x_context dev, uint8_t *value)
Definition: sht1x.c:288
float sht1x_get_temperature(const sht1x_context dev)
Definition: sht1x.c:329
struct _sht1x_context * sht1x_context
sht1x_context sht1x_init(unsigned int clk_pin, unsigned int data_pin)
Definition: sht1x.c:31
upm_result_t sht1x_update(const sht1x_context dev)
Definition: sht1x.c:131
void sht1x_reset(const sht1x_context dev)
Definition: sht1x.c:343
upm_result_t sht1x_write_8bits(const sht1x_context dev, uint8_t byte)
Definition: sht1x.c:201
void sht1x_set_volts(const sht1x_context dev, SHT1X_VOLTS_T volts)
Definition: sht1x.c:382
upm_result_t sht1x_send_command(const sht1x_context dev, SHT1X_CMD_T cmd)
Definition: sht1x.c:248
Definition: sht1x.h:46
void sht1x_start_xmit(const sht1x_context dev)
Definition: sht1x.c:184