upm  0.4.1
Sensor/Actuator repository for libmraa (v0.8.0)
ak8975.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/common.hpp>
28 #include <mraa/i2c.hpp>
29 
30 #define AK8975_I2C_BUS 0
31 #define AK8975_DEFAULT_I2C_ADDR 0x0c
32 
33 namespace upm {
34 
52  class AK8975 {
53  public:
54 
58  typedef enum {
59  REG_WIA = 0x00, // device id
60 
61  REG_INFO = 0x01, // undocumented (AK proprietary data)
62 
63  REG_ST1 = 0x02, // status 1
64 
65  REG_HXL = 0x03, // magnetometer data, X axis low byte
66  REG_HXH = 0x04, // magnetometer data, X axis high byte
67  REG_HYL = 0x05,
68  REG_HYH = 0x06,
69  REG_HZL = 0x07,
70  REG_HZH = 0x08,
71 
72  REG_ST2 = 0x09, // status 2
73 
74  REG_CNTL = 0x0a, // control
75 
76  // REG_RSV 0x0b reserved
77 
78  REG_ASTC = 0x0c, // self test (internal mag field)
79 
80  // REG_TS1, REG_TS2 0x0d, 0x0e reserved/factory test
81 
82  // REG_I2CDIS 0x0f, I2C disable. Not a good idea to use or support.
83  // write a 0x1b to disable i2c. This requires a power cycle to undo.
84 
85  // These registers hold factory calibrated co-efficients needed to
86  // properly compensate for production variations. They can only be
87  // read when device is in fuse mode. They are used to adjust the
88  // measured mag field values.
89  REG_ASAX = 0x10, // X calibration
90  REG_ASAY = 0x11,
91  REG_ASAZ = 0x12
92  } AK8975_REG_T;
93 
97  typedef enum {
98  ST1_DRDY = 0x01 // data ready bit
99  } ST1_BITS_T;
100 
104  typedef enum {
105  ST2_DERR = 0x04, // data error
106  ST2_HOFL = 0x08 // measurement overflow
107  } ST2_BITS_T;
108 
112  typedef enum {
113  CNTL_PWRDWN = 0x00, // power down
114  CNTL_MEASURE = 0x01, // single measurement
115  CNTL_SELFTEST = 0x08,
116  CNTL_FUSE_ACCESS = 0x0f // access fuse (coeff) registers
117  } CNTL_MODES_T;
118 
122  typedef enum {
123  ASTC_SELF = 0x40 // enable self test
124  } ASTC_BITS_T;
125 
132  AK8975(int bus=AK8975_I2C_BUS, uint8_t address=AK8975_DEFAULT_I2C_ADDR);
133 
137  ~AK8975();
138 
145  bool init();
146 
153  bool setMode(CNTL_MODES_T mode);
154 
161  bool isReady();
162 
169  bool waitforDeviceReady();
170 
178  bool update(bool selfTest=false);
179 
192  bool selfTest();
193 
202  void getMagnetometer(float *x, float *y, float *z);
203 
204 
205  protected:
215  float adjustValue(float value, float adj);
216 
217  // compensation coefficients (factory set) for this device
218  float m_xCoeff;
219  float m_yCoeff;
220  float m_zCoeff;
221 
222  // uncompensated magnetometer readings
223  float m_xData;
224  float m_yData;
225  float m_zData;
226 
227  private:
228  mraa::I2c m_i2c;
229  uint8_t m_addr;
230 
231  };
232 }
233 
234 
bool update(bool selfTest=false)
Definition: ak8975.cxx:141
API for the AK8975 magnetometer.
Definition: ak8975.h:52
ASTC_BITS_T
Definition: ak8975.h:122
AK8975_REG_T
Definition: ak8975.h:58
Definition: a110x.h:33
bool init()
Definition: ak8975.cxx:57
ST2_BITS_T
Definition: ak8975.h:104
ST1_BITS_T
Definition: ak8975.h:97
void getMagnetometer(float *x, float *y, float *z)
Definition: ak8975.cxx:230
~AK8975()
Definition: ak8975.cxx:53
bool isReady()
Definition: ak8975.cxx:110
AK8975(int bus=AK8975_I2C_BUS, uint8_t address=AK8975_DEFAULT_I2C_ADDR)
Definition: ak8975.cxx:36
bool selfTest()
Definition: ak8975.cxx:177
float adjustValue(float value, float adj)
Definition: ak8975.cxx:222
bool setMode(CNTL_MODES_T mode)
Definition: ak8975.cxx:94
bool waitforDeviceReady()
Definition: ak8975.cxx:120
CNTL_MODES_T
Definition: ak8975.h:112