#include #include #include #include #include #include #include #include #include #include #include "deftypes.h" #include "lm75.h" #include "hih61xx.h" #include "hmc5883.h" #define VERSION 1.27 #ifndef _BUILD_TIME_ #define _BUILD_TIME_ __DATE__" "__TIME__ #endif // extern void lm75_print_help(void); // extern void lm75_read_all(const char *opts); // extern void lm75_read_one(const char *opts); // extern void hih61xx_print_help(void); // extern void hih61xx_read_all(const char *opts); // extern void hih61xx_read_one(const char *opts); uint16 devicef; //# Bus-Device file uchar temp[256]; void (*print_all)(void) = NULL; void (*read_all)(const uchar *opts) = NULL; void (*read_one)(const uchar *opts) = NULL; void (*conf_set)(const uchar *opts) = NULL; void print_help(void) { printf ("\n" " i2sensors ()\n" "\n" "Aviable options: \n" " print_all -- list all available measurement parameters of the device\n" " read_all -- read all measured parameters from device\n" " read_one -- read the selected parameters from device\n" " read_repeat -- read measurements continously\n" " conf_set -- set the configuration register\n" "\n" "Usage:\n" " i2sensors read_all
\n" " i2sensors read_one
\n" " i2sensors conf_set
\n" "\n" "Examples:\n" " i2sensors read_all 1 lm75 0x4f \n" " i2sensors read_one 1 lm75 0x4f 00,-1.35\n" "\n" ); printf ("Version: v%1.2f, build: [%s] (gcc %s) \n\n",VERSION,_BUILD_TIME_,__VERSION__); } void bus_err(int ern){ //printf("I2C communication(rd) error. errno: %d\n",errno); fprintf (stderr, "Internal error %d at %s, line %d. (function: %s/%s)\n", ern, __FILE__, __LINE__, __func__, __FUNCTION__); } void bus_errr(int ern){ //printf("I2C communication(rd) error. errno: %d\n",errno); fprintf (stderr, "Internal error %d at %s, line %d. (function: %s/%s)\n", errno, __FILE__, __LINE__, __func__, __FUNCTION__); } uchar *xchg_data (uchar *buf, uint16 wrlen, uint16 waitlen, uint16 rdlen){ //# [buf] = 32 byte if(write(devicef, buf, 1) != 1){ //# write one byte to device perror("I2C communication(wrr) error."); fprintf (stderr, "I2C error (WR) %d at %s, line %d. (function: %s/%s)\n", errno, __FILE__, __LINE__, __func__, __FUNCTION__); bus_err(errno); bus_errr(0); } usleep(waitlen*1000); //# Wait 10ms for reading if(read(devicef, buf, rdlen) != rdlen) { //# read the result perror("I2C communication(rd) error."); fprintf (stderr, "I2C error (WR) %d at %s, line %d. (function: %s/%s)\n", errno, __FILE__, __LINE__, __func__, __FUNCTION__); bus_err(errno); bus_errr(0); } 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", (char *)dev_type)) { print_all = hih61xx_print_all; read_all = hih61xx_read_all; read_one = hih61xx_read_one; conf_set = hih61xx_conf_set; } // else if(!strcmp("hmc5883", dev_type)) { // print_all = hmc5883_print_all; // read_all = hmc5883_read_all; // read_one = hmc5883_read_one; // conf_set = hmc5883_conf_set; // } } int main(int argc, char *argv[]) { // >> i2sensor () // >> i2sensor read_all 0 lm75 4f 1,-1.0;2,+0.2;3,+10;4,-2 // read_all, list_all, // ...read_all..., // read_one..., // ...read_reg_hex... , // ...read_reg_dec... , // ...set_param... , // ...set_reg... , if(4 < argc && argc < 10) //Minimum 5 arguments should be passed. { char filename[32]; int i; preinit((uchar *)argv[3]); for( i = 0; i < strlen(argv[2]); i++){ //# Test on bus address, for error detection. 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]); devicef = open(filename, O_RDWR); //# Open the i2c bus if (devicef < 0){ 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'){ //# Hexadecimal representetion of device address long addr = (int)strtol(argv[4], NULL, 0); //# strtol() is working with hex-string correctly if (ioctl(devicef, I2C_SLAVE, addr) < 0){ //# Seeking/Opening the device on the Bus 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("print_all",argv[1]) && print_all != NULL){ //# Gets the device's help message. print_all(); } else if(!strcmp("read_all",argv[1]) && read_all != NULL){ //# Pulls all readable registers data from device // read_all(argv[5]); read_all(NULL); } else if(!strcmp("read_one",argv[1]) && read_one != NULL && argc > 5) //# Prints out the selected register { read_one((uchar*)argv[5]); } else if(!strcmp("conf_set",argv[1]) && conf_set != NULL && argc > 5) //# Prints out the selected register { conf_set((uchar*)argv[5]); } else //# No other option { print_help(); } close(devicef); } else { print_help(); } return 0; }