@@ -20,8 +20,6 @@ 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-
2523TMAG5273::TMAG5273 ()
2624{
2725 /* Nothing to do */
@@ -43,7 +41,7 @@ int8_t TMAG5273::begin(uint8_t sensorAddress, TwoWire &wirePort)
4341 if (isConnected () != 0 )
4442 return 0 ;
4543
46- // Following the Detailed Design Prodedure on page 42 of the datasheet
44+ // Following the Detailed Design Procedure on page 42 of the datasheet
4745 setMagneticChannel (TMAG5273_X_Y_Z_ENABLE);
4846 setTemperatureEn (true );
4947 setOperatingMode (TMAG5273_CONTINUOUS_MEASURE_MODE);
@@ -133,37 +131,14 @@ int8_t TMAG5273::setupWakeUpAndSleep()
133131// / @return Error code (0 is success, negative is failure, positive is warning)
134132int8_t TMAG5273::readWakeUpAndSleepData (float *xVal, float *yVal, float *zVal, float *temperature)
135133{
136- uint8_t wakeupRegisterRead[8 ];
137- size_t nRead = 0 ;
138134
139- // Read 4 bits of data
140- sfTkError_t rc = _theI2CBus.readRegister (TMAG5273_REG_T_MSB_RESULT, wakeupRegisterRead, 4 , nRead);
141- if (rc != ksfTkErrOk)
135+ if (xVal == nullptr || yVal == nullptr || zVal == nullptr || temperature == nullptr )
142136 return -1 ;
143137
144- // Need to get the values to themselves (bitwise operation)
145- *zVal = (wakeupRegisterRead[6 ] << 8 ) & wakeupRegisterRead[7 ];
146- *yVal = (wakeupRegisterRead[4 ] << 8 ) & wakeupRegisterRead[5 ];
147- *xVal = (wakeupRegisterRead[2 ] << 8 ) & wakeupRegisterRead[3 ];
148- *temperature = (wakeupRegisterRead[0 ] << 8 ) & wakeupRegisterRead[1 ];
149-
150- // Reads to see if the range is set to 40mT or 80mT
151- uint8_t rangeValXY = getXYAxisRange ();
152- uint8_t range = 0 ;
153- if (rangeValXY == 0 )
154- {
155- range = 40 ;
156- }
157- else if (rangeValXY == 1 )
158- {
159- range = 80 ;
160- }
161-
162- // Return the values in the form that the equation will give
163- *temperature = TMAG5273_TSENSE_T0 + (256 * (*temperature - (TMAG5273_TADC_T0 / 256 )) / TMAG5273_TADC_RES);
164- *xVal = -(range * (*xVal)) / kDataConversionDivisor ;
165- *yVal = -(range * (*yVal)) / kDataConversionDivisor ;
166- *zVal = -(range * (*zVal)) / kDataConversionDivisor ;
138+ *xVal = getXData ();
139+ *yVal = getYData ();
140+ *zVal = getZData ();
141+ *temperature = getTemp ();
167142
168143 return getError ();
169144}
@@ -2158,7 +2133,7 @@ uint8_t TMAG5273::getZAxisRange()
21582133
21592134// / @brief Returns an 8-bit, 2's complement X axis threshold code for
21602135// / limit check. The range of possible threshold entrees can be +/-128.
2161- // / The thershold value in mT is calculated as (40(1+X_Y_RANGE)/128)*X_THR_CONFIG.
2136+ // / The threshold value in mT is calculated as (40(1+X_Y_RANGE)/128)*X_THR_CONFIG.
21622137// / Default 0h means no threshold comparison.
21632138// / TMAG5273_REG_X_THR_CONFIG - bits 7-0
21642139// / @return Returns the X threshold code for limit check
@@ -2559,57 +2534,41 @@ int8_t TMAG5273::getError()
25592534float TMAG5273::getTemp ()
25602535{
25612536
2562- // get the LSB and MSB values of the temperature data from the sensor
2563- uint8_t tLSB = 0 ;
2564- uint8_t tMSB = 0 ;
2565-
2566- // Read in the MSB and LSB registers
2567- if (_theI2CBus.readRegister (TMAG5273_REG_T_MSB_RESULT, tMSB) != ksfTkErrOk)
2568- return 0 ;
2569- if (_theI2CBus.readRegister (TMAG5273_REG_T_LSB_RESULT, tLSB) != ksfTkErrOk)
2537+ // Read the Temp data - returns the MSB and LSB in one read
2538+ uint8_t dataBuffer[2 ];
2539+ size_t nRead;
2540+ if (_theI2CBus.readRegister (TMAG5273_REG_T_MSB_RESULT, dataBuffer, 2 , nRead) != ksfTkErrOk)
25702541 return 0 ;
25712542
25722543 // Combines the two in one register where the MSB is shifted to the correct location
2573- int16_t tData = (tMSB << 8 ) | tLSB;
2544+ // convert to int16_t (the data is in 2's complement format)
2545+ int16_t tData = (dataBuffer[0 ] << 8 ) | dataBuffer[1 ];
25742546
25752547 // Formula for correct output value
25762548 return TMAG5273_TSENSE_T0 + ((float )(tData - TMAG5273_TADC_T0) / (TMAG5273_TADC_RES));
25772549}
25782550
2579- // / @brief Readcs back the X-Channel data conversion results, the
2551+ // /------------------------------------------------------------------------------------------------
2552+ // / @brief Reads back the X-Channel data conversion results, the
25802553// / MSB 8-Bit and LSB 8-Bits. This reads from the following registers:
25812554// / X_MSB_RESULT and X_LSB_RESULT
25822555// / @return X-Channel data conversion results
25832556float TMAG5273::getXData ()
25842557{
2585- uint8_t xLSB = 0 ;
2586- uint8_t xMSB = 0 ;
2587- if (_theI2CBus.readRegister (TMAG5273_REG_X_LSB_RESULT, xLSB) != ksfTkErrOk)
2588- return 0 ;
2589-
2590- if (_theI2CBus.readRegister (TMAG5273_REG_X_MSB_RESULT, xMSB) != ksfTkErrOk)
2558+ // Read the X data - returns the MSB and LSB in one read
2559+ uint8_t dataBuffer[2 ];
2560+ size_t nRead;
2561+ if (_theI2CBus.readRegister (TMAG5273_REG_X_MSB_RESULT, dataBuffer, 2 , nRead) != ksfTkErrOk)
25912562 return 0 ;
25922563
25932564 // Combines the two in one register where the MSB is shifted to the correct location
2594- // convert the uint16_t to int16_t
2595- int16_t xData = (xMSB << 8 ) + xLSB ;
2565+ // convert to int16_t ( the data is in 2's complement format)
2566+ int16_t xData = (dataBuffer[ 0 ] << 8 ) | dataBuffer[ 1 ] ;
25962567
25972568 // Reads to see if the range is set to 40mT or 80mT
2598- uint8_t rangeValXY = getXYAxisRange ();
2599- uint32_t range = 0 ;
2600- if (rangeValXY == 0 )
2601- {
2602- range = 40 ;
2603- }
2604- else if (rangeValXY == 1 )
2605- {
2606- range = 80 ;
2607- }
2569+ int32_t range = getXYAxisRange () == 0 ? 40 : 80 ;
26082570
2609- // 16-bit data format equation
2610- float xOut = -(range * xData) / kDataConversionDivisor ;
2611-
2612- return xOut;
2571+ return calculateMagneticField (xData, range);
26132572}
26142573
26152574// / @brief Reads back the Y-Channel data conversion results, the
@@ -2618,36 +2577,20 @@ float TMAG5273::getXData()
26182577// / @return Y-Channel data conversion results
26192578float TMAG5273::getYData ()
26202579{
2621-
2622- uint8_t yLSB = 0 ;
2623- uint8_t yMSB = 0 ;
2624- if (_theI2CBus.readRegister (TMAG5273_REG_Y_LSB_RESULT, yLSB) != ksfTkErrOk)
2625- return 0 ;
2626-
2627- if (_theI2CBus.readRegister (TMAG5273_REG_Y_MSB_RESULT, yMSB) != ksfTkErrOk)
2580+ // Read the Y data - returns the MSB and LSB in one read
2581+ uint8_t dataBuffer[2 ];
2582+ size_t nRead;
2583+ if (_theI2CBus.readRegister (TMAG5273_REG_Y_MSB_RESULT, dataBuffer, 2 , nRead) != ksfTkErrOk)
26282584 return 0 ;
26292585
26302586 // Combines the two in one register where the MSB is shifted to the correct location
2631- // convert the uint16_t to int16_t
2632- int16_t yData = (yMSB << 8 ) + yLSB ;
2587+ // convert to int16_t ( the data is in 2's complement format)
2588+ int16_t yData = (dataBuffer[ 0 ] << 8 ) | dataBuffer[ 1 ] ;
26332589
26342590 // Reads to see if the range is set to 40mT or 80mT
2635- uint8_t rangeValXY = getXYAxisRange ();
2636- uint32_t range = 0 ;
2637- if (rangeValXY == 0 )
2638- {
2639- range = 40 ;
2640- }
2641- else if (rangeValXY == 1 )
2642- {
2643- range = 80 ;
2644- }
2645-
2646- // 16-bit data format equation
2591+ int32_t range = getXYAxisRange () == 0 ? 40 : 80 ;
26472592
2648- float yOut = (range * yData) / kDataConversionDivisor ;
2649-
2650- return yOut;
2593+ return calculateMagneticField (yData, range);
26512594}
26522595
26532596// / @brief Reads back the Z-Channel data conversion results, the
@@ -2656,37 +2599,21 @@ float TMAG5273::getYData()
26562599// / @return Z-Channel data conversion results.
26572600float TMAG5273::getZData ()
26582601{
2659- uint8_t zLSB = 0 ;
2660- uint8_t zMSB = 0 ;
2661-
2662- if (_theI2CBus.readRegister (TMAG5273_REG_Z_LSB_RESULT, zLSB) != ksfTkErrOk)
2663- return 0 ;
26642602
2665- if (_theI2CBus.readRegister (TMAG5273_REG_Z_MSB_RESULT, zMSB) != ksfTkErrOk)
2603+ // Read the Z data - returns the MSB and LSB in one read
2604+ uint8_t dataBuffer[2 ];
2605+ size_t nRead;
2606+ if (_theI2CBus.readRegister (TMAG5273_REG_Z_MSB_RESULT, dataBuffer, 2 , nRead) != ksfTkErrOk)
26662607 return 0 ;
26672608
26682609 // Combines the two in one register where the MSB is shifted to the correct location
2669- // convert the uint16_t to int16_t
2670- int16_t zData = (zMSB << 8 ) + zLSB ;
2610+ // convert to int16_t ( the data is in 2's complement format)
2611+ int16_t zData = (dataBuffer[ 0 ] << 8 ) | dataBuffer[ 1 ] ;
26712612
26722613 // Reads to see if the range is set to 40mT or 80mT
2673- uint8_t rangeValZ = getZAxisRange ();
2674- uint32_t range = 0 ;
2675- if (rangeValZ == 0 )
2676- {
2677- range = 40 ;
2678- }
2679- else if (rangeValZ == 1 )
2680- {
2681- range = 80 ;
2682- }
2683-
2684- // div = (2^16) / 2 (as per the datasheet equation 10)
2685- // 16-bit data format equation
2686-
2687- float zOut = (range * zData) / kDataConversionDivisor ;
2614+ int32_t range = getZAxisRange () == 0 ? 40 : 80 ;
26882615
2689- return zOut ;
2616+ return calculateMagneticField (zData, range) ;
26902617}
26912618
26922619// / @brief Returns the angle measurement result in degree. The data
0 commit comments