Skip to content

Commit 1fc1b49

Browse files
committed
lastest phase of the code review/update
1 parent 020d192 commit 1fc1b49

File tree

3 files changed

+80
-129
lines changed

3 files changed

+80
-129
lines changed

src/SparkFun_TMAG5273_Arduino_Library.cpp

Lines changed: 53 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,31 +1398,8 @@ uint8_t TMAG5273::getAngleEn()
13981398
if (_theI2CBus.readRegister(TMAG5273_REG_SENSOR_CONFIG_2, angleReg) != ksfTkErrOk)
13991399
return 0;
14001400

1401-
uint8_t angleDir2 = bitRead(angleReg, 2);
1402-
uint8_t angleDir3 = bitRead(angleReg, 3);
1403-
1404-
if ((angleDir2 == 0) && (angleDir3 == 0)) // 0b00
1405-
{
1406-
// NO angle calculation
1407-
return 0;
1408-
}
1409-
else if ((angleDir2 == 1) && (angleDir3 == 0)) // 0b01
1410-
{
1411-
// X 1st, Y 2nd
1412-
return 1;
1413-
}
1414-
else if ((angleDir2 == 0) && (angleDir3 == 1)) // 0b10
1415-
{
1416-
// Y 1st, Z 2nd
1417-
return 2;
1418-
}
1419-
else if ((angleDir2 == 1) && (angleDir3 == 1)) // 0b11
1420-
{
1421-
// X 1st, Z 2nd
1422-
return 3;
1423-
}
1424-
1425-
return 0;
1401+
angleReg &= ~TMAG5273_ANGLE_CALCULATION_BITS;
1402+
return angleReg >> TMAG5273_ANGLE_CALCULATION_LSB;
14261403
}
14271404

14281405
/// @brief Returns the X and Y axes magnetic range from the
@@ -1437,9 +1414,8 @@ uint8_t TMAG5273::getXYAxisRange()
14371414
if (_theI2CBus.readRegister(TMAG5273_REG_SENSOR_CONFIG_2, XYrangeReg) != ksfTkErrOk)
14381415
return 0;
14391416

1440-
uint8_t axisRange = bitRead(XYrangeReg, 1);
1441-
1442-
return (axisRange == 1 ? 1 : 0);
1417+
XYrangeReg &= ~TMAG5273_XY_RANGE_BITS;
1418+
return XYrangeReg >> TMAG5273_XY_RANGE_LSB;
14431419
}
14441420

14451421
/// @brief Returns the Z axis magnetic range from the
@@ -1454,67 +1430,58 @@ uint8_t TMAG5273::getZAxisRange()
14541430
if (_theI2CBus.readRegister(TMAG5273_REG_SENSOR_CONFIG_2, ZrangeReg) != ksfTkErrOk)
14551431
return 0;
14561432

1457-
uint8_t ZaxisRange = bitRead(ZrangeReg, 0);
1458-
1459-
return (ZaxisRange == 1 ? 1 : 0);
1433+
ZrangeReg &= ~TMAG5273_Z_RANGE_BITS;
1434+
return ZrangeReg >> TMAG5273_Z_RANGE_LSB;
14601435
}
14611436

14621437
/// @brief Returns an 8-bit, 2's complement X axis threshold code for
14631438
/// limit check. The range of possible threshold entrees can be +/-128.
14641439
/// The threshold value in mT is calculated as (40(1+X_Y_RANGE)/128)*X_THR_CONFIG.
1440+
/// The value of X_THR_CONFIG is returned by this method
14651441
/// Default 0h means no threshold comparison.
14661442
/// TMAG5273_REG_X_THR_CONFIG - bits 7-0
14671443
/// @return Returns the X threshold code for limit check
1468-
float TMAG5273::getXThreshold()
1444+
int8_t TMAG5273::getXThreshold()
14691445
{
14701446

14711447
uint8_t tempRead;
14721448
if (_theI2CBus.readRegister(TMAG5273_REG_X_THR_CONFIG, tempRead) != ksfTkErrOk)
14731449
return 0;
14741450

1475-
int8_t xThresh = *(int8_t *)&tempRead;
1476-
1477-
uint8_t range = getXYAxisRange();
1478-
1479-
return (float)(40. * (1 + range)) / 128. * xThresh;
1451+
return *(int8_t *)&tempRead;
14801452
}
14811453

14821454
/// @brief Returns an 8-bit, 2's complement Y axis threshold code for
14831455
/// limit check. The range of possible threshold entrees can be +/-128.
1484-
/// The thershold value in mT is calculated as (40(1+X_Y_RANGE)/128)*Y_THR_CONFIG.
1456+
/// The threshold value in mT is calculated as (40(1+X_Y_RANGE)/128)*Y_THR_CONFIG.
1457+
/// The value of Y_THR_CONFIG is returned by this method
14851458
/// Default 0h means no threshold comparison.
14861459
/// TMAG5273_REG_Y_THR_CONFIG - bits 7-0
14871460
/// @return Returns the Y threshold code for limit check
1488-
float TMAG5273::getYThreshold()
1461+
int8_t TMAG5273::getYThreshold()
14891462
{
14901463

14911464
uint8_t tempRead;
14921465
if (_theI2CBus.readRegister(TMAG5273_REG_Y_THR_CONFIG, tempRead) != ksfTkErrOk)
14931466
return 0;
14941467

1495-
int8_t yThresh = *(int8_t *)&tempRead;
1496-
1497-
uint8_t range = getXYAxisRange();
1498-
1499-
return (float)(40. * (1. + range)) / 128. * yThresh;
1468+
return *(int8_t *)&tempRead;
15001469
}
15011470

15021471
/// @brief Returns an 8-bit, 2's complement Z axis threshold code for
15031472
/// limit check. The range of possible threshold entrees can be +/-128.
1504-
/// The thershold value in mT is calculated as (40(1+Z_RANGE)/128)*Z_THR_CONFIG.
1473+
/// The threshold value in mT is calculated as (40(1+Z_RANGE)/128)*Z_THR_CONFIG.
1474+
/// The value of Z_THR_CONFIG is returned by this method
15051475
/// Default 0h means no threshold comparison.
15061476
/// TMAG5273_REG_Z_THR_CONFIG - bits 7-0
15071477
/// @return Returns the Z threshold code for limit check
1508-
float TMAG5273::getZThreshold()
1478+
int8_t TMAG5273::getZThreshold()
15091479
{
15101480
uint8_t tempRead;
15111481
if (_theI2CBus.readRegister(TMAG5273_REG_Z_THR_CONFIG, tempRead) != ksfTkErrOk)
15121482
return 0;
15131483

1514-
int8_t zThresh = *(int8_t *)&tempRead;
1515-
uint8_t range = getZAxisRange();
1516-
1517-
return (float)(40. * (1. + range)) / 128. * zThresh;
1484+
return *(int8_t *)&tempRead;
15181485
}
15191486

15201487
/// @brief Returns the temperature threshold code entered by the user.
@@ -1523,16 +1490,15 @@ float TMAG5273::getZThreshold()
15231490
/// 8 degree C/LSB. Default 0x0 means no threshold comparison.
15241491
/// TMAG5273_REG_T_CONFIG - bits 7-1
15251492
/// @return Temperature threshold code entered by the user originally.
1526-
float TMAG5273::getTemperatureThreshold()
1493+
uint8_t TMAG5273::getTemperatureThreshold()
15271494
{
15281495

15291496
uint8_t tempRead;
15301497
if (_theI2CBus.readRegister(TMAG5273_REG_T_CONFIG, tempRead) != ksfTkErrOk)
15311498
return 0;
15321499

1533-
int8_t tempThreshReg = *(int8_t *)&tempRead;
1534-
1535-
return (float)(tempThreshReg >> 1); // degrees C
1500+
// sift to start at bit 0, return the temperature threshold code value
1501+
return tempRead >> 1;
15361502
}
15371503

15381504
/// @brief Returns the enable bit that determines the data
@@ -1548,7 +1514,8 @@ uint8_t TMAG5273::getTemperatureEN()
15481514
if (_theI2CBus.readRegister(TMAG5273_REG_T_CONFIG, tempENreg) != ksfTkErrOk)
15491515
return 0;
15501516

1551-
return bitRead(tempENreg, 0);
1517+
tempENreg &= ~TMAG5273_TEMPERATURE_BITS;
1518+
return tempENreg >> TMAG5273_TEMPERATURE_LSB;
15521519
}
15531520

15541521
/// @brief Returns the enable interrupt response bit on conversion
@@ -1558,15 +1525,16 @@ uint8_t TMAG5273::getTemperatureEN()
15581525
/// 0X1 = Interrupt is asserted when the configured set of
15591526
/// conversions are complete
15601527
/// TMAG5273_REG_INT_CONFIG_1 - bit 7
1561-
/// @return Interrupt responce bit for conversion complete.
1528+
/// @return Interrupt response bit for conversion complete.
15621529
uint8_t TMAG5273::getInterruptResult()
15631530
{
15641531
uint8_t intRsltReg = 0;
15651532

15661533
if (_theI2CBus.readRegister(TMAG5273_REG_INT_CONFIG_1, intRsltReg) != ksfTkErrOk)
15671534
return 0;
15681535

1569-
return bitRead(intRsltReg, 7);
1536+
intRsltReg &= ~TMAG5273_INTERRUPT_RESULT_BITS;
1537+
return intRsltReg >> TMAG5273_INTERRUPT_RESULT_LSB;
15701538
}
15711539

15721540
/// @brief Returns the bit that enables the interrupt response
@@ -1582,10 +1550,11 @@ uint8_t TMAG5273::getThresholdEn()
15821550
if (_theI2CBus.readRegister(TMAG5273_REG_INT_CONFIG_1, threshReg) != ksfTkErrOk)
15831551
return 0;
15841552

1585-
return bitRead(threshReg, 6);
1553+
threshReg &= ~TMAG5273_INTERRUPT_THRESHOLD_BITS;
1554+
return threshReg >> TMAG5273_INTERRUPT_THRESHOLD_LSB;
15861555
}
15871556

1588-
/// @brief Returns the !INT interrupt if it is latched or pusled.
1557+
/// @brief Returns the !INT interrupt if it is latched or pulsed.
15891558
/// 0X0 = !INT interrupt latched until clear by a primary
15901559
/// addressing the device
15911560
/// 0X1 = !INT interrupt pulse for 10us
@@ -1598,7 +1567,8 @@ uint8_t TMAG5273::getIntPinState()
15981567
if (_theI2CBus.readRegister(TMAG5273_REG_INT_CONFIG_1, intStateReg) != ksfTkErrOk)
15991568
return 0;
16001569

1601-
return bitRead(intStateReg, 5);
1570+
intStateReg &= ~TMAG5273_INTERRUPT_PIN_STATE_BITS;
1571+
return intStateReg >> TMAG5273_INTERRUPT_PIN_STATE_LSB;
16021572
}
16031573

16041574
/// @brief Returns the configuration for the interrupt mode select
@@ -1615,40 +1585,8 @@ uint8_t TMAG5273::getInterruptMode()
16151585
if (_theI2CBus.readRegister(TMAG5273_REG_INT_CONFIG_1, intModeReg) != ksfTkErrOk)
16161586
return 0;
16171587

1618-
uint8_t intCon2 = bitRead(intModeReg, 2);
1619-
uint8_t intCon3 = bitRead(intModeReg, 3);
1620-
uint8_t intCon4 = bitRead(intModeReg, 4);
1621-
1622-
if ((intCon2 == 0) && (intCon3 == 0) && (intCon4 == 0)) // 0b000
1623-
{
1624-
// no interrupt
1625-
return 0;
1626-
}
1627-
else if ((intCon2 == 1) && (intCon3 == 0) && (intCon4 == 0)) // 0b001
1628-
{
1629-
// interrupt through !INT
1630-
return 1;
1631-
}
1632-
else if ((intCon2 == 0) && (intCon3 == 1) && (intCon4 == 0)) // 0b010
1633-
{
1634-
// Interrupt through !INT except when I2C is busy
1635-
return 2;
1636-
}
1637-
else if ((intCon2 == 1) && (intCon3 == 1) && (intCon4 == 0)) // 0b011
1638-
{
1639-
// Interrupt through SCL
1640-
return 3;
1641-
}
1642-
else if ((intCon2 == 0) && (intCon3 == 0) && (intCon4 == 1)) // 0b100
1643-
{
1644-
// Interrupt through SCL except when I2C is busy
1645-
return 4;
1646-
}
1647-
else
1648-
{
1649-
// default
1650-
return 0;
1651-
}
1588+
intModeReg &= ~TMAG5273_INTERRUPT_MODE_BITS;
1589+
return intModeReg >> TMAG5273_INTERRUPT_MODE_LSB;
16521590
}
16531591

16541592
/// @brief Returns the Mask !INT pin when !INT is connected to GND
@@ -1662,7 +1600,8 @@ uint8_t TMAG5273::getMaskInt()
16621600
if (_theI2CBus.readRegister(TMAG5273_REG_INT_CONFIG_1, maskIntReg) != ksfTkErrOk)
16631601
return 0;
16641602

1665-
return bitRead(maskIntReg, 0);
1603+
maskIntReg &= ~TMAG5273_I2C_ADDRESS_CHANGE_BITS;
1604+
return maskIntReg >> TMAG5273_I2C_ADDRESS_CHANGE_LSB;
16661605
}
16671606

16681607
/// @brief Returns the rolling count of conversion data sets
@@ -1674,7 +1613,8 @@ uint8_t TMAG5273::getSetCount()
16741613
if (_theI2CBus.readRegister(TMAG5273_REG_CONV_STATUS, convReg) != ksfTkErrOk)
16751614
return 0;
16761615

1677-
return convReg >> 5;
1616+
convReg &= ~TMAG5273_CONV_STATUS_SET_COUNT_BITS;
1617+
return convReg >> TMAG5273_CONV_STATUS_SET_COUNT_LSB;
16781618
}
16791619

16801620
/// @brief Returns if the device is powered up, or experienced power-
@@ -1689,7 +1629,8 @@ uint8_t TMAG5273::getPOR()
16891629
if (_theI2CBus.readRegister(TMAG5273_REG_CONV_STATUS, convReg) != ksfTkErrOk)
16901630
return 0;
16911631

1692-
return bitRead(convReg, 4);
1632+
convReg &= ~TMAG5273_CONV_STATUS_POR_BITS;
1633+
return convReg >> TMAG5273_CONV_STATUS_POR_LSB;
16931634
}
16941635

16951636
/// @brief Returns if there was a detection of any internal
@@ -1706,13 +1647,15 @@ uint8_t TMAG5273::getDiagStatus()
17061647
if (_theI2CBus.readRegister(TMAG5273_REG_CONV_STATUS, convReg) != ksfTkErrOk)
17071648
return 0;
17081649

1650+
convReg &= ~TMAG5273_CONV_STATUS_DIAG_STATUS_BITS;
1651+
return convReg >> TMAG5273_CONV_STATUS_DIAG_STATUS_LSB;
17091652
return bitRead(convReg, 1);
17101653
}
17111654

17121655
/// @brief Returns if the conversion data buffer is ready
17131656
/// to be read.
17141657
/// 0X0 = Conversion data not complete
1715-
/// 0X1 = Converstion data complete
1658+
/// 0X1 = Conversion data complete
17161659
/// TMAG5273_REG_CONV_STATUS - bit 0
17171660
/// @return Conversion data buffer status
17181661
uint8_t TMAG5273::getResultStatus()
@@ -1721,7 +1664,8 @@ uint8_t TMAG5273::getResultStatus()
17211664
if (_theI2CBus.readRegister(TMAG5273_REG_CONV_STATUS, convReg) != ksfTkErrOk)
17221665
return 0;
17231666

1724-
return bitRead(convReg, 0);
1667+
convReg &= ~TMAG5273_CONV_STATUS_RESULT_STATUS_BITS;
1668+
return convReg >> TMAG5273_CONV_STATUS_RESULT_STATUS_LSB;
17251669
}
17261670

17271671
/// @brief Returns the I2C address of the device. There is a 7-bit
@@ -1737,7 +1681,8 @@ uint8_t TMAG5273::getI2CAddress()
17371681
if (_theI2CBus.readRegister(TMAG5273_REG_I2C_ADDRESS, addressReg) != ksfTkErrOk)
17381682
return 0;
17391683

1740-
return addressReg >> 1; // Shift off the last bit to return the first 7
1684+
addressReg &= ~TMAG5273_I2C_ADDRESS_BITS;
1685+
return addressReg >> TMAG5273_I2C_ADDRESS_LSB;
17411686
}
17421687

17431688
/// @brief Returns the device version indicator. The reset value of the
@@ -1754,22 +1699,8 @@ uint8_t TMAG5273::getDeviceID()
17541699
if (_theI2CBus.readRegister(TMAG5273_REG_DEVICE_ID, deviceReg) != ksfTkErrOk)
17551700
return 0;
17561701

1757-
uint8_t reg1 = bitRead(deviceReg, 0);
1758-
uint8_t reg2 = bitRead(deviceReg, 1);
1759-
1760-
if ((reg1 == 1) && (reg2 == 0))
1761-
{
1762-
return 0;
1763-
}
1764-
else if ((reg1 == 0) && (reg2 == 1))
1765-
{
1766-
return 1;
1767-
}
1768-
else
1769-
{
1770-
// returns an error
1771-
return -1;
1772-
}
1702+
deviceReg &= ~TMAG5273_DEVICE_ID_BITS;
1703+
return deviceReg >> TMAG5273_DEVICE_ID_LSB;
17731704
}
17741705

17751706
/// @brief Returns the 8-Bit Manufacturer ID. There are two
@@ -1779,19 +1710,18 @@ uint8_t TMAG5273::getDeviceID()
17791710
/// @return 16-Bit Manufacturer ID
17801711
uint16_t TMAG5273::getManufacturerID()
17811712
{
1782-
uint16_t deviceIDReg = 0;
17831713
uint8_t databuffer[2];
17841714
size_t nRead;
17851715

1716+
// Read both the LSB and MSB in one read. Results [LSB, MSB]
17861717
if (_theI2CBus.readRegister(TMAG5273_REG_MANUFACTURER_ID_LSB, databuffer, 2, nRead) != ksfTkErrOk)
17871718
return 0;
17881719

1789-
deviceIDReg = (databuffer[1] << 8) | (databuffer[0]);
1790-
1791-
return deviceIDReg;
1720+
// make a short word out of the two bytes
1721+
return (databuffer[1] << 8) | (databuffer[0]);
17921722
}
17931723

1794-
/// @brief This function iundicates the level that the device is
1724+
/// @brief This function indicates the level that the device is
17951725
/// reading back from the !INT pin. The reset value of DEVICE_STATUS
17961726
/// depends on the status of the !INT pin at power-up.
17971727
/// @return Returns the following:
@@ -1804,8 +1734,8 @@ uint8_t TMAG5273::getInterruptPinStatus()
18041734
if (_theI2CBus.readRegister(TMAG5273_REG_DEVICE_STATUS, deviceStatusReg) != ksfTkErrOk)
18051735
return 0;
18061736

1807-
// Reads back the bit we want to investigate
1808-
return bitRead(deviceStatusReg, 4);
1737+
deviceStatusReg &= ~TMAG5273_DEVICE_STATUS_INTR_RB_BITS;
1738+
return deviceStatusReg >> TMAG5273_DEVICE_STATUS_INTR_RB_LSB;
18091739
}
18101740

18111741
/// @brief This function returns the device status register as its

src/SparkFun_TMAG5273_Arduino_Library.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ class TMAG5273
9595
uint8_t getAngleEn(); // Returns the angle calculation and associated channel order
9696
uint8_t getXYAxisRange(); // Returns the X and Y axes magnetic range
9797
uint8_t getZAxisRange(); // Returns the Z axis magnetic range
98-
float getXThreshold(); // Returns the X axis threshold code for limit check
99-
float getYThreshold(); // Returns the Y axis threshold code for limit check
100-
float getZThreshold(); // Returns the Z axis threshold code for limit check
101-
float getTemperatureThreshold(); // Returns the temperature threshold code entered by the user
98+
int8_t getXThreshold(); // Returns the X axis threshold code for limit check
99+
int8_t getYThreshold(); // Returns the Y axis threshold code for limit check
100+
int8_t getZThreshold(); // Returns the Z axis threshold code for limit check
101+
uint8_t getTemperatureThreshold(); // Returns the temperature threshold code entered by the user
102102
uint8_t getTemperatureEN(); // Returns the enable bit to determine if temp channel is on or off
103103
uint8_t getInterruptResult(); // Returns the enable interrupt response bit on conversion complete.
104104
uint8_t getThresholdEn(); // Returns the bit that enables the interrupt

0 commit comments

Comments
 (0)