Skip to content

Commit 7c599e0

Browse files
committed
More bitfield updates
1 parent 9dafcff commit 7c599e0

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

src/SparkFun_KX13X_regs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,12 @@ typedef struct
709709
uint8_t bufe : 1; // Activation of sample buffer
710710
} sfe_kx13x_buf_cntl2_t;
711711

712+
typedef union
713+
{
714+
uint8_t all;
715+
sfe_kx13x_buf_cntl2_t bits;
716+
} sfe_kx13x_buf_cntl2_bitfield_t;
717+
712718
#define SFE_KX13X_BUF_STATUS_1 0x60
713719
// The buffer status registers (buff status one and two) report the number of
714720
// data bytes in the sample buffer.

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -893,10 +893,10 @@ bool QwDevKX13X::freeFall()
893893
if (retVal != 0)
894894
return false;
895895

896-
if (tempVal & 0x80)
897-
return true;
896+
sfe_kx13x_ins2_bitfield_t ins2;
897+
ins2.all = tempVal;
898898

899-
return false;
899+
return ins2.bits.ffs;
900900
}
901901

902902
//////////////////////////////////////////////////
@@ -916,10 +916,10 @@ bool QwDevKX13X::bufferFull()
916916
if (retVal != 0)
917917
return false;
918918

919-
if (tempVal & 0x40)
920-
return true;
919+
sfe_kx13x_ins2_bitfield_t ins2;
920+
ins2.all = tempVal;
921921

922-
return false;
922+
return ins2.bits.bfi;
923923
}
924924

925925
//////////////////////////////////////////////////
@@ -939,10 +939,10 @@ bool QwDevKX13X::waterMarkReached()
939939
if (retVal != 0)
940940
return false;
941941

942-
if (tempVal & 0x10)
943-
return true;
942+
sfe_kx13x_ins2_bitfield_t ins2;
943+
ins2.all = tempVal;
944944

945-
return false;
945+
return ins2.bits.wmi;
946946
}
947947

948948
//////////////////////////////////////////////////
@@ -962,13 +962,10 @@ bool QwDevKX13X::tapDetected()
962962
if (retVal != 0)
963963
return false;
964964

965-
tempVal = tempVal & 0x0C; // Three states of interest: single tap detected
966-
// undefined, and no tap.
967-
968-
if (tempVal == 0x04) // True if tap - not undefined or no tap.
969-
return true;
965+
sfe_kx13x_ins2_bitfield_t ins2;
966+
ins2.all = tempVal;
970967

971-
return false;
968+
return (ins2.bits.tdts == 0x01); // Single tap
972969
}
973970

974971
//////////////////////////////////////////////////
@@ -1009,13 +1006,10 @@ bool QwDevKX13X::unknownTap()
10091006
if (retVal != 0)
10101007
return false;
10111008

1012-
tempVal = tempVal & 0x0C; // Three states of interest: single tap detected
1013-
// undefined, and no tap.
1014-
1015-
if (tempVal == 0x0C) // True if undefined
1016-
return true;
1009+
sfe_kx13x_ins2_bitfield_t ins2;
1010+
ins2.all = tempVal;
10171011

1018-
return false;
1012+
return (ins2.bits.tdts == 0x03); // undefined tap event
10191013
}
10201014

10211015
//////////////////////////////////////////////////
@@ -1035,13 +1029,10 @@ bool QwDevKX13X::doubleTapDetected()
10351029
if (retVal != 0)
10361030
return false;
10371031

1038-
tempVal = tempVal & 0x0C; // Two states of interest: single tap detected
1039-
// and undefined.
1040-
1041-
if (tempVal == 0x08) // True if tap - not undefined.
1042-
return true;
1032+
sfe_kx13x_ins2_bitfield_t ins2;
1033+
ins2.all = tempVal;
10431034

1044-
return false;
1035+
return (ins2.bits.tdts == 0x02); // Double tap
10451036
}
10461037

10471038
//////////////////////////////////////////////////
@@ -1061,10 +1052,10 @@ bool QwDevKX13X::tiltChange()
10611052
if (retVal != 0)
10621053
return false;
10631054

1064-
if (tempVal == 0x01)
1065-
return true;
1055+
sfe_kx13x_ins2_bitfield_t ins2;
1056+
ins2.all = tempVal;
10661057

1067-
return false;
1058+
return (ins2.bits.tps); // Tilt position status
10681059
}
10691060

10701061
//////////////////////////////////////////////////
@@ -1082,7 +1073,6 @@ bool QwDevKX13X::setBufferThreshold(uint8_t threshold)
10821073

10831074
int retVal;
10841075
uint8_t tempVal;
1085-
uint8_t resolution;
10861076

10871077
if (threshold < 2 || threshold > 171)
10881078
return false;
@@ -1092,17 +1082,22 @@ bool QwDevKX13X::setBufferThreshold(uint8_t threshold)
10921082
if (retVal != 0)
10931083
return false;
10941084

1095-
resolution = (tempVal & 0x40) >> 6; // Isolate it, move it
1085+
sfe_kx13x_buf_cntl2_bitfield_t bufCntl2;
1086+
bufCntl2.all = tempVal;
1087+
1088+
// BRES – determines the resolution of the acceleration data samples collected by the sample buffer.
1089+
// BRES = 0 – 8-bit samples are accumulated in the buffer
1090+
// BRES = 1 – 16-bit samples are accumulated in the buffer
10961091

1097-
if (threshold > 86 && resolution == 1) // 1 = 16bit resolution, max samples: 86
1092+
if ((threshold > 86) && (bufCntl2.bits.bres == 1)) // 1 = 16bit resolution, max samples: 86
10981093
threshold = 86;
10991094

11001095
retVal = writeRegisterByte(SFE_KX13X_BUF_CNTL1, threshold);
11011096

11021097
if (retVal != 0)
1103-
return true;
1098+
return false;
11041099

1105-
return false;
1100+
return true;
11061101
}
11071102

11081103
//////////////////////////////////////////////////
@@ -1127,7 +1122,10 @@ bool QwDevKX13X::setBufferOperationMode(uint8_t operationMode)
11271122
if (retVal != 0)
11281123
return true;
11291124

1130-
tempVal = tempVal | operationMode;
1125+
sfe_kx13x_buf_cntl2_bitfield_t bufCntl2;
1126+
bufCntl2.all = tempVal;
1127+
bufCntl2.bits.bm = operationMode; // This is a long winded but definitive way of setting/clearing the operating mode
1128+
tempVal = bufCntl2.all;
11311129

11321130
retVal = writeRegisterByte(SFE_KX13X_BUF_CNTL2, tempVal);
11331131

0 commit comments

Comments
 (0)