-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathClosedCube_SHT31D.h
executable file
·215 lines (162 loc) · 5.54 KB
/
ClosedCube_SHT31D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Particle Photon SHT-3X-DIS Library
ported from
Arduino Library for Sensirion SHT3X-DIS Digital Humidity & Temperature Sensors
Written by AA
---
The MIT License (MIT)
Copyright (c) 2015-2016 ClosedCube Limited
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef CLOSEDCUBE_SHT31D
#define CLOSEDCUBE_SHT31D
#include "application.h"
namespace SHT31D_CC {
typedef enum {
CMD_READ_SERIAL_NUMBER = 0x3780,
CMD_READ_STATUS = 0xF32D,
CMD_CLEAR_STATUS = 0x3041,
CMD_HEATER_ENABLE = 0x306D,
CMD_HEATER_DISABLE = 0x3066,
CMD_SOFT_RESET = 0x30A2,
CMD_CLOCK_STRETCH_H = 0x2C06,
CMD_CLOCK_STRETCH_M = 0x2C0D,
CMD_CLOCK_STRETCH_L = 0x2C10,
CMD_POLLING_H = 0x2400,
CMD_POLLING_M = 0x240B,
CMD_POLLING_L = 0x2416,
CMD_ART = 0x2B32,
CMD_PERIODIC_HALF_H = 0x2032,
CMD_PERIODIC_HALF_M = 0x2024,
CMD_PERIODIC_HALF_L = 0x202F,
CMD_PERIODIC_1_H = 0x2130,
CMD_PERIODIC_1_M = 0x2126,
CMD_PERIODIC_1_L = 0x212D,
CMD_PERIODIC_2_H = 0x2236,
CMD_PERIODIC_2_M = 0x2220,
CMD_PERIODIC_2_L = 0x222B,
CMD_PERIODIC_4_H = 0x2334,
CMD_PERIODIC_4_M = 0x2322,
CMD_PERIODIC_4_L = 0x2329,
CMD_PERIODIC_10_H = 0x2737,
CMD_PERIODIC_10_M = 0x2721,
CMD_PERIODIC_10_L = 0x272A,
CMD_FETCH_DATA = 0xE000,
CMD_STOP_PERIODIC = 0x3093,
CMD_READ_ALR_LIMIT_LS = 0xE102,
CMD_READ_ALR_LIMIT_LC = 0xE109,
CMD_READ_ALR_LIMIT_HS = 0xE11F,
CMD_READ_ALR_LIMIT_HC = 0xE114,
CMD_WRITE_ALR_LIMIT_HS = 0x611D,
CMD_WRITE_ALR_LIMIT_HC = 0x6116,
CMD_WRITE_ALR_LIMIT_LC = 0x610B,
CMD_WRITE_ALR_LIMIT_LS = 0x6100,
CMD_NO_SLEEP = 0x303E,
} SHT31D_Commands;
typedef enum {
REPEATABILITY_HIGH,
REPEATABILITY_MEDIUM,
REPEATABILITY_LOW,
} SHT31D_Repeatability;
typedef enum {
MODE_CLOCK_STRETCH,
MODE_POLLING,
} SHT31D_Mode;
typedef enum {
FREQUENCY_HZ5,
FREQUENCY_1HZ,
FREQUENCY_2HZ,
FREQUENCY_4HZ,
FREQUENCY_10HZ
} SHT31D_Frequency;
typedef enum {
NO_ERROR = 0,
CRC_ERROR = -101,
TIMEOUT_ERROR = -102,
PARAM_WRONG_MODE = -501,
PARAM_WRONG_REPEATABILITY = -502,
PARAM_WRONG_FREQUENCY = -503,
PARAM_WRONG_ALERT = -504,
// Wire I2C translated error codes
WIRE_I2C_DATA_TOO_LOG = -10,
WIRE_I2C_RECEIVED_NACK_ON_ADDRESS = -20,
WIRE_I2C_RECEIVED_NACK_ON_DATA = -30,
WIRE_I2C_UNKNOW_ERROR = -40
} SHT31D_ErrorCode;
typedef union {
uint16_t rawData;
struct {
uint8_t WriteDataChecksumStatus : 1;
uint8_t CommandStatus : 1;
uint8_t Reserved0 : 2;
uint8_t SystemResetDetected : 1;
uint8_t Reserved1 : 5;
uint8_t T_TrackingAlert : 1;
uint8_t RH_TrackingAlert : 1;
uint8_t Reserved2 : 1;
uint8_t HeaterStatus : 1;
uint8_t Reserved3 : 1;
uint8_t AlertPending : 1;
};
} SHT31D_RegisterStatus;
struct SHT31D {
float t;
float rh;
SHT31D_ErrorCode error;
};
class ClosedCube_SHT31D {
public:
ClosedCube_SHT31D();
SHT31D_ErrorCode begin(uint8_t address);
SHT31D_ErrorCode clearAll();
SHT31D_RegisterStatus readStatusRegister();
SHT31D_ErrorCode heaterEnable();
SHT31D_ErrorCode heaterDisable();
SHT31D_ErrorCode softReset();
SHT31D_ErrorCode generalCallReset();
SHT31D_ErrorCode artEnable();
uint32_t readSerialNumber();
SHT31D readTempAndHumidity(SHT31D_Repeatability repeatability, SHT31D_Mode mode, uint8_t timeout);
SHT31D readTempAndHumidityClockStretch(SHT31D_Repeatability repeatability);
SHT31D readTempAndHumidityPolling(SHT31D_Repeatability repeatability, uint8_t timeout);
SHT31D_ErrorCode periodicStart(SHT31D_Repeatability repeatability, SHT31D_Frequency frequency);
SHT31D periodicFetchData();
SHT31D_ErrorCode periodicStop();
SHT31D_ErrorCode writeAlertHigh(float temperatureSet, float temperatureClear, float humiditySet, float humidityClear);
SHT31D readAlertHighSet();
SHT31D readAlertHighClear();
SHT31D_ErrorCode writeAlertLow(float temperatureClear, float temperatureSet, float humidityClear, float humiditySet);
SHT31D readAlertLowSet();
SHT31D readAlertLowClear();
private:
uint8_t _address;
SHT31D_RegisterStatus _status;
SHT31D_ErrorCode writeCommand(SHT31D_Commands command);
SHT31D_ErrorCode writeAlertData(SHT31D_Commands command, float temperature, float humidity);
uint8_t checkCrc(uint8_t data[], uint8_t checksum);
uint8_t calculateCrc(uint8_t data[]);
float calculateHumidity(uint16_t rawValue);
float calculateTemperature(uint16_t rawValue);
uint16_t calculateRawHumidity(float value);
uint16_t calculateRaWTemperature(float value);
SHT31D readTemperatureAndHumidity();
SHT31D readAlertData(SHT31D_Commands command);
SHT31D_ErrorCode read(uint16_t* data, uint8_t numOfPair);
SHT31D returnError(SHT31D_ErrorCode command);
};
}
#endif