Skip to content

Commit ecbb70b

Browse files
committed
Adds buffer example
1 parent 8558af6 commit ecbb70b

File tree

4 files changed

+79
-44
lines changed

4 files changed

+79
-44
lines changed

examples/Ex2_BasicSPI/Ex2_BasicSPI.ino

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/Ex4_Buffer/Ex4_Buffer.ino

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <Wire.h>
2+
#include "SparkFun_Qwiic_KX13X.h"
3+
4+
QwiicKX132 kxAccel;
5+
outputData myData; // This will hold the accelerometer's output.
6+
int dataReadyPin = D1;
7+
8+
void setup() {
9+
10+
pinMode(dataReadyPin, INPUT);
11+
12+
while(!Serial){
13+
delay(50);
14+
}
15+
16+
Serial.begin(115200);
17+
Serial.println("Welcome.");
18+
19+
Wire.begin();
20+
if( !kxAccel.begin() ){
21+
Serial.println("Could not communicate with the the KX13X. Freezing.");
22+
while(1);
23+
}
24+
else
25+
Serial.println("Ready.");
26+
27+
28+
29+
if( !kxAccel.initialize(BUFFER_SETTINGS)){
30+
Serial.println("Could not initialize the chip. Freezing.");
31+
while(1);
32+
}
33+
else
34+
Serial.println("Initialized...");
35+
36+
// kxAccel.setRange(KX132_RANGE16G);
37+
38+
}
39+
40+
void loop() {
41+
42+
43+
if( digitalRead(dataReadyPin) == HIGH ){
44+
45+
myData = kxAccel.getAccelData();
46+
Serial.print("X: ");
47+
Serial.print(myData.xData, 4);
48+
Serial.print("g ");
49+
Serial.print(" Y: ");
50+
Serial.print(myData.zData, 4);
51+
Serial.print("g ");
52+
Serial.print(" Z: ");
53+
Serial.print(myData.zData, 4);
54+
Serial.println("g ");
55+
56+
}
57+
58+
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz
59+
}

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ bool QwiicKX13xCore::initialize(uint8_t settings)
8383
returnError = writeRegister(KX13X_CNTL1, 0x00, INT_SETTINGS, 0);
8484
}
8585
if( settings == BUFFER_SETTINGS ){
86-
setInterruptPin(true);
87-
routeHardwareInterrupt(HI_WATERMARK);
86+
setInterruptPin(true, 1);
87+
routeHardwareInterrupt(HI_BUFFER_FULL);
8888
enableBuffer(true, true);
8989
setBufferOperation(BUFFER_MODE_FIFO, BUFFER_16BIT_SAMPLES);
9090
returnError = writeRegister(KX13X_CNTL1, 0x00, INT_SETTINGS, 0);
@@ -406,10 +406,23 @@ bool QwiicKX13xCore::writeBit(uint8_t regAddr, uint8_t bitAddr, bool bitToWrite)
406406

407407
bool QwiicKX13xCore::getRawAccelData(rawOutputData *rawAccelData){
408408

409-
uint8_t tempRegData[TOTAL_ACCEL_DATA] {};
409+
410+
uint8_t tempRegVal;
410411
KX13X_STATUS_t returnError;
412+
uint8_t tempRegData[TOTAL_ACCEL_DATA_16BIT] {};
413+
414+
returnError = readRegister(&tempRegVal, KX13X_INC4);
415+
if( returnError != KX13X_SUCCESS )
416+
return false;
417+
418+
if( tempRegVal & 0x40 ){ // If Buffer interrupt is enabled, then we'll read accelerometer data from buffer register.
419+
returnError = readMultipleRegisters(KX13X_BUF_READ, tempRegData, TOTAL_ACCEL_DATA_16BIT);
420+
}
421+
else
422+
returnError = readMultipleRegisters(KX13X_XOUT_L, tempRegData, TOTAL_ACCEL_DATA_16BIT);
423+
424+
411425

412-
returnError = readMultipleRegisters(KX13X_XOUT_L, tempRegData, TOTAL_ACCEL_DATA);
413426
if( returnError == KX13X_SUCCESS ) {
414427
rawAccelData->xData = tempRegData[XLSB];
415428
rawAccelData->xData |= (static_cast<uint16_t>(tempRegData[XMSB]) << 8);
@@ -423,6 +436,7 @@ bool QwiicKX13xCore::getRawAccelData(rawOutputData *rawAccelData){
423436
return false;
424437
}
425438

439+
426440
KX13X_STATUS_t QwiicKX13xCore::readRegister(uint8_t *dataPointer, uint8_t reg)
427441
{
428442

src/SparkFun_Qwiic_KX13X.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Distributed as-is; no warranty is given.
2727

2828
#define KX132_WHO_AM_I 0x3D
2929
#define KX134_WHO_AM_I 0x46
30-
#define TOTAL_ACCEL_DATA 6
30+
#define TOTAL_ACCEL_DATA_16BIT 6
31+
#define TOTAL_ACCEL_DATA_8BIT 3
3132
#define MAX_BUFFER_LENGTH 32
3233

3334
#define XLSB 0

0 commit comments

Comments
 (0)