LibDriver SHT35
Loading...
Searching...
No Matches
driver_sht35_register_test.c
Go to the documentation of this file.
1
37
39#include <stdlib.h>
40
41static sht35_handle_t gs_handle;
42
52{
53 uint8_t res;
54 uint16_t reg;
55 uint16_t status;
56 uint8_t sn[4];
57 uint16_t set;
58 uint16_t clear;
59 uint16_t set_check;
60 uint16_t clear_check;
61 float temperature;
62 float humidity;
63 sht35_info_t info;
64 sht35_address_t address_pin;
65 sht35_repeatability_t repeatability;
66
67 /* link functions */
76
77 /* sht35 info */
78 res = sht35_info(&info);
79 if (res != 0)
80 {
81 sht35_interface_debug_print("sht35: get info failed.\n");
82
83 return 1;
84 }
85 else
86 {
87 /* print chip information */
88 sht35_interface_debug_print("sht35: chip is %s.\n", info.chip_name);
89 sht35_interface_debug_print("sht35: manufacturer is %s.\n", info.manufacturer_name);
90 sht35_interface_debug_print("sht35: interface is %s.\n", info.interface);
91 sht35_interface_debug_print("sht35: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
92 sht35_interface_debug_print("sht35: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
93 sht35_interface_debug_print("sht35: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
94 sht35_interface_debug_print("sht35: max current is %0.2fmA.\n", info.max_current_ma);
95 sht35_interface_debug_print("sht35: max temperature is %0.1fC.\n", info.temperature_max);
96 sht35_interface_debug_print("sht35: min temperature is %0.1fC.\n", info.temperature_min);
97 }
98
99 /* start register test */
100 sht35_interface_debug_print("sht35: start register test.\n");
101
102 /* sht35_set_addr_pin/sht35_get_addr_pin test */
103 sht35_interface_debug_print("sht35: sht35_set_addr_pin/sht35_get_addr_pin test.\n");
104
105 /* set address pin 0 */
106 res = sht35_set_addr_pin(&gs_handle, SHT35_ADDRESS_0);
107 if (res != 0)
108 {
109 sht35_interface_debug_print("sht35: set addr pin failed.\n");
110
111 return 1;
112 }
113 sht35_interface_debug_print("sht35: set address pin 0.\n");
114 res = sht35_get_addr_pin(&gs_handle, &address_pin);
115 if (res != 0)
116 {
117 sht35_interface_debug_print("sht35: get addr pin failed.\n");
118
119 return 1;
120 }
121 sht35_interface_debug_print("sht35: check addr pin %s.\n", (SHT35_ADDRESS_0 == address_pin) ? "ok" : "error");
122
123 /* set address pin 1 */
124 res = sht35_set_addr_pin(&gs_handle, SHT35_ADDRESS_1);
125 if (res != 0)
126 {
127 sht35_interface_debug_print("sht35: set addr pin failed.\n");
128
129 return 1;
130 }
131 sht35_interface_debug_print("sht35: set address pin 1.\n");
132 res = sht35_get_addr_pin(&gs_handle, &address_pin);
133 if (res != 0)
134 {
135 sht35_interface_debug_print("sht35: get addr pin failed.\n");
136
137 return 1;
138 }
139 sht35_interface_debug_print("sht35: check addr pin %s.\n", (SHT35_ADDRESS_1 == address_pin) ? "ok" : "error");
140
141 /* set address pin */
142 res = sht35_set_addr_pin(&gs_handle, addr_pin);
143 if (res != 0)
144 {
145 sht35_interface_debug_print("sht35: set addr pin failed.\n");
146
147 return 1;
148 }
149
150 /* sht35 init */
151 res = sht35_init(&gs_handle);
152 if (res != 0)
153 {
154 sht35_interface_debug_print("sht35: init failed.\n");
155
156 return 1;
157 }
158
159 /* wait 10 ms */
161
162 /* sht35_set_repeatability/sht35_get_repeatability test */
163 sht35_interface_debug_print("sht35: sht35_set_repeatability/sht35_get_repeatability test.\n");
164
165 /* set high */
167 if (res != 0)
168 {
169 sht35_interface_debug_print("sht35: set repeatability failed.\n");
170 (void)sht35_deinit(&gs_handle);
171
172 return 1;
173 }
174 sht35_interface_debug_print("sht35: set repeatability high.\n");
175 res = sht35_get_repeatability(&gs_handle, &repeatability);
176 if (res != 0)
177 {
178 sht35_interface_debug_print("sht35: get repeatability failed.\n");
179 (void)sht35_deinit(&gs_handle);
180
181 return 1;
182 }
183 sht35_interface_debug_print("sht35: check repeatability pin %s.\n", (SHT35_REPEATABILITY_HIGH == repeatability) ? "ok" : "error");
184
185 /* set medium */
187 if (res != 0)
188 {
189 sht35_interface_debug_print("sht35: set repeatability failed.\n");
190 (void)sht35_deinit(&gs_handle);
191
192 return 1;
193 }
194 sht35_interface_debug_print("sht35: set repeatability medium.\n");
195 res = sht35_get_repeatability(&gs_handle, &repeatability);
196 if (res != 0)
197 {
198 sht35_interface_debug_print("sht35: get repeatability failed.\n");
199 (void)sht35_deinit(&gs_handle);
200
201 return 1;
202 }
203 sht35_interface_debug_print("sht35: check repeatability pin %s.\n", (SHT35_REPEATABILITY_MEDIUM == repeatability) ? "ok" : "error");
204
205 /* set low */
207 if (res != 0)
208 {
209 sht35_interface_debug_print("sht35: set repeatability failed.\n");
210 (void)sht35_deinit(&gs_handle);
211
212 return 1;
213 }
214 sht35_interface_debug_print("sht35: set repeatability low.\n");
215 res = sht35_get_repeatability(&gs_handle, &repeatability);
216 if (res != 0)
217 {
218 sht35_interface_debug_print("sht35: get repeatability failed.\n");
219 (void)sht35_deinit(&gs_handle);
220
221 return 1;
222 }
223 sht35_interface_debug_print("sht35: check repeatability pin %s.\n", (SHT35_REPEATABILITY_LOW == repeatability) ? "ok" : "error");
224
225 /* set art test */
226 sht35_interface_debug_print("sht35: set art test.\n");
227 res = sht35_set_art(&gs_handle);
228 if (res != 0)
229 {
230 sht35_interface_debug_print("sht35: set art failed.\n");
231 (void)sht35_deinit(&gs_handle);
232
233 return 1;
234 }
235
236 /* wait 10 ms */
238 sht35_interface_debug_print("sht35: check art %s.\n", (res == 0) ? "ok" : "error");
239
240 /* sht35_set_heater test*/
241 sht35_interface_debug_print("sht35: set heater test.\n");
242
243 /* enable heater */
244 res = sht35_set_heater(&gs_handle, SHT35_BOOL_TRUE);
245 if (res != 0)
246 {
247 sht35_interface_debug_print("sht35: set heater failed.\n");
248 (void)sht35_deinit(&gs_handle);
249
250 return 1;
251 }
252
253 /* wait 10 ms */
255 sht35_interface_debug_print("sht35: enable heater.\n");
256 sht35_interface_debug_print("sht35: check heater %s.\n", (res == 0) ? "ok" : "error");
257
258 /* disable heater */
259 res = sht35_set_heater(&gs_handle, SHT35_BOOL_FALSE);
260 if (res != 0)
261 {
262 sht35_interface_debug_print("sht35: set heater failed.\n");
263 (void)sht35_deinit(&gs_handle);
264
265 return 1;
266 }
267
268 /* wait 10 ms */
270 sht35_interface_debug_print("sht35: disable heater.\n");
271 sht35_interface_debug_print("sht35: check heater %s.\n", (res == 0) ? "ok" : "error");
272
273 /* get_status test*/
274 sht35_interface_debug_print("sht35: sht35_get_status test.\n");
275 res = sht35_get_status(&gs_handle, (uint16_t *)&status);
276 if (res != 0)
277 {
278 sht35_interface_debug_print("sht35: get status failed.\n");
279 (void)sht35_deinit(&gs_handle);
280
281 return 1;
282 }
283
284 /* wait 10 ms */
286 sht35_interface_debug_print("sht35: check status 0x%02X.\n", status);
287
288 /* clear_status test*/
289 sht35_interface_debug_print("sht35: sht35_clear_status test.\n");
290 res = sht35_clear_status(&gs_handle);
291 if (res != 0)
292 {
293 sht35_interface_debug_print("sht35: clear status failed.\n");
294 (void)sht35_deinit(&gs_handle);
295
296 return 1;
297 }
298
299 /* wait 10 ms */
301 sht35_interface_debug_print("sht35: check clear status %s.\n", (res == 0) ? "ok" : "error");
302
303 /* sht35_get_serial_number test*/
304 sht35_interface_debug_print("sht35: sht35_get_serial_number test.\n");
305
306 /* get serial number */
307 res = sht35_get_serial_number(&gs_handle, sn);
308 if (res != 0)
309 {
310 sht35_interface_debug_print("sht35: get serial number failed.\n");
311 (void)sht35_deinit(&gs_handle);
312
313 return 1;
314 }
315 sht35_interface_debug_print("sht35: serial number is 0x%02X 0x%02X 0x%02X 0x%02X.\n", sn[0], sn[1], sn[2], sn[3]);
316
317 /* sht35_set_high_alert_limit/sht35_get_high_alert_limit test */
318 sht35_interface_debug_print("sht35: sht35_set_high_alert_limit/sht35_get_high_alert_limit test.\n");
319
320 set = rand() % 0xFFFF;
321 clear = rand() % 0xFFFF;
322 res = sht35_set_high_alert_limit(&gs_handle, set, clear);
323 if (res != 0)
324 {
325 sht35_interface_debug_print("sht35: set high alert limit failed.\n");
326 (void)sht35_deinit(&gs_handle);
327
328 return 1;
329 }
330 sht35_interface_debug_print("sht35: set is 0x%04X.\n", set);
331 sht35_interface_debug_print("sht35: clear is 0x%04X.\n", clear);
332 res = sht35_get_high_alert_limit(&gs_handle, &set_check, &clear_check);
333 if (res != 0)
334 {
335 sht35_interface_debug_print("sht35: get high alert limit failed.\n");
336 (void)sht35_deinit(&gs_handle);
337
338 return 1;
339 }
340 sht35_interface_debug_print("sht35: check high alert limit set %s.\n", (set == set_check) ? "ok" : "error");
341 sht35_interface_debug_print("sht35: check high alert limit clear %s.\n", (clear == clear_check) ? "ok" : "error");
342
343 /* sht35_set_low_alert_limit/sht35_get_low_alert_limit test */
344 sht35_interface_debug_print("sht35: sht35_set_low_alert_limit/sht35_get_low_alert_limit test.\n");
345
346 set = rand() % 0xFFFF;
347 clear = rand() % 0xFFFF;
348 res = sht35_set_low_alert_limit(&gs_handle, set, clear);
349 if (res != 0)
350 {
351 sht35_interface_debug_print("sht35: set low alert limit failed.\n");
352 (void)sht35_deinit(&gs_handle);
353
354 return 1;
355 }
356 sht35_interface_debug_print("sht35: set is 0x%04X.\n", set);
357 sht35_interface_debug_print("sht35: clear is 0x%04X.\n", clear);
358 res = sht35_get_low_alert_limit(&gs_handle, &set_check, &clear_check);
359 if (res != 0)
360 {
361 sht35_interface_debug_print("sht35: get low alert limit failed.\n");
362 (void)sht35_deinit(&gs_handle);
363
364 return 1;
365 }
366 sht35_interface_debug_print("sht35: check low alert limit set %s.\n", (set == set_check) ? "ok" : "error");
367 sht35_interface_debug_print("sht35: check low alert limit clear %s.\n", (clear == clear_check) ? "ok" : "error");
368
369 /* sht35_alert_limit_convert_to_register test */
370 sht35_interface_debug_print("sht35: sht35_alert_limit_convert_to_register test.\n");
371
372 /* alert limit convert to register */
373 temperature = 60.0f;
374 humidity = 80.0f;
375 res = sht35_alert_limit_convert_to_register(&gs_handle, temperature, humidity, &reg);
376 if (res != 0)
377 {
378 sht35_interface_debug_print("sht35: alert limit convert to register failed.\n");
379 (void)sht35_deinit(&gs_handle);
380
381 return 1;
382 }
383 sht35_interface_debug_print("sht35: %0.2f%% %0.2fC converted 0x%04X.\n", humidity, temperature, reg);
384
385 /* alert limit convert to register */
386 temperature = -9.0f;
387 humidity = 22.0f;
388 res = sht35_alert_limit_convert_to_register(&gs_handle, temperature, humidity, &reg);
389 if (res != 0)
390 {
391 sht35_interface_debug_print("sht35: alert limit convert to register failed.\n");
392 (void)sht35_deinit(&gs_handle);
393
394 return 1;
395 }
396 sht35_interface_debug_print("sht35: %0.2f%% %0.2fC converted 0x%04X.\n", humidity, temperature, reg);
397
398 /* finish register test */
399 sht35_interface_debug_print("sht35: finish register test.\n");
400 (void)sht35_deinit(&gs_handle);
401
402 return 0;
403}
driver sht35 register test header file
uint8_t sht35_set_repeatability(sht35_handle_t *handle, sht35_repeatability_t repeatability)
set the measurement repeatability
uint8_t sht35_info(sht35_info_t *info)
get chip's information
uint8_t sht35_get_high_alert_limit(sht35_handle_t *handle, uint16_t *set, uint16_t *clear)
get high alert limit
uint8_t sht35_get_low_alert_limit(sht35_handle_t *handle, uint16_t *set, uint16_t *clear)
get low alert limit
uint8_t sht35_deinit(sht35_handle_t *handle)
close the chip
uint8_t sht35_alert_limit_convert_to_register(sht35_handle_t *handle, float temperature, float humidity, uint16_t *reg)
alert limit convert to register raw data
uint8_t sht35_init(sht35_handle_t *handle)
initialize the chip
uint8_t sht35_clear_status(sht35_handle_t *handle)
clear the current status
sht35_address_t
sht35 address enumeration definition
uint8_t sht35_get_addr_pin(sht35_handle_t *handle, sht35_address_t *addr_pin)
get the iic address pin
sht35_repeatability_t
sht35 repeatability enumeration definition
uint8_t sht35_get_serial_number(sht35_handle_t *handle, uint8_t sn[4])
get serial number
uint8_t sht35_get_status(sht35_handle_t *handle, uint16_t *status)
get the current status
uint8_t sht35_set_heater(sht35_handle_t *handle, sht35_bool_t enable)
enable or disable the chip heater
struct sht35_info_s sht35_info_t
sht35 information structure definition
uint8_t sht35_set_low_alert_limit(sht35_handle_t *handle, uint16_t set, uint16_t clear)
set low alert limit
struct sht35_handle_s sht35_handle_t
sht35 handle structure definition
uint8_t sht35_get_repeatability(sht35_handle_t *handle, sht35_repeatability_t *repeatability)
get the measurement repeatability
uint8_t sht35_set_high_alert_limit(sht35_handle_t *handle, uint16_t set, uint16_t clear)
set high alert limit
uint8_t sht35_set_art(sht35_handle_t *handle)
set the chip art
uint8_t sht35_set_addr_pin(sht35_handle_t *handle, sht35_address_t addr_pin)
set the iic address pin
@ SHT35_ADDRESS_1
@ SHT35_ADDRESS_0
@ SHT35_REPEATABILITY_MEDIUM
@ SHT35_REPEATABILITY_LOW
@ SHT35_REPEATABILITY_HIGH
@ SHT35_BOOL_FALSE
@ SHT35_BOOL_TRUE
uint8_t sht35_interface_iic_init(void)
interface iic bus init
uint8_t sht35_interface_iic_write_address16(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
interface iic bus write with 16 bits register address
void sht35_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t sht35_interface_iic_read_address16(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
interface iic bus read with 16 bits register address
uint8_t sht35_interface_iic_deinit(void)
interface iic bus deinit
void sht35_interface_debug_print(const char *const fmt,...)
interface print format data
void sht35_interface_receive_callback(uint16_t type)
interface receive callback
uint8_t sht35_register_test(sht35_address_t addr_pin)
register test
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]