-
Notifications
You must be signed in to change notification settings - Fork 1
/
QMC6310.cpp
38 lines (33 loc) · 1.2 KB
/
QMC6310.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
#include "QMC6310.h"
bool QMC6310::begin() {
Wire.begin();
Wire.beginTransmission(QMC6310_ADDRESS);
Wire.write(QMC6310_CONTROL_1);
Wire.write(0x1D); // Set mode to continuous, ODR to 200Hz, OSR to 512
return (Wire.endTransmission() == 0); // Return true if transmission was successful
}
bool QMC6310::readData(int16_t &x, int16_t &y, int16_t &z) {
// Check if data is ready
Wire.beginTransmission(QMC6310_ADDRESS);
Wire.write(QMC6310_STATUS);
if (Wire.endTransmission() != 0 || Wire.requestFrom(QMC6310_ADDRESS, 1) != 1) {
return false; // I2C transmission error
}
if ((Wire.read() & QMC6310_STATUS_DRDY) == 0) {
return false; // Data not ready
}
// Read data
Wire.beginTransmission(QMC6310_ADDRESS);
Wire.write(QMC6310_X_LSB);
if (Wire.endTransmission() != 0 || Wire.requestFrom(QMC6310_ADDRESS, 6) != 6) {
return false; // I2C transmission error
}
x = Wire.read() | Wire.read() << 8;
y = Wire.read() | Wire.read() << 8;
z = Wire.read() | Wire.read() << 8;
// Check if data is within expected range
if (x < -32768 || x > 32767 || y < -32768 || y > 32767 || z < -32768 || z > 32767) {
return false; // Data out of range
}
return true; // Data read successfully
}