aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGergő J. Miklós2019-05-15 20:27:59 +0200
committerGergő J. Miklós2019-05-15 20:27:59 +0200
commit812af485ea41fe162bb6fb888045318152bf2c66 (patch)
tree0ca7966bb8ea819e87d80a9282a20c90c6f17c15
parentd150a9f31a81a7dc02f2359529a7351c743a0067 (diff)
downloadi2sensors-812af485ea41fe162bb6fb888045318152bf2c66.tar.gz
i2sensors-812af485ea41fe162bb6fb888045318152bf2c66.zip
hih61xx refactoredv1.001
-rw-r--r--src/hih61xx.c165
-rw-r--r--src/hih61xx.h28
-rw-r--r--src/lm75.c40
-rw-r--r--src/lm75.h28
-rw-r--r--src/main.c23
5 files changed, 175 insertions, 109 deletions
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 <string.h>
#include <unistd.h>
#include <errno.h>
+#include <ctype.h> //# 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);
+ }
+ }
}
diff --git a/src/hih61xx.h b/src/hih61xx.h
index 8675562..44effd6 100644
--- a/src/hih61xx.h
+++ b/src/hih61xx.h
@@ -11,12 +11,28 @@ void hih61xx_list_all(void);
void hih61xx_read_all(const char *opts);
*/
void hih61xx_print_all(void);
-void hih61xx_get_data(void);
-void hih61xx_conf_set(const uchar *opts);
-unsigned char hih61xx_read_status(void);
-float hih61xx_calc_humidity(float offset);
-float hih61xx_read_temp(float offset);
void hih61xx_read_all(const uchar *opts);
void hih61xx_read_one(const uchar *opts);
+void hih61xx_conf_set(const uchar *opts);
+
+
+// Local functions
+
+// uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen);
+// static float calc_temp(uchar *buf, float offset);
+// static uchar calc_status(uchar *buf);
+// static float calc_humidity(uchar *buf, float offset);
+
+
+
+
+
+// void hih61xx_get_data(void);
+// unsigned char hih61xx_read_status(void);
+// float hih61xx_calc_humidity(float offset);
+// float hih61xx_read_temp(float offset);
+
+
+
+#endif
-#endif \ No newline at end of file
diff --git a/src/lm75.c b/src/lm75.c
index c1b67f0..2e14738 100644
--- a/src/lm75.c
+++ b/src/lm75.c
@@ -15,6 +15,7 @@
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);
uchar buf[32];
@@ -40,21 +41,21 @@ void lm75_print_all(void)
}
-uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen){ //# [buf] = 32 byte
+// 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(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;
-}
+// if(read(devicef, buf, rdlen) != rdlen) { //# read the result
+// bus_err(errno);
+// }
+// return buf;
+// }
-float calculate_temp(float offset){ // Calculate temperature
+static float calc_temp(float offset){ // Calculate temperature
uint16 rawtemp = 0; //# Signed by default
float temp;
@@ -73,7 +74,7 @@ float calculate_temp(float offset){ // Calculate temperature
}
-float read_tos(void){ // Over-Temperature Shutdown register
+static float read_tos(void){ // Over-Temperature Shutdown register
uint16 rawtemp = 0; //# Int16 default signed
buf[0] = 0x03; //# Tos = REG[2]
xchg_data(buf,1,0,2);
@@ -91,7 +92,7 @@ float read_tos(void){ // Over-Temperature Shutdown reg
}
-float read_thys(void){ // Over-Temp Hysteresis Register
+static float read_thys(void){ // Over-Temp Hysteresis Register
uint16 rawtemp = 0; //# signed
buf[0] = 0x02;
xchg_data(buf,1,0,2); //# read from REG[3]
@@ -108,7 +109,7 @@ float read_thys(void){ // Over-Temp Hysteresis Register
}
-uchar read_conf(void){ //Configuration register
+static uchar read_conf(void){ //Configuration register
buf[0] = 0x01;
xchg_data(buf,1,0,1);
return buf[0];
@@ -118,9 +119,9 @@ uchar read_conf(void){ //Configuration register
void lm75_read_all(const uchar *opts){ // Print out whole device's data
// if(opts != NULL)
// {
- // printf("00:%f\n", calculate_temp(strtof(opts, NULL)));
+ // printf("00:%f\n", calc_temp(strtof(opts, NULL)));
// } else {
- printf("00:%f\n", calculate_temp(0.0));
+ printf("00:%f\n", calc_temp(0.0));
// }
printf("01:0x%x\n", read_conf());
printf("02:%f\n", read_thys());
@@ -143,6 +144,7 @@ void lm75_read_one(const uchar *opts){ // Prints the selected r
if( !isdigit(*(opts+i)) ){ //# Check the register string
printf("The Register address must be an integer!\n");
print_help();
+ lm75_print_all();
exit (EXIT_FAILURE);
}
temp[i] = *(opts+i); //# copy register string
@@ -156,7 +158,7 @@ void lm75_read_one(const uchar *opts){ // Prints the selected r
switch (id) //# Which register is selected?
{
case 0x00:
- printf("%f\n", calculate_temp(atof((char*)temp))); //# with the offset
+ printf("%f\n", calc_temp(atof((char*)temp))); //# with the offset
break;
case 0x01:
printf("0x%x\n", read_conf());
@@ -176,11 +178,11 @@ void lm75_read_one(const uchar *opts){ // Prints the selected r
void lm75_conf_set(const uchar *opts){ // Prints the selected register's data
- uint16 i,j;
+ uint16 i;
uchar temp[256];
if(opts != NULL){
- for(i = 0; i < strlen((char*)opts); i++, j++){
+ for(i = 0; i < strlen((char*)opts); i++){
if (*(opts+i) == ','){ //# .... conf_set 0x49 thys,-10.5
break;
}
diff --git a/src/lm75.h b/src/lm75.h
index 0c8b6a8..60cba38 100644
--- a/src/lm75.h
+++ b/src/lm75.h
@@ -5,32 +5,18 @@
#include "deftypes.h"
// extern int file;
-//
-// void lm75_list_all(void);
-// void lm75_read_all(const char *opts);
-//
+
void lm75_print_all(void); //# Global funcions, required by main()
void lm75_read_all(const uchar *opts);
void lm75_read_one(const uchar *opts);
void lm75_conf_set(const uchar *opts);
-//unsigned char *xchg_data(unsigned char *buf);
-// float calculate_temp(float offset);
-// float read_tos(void);
-// float read_thys(void);
-// uchar read_conf(void);
+// //Local functions
+// uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen);
+// static float calc_temp(float offset);
+// static float read_tos(void);
+// static float read_thys(void);
+// static uchar read_conf(void);
-//__uint8_t
-//__uint16_t
-//__uint32_t
-//__uint64_t
-//__uint128_t
-//__int8_t
-// __int16_t
-// __int32_t
-// __int64_t
-// __int128_t
-// __u_char
-//__u_long
#endif \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 6012a03..4623919 100644
--- a/src/main.c
+++ b/src/main.c
@@ -63,16 +63,31 @@ void bus_err(int ern){
printf("I2C communication(rd) error. errno: %d\n",errno);
}
-void preinit(const char *dev_type){
- if(!strcmp("lm75", dev_type)){
+uchar *xchg_data (uchar *buf, uint16 wrlen, uint16 waitlen, uint16 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;
+}
+
+
+void preinit(const uchar *dev_type){
+
+ if(!strcmp("lm75", (char *)dev_type)){
print_all = lm75_print_all;
read_all = lm75_read_all;
read_one = lm75_read_one;
conf_set = lm75_conf_set;
}
- else if(!strcmp("hih61xx", dev_type)) {
+ else if(!strcmp("hih61xx", (char *)dev_type)) {
print_all = hih61xx_print_all;
read_all = hih61xx_read_all;
read_one = hih61xx_read_one;
@@ -108,7 +123,7 @@ int main(int argc, char *argv[])
char filename[32];
int i;
- preinit(argv[3]);
+ preinit((uchar *)argv[3]);
for( i = 0; i < strlen(argv[2]); i++){ //# Test on bus address, for error detection.
if(!isdigit(argv[2][i])){