1+ #include "hdc2021debr.h"
2+ #include <stdbool.h>
3+ #include <stdio.h>
4+
5+ static int hdc2021_write_reg (hdc2021debr_t * hdc2021 , uint8_t reg , uint8_t data )
6+ {
7+ uint8_t buffer [2 ] = {reg , data };
8+ return hdc2021 -> write_reg (buffer , hdc2021 -> dev_address , sizeof (buffer ));
9+ }
10+
11+ static int hdc2021_read_reg (hdc2021debr_t * hdc2021 , uint8_t reg , uint8_t * data ,
12+ uint8_t length )
13+ {
14+ return hdc2021 -> read_reg (data , reg , hdc2021 -> dev_address , length );
15+ }
16+
17+ int hdc2021_init (hdc2021debr_t * hdc2021 , Write_ptr_hdc write_reg , Read_ptr_hdc read_reg ,
18+ uint8_t dev_address )
19+ {
20+ hdc2021 -> write_reg = write_reg ;
21+ hdc2021 -> read_reg = read_reg ;
22+ hdc2021 -> dev_address = dev_address << 1u ;
23+
24+ return 0 ;
25+ }
26+
27+ int hdc2021_toggle_heater (hdc2021debr_t * hdc2021 , bool enable )
28+ {
29+ uint8_t device_config ;
30+
31+ if (hdc2021_read_reg (hdc2021 , REG_DEVICE_CONFIG , & device_config ,
32+ sizeof (device_config )))
33+ {
34+ return 1 ;
35+ }
36+
37+ // the 3rd bit toggles the heater
38+ if (enable )
39+ {
40+ device_config |= (1 << 3 );
41+ } else
42+ {
43+ device_config &= ~(1 << 3 );
44+ }
45+
46+ if (hdc2021_write_reg (hdc2021 , REG_DEVICE_CONFIG , device_config ))
47+ {
48+ return 1 ;
49+ }
50+
51+ hdc2021 -> is_heater_enabled = enable ;
52+
53+ return 0 ;
54+ }
55+
56+ int hdc2021_trigger_oneshot (hdc2021debr_t * hdc2021 )
57+ {
58+ uint8_t config ;
59+
60+ if (hdc2021_read_reg (hdc2021 , REG_MEASURE_CONFIG , & config , 1 ))
61+ {
62+ return 1 ;
63+ }
64+
65+ config |= 0x01 ;
66+
67+ if (hdc2021_write_reg (hdc2021 , REG_MEASURE_CONFIG , config ))
68+ {
69+ return 1 ;
70+ }
71+
72+ return 0 ;
73+ }
74+
75+ int hdc2021_get_temp_humid (hdc2021debr_t * hdc2021 )
76+ {
77+ uint8_t buffer [4 ];
78+
79+ if (hdc2021_read_reg (hdc2021 , REG_TEMP_LOW , buffer , 4 ))
80+ {
81+ return 1 ;
82+ }
83+
84+ uint16_t temp_bytes = ((uint16_t )(buffer [1 ]) << 8 ) | buffer [0 ];
85+ uint16_t humid_bytes = ((uint16_t )(buffer [3 ]) << 8 ) | buffer [2 ];
86+
87+ float tempVal = (((float )temp_bytes /65536.0f ) * 165.0f ) - 40.0f ;
88+ hdc2021 -> temp = tempVal ;
89+
90+ float humidVal = ((float )humid_bytes /65536.0f ) * 100.0f ;
91+ hdc2021 -> humidity = humidVal ;
92+
93+ return 0 ;
94+ }
0 commit comments