Subversion Repositories svnkaklik

Rev

Rev 354 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
354 kaklik 1
/* compass.c
2
 *
3
 * Software to read from the CMPS03 magnetic compass. 
4
 *
5
 * By Josh Marshall, joshua dot marshall at studentmail dot newcastle dot edu dot au
6
 * October 15, 2004.
7
 *
8
 * Adapted from code for the SP03 speech synthesiser 
9
 * By Bram Stolk, b.stolk at chello.nl
10
 *
11
 * Bram Stolk's notes:
12
 * In linux, you can do I2C stuff in user-space, if you enable
13
 * the device-interface to i2c. You can use plain read() and 
14
 * write() calls on your /dev/i2c-0 file.
15
 *
16
 * NOTE: Check if you have an i2c adapter active by doing:
17
 * cat /proc/bus/i2c
18
 * If this comes up empty, then you have no adapters. If the
19
 * file does not exist at all, you probably lack i2c kernel
20
 * support alltogether.
21
 */
22
 
23
#include <errno.h>
24
#include <string.h>
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <unistd.h>
28
#include <fcntl.h>
29
#include <linux/i2c-dev.h>
30
 
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.
33
 * Linux i2c does not include this bit in this address, so the actual
34
 * address is 0xC0 shifted down, 0x60.
35
 */
358 kaklik 36
#define CMPS03_ADDR	0x60
37
#define I2C_SLAVE	0x0B	/* Change slave address	
354 kaklik 38
 
39
/* The important registers on the compass. Internal/test registers omitted. */
40
#define CMPS03_SOFTWARE_REVISION 0x0
41
#define CMPS03_BEARING_BYTE 0x1
42
#define CMPS03_BEARING_WORD_HIGH 0x2
43
#define CMPS03_BEARING_WORD_LOW 0x3
44
#define CMPS03_CALIBRATE_CMD 0xF
45
 
46
int main(int argc, char *argv[]) {
47
   char *end;
48
   int res,file;
358 kaklik 49
   int error;
354 kaklik 50
   char filename[20] ;
51
   long funcs;
52
 
53
   int heading_byte, heading_word_h, heading_word_l;
54
   int bearing_long, bearing_degrees;
55
 
358 kaklik 56
   sprintf(filename,"/dev/i2c-0");
57
   file = open(filename,O_RDWR);
58
   if (errno != 0)
59
   {
60
     switch (errno)
61
     {
62
     case EACCES:
63
         fprintf(stderr,"Run as root? \n");
64
         break;
65
     default:
66
         fprintf(stderr,"Error: Could not open file '%s' : %s \n", filename, strerror(errno));
67
         break;
354 kaklik 68
     }
69
   } 
70
 
71
   if (ioctl(file,I2C_SLAVE,CMPS03_ADDR) < 0) {
358 kaklik 72
     switch (errno) 
73
     {
74
     case EBUSY:  
75
         printf("device is busy \n");
76
         break;
77
 
78
     default:
79
         printf("Got error: %s \n", strerror(errno));
80
         exit(0);
81
      }
354 kaklik 82
   }
83
 
84
   /* Get software revision number */
358 kaklik 85
   res = read(file, CMPS03_SOFTWARE_REVISION,1);
354 kaklik 86
   if (res < 0) {
358 kaklik 87
     printf("Cannot read software revision level \n");
354 kaklik 88
   } else {
358 kaklik 89
     printf("Software revision level: %02x \n", res);
354 kaklik 90
   }
91
 
92
   /* Loop and read from the compass. */
93
   while (1) {
94
     /* The heading byte is 0-255 for the 360 degrees. */
358 kaklik 95
     heading_byte = read(file, CMPS03_BEARING_BYTE,1);
96
     if (heading_byte < 0) { printf("Error reading from compass. \n"); exit(1);}
354 kaklik 97
 
98
     /* The high resolution heading is given in two registers, and is 10 * the 
99
      * heading in degrees, ie 359.9 degrees reads as 3599. */
358 kaklik 100
     heading_word_h = read(file, CMPS03_BEARING_WORD_HIGH,1);
101
     if (heading_word_h < 0) { printf("Error reading from compass. \n"); exit(1);}
102
     heading_word_l = read(file, CMPS03_BEARING_WORD_LOW,1);
103
     if (heading_word_l < 0) { printf("Error reading from compass. \n"); exit(1);}
354 kaklik 104
 
105
     /* Combine the two bytes, and get the heading in degrees. */
106
     bearing_long = heading_word_h * 256 + heading_word_l;
107
     bearing_degrees = bearing_long / 10;
108
 
109
     printf("Bearing: %d \n", bearing_degrees);
110
 
111
     /* Wait for a while. */
112
     usleep(200000);
113
   }
114
}