summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/lsm303/lsm303.h
blob: 767d547faaf6b615f9fa068d90a338bc814273d5 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Author: Brendan Le Foll<brendan.le.foll@intel.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * Code based on LSM303DLH sample by Jim Lindblom SparkFun Electronics
 * and the CompensatedCompass.ino by Frankie Chu from SeedStudio
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#pragma once

#include <string.h>
#include <mraa/i2c.hpp>
#include <math.h>

namespace upm {

/* LSM303 Address definitions */
#define LSM303_MAG 0x1E
#define LSM303_ACC 0x19

/* LSM303 Register definitions */
#define CTRL_REG1_A 0x20
#define CTRL_REG2_A 0x21
#define CTRL_REG3_A 0x22
#define CTRL_REG4_A 0x23
#define CTRL_REG5_A 0x24

#define CRA_REG_M 0x00
#define CRB_REG_M 0x01

#define MR_REG_M 0x02
#define OUT_X_H_M 0x03

#define OUT_X_L_A 0x28
#define OUT_X_H_A 0x29
#define OUT_Y_L_A 0x2A
#define OUT_Y_H_A 0x2B
#define OUT_Z_L_A 0x2C
#define OUT_Z_H_A 0x2D

#define X 0
#define Y 1
#define Z 2

/**
 * @brief LSM303 Accelerometer/Compass library
 * @defgroup lsm303 libupm-lsm303
 * @ingroup seeed adafruit i2c accelerometer compass
 */

/**
 * @library lsm303
 * @sensor lsm303
 * @comname LSM303 Accelerometer & Compass
 * @altname Grove 6-Axis Accelerometer & Compass
 * @type accelerometer compass
 * @man seeed adafruit
 * @web http://www.seeedstudio.com/wiki/Grove_-_6-Axis_Accelerometer%26Compass
 * @con i2c
 *
 * @brief API for the LSM303 Accelerometer & Compass
 *
 * This module defines the LSM303DLH 3-axis magnetometer/3-axis accelerometer.
 * This module was tested with the Seeed Studio* Grove 6-Axis Accelerometer & Compass
 * module used over I2C. The magnometer and acceleromter are accessed
 * at two seperate I2C addresses.
 *
 * @image html lsm303.jpeg
 * @snippet lsm303.cxx Interesting
 */
class LSM303 {
    public:
         /**
         * Instantiates an LSM303 object
         *
         * @param i2c bus
         * @param addr Magnetometer
         * @param addr Accelerometer
         */
        LSM303 (int bus,
                int addrMag=LSM303_MAG,
                int addrAcc=LSM303_ACC,
                int accScale=8);

        /**
         * LSM303 object destructor
         * where is no more need for this here - I2c connection will be stopped
         * automatically when m_i2c variable will go out of scope
         * ~LSM303 ();
         **/

        /**
         * Gets the current heading; headings <0 indicate an error has occurred
         *
         * @return float
         */
        float getHeading();

        /**
         * Gets the coordinates in the XYZ order
         */
        mraa::Result getCoordinates();

        /**
         * Gets accelerometer values
         * Should be called before other "get" functions for acceleration
         */
        mraa::Result getAcceleration();

        /**
         * Gets raw coordinate data; it is updated when getCoordinates() is called
         */
        int16_t* getRawCoorData();

        /**
         * Gets the X component of the coordinates data
         */
        int16_t getCoorX();

        /**
         * Gets the Y component of the coordinates data
         */
        int16_t getCoorY();

        /**
         * Gets the Z component of the coordinates data
         */
        int16_t getCoorZ();

        /**
         * Gets raw accelerometer data; it is updated when getAcceleration() is called
         */
        int16_t* getRawAccelData();

        /**
         * Gets the X component of the acceleration data
         */
        int16_t getAccelX();

        /**
         * Gets the Y component of the acceleration data
         */
        int16_t getAccelY();

        /**
         * Gets the Z component of the acceleration data
         */
        int16_t getAccelZ();

    private:
        int readThenWrite(uint8_t reg);
        mraa::Result setRegisterSafe(uint8_t slave, uint8_t sregister, uint8_t data);

        mraa::I2c m_i2c;
        int m_addrMag;
    int m_addrAcc;
        uint8_t buf[6];
        int16_t coor[3];
        int16_t accel[3];
};

}