Skip to content

Commit 5290e27

Browse files
committed
added a method that calculates the mag data value - in header so it inlined, used by the three data methods
1 parent d0a4f8a commit 5290e27

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/SparkFun_TMAG5273_Arduino_Library.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,31 @@ class TMAG5273
126126
int8_t getError(); // Returns an error code (0 is success, negative is failure, positive is warning)
127127

128128
private:
129+
//------------------------------------------------------------------------------------------------
130+
// Simple function to calculate the Magnetic field strength in mT from raw sensor data
131+
// Defining this in the class makes it 'inline' for efficiency
132+
//
133+
// To convert the raw magnetic data to mT, the datasheet equation (eq 10) is as follows:
134+
// B = {-(D15*2^15 + D14*2^14 + ... + D1*2^1 + D0*2^0)/2^16 } * 2*|RANGE|
135+
//
136+
// Notes:
137+
//
138+
// - The first section is flipping the sign bit of the data value (2's complement format)
139+
// This is just: -1 * D
140+
//
141+
// B = { (-1 * D / 2^16 } * 2*|RANGE|
142+
// = { (-1 * D / 2^16 } * 2*|RANGE|/1
143+
// = ( (-1 * D * 2 * |RANGE| ) / 2^16
144+
// = ( 2 * (-1 * D * |RANGE| ) ) / 2^16
145+
// = (-1 * D * |RANGE| ) ) / 2^15
146+
//
147+
// Note: 2^15 = 32768
148+
149+
float calculateMagneticField(int16_t rawData, int32_t range)
150+
{
151+
return (float)(-1 * range * rawData) / 32768.f;
152+
}
153+
129154
/**
130155
* @brief Arduino I2C bus interface instance for the AS7343 sensor.
131156
*

0 commit comments

Comments
 (0)