-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccelerometer.c
113 lines (97 loc) · 2.55 KB
/
Accelerometer.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include "Accelerometer.h"
static int i2cFile;
static void writeToFile(char *fileName, char *content)
{
FILE *file = fopen(fileName, "w");
if(file == NULL)
{
printf("ERROR: Unable to write \"%s\" to the file \"%s\".\n", content, fileName);
exit(1);
}
fprintf(file, "%s", content);
fclose(file);
}
void Accelerometer_init()
{
writeToFile("/sys/devices/platform/bone_capemgr/slots", "BB-I2C1"); //Enable bus
unsigned char writeBuffer[2];
int result;
i2cFile = open("/dev/i2c-1", O_RDWR);
if(i2cFile < 0)
{
printf("Unable to open /dev/i2c-1 for read/write.\n");
exit(1);
}
result = ioctl(i2cFile, I2C_SLAVE, 0x1C);
if(result < 0)
{
printf("Cannot set I2C device to slave address.\n");
exit(1);
}
writeBuffer[0] = 0x2A;
writeBuffer[1] = 0x01;
result = write(i2cFile, writeBuffer, sizeof(writeBuffer));
if(result != sizeof(writeBuffer))
{
printf("Cannot activate accelerometer.\n");
exit(1);
}
}
void Accelerometer_cleanup()
{
unsigned char writeBuffer[2];
int result;
writeBuffer[0] = 0x2A;
writeBuffer[1] = 0x00;
result = write(i2cFile, writeBuffer, sizeof(writeBuffer));
if(result != sizeof(writeBuffer))
{
printf("Cannot deactivate accelerometer.\n");
}
close(i2cFile);
}
static int castBytesToInt(unsigned char byte1, unsigned char byte2)
{
//Organizes the first byte into the least significant bits 5-12, and the 4 most
//significant bits of the second byte into the 4 least significant bits
int result = (((int)byte1) << 4) + (((int)byte2) >> 4);
if(byte1 & 128) //Checks most significant bit (The negative indicator)
{
//-4096 is all 1s except for the last 12 bits. This is used to convert a 12 bit
//negative 2's complement number into a n-bit 2's complement number
return result | -4096;
}
else
{
return result;
}
}
Acceleration Accelerometer_getAcceleration()
{
unsigned char readAddress = 0x00;
int result = write(i2cFile, &readAddress, 1);
if(result != 1)
{
printf("Cannot write the accelerometer address to read. %d\n", result);
exit(1);
}
unsigned char value[7];
result = read(i2cFile, &value, sizeof(value));
if(result != sizeof(value))
{
printf("Cannot read acceleration.\n");
exit(1);
}
//printf("Reading address %d: %d\n", (int)address, (int)value);
Acceleration acceleration;
acceleration.x = castBytesToInt(value[1], value[2]);
acceleration.y = castBytesToInt(value[3], value[4]);
acceleration.z = castBytesToInt(value[5], value[6]);
return acceleration;
}