-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for the 0C45:7402 TEMPerHumM12V1.0, which uses a Si7005…
… sensor.
- Loading branch information
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
#include "type-info.h" | ||
#include "../tempered-internal.h" | ||
|
||
bool tempered_type_hid_get_temperature_si7005( | ||
tempered_device *device, struct tempered_type_hid_sensor *sensor, | ||
struct tempered_type_hid_query_result *group_data, float *tempC | ||
) { | ||
if ( | ||
group_data->length <= sensor->temperature_high_byte_offset || | ||
group_data->length <= sensor->temperature_low_byte_offset | ||
) { | ||
tempered_set_error( | ||
device, strdup( "Not enough data was read from the sensor." ) | ||
); | ||
return false; | ||
} | ||
|
||
// Convert from two separate data bytes to a single integer. | ||
// The result should be an unsigned int between 0x0000 and 0xFFFF. | ||
int low_byte_offset = sensor->temperature_low_byte_offset; | ||
int high_byte_offset = sensor->temperature_high_byte_offset; | ||
int temp = ( group_data->data[low_byte_offset] & 0xFF ) | ||
+ ( ( group_data->data[high_byte_offset] & 0xFF ) << 8 ) | ||
; | ||
|
||
// According to the Silicon Labs Si7005 datasheet, there's 32 codes per ℃ | ||
// with 0x0000 = -50℃. | ||
*tempC = ((float)temp) / 32 - 50; | ||
|
||
return true; | ||
} | ||
|
||
bool tempered_type_hid_get_humidity_si7005( | ||
tempered_device *device, struct tempered_type_hid_sensor *sensor, | ||
struct tempered_type_hid_query_result *group_data, float *rel_hum | ||
) { | ||
float tempC; | ||
if ( | ||
!tempered_type_hid_get_temperature_si7005( | ||
device, sensor, group_data, &tempC | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
if ( | ||
group_data->length <= sensor->humidity_high_byte_offset || | ||
group_data->length <= sensor->humidity_low_byte_offset | ||
) | ||
{ | ||
tempered_set_error( | ||
device, strdup( "Not enough data was read from the sensor." ) | ||
); | ||
return false; | ||
} | ||
|
||
int low_byte_offset = sensor->humidity_low_byte_offset; | ||
int high_byte_offset = sensor->humidity_high_byte_offset; | ||
int rh = ( group_data->data[low_byte_offset] & 0xFF ) | ||
+ ( ( group_data->data[high_byte_offset] & 0xFF ) << 8 ) | ||
; | ||
|
||
// These formulas and values are based on the Silicon Labs Si7005 datasheet | ||
|
||
// There's 16 codes per %RH, with 0x0000 = -24%RH | ||
float relhum = ((float)rh) / 16 - 24; | ||
|
||
// Linearization | ||
relhum -= -0.00393 * relhum * relhum + 0.4008 * relhum - 4.7844; | ||
|
||
// Temperature compensation | ||
relhum += ( tempC - 30 ) * ( 0.00237 * relhum + 0.1973 ); | ||
|
||
*rel_hum = relhum; | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef TEMPERED__TYPE_HID__SI7005_H | ||
#define TEMPERED__TYPE_HID__SI7005_H | ||
|
||
#include <stdbool.h> | ||
|
||
#include "type-info.h" | ||
|
||
// These methods are for the Silicon Labs Si7005 chip. | ||
|
||
bool tempered_type_hid_get_temperature_si7005( | ||
tempered_device *device, struct tempered_type_hid_sensor *sensor, | ||
struct tempered_type_hid_query_result *group_data, float *tempC | ||
); | ||
|
||
bool tempered_type_hid_get_humidity_si7005( | ||
tempered_device *device, struct tempered_type_hid_sensor *sensor, | ||
struct tempered_type_hid_query_result *group_data, float *rel_hum | ||
); | ||
|
||
#endif |