diff options
author | Gergő J. Miklós | 2018-09-29 01:32:11 +0200 |
---|---|---|
committer | Gergő J. Miklós | 2018-09-29 01:32:11 +0200 |
commit | ab99f700bace05491919324259c9110c7f387af8 (patch) | |
tree | 699d197b204e482dfb56f8443e036ed6f740ace0 /src/hih61xx.c | |
download | i2sensors-ab99f700bace05491919324259c9110c7f387af8.tar.gz i2sensors-ab99f700bace05491919324259c9110c7f387af8.zip |
Existing code & initialcommit.
Diffstat (limited to 'src/hih61xx.c')
-rw-r--r-- | src/hih61xx.c | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/src/hih61xx.c b/src/hih61xx.c new file mode 100644 index 0000000..b68b653 --- /dev/null +++ b/src/hih61xx.c @@ -0,0 +1,155 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> + + +#include "hih61xx.h" + +extern int file; +extern void bus_err(int ern); +extern void print_help(void); +unsigned char buf[5]; + + + + +void hih61xx_list_all(void) +{ + printf( + "00: Relative Humidity [%%] (reg: 0x00)\n" + "01: Temperature [°C] (reg: 0x00)\n" + "02: Status [hex] (reg: 0x00)\n" + "\n"); +} + + + +void hih61xx_get_data(void) +{ + buf[0] = 0x00; + + if(write(file, buf, 1) != 1) //Start measure + { + bus_err(errno); + } + + usleep(60*1000); // Wait 60ms for measurement + + if(read(file, buf, 4) != 4) //Read the data + { + bus_err(errno); + } +} + + + +unsigned char hih61xx_read_status(void) +{ + return ((buf[0] & 0xc0) >> 6); +} + + + +float hih61xx_read_humidity(float offset) +{ + unsigned int raw = 0; + float humidity; + + if(hih61xx_read_status() != 0x01 ) + { + raw = ((buf[0] & 0x3f) << 8) + buf[1]; // two msb status, 14bit data + humidity = ((float)raw/(16384 - 2))*100; // humidity = (14bit data / (2^14 -2)) * 100% + } + else + { + humidity = -1.0 ; + } + + return (humidity + offset) ; +} + + + +float hih61xx_read_temp(float offset) +{ + unsigned int raw = 0; + float temp; + + if(hih61xx_read_status() != 0x01 ) + { + raw = ((buf[2] << 8) + buf[1]) >> 2; // 14bit temp data + temp = ((float)raw/(16384 - 2))*165 - 40 ; // temp = ((14bit data / (2^14 -2))*165) - 40 [°C] + } + else + { + temp = -40.0 ; + } + + return (temp + offset); +} + + + +void hih61xx_read_all(const char *opts) +{ + hih61xx_get_data(); + + if(opts != NULL) + { + char *ptr; + printf("00:%f\n", hih61xx_read_humidity(strtof(opts, &ptr))); + + ptr++; // One (,) character allowed for delimiter + + printf("01:%f\n", hih61xx_read_temp(strtof(ptr, NULL))); + } + else + { + printf("00:%f\n", hih61xx_read_humidity(0.0)); + printf("01:%f\n", hih61xx_read_temp(0.0)); + } + + printf("02:0x%x\n", hih61xx_read_status()); + +} + + + +void hih61xx_read_one(const char *opts) +{ + int id; + char *ptr; + + hih61xx_get_data(); + id = strtol (opts,&ptr,0); //all format allowed + ptr++; //one separator allowed + + switch (id) + { + case 0x00: + printf("00:%f\n", hih61xx_read_humidity(strtof(ptr, NULL))); + break; + case 0x01: + printf("01:%f\n", hih61xx_read_temp(strtof(ptr, NULL))); + break; + case 0x02: + printf("02:0x%x\n", hih61xx_read_status()); + break; + default: + print_help(); + } +} + + + + + + + + + + + |