Skip to content

Commit 9daccce

Browse files
committed
continued the conversion of bitWrite() to actual understandable bitwise operations
1 parent 1c7e2dd commit 9daccce

File tree

2 files changed

+35
-95
lines changed

2 files changed

+35
-95
lines changed

src/SparkFun_TMAG5273_Arduino_Library.cpp

Lines changed: 22 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,17 @@ int8_t TMAG5273::readWakeUpAndSleepData(float *xVal, float *yVal, float *zVal, f
155155
/// @return Error code (0 is success, negative is failure, positive is warning)
156156
int8_t TMAG5273::setCRCMode(uint8_t crcMode)
157157
{
158+
159+
if (crcMode > TMAG5273_CRC_ENABLE) // only 0x0 and 0x1 are valid
160+
return -1; // invalid crcMode
161+
158162
uint8_t mode = 0;
159163
sfTkError_t rc = _theI2CBus.readRegister(TMAG5273_REG_DEVICE_CONFIG_1, mode);
160164
if (rc != ksfTkErrOk)
161165
return -1;
162166

163-
// If-Else statement for writing values to the register, bit by bit
164-
if (crcMode == 0)
165-
{
166-
// adds the crcMode (0 or 1) to bit 7 in the register
167-
bitWrite(mode, 7, 0);
168-
}
169-
else if (crcMode == 1)
170-
{
171-
// adds the crc_mode (0 or 1) to bit 7 in the register
172-
bitWrite(mode, 7, 1);
173-
}
174-
else
175-
return -1; // invalid crcMode
167+
mode &= ~TMAG5273_CRC_MODE_BITS; // clear our bit
168+
mode |= (crcMode << TMAG5273_CRC_MODE_LSB);
176169

177170
rc = _theI2CBus.writeRegister(TMAG5273_REG_DEVICE_CONFIG_1, mode);
178171
if (rc != ksfTkErrOk)
@@ -190,34 +183,17 @@ int8_t TMAG5273::setCRCMode(uint8_t crcMode)
190183
/// @return Error code (0 is success, negative is failure, positive is warning)
191184
int8_t TMAG5273::setMagTemp(uint8_t magTempMode)
192185
{
186+
if (magTempMode > TMAG5273_MAG_TEMP_0P2PCT ||
187+
magTempMode == TMAG5273_MAG_TEMP_RESERVED) // only 0x0 to 0x3 are valid
188+
return -1;
189+
// invalid magTempMode
193190
uint8_t mode = 0;
194191
sfTkError_t rc = _theI2CBus.readRegister(TMAG5273_REG_DEVICE_CONFIG_1, mode);
195192
if (rc != ksfTkErrOk)
196193
return -1;
197194

198-
// If-Else statement for writing values to the register, bit by bit
199-
if (magTempMode == 0) // 0b00
200-
{
201-
bitWrite(mode, 6, 0);
202-
bitWrite(mode, 5, 0);
203-
}
204-
else if (magTempMode == 0x1) // 0b01
205-
{
206-
bitWrite(mode, 6, 0);
207-
bitWrite(mode, 5, 1);
208-
}
209-
else if (magTempMode == 0x2) // 0b10
210-
{
211-
bitWrite(mode, 6, 1);
212-
bitWrite(mode, 5, 0);
213-
}
214-
else if (magTempMode == 0x3) // 0b11
215-
{
216-
bitWrite(mode, 6, 1);
217-
bitWrite(mode, 5, 1);
218-
}
219-
else
220-
return -1; // invalid magTempMode
195+
mode &= ~TMAG5273_MAG_TEMP_BITS; // clear our bits
196+
mode |= (magTempMode << TMAG5273_MAG_TEMP_LSB);
221197

222198
rc = _theI2CBus.writeRegister(TMAG5273_REG_DEVICE_CONFIG_1, mode);
223199
if (rc != ksfTkErrOk)
@@ -238,51 +214,16 @@ int8_t TMAG5273::setMagTemp(uint8_t magTempMode)
238214
/// @return Error code (0 is success, negative is failure, positive is warning)
239215
int8_t TMAG5273::setConvAvg(uint8_t avgMode)
240216
{
241-
uint8_t mode = 0;
217+
if (avgMode > TMAG5273_X32_CONVERSION) // only 0x0 to 0x5 are valid
218+
return -1; // invalid avgMode
242219

220+
uint8_t mode = 0;
243221
sfTkError_t rc = _theI2CBus.readRegister(TMAG5273_REG_DEVICE_CONFIG_1, mode);
244222
if (rc != ksfTkErrOk)
245223
return -1;
246224

247-
// If-Else statement for writing values to the register, bit by bit
248-
if (avgMode == 0) // 0b000
249-
{
250-
bitWrite(mode, 2, 0);
251-
bitWrite(mode, 3, 0);
252-
bitWrite(mode, 4, 0);
253-
}
254-
else if (avgMode == 0x1) // 0b001
255-
{
256-
bitWrite(mode, 2, 1);
257-
bitWrite(mode, 3, 0);
258-
bitWrite(mode, 4, 0);
259-
}
260-
else if (avgMode == 0x2) // 0b010
261-
{
262-
bitWrite(mode, 2, 0);
263-
bitWrite(mode, 3, 1);
264-
bitWrite(mode, 4, 0);
265-
}
266-
else if (avgMode == 0x3) // 0b011
267-
{
268-
bitWrite(mode, 2, 1);
269-
bitWrite(mode, 3, 1);
270-
bitWrite(mode, 4, 0);
271-
}
272-
else if (avgMode == 0x4) // 0b100
273-
{
274-
bitWrite(mode, 2, 0);
275-
bitWrite(mode, 3, 0);
276-
bitWrite(mode, 4, 1);
277-
}
278-
else if (avgMode == 0x5) // 0b101
279-
{
280-
bitWrite(mode, 2, 1);
281-
bitWrite(mode, 3, 0);
282-
bitWrite(mode, 4, 1);
283-
}
284-
else
285-
return -1; // invalid avgMode
225+
mode &= ~TMAG5273_CONV_AVG_BITS; // clear our bits
226+
mode |= (avgMode << TMAG5273_CONV_AVG_LSB);
286227

287228
rc = _theI2CBus.writeRegister(TMAG5273_REG_DEVICE_CONFIG_1, mode);
288229
if (rc != ksfTkErrOk)
@@ -302,30 +243,16 @@ int8_t TMAG5273::setConvAvg(uint8_t avgMode)
302243
/// @return Error code (0 is success, negative is failure, positive is warning)
303244
int8_t TMAG5273::setReadMode(uint8_t readMode)
304245
{
305-
uint8_t mode = 0;
246+
if (readMode > TMAG5273_I2C_MODE_1BYTE_8BIT) // only 0x0 to 0x2 are valid
247+
return -1; // invalid readMode
306248

249+
uint8_t mode = 0;
307250
sfTkError_t rc = _theI2CBus.readRegister(TMAG5273_REG_DEVICE_CONFIG_1, mode);
308251
if (rc != ksfTkErrOk)
309252
return -1;
310253

311-
// Write values to the register, bit by bit
312-
if (readMode == 0)
313-
{
314-
bitWrite(mode, 0, 0);
315-
bitWrite(mode, 1, 0);
316-
}
317-
else if (readMode == 1)
318-
{
319-
bitWrite(mode, 0, 1);
320-
bitWrite(mode, 1, 0);
321-
}
322-
else if (readMode == 2) //
323-
{
324-
bitWrite(mode, 0, 0);
325-
bitWrite(mode, 1, 1);
326-
}
327-
else
328-
return -1; // invalid readMode
254+
mode &= ~TMAG5273_I2C_READ_MODE_BITS; // clear our bits
255+
mode |= (readMode << TMAG5273_I2C_READ_MODE_LSB);
329256

330257
rc = _theI2CBus.writeRegister(TMAG5273_REG_DEVICE_CONFIG_1, mode);
331258
if (rc != ksfTkErrOk)

src/SparkFun_TMAG5273_Arduino_Library_Defs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,29 @@ Features as per datasheet
3535
#define TMAG5273_TADC_T0 17508 // Temp result in decimal value (from 16-buit format)
3636
#define TMAG5273_TADC_RES 60.1f // Temperature sensing resolution (in 16-bit format)
3737

38+
#define TMAG5273_CRC_MODE_BITS 0x80 // Bit 7
39+
#define TMAG5273_CRC_MODE_LSB 7
3840
#define TMAG5273_CRC_DISABLE 0x0 // Disables I2C CRC byte to be sent
3941
#define TMAG5273_CRC_ENABLE 0x1 // Enable I2C CRC byte to be sent
4042

43+
#define TMAG5273_MAG_TEMP_BITS 0x60 // Bits 6-5
44+
#define TMAG5273_MAG_TEMP_LSB 5
45+
#define TMAG5273_MAG_TEMP_0PCT 0x0 // 0% (No temperature compensation)
46+
#define TMAG5273_MAG_TEMP_0P12PCT 0x1 // 0.12%/deg C (NdBFe)
47+
#define TMAG5273_MAG_TEMP_RESERVED 0x2 // Reserved
48+
#define TMAG5273_MAG_TEMP_0P2PCT 0x3 //
49+
50+
#define TMAG5273_CONV_AVG_BITS 0x1A // Bits 4-2
51+
#define TMAG5273_CONV_AVG_LSB 2
4152
#define TMAG5273_X1_CONVERSION 0x0 // 1X Average
4253
#define TMAG5273_X2_CONVERSION 0x1 // 2X Average
4354
#define TMAG5273_X4_CONVERSION 0x2 // 4X Average
4455
#define TMAG5273_X8_CONVERSION 0x3 // 8X Average
4556
#define TMAG5273_X16_CONVERSION 0x4 // 16X Average
4657
#define TMAG5273_X32_CONVERSION 0x5 // 32X Average
4758

59+
#define TMAG5273_I2C_READ_MODE_BITS 0x03 // Bits 1-0
60+
#define TMAG5273_I2C_READ_MODE_LSB 0
4861
#define TMAG5273_I2C_MODE_3BYTE 0x0 // Standard I2C 3-byte read command
4962
#define TMAG5273_I2C_MODE_1BYTE_16BIT 0x1 // 1-byte I2C read command for 16bit sensor data and conversion status
5063
#define TMAG5273_I2C_MODE_1BYTE_8BIT 0x2 // 1-byte I2C read command for 8 bit sensor MSB data and conversion status

0 commit comments

Comments
 (0)