-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathmraa_i2c.cpp
52 lines (40 loc) · 937 Bytes
/
mraa_i2c.cpp
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
/*
* i2c example in c++ using mraa
*
* Author: Manivannan Sadhasivam <[email protected]>
*
* Usage: Checks for MPU6050 sensor at I2C bus 0
*
* Compilation: g++ mraa_i2c.cpp -o bin -lmraa
*
*/
/* standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* mraa header */
#include "mraa/i2c.hpp"
/* i2c bus */
#define I2C_BUS 0
/* sensor address */
#define MPU6050_ADDR 0x68
/* WHO_AM_I register address */
#define MPU6050_REG_WHO_AM_I 0x75
using namespace mraa;
int main(void)
{
uint8_t data;
/* initialize i2c bus */
I2c i2c(I2C_BUS);
/* set slave address */
i2c.address(MPU6050_ADDR);
/* read WHO_AM_I register and compare its value */
data = i2c.readReg(MPU6050_REG_WHO_AM_I);
if (data & 0x7E != MPU6050_ADDR) {
fprintf(stderr, "Device not found!\n");
return -1;
}
fprintf(stdout, "MPU6050 found at: 0x%02x\n", data & 0x7E);
return 0;
}