diff options
author | Gergő J. Miklós | 2018-09-29 01:32:11 +0200 |
---|---|---|
committer | Gergő J. Miklós | 2018-09-29 01:32:11 +0200 |
commit | ab99f700bace05491919324259c9110c7f387af8 (patch) | |
tree | 699d197b204e482dfb56f8443e036ed6f740ace0 /src/main.c | |
download | i2sensors-ab99f700bace05491919324259c9110c7f387af8.tar.gz i2sensors-ab99f700bace05491919324259c9110c7f387af8.zip |
Existing code & initialcommit.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..c71c872 --- /dev/null +++ b/src/main.c @@ -0,0 +1,161 @@ + + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#include <fcntl.h> +#include <errno.h> +#include <unistd.h> + + +#include <linux/i2c-dev.h> +#include <sys/ioctl.h> + +#include "lm75.h" +#include "hih61xx.h" + +extern void lm75_list_all(void); +extern void lm75_read_all(const char *opts); +extern void lm75_read_one(const char *opts); +extern void hih61xx_list_all(void); +extern void hih61xx_read_all(const char *opts); +extern void hih61xx_read_one(const char *opts); + + +int file; +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" + "Aviable options: <op>\n" + " list_all -- list all measured parameters off device\n" + " read_all -- read all measured parameters from device\n" + " read_one -- read the selected parameters from device\n" + "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"); +} + +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)) + { + list_all = lm75_list_all; + read_all = lm75_read_all; + read_one = lm75_read_one; + } + + else if(!strcmp("hih61xx", dev_type)) + { + list_all = hih61xx_list_all; + read_all = hih61xx_read_all; + read_one = hih61xx_read_one; + } +} + + + +int main(int argc, char *argv[]) +{ + +// >> i2sensor <op> <bus> <dev_typ> <dev_addr> (<others>) +// >> i2sensor read_all 0 lm75 4f 1,-1.0;2,+0.2;3,+10;4,-2 +// read_all, list_all, +// ...read_all...<dev_addr>, <param_id,offs,param_id,offs> +// read_one...<dev_addr>, <param_id,offs> +// ...read_reg_hex... <dev_addr>, <reg_addr,b/w/dw> +// ...read_reg_dec... <dev_addr>, <reg_addr,b/w/dw> +// ...set_param... <dev_addr>, <param_id,value_dec> +// ...set_reg... <dev_addr>, <reg_addr,value_hex> + + + if(4 < argc && 7 > argc) //Minimal 5 argument passed + { + + char filename[32]; + int i; + preinit(argv[3]); + + for(i=0;i<strlen(argv[2]);i++) //Some test on bus address, for safety. + { + if(!isdigit(argv[2][i])) + { + printf("\n\tThe BUS Address must be an integer\n\n"); + print_help(); + 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"); + 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 (ioctl(file, I2C_SLAVE, addr) < 0) + { + printf("\n\tCan't open i2c DEVICE, errno: %d\n\n",errno); + print_help(); + exit (EXIT_FAILURE); + } + } + else + { + printf("\n\tThe DEVICE address must be a hexadecimal number\n\n"); + print_help(); + exit (EXIT_FAILURE); + } + + + + if(!strcmp("list_all",argv[1]) && list_all != NULL) // If list_all is selected. + { + list_all(); + } + else if(!strcmp("read_all",argv[1]) && read_all != NULL) + { + read_all(argv[5]); + } + else if(!strcmp("read_one",argv[1]) && read_one != NULL && argc == 5) + { + read_one(argv[5]); + } + else + { + // print_help(); + } + + close(file); + + } + else + { + print_help(); + } + return 0; +} |