@@ -630,24 +630,16 @@ int8_t TMAG5273::setSleeptime(uint8_t sleepTime)
630630// / @return Error code (0 is success, negative is failure, positive is warning)
631631int8_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)
666658int8_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)
827812int8_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)
0 commit comments