upm  0.3.2
Sensor/Actuator repository for libmraa (v0.7.2)
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/i2c.hpp>
28 
29 #define AK8975_I2C_BUS 0
30 #define AK8975_DEFAULT_I2C_ADDR 0x0c
31 
32 namespace upm {
33 
51  class AK8975 {
52  public:
53 
57  typedef enum {
58  REG_WIA = 0x00, // device id
59 
60  REG_INFO = 0x01, // undocumented (AK proprietary data)
61 
62  REG_ST1 = 0x02, // status 1
63 
64  REG_HXL = 0x03, // magnetometer data, X axis low byte
65  REG_HXH = 0x04, // magnetometer data, X axis high byte
66  REG_HYL = 0x05,
67  REG_HYH = 0x06,
68  REG_HZL = 0x07,
69  REG_HZH = 0x08,
70 
71  REG_ST2 = 0x09, // status 2
72 
73  REG_CNTL = 0x0a, // control
74 
75  // REG_RSV 0x0b reserved
76 
77  REG_ASTC = 0x0c, // self test (internal mag field)
78 
79  // REG_TS1, REG_TS2 0x0d, 0x0e reserved/factory test
80 
81  // REG_I2CDIS 0x0f, I2C disable. Not a good idea to use or support.
82  // write a 0x1b to disable i2c. This requires a power cycle to undo.
83 
84  // These registers hold factory calibrated co-efficients needed to
85  // properly compensate for production variations. They can only be
86  // read when device is in fuse mode. They are used to adjust the
87  // measured mag field values.
88  REG_ASAX = 0x10, // X calibration
89  REG_ASAY = 0x11,
90  REG_ASAZ = 0x12
91  } AK8975_REG_T;
92 
96  typedef enum {
97  ST1_DRDY = 0x01 // data ready bit
98  } ST1_BITS_T;
99 
103  typedef enum {
104  ST2_DERR = 0x04, // data error
105  ST2_HOFL = 0x08 // measurement overflow
106  } ST2_BITS_T;
107 
111  typedef enum {
112  CNTL_PWRDWN = 0x00, // power down
113  CNTL_MEASURE = 0x01, // single measurement
114  CNTL_SELFTEST = 0x08,
115  CNTL_FUSE_ACCESS = 0x0f // access fuse (coeff) registers
116  } CNTL_MODES_T;
117 
121  typedef enum {
122  ASTC_SELF = 0x40 // enable self test
123  } ASTC_BITS_T;
124 
131  AK8975(int bus=AK8975_I2C_BUS, uint8_t address=AK8975_DEFAULT_I2C_ADDR);
132 
136  ~AK8975();
137 
144  bool init();
145 
152  bool setMode(CNTL_MODES_T mode);
153 
160  bool isReady();
161 
168  bool waitforDeviceReady();
169 
177  bool update(bool selfTest=false);
178 
191  bool selfTest();
192 
201  void getMagnetometer(float *x, float *y, float *z);
202 
203 
204  protected:
214  float adjustValue(float value, float adj);
215 
216  // compensation coefficients (factory set) for this device
217  float m_xCoeff;
218  float m_yCoeff;
219  float m_zCoeff;
220 
221  // uncompensated magnetometer readings
222  float m_xData;
223  float m_yData;
224  float m_zData;
225 
226  private:
227  mraa::I2c m_i2c;
228  uint8_t m_addr;
229 
230  };
231 }
232 
233 
bool update(bool selfTest=false)
Definition: ak8975.cxx:137
API for the AK8975 magnetometer.
Definition: ak8975.h:51
ASTC_BITS_T
Definition: ak8975.h:121
AK8975_REG_T
Definition: ak8975.h:57
Definition: a110x.h:29
bool init()
Definition: ak8975.cxx:56
ST2_BITS_T
Definition: ak8975.h:103
ST1_BITS_T
Definition: ak8975.h:96
void getMagnetometer(float *x, float *y, float *z)
Definition: ak8975.cxx:223
~AK8975()
Definition: ak8975.cxx:52
bool isReady()
Definition: ak8975.cxx:106
AK8975(int bus=AK8975_I2C_BUS, uint8_t address=AK8975_DEFAULT_I2C_ADDR)
Definition: ak8975.cxx:35
bool selfTest()
Definition: ak8975.cxx:172
float adjustValue(float value, float adj)
Definition: ak8975.cxx:215
bool setMode(CNTL_MODES_T mode)
Definition: ak8975.cxx:90
bool waitforDeviceReady()
Definition: ak8975.cxx:116
CNTL_MODES_T
Definition: ak8975.h:111