diff options
-rw-r--r-- | src/lm75.c | 58 | ||||
-rw-r--r-- | src/lm75.h | 10 |
2 files changed, 52 insertions, 16 deletions
@@ -29,14 +29,14 @@ void lm75_print_all(void) } -uchar *read_data( uchar *buf ){ //# [buf] = 32 byte +uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen){ //# [buf] = 32 byte - if(write(devicef, buf, 1) != 1){ //# write one byte to device + if(write(devicef, buf, wrlen) != wrlen){ //# write one byte to device bus_err(errno); } - usleep(10*1000); //# Wait 10ms for reading - - if(read(devicef, buf, 2) != 2) { //# read the result + usleep(waitlen*1000); //# Wait 10ms for reading + + if(read(devicef, buf, rdlen) != rdlen) { //# read the result bus_err(errno); } return buf; @@ -49,7 +49,7 @@ float calculate_temp(float offset){ // Calculate temperature float temp; buf[0] = 0x00; //# Measurement in the REG[0] - read_data(buf); //# [buf] = 32 byte + xchg_data(buf,1,0,2); //# Write: 1byte addr, Read: 2byte data, Wait:0, [buf] = 32 byte rawtemp = (buf[0]*256 + buf[1]) >> 5; // (buf[0] << 8), and (>> 5), Device with 11bit data, is also supported @@ -66,7 +66,7 @@ float calculate_temp(float offset){ // Calculate temperature float read_tos(void){ // Over-Temperature Shutdown register int16 rawtemp = 0; //# Int16 default signed buf[0] = 0x02; //# Tos = REG[2] - read_data(buf); + xchg_data(buf,1,0,2); rawtemp = buf[0]*256 + buf[1]; rawtemp = (rawtemp) >> 7; //# 9bit data @@ -82,7 +82,7 @@ float read_tos(void){ // Over-Temperature Shutdown reg float read_thys(void){ // Over-Temp Hysteresis Register int16 rawtemp = 0; //# signed buf[0] = 0x03; - read_data(buf); //# read from REG[3] + xchg_data(buf,1,0,2); //# read from REG[3] rawtemp = buf[0]*256 + buf[1]; rawtemp = (rawtemp) >> 7; //# 9bit data @@ -95,7 +95,7 @@ float read_thys(void){ // Over-Temp Hysteresis Register uchar read_conf(void){ //Configuration register buf[0] = 0x01; - read_data(buf); + xchg_data(buf,1,0,2); return buf[0]; } @@ -156,6 +156,42 @@ void lm75_read_one(const uchar *opts){ // Prints the selected r } -void lm75_conf_set(const uchar *opts){ - // printf(""); + +void lm75_conf_set(const uchar *opts){ // Prints the selected register's data + uint16 id,i,j; + uchar temp[256]; + + if(opts != NULL){ + for(i = 0; i < strlen((char*)opts); i++, j++){ + if (*(opts+i) == ','){ + break; + } + temp[i] = *(opts+i); + temp[i+1] = '\0'; + } + + id = atoi((char*)temp); + strncpy((char*)temp, (char*)opts+i+1, 255); + // id = strtol (opts,&ptr,0); //all format allowed + // ptr++; //one separator allowed + + switch (id) + { + case 0x00: + printf("%f\n", calculate_temp(atof((char*)temp))); + break; + case 0x01: + printf("0x%x\n", read_conf()); + break; + case 0x02: + printf("%f\n", read_tos()); + break; + case 0x03: + printf("%f\n", read_thys()); + break; + default: + print_help(); + } + + } }
\ No newline at end of file @@ -14,11 +14,11 @@ void lm75_read_all(const uchar *opts); void lm75_read_one(const uchar *opts); void lm75_conf_set(const uchar *opts); -//unsigned char *read_data(unsigned char *buf); -float calculate_temp(float offset); -float read_tos(void); -float read_thys(void); -uchar read_conf(void); +//unsigned char *xchg_data(unsigned char *buf); +// float calculate_temp(float offset); +// float read_tos(void); +// float read_thys(void); +// uchar read_conf(void); //__uint8_t //__uint16_t |