#include #include #include #include #include #include "hih61xx.h" #include "deftypes.h" extern uint16 devicef; extern void bus_err(int ern); extern void print_help(void); unsigned char buf[5]; void hih61xx_print_all(void) { printf( "00: Relative Humidity [%%] (reg: 0x00)\n" "01: Temperature [°C] (reg: 0x00)\n" "02: Status [hex] (reg: 0x00)\n" "\n"); } void hih61xx_conf_set(const uchar *opts) {} void hih61xx_get_data(void) //# Get measurement data from device { buf[0] = 0x00; if(write(devicef, buf, 1) != 1){ //# Start the measurement bus_err(errno); } usleep(60*1000); //# Wait 60ms for measurement if(read(devicef, buf, 4) != 4){ //# Read the data bus_err(errno); } } unsigned char hih61xx_read_status(void){ //# Get device status return ((buf[0] & 0xc0) >> 6); //# He ??? } float hih61xx_calc_humidity(float offset){ //# Measure 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 uchar *opts) { hih61xx_get_data(); if(opts != NULL) { char *ptr; printf("00:%f\n", hih61xx_calc_humidity(strtof((char*)opts, &ptr))); ptr++; // One (,) character allowed for delimiter printf("01:%f\n", hih61xx_read_temp(strtof(ptr, NULL))); } else { printf("00:%f\n", hih61xx_calc_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 uchar *opts) { int id; char *ptr; hih61xx_get_data(); id = strtol ((char*)opts,&ptr,0); //all format allowed ptr++; //one separator allowed switch (id) { case 0x00: printf("00:%f\n", hih61xx_calc_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(); } }