Skip to content

Commit d871ac9

Browse files
author
JoelEB
authored
Merge pull request #2 from Ductapemaster/master
ESP8266 compatibility, faster data readout, and general improvements
2 parents e798e5d + dd38334 commit d871ac9

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/SparkFun_MAG3110.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ MAG3110::MAG3110() {
3535
y_scale = 0.0f;
3636
}
3737

38-
void MAG3110::initialize() {
38+
bool MAG3110::initialize() {
3939
Wire.begin();
40+
Wire.setClock(400000); // I2C fast mode, 400kHz
4041
if(readRegister(MAG3110_WHO_AM_I) != MAG3110_WHO_AM_I_RSP){ //Could not find MAG3110
4142
//Serial.println("Could not find MAG3110 connected!");
42-
error = true;
43+
return false;
4344
}
4445
else //Successfully initialized
4546
{
4647
//Initial values
4748
reset();
49+
return true;
4850
}
4951
}
5052

@@ -78,18 +80,43 @@ bool MAG3110::dataReady() {
7880
}
7981

8082
void MAG3110::readMag(int* x, int* y, int* z){
81-
//Read each axis
82-
*x = readAxis(MAG3110_OUT_X_MSB);
83-
*y = readAxis(MAG3110_OUT_Y_MSB);
84-
*z = readAxis(MAG3110_OUT_Z_MSB);
83+
84+
// Start readout at X MSB address
85+
Wire.beginTransmission(MAG3110_I2C_ADDRESS);
86+
Wire.write(MAG3110_OUT_X_MSB);
87+
Wire.endTransmission();
88+
89+
delayMicroseconds(2);
90+
91+
// Read out data using multiple byte read mode
92+
Wire.requestFrom(MAG3110_I2C_ADDRESS, 6);
93+
while( Wire.available() != 6 ) {}
94+
95+
// Combine registers
96+
uint16_t values[3];
97+
for(uint8_t idx = 0; idx <= 2; idx++)
98+
{
99+
values[idx] = Wire.read() << 8; // MSB
100+
values[idx] |= Wire.read(); // LSB
101+
}
102+
103+
// Put data into referenced variables
104+
*x = (int) values[0];
105+
*y = (int) values[1];
106+
*z = (int) values[2];
107+
85108
}
86109

87110
void MAG3110::readMicroTeslas(float* x, float* y, float* z){
88111

112+
// Using internal read function
113+
int x_int, y_int, z_int;
114+
readMag(&x_int, &y_int, &z_int);
115+
89116
//Read each axis and scale to Teslas
90-
*x = (float) readAxis(MAG3110_OUT_X_MSB) * 0.1f;
91-
*y = (float) readAxis(MAG3110_OUT_Y_MSB) * 0.1f;
92-
*z = (float) readAxis(MAG3110_OUT_Z_MSB) * 0.1f;
117+
*x = (float) x_int * 0.1f;
118+
*y = (float) y_int * 0.1f;
119+
*z = (float) z_int * 0.1f;
93120

94121
}
95122

@@ -316,6 +343,6 @@ int MAG3110::readAxis(uint8_t axis){
316343

317344
lsb = readRegister(lsbAddress);
318345

319-
int out = (lsb | (msb << 8)); //concatenate the MSB and LSB
320-
return out;
346+
int16_t out = (lsb | (msb << 8)); //concatenate the MSB and LSB;
347+
return (int)out;
321348
}

src/SparkFun_MAG3110.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class MAG3110
123123
public:
124124
MAG3110();
125125

126-
void initialize();
126+
bool initialize();
127127

128128
//Public methods
129129
uint8_t readRegister(uint8_t address);

0 commit comments

Comments
 (0)