From 812af485ea41fe162bb6fb888045318152bf2c66 Mon Sep 17 00:00:00 2001 From: Gergő J. Miklós Date: Wed, 15 May 2019 20:27:59 +0200 Subject: hih61xx refactored --- src/hih61xx.c | 165 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 106 insertions(+), 59 deletions(-) (limited to 'src/hih61xx.c') diff --git a/src/hih61xx.c b/src/hih61xx.c index 508fd3e..cf00c04 100644 --- a/src/hih61xx.c +++ b/src/hih61xx.c @@ -4,6 +4,8 @@ #include #include #include +#include //# isdigit() + #include "hih61xx.h" @@ -12,51 +14,71 @@ extern uint16 devicef; extern void bus_err(int ern); extern void print_help(void); -unsigned char buf[5]; +extern uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen); +unsigned char buf[32]; void hih61xx_print_all(void) { printf( - "00: Relative Humidity [%%] (reg: 0x00)\n" - "01: Temperature [°C] (reg: 0x00)\n" - "02: Status [hex] (reg: 0x00)\n" + "============ 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) {} +void hih61xx_conf_set(const uchar *opts) {} //Maybe later -void hih61xx_get_data(void) //# Get measurement data from device -{ - buf[0] = 0x00; + + +// 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); - } +// if(write(devicef, buf, 1) != 1){ //# Start the measurement +// bus_err(errno); +// } - usleep(60*1000); //# Wait 60ms for measurement +// usleep(60*1000); //# Wait 60ms for measurement - if(read(devicef, buf, 4) != 4){ //# Read the data - bus_err(errno); - } -} +// 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 - return ((buf[0] & 0xc0) >> 6); //# He ??? +// 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 - unsigned int raw = 0; +//float hih61xx_calc_humidity(float offset){ //# Measure +static float calc_humidity(uchar *buf, float offset){ //# Measure + uint16 raw = 0; float humidity; - if(hih61xx_read_status() != 0x01 ) - { + 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% } @@ -70,12 +92,13 @@ float hih61xx_calc_humidity(float offset){ //# Measure -float hih61xx_read_temp(float offset) +// float hih61xx_read_temp(float offset) +static float calc_temp(uchar *buf, float offset) { - unsigned int raw = 0; + uint16 raw = 0; float temp; - if(hih61xx_read_status() != 0x01 ) + 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] @@ -92,24 +115,24 @@ float hih61xx_read_temp(float offset) void hih61xx_read_all(const uchar *opts) { - hih61xx_get_data(); + //hih61xx_get_data(); + buf[0] = 0x00; + xchg_data(buf,1,60,4); - if(opts != NULL) - { - char *ptr; - printf("00:%f\n", hih61xx_calc_humidity(strtof((char*)opts, &ptr))); + // if(opts != NULL) + // { + // char *ptr; + // printf("00:%f\n", calc_humidity(buf, 0.0)); - ptr++; // One (,) character allowed for delimiter + // 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("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", hih61xx_read_status()); + printf("02:0x%x\n", calc_status(buf)); } @@ -117,27 +140,51 @@ void hih61xx_read_all(const uchar *opts) void hih61xx_read_one(const uchar *opts) { - int id; - char *ptr; + uint16 i,id; + uchar temp[256]; - 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(); - } + 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); + } + } } -- cgit v1.2.3