Skip to content

Commit 1c7e2dd

Browse files
committed
move more methods to bit frobbing from using bitWrite() macro
1 parent 5f9500c commit 1c7e2dd

File tree

2 files changed

+21
-51
lines changed

2 files changed

+21
-51
lines changed

src/SparkFun_TMAG5273_Arduino_Library.cpp

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -630,24 +630,16 @@ int8_t TMAG5273::setSleeptime(uint8_t sleepTime)
630630
/// @return Error code (0 is success, negative is failure, positive is warning)
631631
int8_t TMAG5273::setMagDir(uint8_t threshDir)
632632
{
633-
uint8_t mode = 0;
633+
if (threshDir > TMAG5273_THRESHOLD_INT_BELOW) // only 0x0 and 0x1 are valid
634+
return -1; // invalid threshDir
634635

636+
uint8_t mode = 0;
635637
sfTkError_t rc = _theI2CBus.readRegister(TMAG5273_REG_SENSOR_CONFIG_2, mode);
636638
if (rc != ksfTkErrOk)
637639
return -1;
638640

639-
// If-Else statement for writing values to the register, bit by bit
640-
if (threshDir == 0X0) // 0b0
641-
{
642-
// Write 0 to bit 5 of the register value
643-
bitWrite(mode, 5, 0);
644-
}
645-
else if (threshDir == 0X1) // 0b1
646-
{
647-
bitWrite(mode, 5, 1);
648-
}
649-
else
650-
return -1; // invalid threshDir
641+
mode &= ~TMAG5273_THRESHOLD_INT_BITS; // clear our bit
642+
mode |= (threshDir << TMAG5273_THRESHOLD_INT_LSB);
651643

652644
rc = _theI2CBus.writeRegister(TMAG5273_REG_SENSOR_CONFIG_2, mode);
653645
if (rc != ksfTkErrOk)
@@ -665,23 +657,16 @@ int8_t TMAG5273::setMagDir(uint8_t threshDir)
665657
/// @return Error code (0 is success, negative is failure, positive is warning)
666658
int8_t TMAG5273::setMagnitudeGain(uint8_t gainAdjust)
667659
{
660+
if (gainAdjust > TMAG5273_GAIN_ADJUST_CHANNEL_2) // only 0x0 and 0x1 are valid
661+
return -1;
662+
// invalid threshDir
668663
uint8_t mode = 0;
669664
sfTkError_t rc = _theI2CBus.readRegister(TMAG5273_REG_SENSOR_CONFIG_2, mode);
670665
if (rc != ksfTkErrOk)
671666
return -1;
672667

673-
// If-Else statement for writing values to the register, bit by bit
674-
if (gainAdjust == 0X0) // 0b0
675-
{
676-
// Write 0 to bit 4 of the register value
677-
bitWrite(mode, 4, 0);
678-
}
679-
else if (gainAdjust == 0X1) // 0b1
680-
{
681-
bitWrite(mode, 4, 1);
682-
}
683-
else
684-
return -1; // invalid threshDir
668+
mode &= ~TMAG5273_GAIN_ADJUST_BITS; // clear our bit
669+
mode |= (gainAdjust << TMAG5273_GAIN_ADJUST_LSB);
685670

686671
rc = _theI2CBus.writeRegister(TMAG5273_REG_SENSOR_CONFIG_2, mode);
687672
if (rc != ksfTkErrOk)
@@ -826,37 +811,16 @@ int8_t TMAG5273::setMagneticOffset2(float offset2)
826811
/// @return Error code (0 is success, negative is failure, positive is warning)
827812
int8_t TMAG5273::setAngleEn(uint8_t angleEnable)
828813
{
829-
uint8_t mode = 0;
814+
if (angleEnable > TMAG5273_XZ_ANGLE_CALCULATION) // only 0x0 to 0x3 are valid
815+
return -1; // invalid angleEnable
830816

817+
uint8_t mode = 0;
831818
sfTkError_t rc = _theI2CBus.readRegister(TMAG5273_REG_SENSOR_CONFIG_2, mode);
832819
if (rc != ksfTkErrOk)
833820
return -1;
834821

835-
// If-Else statement for writing values to the register, bit by bit
836-
if (angleEnable == 0X0) // 0b00 - no angle
837-
{
838-
// Write 0 to bit 2 of the register value
839-
bitWrite(mode, 2, 0);
840-
// Write 0 to bit 3 of the register value
841-
bitWrite(mode, 3, 0);
842-
}
843-
else if (angleEnable == 0X1) // 0b01 1 = X 1st, Y 2nd
844-
{
845-
bitWrite(mode, 2, 1);
846-
bitWrite(mode, 3, 0);
847-
}
848-
else if (angleEnable == 0X2) // 0b10 2 = Y 1st, Z 2nd
849-
{
850-
bitWrite(mode, 2, 0);
851-
bitWrite(mode, 3, 1);
852-
}
853-
else if (angleEnable == 0X3) // 0b11 3 = X 1st, Z 2nd
854-
{
855-
bitWrite(mode, 2, 1);
856-
bitWrite(mode, 3, 1);
857-
}
858-
else
859-
return -1; // invalid angleEnable
822+
mode &= ~TMAG5273_ANGLE_CALCULATION_BITS; // clear our bits
823+
mode |= (angleEnable << TMAG5273_ANGLE_CALCULATION_LSB);
860824

861825
rc = _theI2CBus.writeRegister(TMAG5273_REG_SENSOR_CONFIG_2, mode);
862826
if (rc != ksfTkErrOk)

src/SparkFun_TMAG5273_Arduino_Library_Defs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,18 @@ Features as per datasheet
9595
#define TMAG5273_THRESHOLD_1 0x0 // 1 Threshold crossing
9696
#define TMAG5273_THRESHOLD_4 0x1 // 4 Threshold crossing
9797

98+
#define TMAG5273_THRESHOLD_INT_BITS 0x20 // Bit 5
99+
#define TMAG5273_THRESHOLD_INT_LSB 5
98100
#define TMAG5273_THRESHOLD_INT_ABOVE 0x0 // Sets interrupt for field above the threshold
99101
#define TMAG5273_THRESHOLD_INT_BELOW 0x1 // Sets interrupt for field below the threshold
100102

103+
#define TMAG5273_GAIN_ADJUST_BITS 0x10 // bit 4
104+
#define TMAG5273_GAIN_ADJUST_LSB 4
101105
#define TMAG5273_GAIN_ADJUST_CHANNEL_1 0x0 // 1st channel is selected for gain adjustment
102106
#define TMAG5273_GAIN_ADJUST_CHANNEL_2 0x1 // 2nd channel is selected for gain adjustment
103107

108+
#define TMAG5273_ANGLE_CALCULATION_BITS 0x0C // Bits 3-2
109+
#define TMAG5273_ANGLE_CALCULATION_LSB 2
104110
#define TMAG5273_NO_ANGLE_CALCULATION 0x0 // No angle calculation, magnetic gain, and offset correction enabled
105111
#define TMAG5273_XY_ANGLE_CALCULATION 0x1 // X 1st, Y 2nd
106112
#define TMAG5273_YZ_ANGLE_CALCULATION 0x2 // Y 1st, Z 2nd

0 commit comments

Comments
 (0)