upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
lsm303.h
1 /*
2  * Author: Brendan Le Foll<brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Code based on LSM303DLH sample by Jim Lindblom SparkFun Electronics
6  * and the CompensatedCompass.ino by Frankie Chu from SeedStudio
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #pragma once
28 
29 #include <string.h>
30 #include <mraa/i2c.h>
31 #include <math.h>
32 
33 namespace upm {
34 
35 /* LSM303 Address definitions */
36 #define LSM303_MAG 0x1E // assuming SA0 grounded
37 #define LSM303_ACC 0x18 // assuming SA0 grounded
38 
39 /* LSM303 Register definitions */
40 #define CTRL_REG1_A 0x20
41 #define CTRL_REG2_A 0x21
42 #define CTRL_REG3_A 0x22
43 #define CTRL_REG4_A 0x23
44 #define CTRL_REG5_A 0x24
45 
46 #define CRA_REG_M 0x00
47 #define CRB_REG_M 0x01
48 
49 #define MR_REG_M 0x02
50 #define OUT_X_H_M 0x03
51 
52 #define OUT_X_L_A 0x28
53 #define OUT_X_H_A 0x29
54 #define OUT_Y_L_A 0x2A
55 #define OUT_Y_H_A 0x2B
56 #define OUT_Z_L_A 0x2C
57 #define OUT_Z_H_A 0x2D
58 
59 #define X 0
60 #define Y 1
61 #define Z 2
62 
89 class LSM303 {
90  public:
98  LSM303 (int bus,
99  int addrMag=LSM303_MAG,
100  int addrAcc=LSM303_ACC,
101  int accScale=8);
102 
106  ~LSM303 ();
107 
113  float getHeading();
114 
118  mraa_result_t getCoordinates();
119 
124  mraa_result_t getAcceleration();
125 
129  int16_t* getRawCoorData();
130 
134  int16_t getCoorX();
135 
139  int16_t getCoorY();
140 
144  int16_t getCoorZ();
145 
149  int16_t* getRawAccelData();
150 
154  int16_t getAccelX();
155 
159  int16_t getAccelY();
160 
164  int16_t getAccelZ();
165 
166  private:
167  int readThenWrite(uint8_t reg);
168  mraa_result_t setRegisterSafe(uint8_t slave, uint8_t sregister, uint8_t data);
169 
170  mraa_i2c_context m_i2c;
171  int m_addrMag;
172  int m_addrAcc;
173  uint8_t buf[6];
174  int16_t coor[3];
175  int16_t accel[3];
176 };
177 
178 }
int16_t getCoorZ()
Definition: lsm303.cxx:156
int16_t * getRawCoorData()
Definition: lsm303.cxx:95
int16_t getAccelZ()
Definition: lsm303.cxx:113
LSM303(int bus, int addrMag=LSM303_MAG, int addrAcc=LSM303_ACC, int accScale=8)
Definition: lsm303.cxx:36
C++ API for LSM303.
Definition: lsm303.h:89
int16_t * getRawAccelData()
Definition: lsm303.cxx:89
~LSM303()
Definition: lsm303.cxx:69
mraa_result_t getCoordinates()
Definition: lsm303.cxx:119
float getHeading()
Definition: lsm303.cxx:74
Definition: a110x.h:29
int16_t getAccelY()
Definition: lsm303.cxx:107
int16_t getCoorY()
Definition: lsm303.cxx:151
int16_t getAccelX()
Definition: lsm303.cxx:101
int16_t getCoorX()
Definition: lsm303.cxx:146
mraa_result_t getAcceleration()
Definition: lsm303.cxx:172