forked from jrowberg/i2cdevlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAK8975.cpp
186 lines (164 loc) · 5.71 KB
/
AK8975.cpp
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
181
182
183
184
185
186
// I2Cdev library collection - AK8975 I2C device class header file
// Based on AKM AK8975/B datasheet, 12/2009
// 8/27/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2011-08-27 - initial release
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg
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.
===============================================
*/
#include "AK8975.h"
#if (I2CDEV_IMPLEMENTATION == I2CDEV_MSP430)
#define delay(x) _nop()
#endif
/** Default constructor, uses default I2C address.
* @see AK8975_DEFAULT_ADDRESS
*/
AK8975::AK8975() {
devAddr = AK8975_DEFAULT_ADDRESS;
}
/** Specific address constructor.
* @param address I2C address
* @see AK8975_DEFAULT_ADDRESS
* @see AK8975_ADDRESS_00
*/
AK8975::AK8975(uint8_t address) {
devAddr = address;
}
/** Power on and prepare for general usage.
* No specific pre-configuration is necessary for this device.
*/
void AK8975::initialize() {
}
/** Verify the I2C connection.
* Make sure the device is connected and responds as expected.
* @return True if connection is valid, false otherwise
*/
bool AK8975::testConnection() {
if (I2Cdev::readByte(devAddr, AK8975_RA_WIA, buffer) == 1) {
return (buffer[0] == 0x48);
}
return false;
}
// WIA register
uint8_t AK8975::getDeviceID() {
I2Cdev::readByte(devAddr, AK8975_RA_WIA, buffer);
return buffer[0];
}
// INFO register
uint8_t AK8975::getInfo() {
I2Cdev::readByte(devAddr, AK8975_RA_INFO, buffer);
return buffer[0];
}
// ST1 register
bool AK8975::getDataReady() {
I2Cdev::readBit(devAddr, AK8975_RA_ST1, AK8975_ST1_DRDY_BIT, buffer);
return buffer[0];
}
// H* registers
void AK8975::getHeading(int16_t *x, int16_t *y, int16_t *z) {
I2Cdev::writeByte(devAddr, AK8975_RA_CNTL, AK8975_MODE_SINGLE);
delay(10);
I2Cdev::readBytes(devAddr, AK8975_RA_HXL, 6, buffer);
*x = (((int16_t)buffer[1]) << 8) | buffer[0];
*y = (((int16_t)buffer[3]) << 8) | buffer[2];
*z = (((int16_t)buffer[5]) << 8) | buffer[4];
}
int16_t AK8975::getHeadingX() {
I2Cdev::writeByte(devAddr, AK8975_RA_CNTL, AK8975_MODE_SINGLE);
delay(10);
I2Cdev::readBytes(devAddr, AK8975_RA_HXL, 2, buffer);
return (((int16_t)buffer[1]) << 8) | buffer[0];
}
int16_t AK8975::getHeadingY() {
I2Cdev::writeByte(devAddr, AK8975_RA_CNTL, AK8975_MODE_SINGLE);
delay(10);
I2Cdev::readBytes(devAddr, AK8975_RA_HYL, 2, buffer);
return (((int16_t)buffer[1]) << 8) | buffer[0];
}
int16_t AK8975::getHeadingZ() {
I2Cdev::writeByte(devAddr, AK8975_RA_CNTL, AK8975_MODE_SINGLE);
delay(10);
I2Cdev::readBytes(devAddr, AK8975_RA_HZL, 2, buffer);
return (((int16_t)buffer[1]) << 8) | buffer[0];
}
// ST2 register
bool AK8975::getOverflowStatus() {
I2Cdev::readBit(devAddr, AK8975_RA_ST2, AK8975_ST2_HOFL_BIT, buffer);
return buffer[0];
}
bool AK8975::getDataError() {
I2Cdev::readBit(devAddr, AK8975_RA_ST2, AK8975_ST2_DERR_BIT, buffer);
return buffer[0];
}
// CNTL register
uint8_t AK8975::getMode() {
I2Cdev::readBits(devAddr, AK8975_RA_CNTL, AK8975_CNTL_MODE_BIT, AK8975_CNTL_MODE_LENGTH, buffer);
return buffer[0];
}
void AK8975::setMode(uint8_t mode) {
I2Cdev::writeBits(devAddr, AK8975_RA_CNTL, AK8975_CNTL_MODE_BIT, AK8975_CNTL_MODE_LENGTH, mode);
}
void AK8975::reset() {
I2Cdev::writeBits(devAddr, AK8975_RA_CNTL, AK8975_CNTL_MODE_BIT, AK8975_CNTL_MODE_LENGTH, AK8975_MODE_POWERDOWN);
}
// ASTC register
void AK8975::setSelfTest(bool enabled) {
I2Cdev::writeBit(devAddr, AK8975_RA_ASTC, AK8975_ASTC_SELF_BIT, enabled);
}
// I2CDIS
void AK8975::disableI2C() {
I2Cdev::writeBit(devAddr, AK8975_RA_I2CDIS, AK8975_I2CDIS_BIT, true);
}
// ASA* registers
void AK8975::getAdjustment(int8_t *x, int8_t *y, int8_t *z) {
I2Cdev::readBytes(devAddr, AK8975_RA_ASAX, 3, buffer);
*x = buffer[0];
*y = buffer[1];
*z = buffer[2];
}
void AK8975::setAdjustment(int8_t x, int8_t y, int8_t z) {
buffer[0] = x;
buffer[1] = y;
buffer[2] = z;
I2Cdev::writeBytes(devAddr, AK8975_RA_ASAX, 3, buffer);
}
uint8_t AK8975::getAdjustmentX() {
I2Cdev::readByte(devAddr, AK8975_RA_ASAX, buffer);
return buffer[0];
}
void AK8975::setAdjustmentX(uint8_t x) {
I2Cdev::writeByte(devAddr, AK8975_RA_ASAX, x);
}
uint8_t AK8975::getAdjustmentY() {
I2Cdev::readByte(devAddr, AK8975_RA_ASAY, buffer);
return buffer[0];
}
void AK8975::setAdjustmentY(uint8_t y) {
I2Cdev::writeByte(devAddr, AK8975_RA_ASAY, y);
}
uint8_t AK8975::getAdjustmentZ() {
I2Cdev::readByte(devAddr, AK8975_RA_ASAZ, buffer);
return buffer[0];
}
void AK8975::setAdjustmentZ(uint8_t z) {
I2Cdev::writeByte(devAddr, AK8975_RA_ASAZ, z);
}