@@ -22,7 +22,6 @@ uint8_t QwDevKX13X::getUniqueID()
22
22
{
23
23
uint8_t tempVal;
24
24
int retVal = readRegisterRegion (SFE_KX13X_WHO_AM_I, &tempVal, 1 );
25
- Serial.println (tempVal);
26
25
27
26
if ( retVal != 0 )
28
27
return 0 ;
@@ -99,6 +98,12 @@ bool QwDevKX13X::initialize(uint8_t settings)
99
98
return true ;
100
99
}
101
100
101
+ // ////////////////////////////////////////////////
102
+ // softwareReset()
103
+ //
104
+ // Resets the accelerometer
105
+ //
106
+
102
107
bool QwDevKX13X::softwareReset ()
103
108
{
104
109
@@ -107,13 +112,28 @@ bool QwDevKX13X::softwareReset()
107
112
108
113
retVal = writeRegisterByte (SFE_KX13X_CNTL2, reset);
109
114
115
+ // Logic is inverted here - if we reset using I2C the
116
+ // accelerometer immediately shuts off which results
117
+ // in a NACK.
110
118
if ( retVal != 0 )
111
119
return true ;
112
120
113
121
return false ;
114
122
115
123
}
116
124
125
+ // ////////////////////////////////////////////////
126
+ // softwareReset()
127
+ //
128
+ // Enables acceleromter data. In addition
129
+ // some settings can only be set when the accelerometer is
130
+ // powered down
131
+ //
132
+ // Parameter:
133
+ // enable - enables or disables the accelerometer
134
+ //
135
+ //
136
+
117
137
bool QwDevKX13X::enableAccel (bool enable)
118
138
{
119
139
@@ -136,9 +156,12 @@ bool QwDevKX13X::enableAccel(bool enable)
136
156
137
157
}
138
158
139
- // Address: 0x1B, bit[7]: default value is: 0x00
140
- // This function reads whether the accelerometer is in stand by or an active
141
- // mode.
159
+ // ////////////////////////////////////////////////
160
+ // getOperatingMode()
161
+ //
162
+ // Retrieves the current operating mode - low/high power mode
163
+ //
164
+
142
165
uint8_t QwDevKX13X::getOperatingMode (){
143
166
144
167
uint8_t tempVal;
@@ -152,11 +175,16 @@ uint8_t QwDevKX13X::getOperatingMode(){
152
175
return (tempVal >> 7 );
153
176
154
177
}
155
- // Address: 0x1B, bit[1:0]: default value is: 0x00 (2g)
156
- // This function sets the acceleration range of the accelerometer outputs.
157
- // Possible KX132 arguments: 0x00 (2g), 0x01 (4g), 0x02 (8g), 0x03 (16g)
158
- // Possible KX134 arguments: 0x00 (8g), 0x01 (16g), 0x02 (32g), 0x03 (64g)
159
- // KX13X needs to be set into standby mode to change this value
178
+
179
+ // ////////////////////////////////////////////////
180
+ // setRange()
181
+ //
182
+ // Sets the operational g-range of the accelerometer.
183
+ //
184
+ // Parameter:
185
+ // range - sets the range of the accelerometer 2g - 32g depending
186
+ // on the version.
187
+ //
160
188
bool QwDevKX13X::setRange (uint8_t range)
161
189
{
162
190
@@ -174,6 +202,14 @@ bool QwDevKX13X::setRange(uint8_t range)
174
202
175
203
}
176
204
205
+ // ////////////////////////////////////////////////
206
+ // enableDataEngine()
207
+ //
208
+ // Enables the data ready bit.
209
+ //
210
+ // Parameter:
211
+ // enable - enable/disables the data ready bit.
212
+ //
177
213
bool QwDevKX13X::enableDataEngine (bool enable)
178
214
{
179
215
int retVal;
@@ -194,6 +230,14 @@ bool QwDevKX13X::enableDataEngine(bool enable)
194
230
return true ;
195
231
}
196
232
233
+ // ////////////////////////////////////////////////
234
+ // enableTapEngine()
235
+ //
236
+ // Enables the tap and double tap features of the accelerometers
237
+ //
238
+ // Parameter:
239
+ // enable - enables the tap/double tap feature
240
+ //
197
241
bool QwDevKX13X::enableTapEngine (bool enable)
198
242
{
199
243
int retVal;
@@ -214,6 +258,15 @@ bool QwDevKX13X::enableTapEngine(bool enable)
214
258
return true ;
215
259
}
216
260
261
+
262
+ // ////////////////////////////////////////////////
263
+ // enableTiltEngine()
264
+ //
265
+ // Enables the tilt detection feature.
266
+ //
267
+ // Parameter:
268
+ // enable - enables the tilt feature
269
+ //
217
270
bool QwDevKX13X::enableTiltEngine (bool enable)
218
271
{
219
272
int retVal;
@@ -234,9 +287,14 @@ bool QwDevKX13X::enableTiltEngine(bool enable)
234
287
return true ;
235
288
}
236
289
237
- // Address: 0x21, bits[3:0] - default value is 0x06 (50Hz)
238
- // Sets the refresh rate of the accelerometer's data.
239
- // 0.781 * (2 * (n)) derived from pg. 26 of Techincal Reference Manual
290
+ // ////////////////////////////////////////////////
291
+ // setOutputDataRate()
292
+ //
293
+ // Changes the rate at which accelerometer data is generated.
294
+ //
295
+ // Parameter:
296
+ // rate - determines the rate to be applied.
297
+ //
240
298
bool QwDevKX13X::setOutputDataRate (uint8_t rate)
241
299
{
242
300
@@ -261,6 +319,14 @@ bool QwDevKX13X::setOutputDataRate(uint8_t rate)
261
319
return true ;
262
320
}
263
321
322
+ // ////////////////////////////////////////////////
323
+ // setTapDataRate()
324
+ //
325
+ // Changes the rate at which tap data is generated.
326
+ //
327
+ // Parameter:
328
+ // rate - determines the rate to be applied.
329
+ //
264
330
bool QwDevKX13X::setTapDataRate (uint8_t rate)
265
331
{
266
332
@@ -284,8 +350,12 @@ bool QwDevKX13X::setTapDataRate(uint8_t rate)
284
350
285
351
return true ;
286
352
}
287
- // Address:0x21 , bit[3:0]: default value is: 0x06 (50Hz)
288
- // Gets the accelerometer's output data rate.
353
+
354
+ // ////////////////////////////////////////////////
355
+ // getOutputDataRate()
356
+ //
357
+ // Retrieves the output data rate of the acceleromter.
358
+ //
289
359
float QwDevKX13X::getOutputDataRate ()
290
360
{
291
361
int retVal;
@@ -302,11 +372,15 @@ float QwDevKX13X::getOutputDataRate()
302
372
}
303
373
304
374
305
- // Address: 0x22, bit[7:4] default value is 0000.
306
- // This register controls the various interrupt settings, all of which can be
307
- // set here. Note: trying to set just one will set the others to their default
308
- // state.
309
- // Thish configures all of the bits related to the interrupt pin.
375
+ // ////////////////////////////////////////////////
376
+ // configureInterruptPin()
377
+ //
378
+ // This allows you to configure the entire interrupt register
379
+ //
380
+ // Parameter:
381
+ // pinVal - register value to set, note that this overwrites
382
+ // everything in the register.
383
+ //
310
384
bool QwDevKX13X::configureInterruptPin (uint8_t pinVal){
311
385
312
386
int retVal;
@@ -319,6 +393,16 @@ bool QwDevKX13X::configureInterruptPin(uint8_t pinVal){
319
393
return true ;
320
394
}
321
395
396
+
397
+ // ////////////////////////////////////////////////
398
+ // enablePhysInterrupt()
399
+ //
400
+ // Enables interrupts to be routed to the interrupt pins.
401
+ //
402
+ // Parameters:
403
+ // enable - Enables interrupts to report to the physical interrupt pins
404
+ // pin - This determines which pin to route the interrupts.
405
+ //
322
406
bool QwDevKX13X::enablePhysInterrupt (bool enable, uint8_t pin)
323
407
{
324
408
int retVal;
@@ -353,65 +437,136 @@ bool QwDevKX13X::enablePhysInterrupt(bool enable, uint8_t pin)
353
437
return true ;
354
438
}
355
439
356
- bool QwDevKX13X::setPinMode (bool activeLow)
440
+ // ////////////////////////////////////////////////
441
+ // setPinMode()
442
+ //
443
+ // Sets the active state of the physical interupt pins
444
+ //
445
+ // Parameters:
446
+ // enable - Enables interrupts to report to the physical interrupt pins
447
+ // pin - This determines which pin to route the interrupts.
448
+ //
449
+ bool QwDevKX13X::setPinMode (bool activeLow, uint8_t pin)
357
450
{
358
451
int retVal;
359
452
uint8_t tempVal;
360
453
361
- retVal = readRegisterRegion (SFE_KX13X_INC1, &tempVal, 1 );
362
-
363
- if ( retVal != 0 )
454
+ if ( pin > 2 )
364
455
return false ;
365
456
366
- tempVal = tempVal | (activeLow << 5 );
457
+ if ( pin == 1 )
458
+ {
459
+ retVal = readRegisterRegion (SFE_KX13X_INC1, &tempVal, 1 );
367
460
368
- retVal = writeRegisterByte (SFE_KX13X_INC1, tempVal);
461
+ if ( retVal != 0 )
462
+ return false ;
369
463
370
- if ( retVal != 0 )
371
- return false ;
464
+ tempVal = tempVal | (activeLow << 5 );
465
+
466
+ writeRegisterByte (SFE_KX13X_INC1, tempVal);
467
+ }
468
+
469
+ if ( pin == 2 )
470
+ {
471
+ retVal = readRegisterRegion (SFE_KX13X_INC5, &tempVal, 1 );
472
+
473
+ if ( retVal != 0 )
474
+ return false ;
475
+
476
+ tempVal = tempVal | (activeLow << 5 );
477
+
478
+ writeRegisterByte (SFE_KX13X_INC5, tempVal);
479
+ }
372
480
373
481
return true ;
374
482
}
375
483
376
- bool QwDevKX13X::setLatchControl (bool latch)
484
+ // ////////////////////////////////////////////////
485
+ // setLatchControl()
486
+ //
487
+ // Determines whether interrupts are pulsed (default) or latched.
488
+ // If they are latched then the interrupt must be released by reading
489
+ // the INT_REL register - clearInterrupt();
490
+ //
491
+ // Parameters:
492
+ // latch - True enables latch behavior, false enables pulse behavior (default)
493
+ //
494
+ bool QwDevKX13X::setLatchControl (bool latch, uint8_t pin)
377
495
{
378
496
int retVal;
379
497
uint8_t tempVal;
498
+
499
+ if ( pin > 2 )
500
+ return false ;
501
+
502
+ if ( pin == 1 )
503
+ {
504
+ retVal = readRegisterRegion (SFE_KX13X_INC1, &tempVal, 1 );
380
505
381
- retVal = readRegisterRegion (SFE_KX13X_INC1, &tempVal, 1 );
506
+ if ( retVal != 0 )
507
+ return false ;
382
508
383
- if ( retVal != 0 )
384
- return false ;
509
+ tempVal = tempVal | (latch << 3 );
385
510
386
- tempVal = tempVal | (latch << 3 );
511
+ writeRegisterByte (SFE_KX13X_INC1, tempVal);
512
+ }
387
513
388
- retVal = writeRegisterByte (SFE_KX13X_INC1, tempVal);
389
514
390
- if ( retVal != 0 )
391
- return false ;
515
+ if ( pin == 2 )
516
+ {
517
+ retVal = readRegisterRegion (SFE_KX13X_INC5, &tempVal, 1 );
518
+
519
+ if ( retVal != 0 )
520
+ return false ;
521
+
522
+ tempVal = tempVal | (latch << 3 );
523
+
524
+ writeRegisterByte (SFE_KX13X_INC5, tempVal);
525
+ }
392
526
393
527
return true ;
394
528
}
395
529
396
- bool QwDevKX13X::setPulseWidth (uint8_t width)
530
+ // ////////////////////////////////////////////////
531
+ // setPulseWidth()
532
+ //
533
+ // Determines the width of the interrupt pulse
534
+ //
535
+ // Parameters:
536
+ // width - The width setting to be applied.
537
+ // pin - the pin to be configured.
538
+ //
539
+ bool QwDevKX13X::setPulseWidth (uint8_t width, uint8_t pin)
397
540
{
398
541
int retVal;
399
542
uint8_t tempVal;
400
543
401
- if ( width > 4 )
544
+ if ( width > 4 | pin > 2 )
402
545
return false ;
403
546
404
- retVal = readRegisterRegion (SFE_KX13X_INC1, &tempVal, 1 );
547
+ if ( pin == 1 )
548
+ {
549
+ retVal = readRegisterRegion (SFE_KX13X_INC1, &tempVal, 1 );
405
550
406
- if ( retVal != 0 )
407
- return false ;
551
+ if ( retVal != 0 )
552
+ return false ;
408
553
409
- tempVal = tempVal | (width << 6 );
554
+ tempVal = tempVal | (width << 6 );
410
555
411
- retVal = writeRegisterByte (SFE_KX13X_INC1, tempVal);
556
+ writeRegisterByte (SFE_KX13X_INC1, tempVal);
557
+ }
412
558
413
- if ( retVal != 0 )
414
- return false ;
559
+ if ( pin == 2 )
560
+ {
561
+ retVal = readRegisterRegion (SFE_KX13X_INC5, &tempVal, 1 );
562
+
563
+ if ( retVal != 0 )
564
+ return false ;
565
+
566
+ tempVal = tempVal | (width << 6 );
567
+
568
+ writeRegisterByte (SFE_KX13X_INC5, tempVal);
569
+ }
415
570
416
571
return true ;
417
572
}
0 commit comments