From ab99f700bace05491919324259c9110c7f387af8 Mon Sep 17 00:00:00 2001 From: Gergő J. Miklós Date: Sat, 29 Sep 2018 01:32:11 +0200 Subject: Existing code & initialcommit. --- src/hih61xx.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 src/hih61xx.c (limited to 'src/hih61xx.c') 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 +#include +#include +#include +#include + + +#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(); + } +} + + + + + + + + + + + -- cgit v1.2.3