#include #include #include #include #include #include //# isdigit() #include "hih61xx.h" #include "deftypes.h" extern uint16 devicef; extern void bus_err(int ern); extern void print_help(void); extern uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen); unsigned char buf[32]; void hih61xx_print_all(void) { printf( "============ HiH61xx interface ===========\n" "Registers:\n" " 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) {} //Maybe later // 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); // } // } // uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen){ //# [buf] = 32 byte // if(write(devicef, buf, wrlen) != wrlen){ //# write one byte to device // bus_err(errno); // } // usleep(waitlen*1000); //# Wait 10ms for reading // if(read(devicef, buf, rdlen) != rdlen) { //# read the result // bus_err(errno); // } // return buf; // } // unsigned char hih61xx_read_status(void){ //# Get device status static uchar calc_status(uchar *buf){ return ((buf[0] & 0xc0) >> 6); //# He ??? } //float hih61xx_calc_humidity(float offset){ //# Measure static float calc_humidity(uchar *buf, float offset){ //# Measure uint16 raw = 0; float humidity; if(calc_status(buf) != 0x01 ) { //# Data format: |s1,s0,h13,h12|h11,h10,h9,h8|_|h7,h6,h5,h4|h3,h2,h1,h0|_|t13,t12,t11,t10|t9,t8,t7,t6|_|t5,t4,t3,t2|t1,t0,x,x| 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) static float calc_temp(uchar *buf, float offset) { uint16 raw = 0; float temp; if(calc_status(buf) != 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(); buf[0] = 0x00; xchg_data(buf,1,60,4); // if(opts != NULL) // { // char *ptr; // printf("00:%f\n", calc_humidity(buf, 0.0)); // ptr++; // One (,) character allowed for delimiter // printf("01:%f\n", calc_temp(buf, 0.0)); // } else { printf("00:%f\n", calc_humidity(buf, 0.0)); printf("01:%f\n", calc_temp(buf, 0.0)); // } printf("02:0x%x\n", calc_status(buf)); } void hih61xx_read_one(const uchar *opts) { uint16 i,id; uchar temp[256]; buf[0] = 0x00; xchg_data(buf,1,60,4); if(opts != NULL){ for(i = 0; i < strlen((char*)opts); i++ ){ if (*(opts+i) == ','){ //# .... conf_set 0x49 thys,-10.5 break; }else{ if( !isdigit(*(opts+i)) ){ //# Check the register string printf("The Register address must be an integer!\n"); print_help(); hih61xx_print_all(); exit (EXIT_FAILURE); } temp[i] = *(opts+i); temp[i+1] = '\0'; } } id = atoi((char*)temp); //# Convert register to number strncpy((char*)temp, (char*)opts+i+1, 255); //# Copy remain to temp // id = strtol ((char*)opts,&ptr,0); //all format allowed // ptr++; //one separator allowed switch (id) { case 0x00: printf("%f\n", calc_humidity(buf, atof((char*)temp)) ); break; case 0x01: printf("%f\n", calc_temp(buf, atof((char*)temp)) ); break; case 0x02: printf("0x%x\n", calc_status(buf)); break; default: print_help(); hih61xx_print_all(); exit(EXIT_FAILURE); } } }