Skip to content

Commit 2ef114f

Browse files
committed
Adds functions for wake and sleep engines
1 parent 3c2525e commit 2ef114f

File tree

3 files changed

+196
-17
lines changed

3 files changed

+196
-17
lines changed

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun KX13X Arduino Library
2-
version=2.0.0
2+
version=2.0.1
33
author=SparkFun Electronics <[email protected]>
44
maintainer=Elias Santistevan @ SparkFun Electronics
55
sentence=Communicates and configures the SparkFun KX132/KX134 Accelerometer.

src/SparkFun_Qwiic_KX13X.cpp

+177-10
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,64 @@ bool QwDevKX13X::enableTiltEngine(bool enable)
265265
return true;
266266
}
267267

268+
269+
//////////////////////////////////////////////////
270+
// enableWakeEngine()
271+
//
272+
// Enables the wake detection feature.
273+
//
274+
// Parameter:
275+
// enable - enables/disables the wake detection feature
276+
//
277+
bool QwDevKX13X::enableWakeEngine(bool enable)
278+
{
279+
int retVal;
280+
uint8_t tempVal;
281+
282+
retVal = readRegisterRegion(SFE_KX13X_CNTL4, &tempVal, 1);
283+
284+
if( retVal != 0 )
285+
return false;
286+
287+
tempVal = tempVal | (enable << 5);
288+
289+
retVal = writeRegisterByte(SFE_KX13X_CNTL4, tempVal);
290+
291+
if( retVal != 0 )
292+
return false;
293+
294+
return true;
295+
}
296+
297+
//////////////////////////////////////////////////
298+
// enableSleepEngine()
299+
//
300+
// Enables the sleep feature.
301+
//
302+
// Parameter:
303+
// enable - enables/disables the sleep feature
304+
//
305+
bool QwDevKX13X::enableSleepEngine(bool enable)
306+
{
307+
int retVal;
308+
uint8_t tempVal;
309+
310+
retVal = readRegisterRegion(SFE_KX13X_CNTL4, &tempVal, 1);
311+
312+
if( retVal != 0 )
313+
return false;
314+
315+
tempVal = tempVal | (enable << 4);
316+
317+
retVal = writeRegisterByte(SFE_KX13X_CNTL4, tempVal);
318+
319+
if( retVal != 0 )
320+
return false;
321+
322+
return true;
323+
}
324+
325+
268326
//////////////////////////////////////////////////
269327
// setOutputDataRate()
270328
//
@@ -329,6 +387,71 @@ bool QwDevKX13X::setTapDataRate(uint8_t rate)
329387
return true;
330388
}
331389

390+
391+
//////////////////////////////////////////////////
392+
// setTiltDataRate()
393+
//
394+
// Changes the rate at which the tilt position is polled.
395+
//
396+
// Parameter:
397+
// rate - determines the rate to be applied.
398+
//
399+
bool QwDevKX13X::setTiltDataRate(uint8_t rate)
400+
{
401+
402+
if( rate > 3 )
403+
return false;
404+
405+
uint8_t tempVal;
406+
int retVal;
407+
408+
retVal = readRegisterRegion(SFE_KX13X_CNTL3, &tempVal, 1);
409+
410+
if( retVal != 0 )
411+
return false;
412+
413+
tempVal = tempVal | (rate << 6);
414+
415+
retVal = writeRegisterByte(SFE_KX13X_CNTL3, tempVal);
416+
417+
if( retVal != 0 )
418+
return false;
419+
420+
return true;
421+
}
422+
423+
424+
//////////////////////////////////////////////////
425+
// setWakeDataRate()
426+
//
427+
// Changes the rate at which the wake function is performed.
428+
//
429+
// Parameter:
430+
// rate - determines the rate to be applied.
431+
//
432+
bool QwDevKX13X::setWakeDataRate(uint8_t rate)
433+
{
434+
435+
if( rate > 7 )
436+
return false;
437+
438+
uint8_t tempVal;
439+
int retVal;
440+
441+
retVal = readRegisterRegion(SFE_KX13X_CNTL3, &tempVal, 1);
442+
443+
if( retVal != 0 )
444+
return false;
445+
446+
tempVal = tempVal | rate;
447+
448+
retVal = writeRegisterByte(SFE_KX13X_CNTL3, tempVal);
449+
450+
if( retVal != 0 )
451+
return false;
452+
453+
return true;
454+
}
332455
//////////////////////////////////////////////////
333456
// getOutputDataRate()
334457
//
@@ -519,7 +642,7 @@ bool QwDevKX13X::setPulseWidth(uint8_t width, uint8_t pin)
519642
int retVal;
520643
uint8_t tempVal;
521644

522-
if( width > 4 | pin > 2 )
645+
if( (width > 4) | (pin > 2) )
523646
return false;
524647

525648
if( pin == 1 )
@@ -1173,6 +1296,57 @@ bool QwDevKX13X::getRawAccelData(rawOutputData *rawAccelData){
11731296
return true;
11741297
}
11751298

1299+
//////////////////////////////////////////////////
1300+
// forceSleep()
1301+
//
1302+
// Forces the accelerometer into a sleep state.
1303+
//
1304+
bool QwDevKX13X::forceSleep()
1305+
{
1306+
int retVal;
1307+
uint8_t tempVal;
1308+
uint8_t forceSleep = 0x01;
1309+
1310+
retVal = readRegisterRegion(SFE_KX13X_CNTL5, &tempVal, 1);
1311+
1312+
if( retVal != 0 )
1313+
return false;
1314+
1315+
tempVal |= forceSleep;
1316+
1317+
retVal = writeRegisterByte(SFE_KX13X_CNTL5, tempVal);
1318+
1319+
if( retVal != 0 )
1320+
return false;
1321+
1322+
return true;
1323+
}
1324+
1325+
//////////////////////////////////////////////////
1326+
// forceWake()
1327+
//
1328+
// Forces the accelerometer into a sleep state.
1329+
//
1330+
bool QwDevKX13X::forceWake()
1331+
{
1332+
int retVal;
1333+
uint8_t tempVal;
1334+
uint8_t forceWake = 0x02;
1335+
1336+
retVal = readRegisterRegion(SFE_KX13X_CNTL5, &tempVal, 1);
1337+
1338+
if( retVal != 0 )
1339+
return false;
1340+
1341+
tempVal |= forceWake;
1342+
1343+
retVal = writeRegisterByte(SFE_KX13X_CNTL5, tempVal);
1344+
1345+
if( retVal != 0 )
1346+
return false;
1347+
1348+
return true;
1349+
}
11761350

11771351
//////////////////////////////////////////////////////////////////////////////////
11781352
// readRegisterRegion()
@@ -1223,10 +1397,7 @@ int QwDevKX13X::writeRegisterByte(uint8_t reg, uint8_t data)
12231397
}
12241398

12251399

1226-
//***************************************** KX132 ******************************************
1227-
//******************************************************************************************
1228-
//******************************************************************************************
1229-
//******************************************************************************************
1400+
//***************************************** KX132 *********************************************************
12301401

12311402

12321403
//////////////////////////////////////////////////////////////////////////////////
@@ -1323,11 +1494,7 @@ bool QwDevKX132::convAccelData(outputData *userAccel, rawOutputData *rawAccelDat
13231494
return true;
13241495
}
13251496

1326-
//***************************************** KX134 ******************************************
1327-
//******************************************************************************************
1328-
//******************************************************************************************
1329-
//******************************************************************************************
1330-
1497+
//***************************************** KX134 ******************************************************
13311498

13321499
//////////////////////////////////////////////////////////////////////////////////
13331500
// init()

src/SparkFun_Qwiic_KX13X.h

+18-6
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,38 @@ class QwDevKX13X
119119
bool setRange(uint8_t);
120120
bool setInterruptPin(bool enable, uint8_t polarity = 0, uint8_t pulseWidth = 0, bool latchControl = false);
121121
bool enableDataEngine(bool enable = true);
122-
bool enableTapEngine(bool enable = true);
123-
bool enableTiltEngine(bool enable = true);
124122
bool setOutputDataRate(uint8_t);
125123
float getOutputDataRate();
126-
bool routeHardwareInterrupt(uint8_t, uint8_t pin = 1);
127124
bool dataReady();
128125
bool runCommandTest();
129126
uint8_t readAccelState();
130127
bool getRawAccelData(rawOutputData*);
131-
bool setTapDataRate(uint8_t rate);
132128
float readOutputDataRate();
133129

130+
// Tap/Double settings
131+
bool enableTapEngine(bool enable = true);
132+
bool setTapDataRate(uint8_t rate);
133+
bool enableDirecTapInterupt(bool enable = true);
134+
bool enableDoubleTapInterrupt(bool enable = true);
135+
136+
// Tilt Settings
137+
bool enableTiltEngine(bool enable = true);
138+
bool setTiltDataRate(uint8_t rate);
139+
140+
// Wake/Sleep Settings
141+
bool setWakeDataRate(uint8_t rate);
142+
bool enableSleepEngine(bool enable = true);
143+
bool enableWakeEngine(bool enable = true);
144+
bool forceWake();
145+
bool forceSleep();
146+
134147
// Interrupt Settings
135148
bool configureInterruptPin(uint8_t pinVal);
149+
bool routeHardwareInterrupt(uint8_t, uint8_t pin = 1);
136150
bool enablePhysInterrupt(bool enable = true, uint8_t pin = 1);
137151
bool setPinMode(bool activeLow = true, uint8_t pin = 1);
138152
bool setLatchControl(bool latch = true, uint8_t pin = 1);
139153
bool setPulseWidth(uint8_t width, uint8_t pin = 1);
140-
bool enableDirecTapInterupt(bool enable = true);
141-
bool enableDoubleTapInterrupt(bool enable = true);
142154
bool clearInterrupt();
143155
bool tapDetected();
144156
int8_t getDirection();

0 commit comments

Comments
 (0)