@@ -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)
156156int8_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)
191184int8_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)
239215int8_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)
303244int8_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)
0 commit comments