upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
ak8975.hpp
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 
53  class AK8975 {
54  public:
55 
59  typedef enum {
60  REG_WIA = 0x00, // device id
61 
62  REG_INFO = 0x01, // undocumented (AK proprietary data)
63 
64  REG_ST1 = 0x02, // status 1
65 
66  REG_HXL = 0x03, // magnetometer data, X axis low byte
67  REG_HXH = 0x04, // magnetometer data, X axis high byte
68  REG_HYL = 0x05,
69  REG_HYH = 0x06,
70  REG_HZL = 0x07,
71  REG_HZH = 0x08,
72 
73  REG_ST2 = 0x09, // status 2
74 
75  REG_CNTL = 0x0a, // control
76 
77  // REG_RSV 0x0b reserved
78 
79  REG_ASTC = 0x0c, // self test (internal mag field)
80 
81  // REG_TS1, REG_TS2 0x0d, 0x0e reserved/factory test
82 
83  // REG_I2CDIS 0x0f, I2C disable. Not a good idea to use or support.
84  // write a 0x1b to disable i2c. This requires a power cycle to undo.
85 
86  // These registers hold factory calibrated co-efficients needed to
87  // properly compensate for production variations. They can only be
88  // read when device is in fuse mode. They are used to adjust the
89  // measured mag field values.
90  REG_ASAX = 0x10, // X calibration
91  REG_ASAY = 0x11,
92  REG_ASAZ = 0x12
93  } AK8975_REG_T;
94 
98  typedef enum {
99  ST1_DRDY = 0x01 // data ready bit
100  } ST1_BITS_T;
101 
105  typedef enum {
106  ST2_DERR = 0x04, // data error
107  ST2_HOFL = 0x08 // measurement overflow
108  } ST2_BITS_T;
109 
113  typedef enum {
114  CNTL_PWRDWN = 0x00, // power down
115  CNTL_MEASURE = 0x01, // single measurement
116  CNTL_SELFTEST = 0x08,
117  CNTL_FUSE_ACCESS = 0x0f // access fuse (coeff) registers
118  } CNTL_MODES_T;
119 
123  typedef enum {
124  ASTC_SELF = 0x40 // enable self test
125  } ASTC_BITS_T;
126 
133  AK8975(int bus=AK8975_I2C_BUS, uint8_t address=AK8975_DEFAULT_I2C_ADDR);
134 
138  ~AK8975();
139 
146  bool init();
147 
154  bool setMode(CNTL_MODES_T mode);
155 
162  bool isReady();
163 
170  bool waitforDeviceReady();
171 
179  bool update(bool selfTest=false);
180 
193  bool selfTest();
194 
203  void getMagnetometer(float *x, float *y, float *z);
204 
205 
206  protected:
216  float adjustValue(float value, float adj);
217 
218  // compensation coefficients (factory set) for this device
219  float m_xCoeff;
220  float m_yCoeff;
221  float m_zCoeff;
222 
223  // uncompensated magnetometer readings
224  float m_xData;
225  float m_yData;
226  float m_zData;
227 
228  private:
229  mraa::I2c m_i2c;
230  uint8_t m_addr;
231 
232  };
233 }
234 
235 
bool update(bool selfTest=false)
Definition: ak8975.cxx:141
API for the AK8975 magnetometer.
Definition: ak8975.hpp:53
ASTC_BITS_T
Definition: ak8975.hpp:123
ST1_BITS_T
Definition: ak8975.hpp:98
CNTL_MODES_T
Definition: ak8975.hpp:113
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
bool init()
Definition: ak8975.cxx:57
void getMagnetometer(float *x, float *y, float *z)
Definition: ak8975.cxx:230
~AK8975()
Definition: ak8975.cxx:53
ST2_BITS_T
Definition: ak8975.hpp:105
bool isReady()
Definition: ak8975.cxx:110
AK8975(int bus=AK8975_I2C_BUS, uint8_t address=AK8975_DEFAULT_I2C_ADDR)
Definition: ak8975.cxx:36
AK8975_REG_T
Definition: ak8975.hpp:59
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