forked from jarzebski/Arduino-HMC5883L
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMC5883L.h
128 lines (104 loc) · 3.21 KB
/
HMC5883L.h
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
/*
HMC5883L.h - Header file for the HMC5883L Triple Axis Digital Compass Arduino Library.
Version: 1.1.0
(c) 2014 Korneliusz Jarzebski
www.jarzebski.pl
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HMC5883L_h
#define HMC5883L_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define HMC5883L_ADDRESS (0x1E)
#define HMC5883L_REG_CONFIG_A (0x00)
#define HMC5883L_REG_CONFIG_B (0x01)
#define HMC5883L_REG_MODE (0x02)
#define HMC5883L_REG_OUT_X_M (0x03)
#define HMC5883L_REG_OUT_X_L (0x04)
#define HMC5883L_REG_OUT_Z_M (0x05)
#define HMC5883L_REG_OUT_Z_L (0x06)
#define HMC5883L_REG_OUT_Y_M (0x07)
#define HMC5883L_REG_OUT_Y_L (0x08)
#define HMC5883L_REG_STATUS (0x09)
#define HMC5883L_REG_IDENT_A (0x0A)
#define HMC5883L_REG_IDENT_B (0x0B)
#define HMC5883L_REG_IDENT_C (0x0C)
typedef enum
{
HMC5883L_SAMPLES_8 = 0b11,
HMC5883L_SAMPLES_4 = 0b10,
HMC5883L_SAMPLES_2 = 0b01,
HMC5883L_SAMPLES_1 = 0b00
} hmc5883l_samples_t;
typedef enum
{
HMC5883L_DATARATE_75HZ = 0b110,
HMC5883L_DATARATE_30HZ = 0b101,
HMC5883L_DATARATE_15HZ = 0b100,
HMC5883L_DATARATE_7_5HZ = 0b011,
HMC5883L_DATARATE_3HZ = 0b010,
HMC5883L_DATARATE_1_5HZ = 0b001,
HMC5883L_DATARATE_0_75_HZ = 0b000
} hmc5883l_dataRate_t;
typedef enum
{
HMC5883L_RANGE_8_1GA = 0b111,
HMC5883L_RANGE_5_6GA = 0b110,
HMC5883L_RANGE_4_7GA = 0b101,
HMC5883L_RANGE_4GA = 0b100,
HMC5883L_RANGE_2_5GA = 0b011,
HMC5883L_RANGE_1_9GA = 0b010,
HMC5883L_RANGE_1_3GA = 0b001,
HMC5883L_RANGE_0_88GA = 0b000
} hmc5883l_range_t;
typedef enum
{
HMC5883L_IDLE = 0b10,
HMC5883L_SINGLE = 0b01,
HMC5883L_CONTINOUS = 0b00
} hmc5883l_mode_t;
#ifndef VECTOR_STRUCT_H
#define VECTOR_STRUCT_H
struct Vector
{
float XAxis;
float YAxis;
float ZAxis;
};
#endif
class HMC5883L
{
public:
bool begin(void);
Vector readRaw(void);
Vector readNormalize(void);
void setOffset(int xo, int yo);
void setRange(hmc5883l_range_t range);
hmc5883l_range_t getRange(void);
void setMeasurementMode(hmc5883l_mode_t mode);
hmc5883l_mode_t getMeasurementMode(void);
void setDataRate(hmc5883l_dataRate_t dataRate);
hmc5883l_dataRate_t getDataRate(void);
void setSamples(hmc5883l_samples_t samples);
hmc5883l_samples_t getSamples(void);
private:
float mgPerDigit;
Vector v;
int xOffset, yOffset;
void writeRegister8(uint8_t reg, uint8_t value);
uint8_t readRegister8(uint8_t reg);
uint8_t fastRegister8(uint8_t reg);
int16_t readRegister16(uint8_t reg);
};
#endif