Skip to content

Commit 93e9b79

Browse files
committed
fixes
1 parent 61c65b0 commit 93e9b79

2 files changed

Lines changed: 34 additions & 28 deletions

File tree

general/include/hdc2021debr.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ typedef enum {
2222
} HDC2021_REG_ADDR_t;
2323

2424
/** Function Pointers */
25-
typedef int (*Write_ptr)(uint8_t *data, uint8_t dev_address, uint8_t length);
26-
typedef int (*Read_ptr)(uint8_t *data, uint8_t reg, uint8_t dev_address,
25+
typedef int (*Write_ptr_hdc)(uint8_t *data, uint8_t dev_address, uint8_t length);
26+
typedef int (*Read_ptr_hdc)(uint8_t *data, uint8_t reg, uint8_t dev_address,
2727
uint8_t length);
2828

2929
typedef struct {
30-
Write_ptr write_reg;
31-
Read_ptr read_reg;
30+
Write_ptr_hdc write_reg;
31+
Read_ptr_hdc read_reg;
3232
float temp;
3333
float humidity;
3434
bool is_heater_enabled;
@@ -41,8 +41,9 @@ typedef struct {
4141
* @param hdc2021 - hdc2021debr driver
4242
* @return int - Status code
4343
*/
44-
int hdc2021_init(hdc2021debr_t *hdc2021, Write_ptr write_reg, Read_ptr read_reg,
44+
int hdc2021_init(hdc2021debr_t *hdc2021, Write_ptr_hdc write_reg, Read_ptr_hdc read_reg,
4545
uint8_t dev_address);
46+
4647
/**
4748
* @brief Toggles the status of the internal heater
4849
*
@@ -51,11 +52,21 @@ int hdc2021_init(hdc2021debr_t *hdc2021, Write_ptr write_reg, Read_ptr read_reg,
5152
* @return int - Status code
5253
*/
5354
int hdc2021_toggle_heater(hdc2021debr_t *hdc2021, bool enable);
55+
56+
/**
57+
* @brief triggers oneshot measurement
58+
*
59+
* @param hdc2021 - hdc2021debr driver
60+
* @return int - Status code
61+
*/
62+
int hdc2021_trigger_oneshot(hdc2021debr_t *hdc2021);
63+
5464
/**
5565
* @brief Retrieves the temperature and humidity
5666
* @note Call hdc2021_trigger_oneshot() and wait a couple ms before calling this
5767
* @param hdc2021 - hdc2021debr driver
5868
* @return int - Status code
5969
*/
6070
int hdc2021_get_temp_humid(hdc2021debr_t *hdc2021);
71+
6172
#endif

general/src/hdc2021debr.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static int hdc2021_read_reg(hdc2021debr_t *hdc2021, uint8_t reg, uint8_t *data,
1414
return hdc2021->read_reg(data, reg, hdc2021->dev_address, length);
1515
}
1616

17-
int hdc2021_init(hdc2021debr_t *hdc2021, Write_ptr write_reg, Read_ptr read_reg,
17+
int hdc2021_init(hdc2021debr_t *hdc2021, Write_ptr_hdc write_reg, Read_ptr_hdc read_reg,
1818
uint8_t dev_address)
1919
{
2020
hdc2021->write_reg = write_reg;
@@ -32,40 +32,40 @@ int hdc2021_toggle_heater(hdc2021debr_t *hdc2021, bool enable)
3232
sizeof(device_config)))
3333
{
3434
return 1;
35-
};
35+
}
3636

3737
// the 3rd bit toggles the heater
38-
if (enable) {
38+
if (enable)
39+
{
3940
device_config |= (1 << 3);
40-
} else {
41+
} else
42+
{
4143
device_config &= ~(1 << 3);
42-
};
44+
}
4345

4446
if (hdc2021_write_reg(hdc2021, REG_DEVICE_CONFIG, device_config))
4547
{
4648
return 1;
47-
};
49+
}
4850

49-
return 0;
51+
hdc2021->is_heater_enabled = enable;
5052

53+
return 0;
5154
}
52-
/**
53-
* @brief triggers oneshot measurement
54-
*
55-
* @param hdc2021 - hdc2021debr driver
56-
* @return int - Status code
57-
*/
55+
5856
int hdc2021_trigger_oneshot(hdc2021debr_t *hdc2021)
5957
{
6058
uint8_t config;
6159

62-
if (hdc2021_read_reg(hdc2021, REG_MEASURE_CONFIG, &config, 1)) {
60+
if (hdc2021_read_reg(hdc2021, REG_MEASURE_CONFIG, &config, 1))
61+
{
6362
return 1;
6463
}
6564

6665
config |= 0x01;
6766

68-
if (hdc2021_write_reg(hdc2021, REG_MEASURE_CONFIG, config)) {
67+
if (hdc2021_write_reg(hdc2021, REG_MEASURE_CONFIG, config))
68+
{
6969
return 1;
7070
}
7171

@@ -76,18 +76,13 @@ int hdc2021_get_temp_humid(hdc2021debr_t *hdc2021)
7676
{
7777
uint8_t buffer[4];
7878

79-
if (hdc2021_trigger_oneshot(hdc2021)) {
80-
return 1;
81-
}
82-
83-
8479
if (hdc2021_read_reg(hdc2021, REG_TEMP_LOW, buffer, 4))
8580
{
8681
return 1;
87-
};
82+
}
8883

89-
uint16_t temp_bytes = (buffer[1] << 8) | buffer[0];
90-
uint16_t humid_bytes = (buffer[3] << 8) | buffer[2];
84+
uint16_t temp_bytes = ((uint16_t)(buffer[1]) << 8) | buffer[0];
85+
uint16_t humid_bytes = ((uint16_t)(buffer[3]) << 8) | buffer[2];
9186

9287
float tempVal = (((float)temp_bytes/65536.0f) * 165.0f) - 40.0f;
9388
hdc2021->temp = tempVal;

0 commit comments

Comments
 (0)