Subversion Repositories svnkaklik

Rev

Rev 354 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 354 Rev 358
Line 31... Line 31...
31
/* Note that the documentation for the compass states its address as 0xC0.
31
/* Note that the documentation for the compass states its address as 0xC0.
32
 * However, this includes the low bit which specifies read or write.
32
 * However, this includes the low bit which specifies read or write.
33
 * Linux i2c does not include this bit in this address, so the actual
33
 * Linux i2c does not include this bit in this address, so the actual
34
 * address is 0xC0 shifted down, 0x60.
34
 * address is 0xC0 shifted down, 0x60.
35
 */
35
 */
36
#define CMPS03_ADDR 0x60
36
#define CMPS03_ADDR	0x60
-
 
37
#define I2C_SLAVE	0x0B	/* Change slave address	
37
 
38
 
38
/* The important registers on the compass. Internal/test registers omitted. */
39
/* The important registers on the compass. Internal/test registers omitted. */
39
#define CMPS03_SOFTWARE_REVISION 0x0
40
#define CMPS03_SOFTWARE_REVISION 0x0
40
#define CMPS03_BEARING_BYTE 0x1
41
#define CMPS03_BEARING_BYTE 0x1
41
#define CMPS03_BEARING_WORD_HIGH 0x2
42
#define CMPS03_BEARING_WORD_HIGH 0x2
Line 43... Line 44...
43
#define CMPS03_CALIBRATE_CMD 0xF
44
#define CMPS03_CALIBRATE_CMD 0xF
44
 
45
 
45
int main(int argc, char *argv[]) {
46
int main(int argc, char *argv[]) {
46
   char *end;
47
   char *end;
47
   int res,file;
48
   int res,file;
48
   int e1;
49
   int error;
49
   char filename[20] ;
50
   char filename[20] ;
50
   long funcs;
51
   long funcs;
51
    
52
    
52
   int heading_byte, heading_word_h, heading_word_l;
53
   int heading_byte, heading_word_h, heading_word_l;
53
   int bearing_long, bearing_degrees;
54
   int bearing_long, bearing_degrees;
54
   
55
   
55
   sprintf(filename,"/dev/i2c-%d");
56
   sprintf(filename,"/dev/i2c-0");
56
   if ((file = open(filename,O_RDWR)) < 0) {
57
   file = open(filename,O_RDWR);
-
 
58
   if (errno != 0)
-
 
59
   {
57
     e1 = errno;
60
     switch (errno)
-
 
61
     {
58
     if (e1 != ENOENT) {
62
     case EACCES:
59
       fprintf(stderr,"Error: Could not open file '%s' : %sn",
63
         fprintf(stderr,"Run as root? \n");
60
               filename,strerror(e1));
64
         break;
61
       if(e1 == EACCES)
65
     default:
62
         fprintf(stderr,"Run as root?n");
66
         fprintf(stderr,"Error: Could not open file '%s' : %s \n", filename, strerror(errno));
-
 
67
         break;
63
     }
68
     }
64
   } 
69
   } 
65
      
70
      
66
   if (ioctl(file,I2C_SLAVE,CMPS03_ADDR) < 0) {
71
   if (ioctl(file,I2C_SLAVE,CMPS03_ADDR) < 0) {
-
 
72
     switch (errno) 
-
 
73
     {
67
     if (errno == EBUSY) {
74
     case EBUSY:  
68
       printf("device is busyn");
75
         printf("device is busy \n");
-
 
76
         break;
-
 
77
 
69
     }
78
     default:
70
     printf("Got error: %d\n", errno);
79
         printf("Got error: %s \n", strerror(errno));
71
     exit(0);
80
         exit(0);
-
 
81
      }
72
   }
82
   }
73
   
83
   
74
   /* Get software revision number */
84
   /* Get software revision number */
75
   res = i2c_smbus_read_byte_data(file, CMPS03_SOFTWARE_REVISION);
85
   res = read(file, CMPS03_SOFTWARE_REVISION,1);
76
   if (res < 0) {
86
   if (res < 0) {
77
     printf("Cannot read software revision leveln");
87
     printf("Cannot read software revision level \n");
78
   } else {
88
   } else {
79
     printf("Software revision level: %02xn", res);
89
     printf("Software revision level: %02x \n", res);
80
   }
90
   }
81
   
91
   
82
   /* Loop and read from the compass. */
92
   /* Loop and read from the compass. */
83
   while (1) {
93
   while (1) {
84
     /* The heading byte is 0-255 for the 360 degrees. */
94
     /* The heading byte is 0-255 for the 360 degrees. */
85
     heading_byte = i2c_smbus_read_byte_data(file, CMPS03_BEARING_BYTE);
95
     heading_byte = read(file, CMPS03_BEARING_BYTE,1);
86
     if (heading_byte < 0) { printf("Error reading from compass."); exit(1);}
96
     if (heading_byte < 0) { printf("Error reading from compass. \n"); exit(1);}
87
     
97
     
88
     /* The high resolution heading is given in two registers, and is 10 * the 
98
     /* The high resolution heading is given in two registers, and is 10 * the 
89
      * heading in degrees, ie 359.9 degrees reads as 3599. */
99
      * heading in degrees, ie 359.9 degrees reads as 3599. */
90
     heading_word_h = i2c_smbus_read_byte_data(file, CMPS03_BEARING_WORD_HIGH);
100
     heading_word_h = read(file, CMPS03_BEARING_WORD_HIGH,1);
91
     if (heading_word_h < 0) { printf("Error reading from compass."); exit(1);}
101
     if (heading_word_h < 0) { printf("Error reading from compass. \n"); exit(1);}
92
     heading_word_l = i2c_smbus_read_byte_data(file, CMPS03_BEARING_WORD_LOW);
102
     heading_word_l = read(file, CMPS03_BEARING_WORD_LOW,1);
93
     if (heading_word_l < 0) { printf("Error reading from compass."); exit(1);}
103
     if (heading_word_l < 0) { printf("Error reading from compass. \n"); exit(1);}
94
     
104
     
95
     /* Combine the two bytes, and get the heading in degrees. */
105
     /* Combine the two bytes, and get the heading in degrees. */
96
     bearing_long = heading_word_h * 256 + heading_word_l;
106
     bearing_long = heading_word_h * 256 + heading_word_l;
97
     bearing_degrees = bearing_long / 10;
107
     bearing_degrees = bearing_long / 10;
98
     
108