-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.cpp
More file actions
140 lines (120 loc) · 3.3 KB
/
i2c.cpp
File metadata and controls
140 lines (120 loc) · 3.3 KB
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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <iostream>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#define I2C_ADDR (0x3C >> 1)
int file_i2c;
int length;
unsigned char buffer[60] = {0};
void initI2C();
void compassInit();
void readCompass(short&, short&, short&);
int I2CRead(unsigned char, unsigned char&);
int I2CWrite(unsigned char, unsigned char);
/*int main()
{
short x, y, z;
openi2c();
compassInit();
while(1)
{
readCompass(x, y, z);
std::cout << "x: " << x/2048.0*360 << " y: " << y/2048.0*360 << " z: " << z/2048.0*360 << std::endl ;
usleep(1000000);
}
close(file_i2c);
return 0;
}*/
void openi2c() {
char *filename = (char*)"/dev/i2c-1";
if((file_i2c = open(filename, O_RDWR)) < 0)
{
//ERROR HANDLING: you can check errno to see what went wrong
printf("Failed to open the i2c bus\n");
return;
}
}
void compassInit()
{
I2CWrite(0x00, 0x90);
I2CWrite(0x02, 0x00);
usleep(100000);
}
void readCompass(short& x, short& y, short& z)
{
unsigned char value[6];
I2CRead(0x03, value[0]);
I2CRead(0x04, value[1]);
I2CRead(0x05, value[2]);
I2CRead(0x06, value[3]);
I2CRead(0x07, value[4]);
I2CRead(0x08, value[5]);
/*buffer[0] = 0x03;
length = 1;
if(write(file_i2c, buffer, length) != length) //write() returns the number of bytes actually written, if it doesn't match then an error occurred (e.g. no response from the device)
{
// ERROR HANDLING: i2c transaction failed
printf("3) Failed to write to the i2c bus.\n");
}
for(int i = 0; i < 6; i++)
{
usleep(1000);
length = 1; //<<< Number of bytes to read
if(read(file_i2c, buffer, length) != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device)
{
//ERROR HANDLING: i2c transaction failed
printf("Failed to read from the i2c bus.\n");
}
else
{
printf("Data read: %x\n", buffer[0]);
value[i] = buffer[0];
}
}*/
x = (value[0] << 8) | value[1];
z = (value[2] << 8) | value[3];
y = (value[4] << 8) | value[5];
}
int I2CRead(unsigned char registerAddress, unsigned char &data) {
unsigned char *inbuff, outbuff;
int retVal = -1;
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[2];
outbuff = registerAddress;
messages[0].addr = I2C_ADDR;
messages[0].flags= 0;
messages[0].len = sizeof(outbuff);
messages[0].buf = &outbuff;
inbuff = &data;
messages[1].addr = I2C_ADDR;
messages[1].flags = I2C_M_RD;
messages[1].len = sizeof(*inbuff);
messages[1].buf = inbuff;
packets.msgs = messages;
packets.nmsgs = 2;
retVal = ioctl(file_i2c, I2C_RDWR, &packets);
if(retVal < 0)
perror("Read from I2C Device failed");
return retVal;
}
int I2CWrite(unsigned char registerAddress, unsigned char data) {
unsigned char buff[2];
int retVal = -1;
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[1];
buff[0] = registerAddress;
buff[1] = data;
messages[0].addr = I2C_ADDR;
messages[0].flags = 0;
messages[0].len = sizeof(buff);
messages[0].buf = buff;
packets.msgs = messages;
packets.nmsgs = 1;
retVal = ioctl(file_i2c, I2C_RDWR, &packets);
if(retVal < 0)
perror("Write to I2C Device failed");
return retVal;
}