33,7 → 33,8 |
* Linux i2c does not include this bit in this address, so the actual |
* address is 0xC0 shifted down, 0x60. |
*/ |
#define CMPS03_ADDR 0x60 |
#define CMPS03_ADDR 0x60 |
#define I2C_SLAVE 0x0B /* Change slave address |
|
/* The important registers on the compass. Internal/test registers omitted. */ |
#define CMPS03_SOFTWARE_REVISION 0x0 |
45,7 → 46,7 |
int main(int argc, char *argv[]) { |
char *end; |
int res,file; |
int e1; |
int error; |
char filename[20] ; |
long funcs; |
|
52,45 → 53,54 |
int heading_byte, heading_word_h, heading_word_l; |
int bearing_long, bearing_degrees; |
|
sprintf(filename,"/dev/i2c-%d"); |
if ((file = open(filename,O_RDWR)) < 0) { |
e1 = errno; |
if (e1 != ENOENT) { |
fprintf(stderr,"Error: Could not open file '%s' : %sn", |
filename,strerror(e1)); |
if(e1 == EACCES) |
fprintf(stderr,"Run as root?n"); |
sprintf(filename,"/dev/i2c-0"); |
file = open(filename,O_RDWR); |
if (errno != 0) |
{ |
switch (errno) |
{ |
case EACCES: |
fprintf(stderr,"Run as root? \n"); |
break; |
default: |
fprintf(stderr,"Error: Could not open file '%s' : %s \n", filename, strerror(errno)); |
break; |
} |
} |
|
if (ioctl(file,I2C_SLAVE,CMPS03_ADDR) < 0) { |
if (errno == EBUSY) { |
printf("device is busyn"); |
} |
printf("Got error: %d\n", errno); |
exit(0); |
switch (errno) |
{ |
case EBUSY: |
printf("device is busy \n"); |
break; |
|
default: |
printf("Got error: %s \n", strerror(errno)); |
exit(0); |
} |
} |
|
/* Get software revision number */ |
res = i2c_smbus_read_byte_data(file, CMPS03_SOFTWARE_REVISION); |
res = read(file, CMPS03_SOFTWARE_REVISION,1); |
if (res < 0) { |
printf("Cannot read software revision leveln"); |
printf("Cannot read software revision level \n"); |
} else { |
printf("Software revision level: %02xn", res); |
printf("Software revision level: %02x \n", res); |
} |
|
/* Loop and read from the compass. */ |
while (1) { |
/* The heading byte is 0-255 for the 360 degrees. */ |
heading_byte = i2c_smbus_read_byte_data(file, CMPS03_BEARING_BYTE); |
if (heading_byte < 0) { printf("Error reading from compass."); exit(1);} |
heading_byte = read(file, CMPS03_BEARING_BYTE,1); |
if (heading_byte < 0) { printf("Error reading from compass. \n"); exit(1);} |
|
/* The high resolution heading is given in two registers, and is 10 * the |
* heading in degrees, ie 359.9 degrees reads as 3599. */ |
heading_word_h = i2c_smbus_read_byte_data(file, CMPS03_BEARING_WORD_HIGH); |
if (heading_word_h < 0) { printf("Error reading from compass."); exit(1);} |
heading_word_l = i2c_smbus_read_byte_data(file, CMPS03_BEARING_WORD_LOW); |
if (heading_word_l < 0) { printf("Error reading from compass."); exit(1);} |
heading_word_h = read(file, CMPS03_BEARING_WORD_HIGH,1); |
if (heading_word_h < 0) { printf("Error reading from compass. \n"); exit(1);} |
heading_word_l = read(file, CMPS03_BEARING_WORD_LOW,1); |
if (heading_word_l < 0) { printf("Error reading from compass. \n"); exit(1);} |
|
/* Combine the two bytes, and get the heading in degrees. */ |
bearing_long = heading_word_h * 256 + heading_word_l; |