diff options
author | Gergő J. Miklós | 2019-05-11 03:27:13 +0200 |
---|---|---|
committer | Gergő J. Miklós | 2019-05-11 03:27:13 +0200 |
commit | 5cbc1bcd61ccd50b3c37f3f70083afca13c9a175 (patch) | |
tree | 83d72b3882b109c93407da8d45da43a43f9434d8 /src | |
parent | 7dd3691d171f10a76375796936b0ad215f07efa0 (diff) | |
download | i2sensors-5cbc1bcd61ccd50b3c37f3f70083afca13c9a175.tar.gz i2sensors-5cbc1bcd61ccd50b3c37f3f70083afca13c9a175.zip |
some debugging + read
Diffstat (limited to 'src')
-rw-r--r-- | src/hih61xx.c | 2 | ||||
-rw-r--r-- | src/lm75.c | 85 | ||||
-rw-r--r-- | src/lm75.h | 2 | ||||
-rw-r--r-- | src/main.c | 34 |
4 files changed, 68 insertions, 55 deletions
diff --git a/src/hih61xx.c b/src/hih61xx.c index b68b653..221f2b2 100644 --- a/src/hih61xx.c +++ b/src/hih61xx.c @@ -14,7 +14,7 @@ extern void print_help(void); unsigned char buf[5]; - + void hih61xx_list_all(void) { @@ -12,7 +12,7 @@ extern int file; extern void bus_err(int ern); extern void print_help(void); unsigned char buf[5]; - + void lm75_list_all(void) { @@ -26,15 +26,13 @@ void lm75_list_all(void) -void lm75_get_data(void) -{ - if(write(file, buf, 1) != 1) - { +void lm75_get_data(void){ + + if(write(file, buf, 1) != 1){ bus_err(errno); } - if(read(file, buf, 2) != 2) - { + if(read(file, buf, 2) != 2) { bus_err(errno); } } @@ -118,14 +116,14 @@ char lm75_read_conf(void) void lm75_read_all(const char *opts) { - if(opts != NULL) - { - printf("00:%f\n", lm75_read_temp(strtof(opts, NULL))); - } - else - { + // if(opts != NULL) + // { + // printf("00:%f\n", lm75_read_temp(strtof(opts, NULL))); + // } + // else + // { printf("00:%f\n", lm75_read_temp(0.0)); - } + // } printf("01:0x%x\n", lm75_read_conf()); printf("02:%f\n", lm75_read_tos()); @@ -136,28 +134,41 @@ void lm75_read_all(const char *opts) void lm75_read_one(const char *opts) { - int id; - char *ptr; - - id = strtol (opts,&ptr,0); //all format allowed - ptr++; //one separator allowed - - switch (id) - { - case 0x00: - printf("00:%f\n", lm75_read_temp(strtof(ptr, NULL))); - break; - case 0x01: - printf("01:0x%x\n", lm75_read_conf()); - break; - case 0x02: - printf("02:%f\n", lm75_read_tos()); - break; - case 0x03: - printf("03:%f\n", lm75_read_thys()); - break; - default: - print_help(); - } + int id,i; + //char *ptr; + char temp[256]; + + if(opts != NULL){ + for(i = 0; i < strlen(opts); i++){ + if (*(opts+i) == ','){ + break; + } + temp[i] = *(opts+i); + temp[i+1] = '\0'; + } + + id = atoi(temp); + strncpy(temp, opts+i+1, 255); + // id = strtol (opts,&ptr,0); //all format allowed + // ptr++; //one separator allowed + + switch (id) + { + case 0x00: + printf("00:%f\n", lm75_read_temp(atof(temp))); + break; + case 0x01: + printf("01:0x%x\n", lm75_read_conf()); + break; + case 0x02: + printf("02:%f\n", lm75_read_tos()); + break; + case 0x03: + printf("03:%f\n", lm75_read_thys()); + break; + default: + print_help(); + } + } } @@ -11,7 +11,7 @@ void lm75_list_all(void); void lm75_get_data(void); float lm75_read_temp(float offset); float lm75_read_tos(void); -float lm75_read_thys(void); +float lm75_read_thys(void); char lm75_read_conf(void); void lm75_read_all(const char *opts); void lm75_read_one(const char *opts); @@ -24,16 +24,17 @@ extern void hih61xx_read_all(const char *opts); extern void hih61xx_read_one(const char *opts); -int file; +int file; +char temp[256]; void (*list_all)(void) = NULL; void (*read_all)(const char *opts) = NULL; void (*read_one)(const char *opts) = NULL; void print_help(void) -{ +{ printf ("Usage:\n" - " i2sensor <op> <bus> <dev_type> <dev_addr> (<options>)\n" + " i2sensor <op> <bus> <dev_type> <dev_addr> (<options>)\n" "Aviable options: <op>\n" " list_all -- list all measured parameters off device\n" " read_all -- read all measured parameters from device\n" @@ -41,9 +42,11 @@ void print_help(void) "Exmples:\n" " i2sensor read_all <bus> <device> <address> <offset_for_param-01,offset_for_param-02,...>\n" " i2sensor read_one <bus> <device> <address> <param-id,offset_for_param-id>\n" - " i2sensor read_all 1 lm75 0x4f -1.35,\n" - " i2sensor read_one 1 lm75 0x4f 01,-1.35\n" - "" "\n"); + " i2sensor read_all 1 lm75 0x4f ---,\n" + " i2sensor read_one 1 lm75 0x4f 00,-1.35\n" + "" ""); + printf ("Build: %s %s\n\n",__DATE__,__TIME__); + } void bus_err(int ern) @@ -84,7 +87,7 @@ int main(int argc, char *argv[]) // ...set_reg... <dev_addr>, <reg_addr,value_hex> - if(4 < argc && 7 > argc) //Minimal 5 argument passed + if(4 < argc && argc < 10) //Minimal 5 argument passed { char filename[32]; @@ -100,22 +103,20 @@ int main(int argc, char *argv[]) exit (EXIT_FAILURE); } } - snprintf(filename, 31, "/dev/i2c-%s", argv[2]); //Open i2c bus file = open(filename, O_RDWR); if (file < 0) { - printf("\n\tCan't open i2c BUS, are you root?\n\n"); + printf("\n\tCan't open i2c BUS. Have you any permission?\n\n"); print_help(); exit (EXIT_FAILURE); } - if(argv[4][0]=='0' && argv[4][1]=='x') //? - { - - int addr = (int)strtol(argv[4], NULL, 0); //Open i2c device + if(argv[4][0]=='0' && argv[4][1]=='x'){ // Hexadecimal device address + + long addr = (int)strtol(argv[4], NULL, 0); //# strtol() is working with hex string if (ioctl(file, I2C_SLAVE, addr) < 0) { @@ -139,15 +140,16 @@ int main(int argc, char *argv[]) } else if(!strcmp("read_all",argv[1]) && read_all != NULL) { - read_all(argv[5]); + // read_all(argv[5]); + read_all(NULL); } - else if(!strcmp("read_one",argv[1]) && read_one != NULL && argc == 5) + else if(!strcmp("read_one",argv[1]) && read_one != NULL && argc > 5) { read_one(argv[5]); } else { - // print_help(); + print_help(); } close(file); |