aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 7afe9c0e30c812c32f76f3fd462d6e7806879a9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#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 "deftypes.h"  
#include "lm75.h"
#include "hih61xx.h"
#include "hmc5883.h"

#define VERSION 1.22
 
// 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  <op> <bus> <dev_type> <dev_addr> (<options>)\n"
            "\n"
            "Aviable options: <op>\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 <bus> <device-type> <address> \n"
            "   i2sensors read_one <bus> <device-type> <address> <param-id,offset_for_param>\n"
            "   i2sensors conf_set <bus> <device-type> <address> <reg-id,word-of-bits>\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 %s\n\n",VERSION,__DATE__,__TIME__);
}

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)){
        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)) {                            
        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 <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 && argc < 10)                                           //Minimum 5 arguments should be passed.
    {
        
        char filename[32];
        int i;
        preinit(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                                                            //# No other option
        { 
            print_help();
        }
    
	close(devicef);
  
  }
  else
  {
    print_help();
  }
  return 0;
}