Skip to content

Commit eac46c6

Browse files
committed
moved magic number to a constant; fixed data type issues for data value calculations
1 parent 5cd808d commit eac46c6

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/SparkFun_TMAG5273_Arduino_Library.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Distributed as-is; no warranty is given.
2020
#include <Arduino.h>
2121
#include <Wire.h>
2222

23+
static const float kDataConversionDivisor = 32768; // 1 << 15
24+
2325
TMAG5273::TMAG5273()
2426
{
2527
/* Nothing to do */
@@ -148,13 +150,11 @@ int8_t TMAG5273::readWakeUpAndSleepData(float *xVal, float *yVal, float *zVal, f
148150
range = 80;
149151
}
150152

151-
float div = 32768;
152-
153153
// Return the values in the form that the equation will give
154154
*temperature = TMAG5273_TSENSE_T0 + (256 * (*temperature - (TMAG5273_TADC_T0 / 256)) / TMAG5273_TADC_RES);
155-
*xVal = -(range * (*xVal)) / div;
156-
*yVal = -(range * (*yVal)) / div;
157-
*zVal = -(range * (*zVal)) / div;
155+
*xVal = -(range * (*xVal)) / kDataConversionDivisor;
156+
*yVal = -(range * (*yVal)) / kDataConversionDivisor;
157+
*zVal = -(range * (*zVal)) / kDataConversionDivisor;
158158

159159
return getError();
160160
}
@@ -2580,11 +2580,11 @@ float TMAG5273::getXData()
25802580
int8_t xMSB = *(int8_t *)&tempRead;
25812581

25822582
// Combines the two in one register where the MSB is shifted to the correct location
2583-
int16_t xData = xLSB + (xMSB << 8);
2583+
int32_t xData = xLSB + (xMSB << 8);
25842584

25852585
// Reads to see if the range is set to 40mT or 80mT
25862586
uint8_t rangeValXY = getXYAxisRange();
2587-
uint8_t range = 0;
2587+
uint32_t range = 0;
25882588
if (rangeValXY == 0)
25892589
{
25902590
range = 40;
@@ -2595,8 +2595,7 @@ float TMAG5273::getXData()
25952595
}
25962596

25972597
// 16-bit data format equation
2598-
float div = 32768;
2599-
float xOut = -(range * xData) / div;
2598+
float xOut = -(range * xData) / kDataConversionDivisor;
26002599

26012600
return xOut;
26022601
}
@@ -2617,11 +2616,11 @@ float TMAG5273::getYData()
26172616
int8_t yMSB = *(int8_t *)&tempRead;
26182617

26192618
// Combines the two in one register where the MSB is shifted to the correct location
2620-
int16_t yData = yLSB + (yMSB << 8);
2619+
int32_t yData = yLSB + (yMSB << 8);
26212620

26222621
// Reads to see if the range is set to 40mT or 80mT
26232622
uint8_t rangeValXY = getXYAxisRange();
2624-
uint8_t range = 0;
2623+
uint32_t range = 0;
26252624
if (rangeValXY == 0)
26262625
{
26272626
range = 40;
@@ -2633,7 +2632,7 @@ float TMAG5273::getYData()
26332632

26342633
// 16-bit data format equation
26352634

2636-
float yOut = (range * yData) / 32768.;
2635+
float yOut = (range * yData) / kDataConversionDivisor;
26372636

26382637
return yOut;
26392638
}
@@ -2654,11 +2653,11 @@ float TMAG5273::getZData()
26542653
int8_t zMSB = *(int8_t *)&tempRead;
26552654

26562655
// Combines the two in one register where the MSB is shifted to the correct location
2657-
int16_t zData = zLSB + (zMSB << 8);
2656+
int32_t zData = zLSB + (zMSB << 8);
26582657

26592658
// Reads to see if the range is set to 40mT or 80mT
26602659
uint8_t rangeValZ = getZAxisRange();
2661-
uint8_t range = 0;
2660+
uint32_t range = 0;
26622661
if (rangeValZ == 0)
26632662
{
26642663
range = 40;
@@ -2671,7 +2670,7 @@ float TMAG5273::getZData()
26712670
// div = (2^16) / 2 (as per the datasheet equation 10)
26722671
// 16-bit data format equation
26732672

2674-
float zOut = (range * zData) / 32768.;
2673+
float zOut = (range * zData) / kDataConversionDivisor;
26752674

26762675
return zOut;
26772676
}

0 commit comments

Comments
 (0)