Skip to content

Commit eeaea80

Browse files
committed
Adds Arduino interface classes for each accel version
* Fixes typos in regs header file * Adds a number of functions * Creates two sub classes for each accel version that inherits a class with all the shared functionality
1 parent e853a88 commit eeaea80

File tree

5 files changed

+441
-201
lines changed

5 files changed

+441
-201
lines changed

examples/Ex1_BasicReadings/Ex1_BasicReadings.ino

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,37 @@ Distributed as-is; no warranty is given.
1515
*******************************************************/
1616

1717
#include <Wire.h>
18-
#include "SparkFun_Qwiic_KX13X.h"
18+
#include "SparkFun_KX13X.h"
19+
20+
SparkFun_KX132 kxAccel;
1921

20-
QwiicKX132 kxAccel;
21-
//QwiicKX134 kxAccel; // Uncomment this if using the KX134 - check your board
22-
//if unsure.
2322
outputData myData; // This will hold the accelerometer's output.
2423

2524
void setup() {
2625

26+
Wire.begin();
27+
Serial.begin(115200);
28+
Serial.println("Welcome.");
2729

28-
while(!Serial){
30+
while(!Serial)
2931
delay(50);
30-
}
3132

32-
Serial.begin(115200);
33-
Serial.println("Welcome.");
3433

35-
Wire.begin();
36-
if( !kxAccel.begin() ){
34+
if( !kxAccel.begin() )
35+
{
3736
Serial.println("Could not communicate with the the KX13X. Freezing.");
3837
while(1);
39-
}
40-
else
41-
Serial.println("Ready.");
42-
43-
44-
45-
if( !kxAccel.initialize(DEFAULT_SETTINGS)){ // Loading default settings.
46-
Serial.println("Could not initialize the chip.");
47-
while(1);
48-
}
49-
else
50-
Serial.println("Initialized...");
38+
}
5139

52-
// kxAccel.setRange(KX132_RANGE16G);
53-
// kxAccel.setRange(KX134_RANGE32G); // For a larger range uncomment
40+
Serial.println("Ready.");
41+
42+
kxAccel.setRange(KX134_RANGE32G); // For a larger range uncomment
5443

5544
}
5645

5746
void loop() {
5847

59-
myData = kxAccel.getAccelData();
48+
kxAccel.getAccelData(&myData);
6049
Serial.print("X: ");
6150
Serial.print(myData.xData, 4);
6251
Serial.print("g ");

src/SparkFun_KX13X.h

Lines changed: 132 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#pragma once
2-
#include "sfe_ism330dhcx.h"
3-
#include "sfe_bus.h"
2+
#include "SparkFun_Qwiic_KX13X.h"
43
#include <Wire.h>
54
#include <SPI.h>
65

7-
class SparkFun_KX13X : public QwDevKX13X
6+
class SparkFun_KX132 : public QwDevKX132
87
{
98

109
public:
1110

12-
SparkFun_KX13X() {};
11+
SparkFun_KX132() {};
1312

1413
///////////////////////////////////////////////////////////////////////
1514
// begin()
@@ -31,7 +30,7 @@ class SparkFun_KX13X : public QwDevKX13X
3130
//
3231
// Version 1:
3332
// User skips passing in an I2C object which then defaults to Wire.
34-
bool begin(uint8_t deviceAddress = ISM330DHCX_ADDRESS_HIGH)
33+
bool begin(uint8_t deviceAddress = KX13X_ADDRESS_HIGH)
3534
{
3635
// Setup I2C object and pass into the superclass
3736
setCommunicationBus(_i2cBus, deviceAddress);
@@ -40,12 +39,12 @@ class SparkFun_KX13X : public QwDevKX13X
4039
_i2cBus.init();
4140

4241
// Initialize the system - return results
43-
return this->QwDevKX13X::init();
42+
return this->QwDevKX132::init();
4443
}
4544

4645
//Version 2:
4746
// User passes in an I2C object and an address (optional).
48-
bool begin(TwoWire &wirePort, uint8_t deviceAddress = ISM330DHCX_ADDRESS_HIGH)
47+
bool begin(TwoWire &wirePort, uint8_t deviceAddress = KX13X_ADDRESS_HIGH)
4948
{
5049
// Setup I2C object and pass into the superclass
5150
setCommunicationBus(_i2cBus, deviceAddress);
@@ -54,7 +53,7 @@ class SparkFun_KX13X : public QwDevKX13X
5453
_i2cBus.init(wirePort, true);
5554

5655
// Initialize the system - return results
57-
return this->QwDevKX13X::init();
56+
return this->QwDevKX132::init();
5857
}
5958

6059
private:
@@ -64,11 +63,11 @@ class SparkFun_KX13X : public QwDevKX13X
6463

6564
};
6665

67-
class SparkFun_KX13X_SPI : public QwDevKX13X
66+
class SparkFun_KX132_SPI : public QwDevKX132
6867
{
6968
public:
7069

71-
SparkFun_KX13X_SPI() {};
70+
SparkFun_KX132_SPI() {};
7271

7372
///////////////////////////////////////////////////////////////////////
7473
// begin()
@@ -103,7 +102,7 @@ class SparkFun_KX13X_SPI : public QwDevKX13X
103102
_spiBus.init(cs, true);
104103

105104
// Initialize the system - return results
106-
return this->QwDevKX13X::init();
105+
return this->QwDevKX132::init();
107106
}
108107

109108
bool begin(SPIClass &spiPort, SPISettings ismSettings, uint8_t cs)
@@ -115,7 +114,128 @@ class SparkFun_KX13X_SPI : public QwDevKX13X
115114
_spiBus.init(spiPort, ismSettings, cs, true);
116115

117116
// Initialize the system - return results
118-
return this->QwDevKX13X::init();
117+
return this->QwDevKX132::init();
118+
}
119+
120+
private:
121+
122+
// SPI bus class
123+
SfeSPI _spiBus;
124+
125+
};
126+
127+
class SparkFun_KX134 : public QwDevKX134
128+
{
129+
130+
public:
131+
132+
SparkFun_KX134() {};
133+
134+
///////////////////////////////////////////////////////////////////////
135+
// begin()
136+
//
137+
// This method is called to initialize the ISM330DHCX library and connect to
138+
// the ISM330DHCX device. This method must be called before calling any other method
139+
// that interacts with the device.
140+
//
141+
// This method follows the standard startup pattern in SparkFun Arduino
142+
// libraries.
143+
//
144+
// Parameter Description
145+
// --------- ----------------------------
146+
// wirePort optional. The Wire port. If not provided, the default port is used
147+
// address optional. I2C Address. If not provided, the default address is used.
148+
// retval true on success, false on startup failure
149+
//
150+
// This methond is overridden, implementing two versions.
151+
//
152+
// Version 1:
153+
// User skips passing in an I2C object which then defaults to Wire.
154+
bool begin(uint8_t deviceAddress = KX13X_ADDRESS_HIGH)
155+
{
156+
// Setup I2C object and pass into the superclass
157+
setCommunicationBus(_i2cBus, deviceAddress);
158+
159+
// Initialize the I2C buss class i.e. setup default Wire port
160+
_i2cBus.init();
161+
162+
// Initialize the system - return results
163+
return this->QwDevKX134::init();
164+
}
165+
166+
//Version 2:
167+
// User passes in an I2C object and an address (optional).
168+
bool begin(TwoWire &wirePort, uint8_t deviceAddress = KX13X_ADDRESS_HIGH)
169+
{
170+
// Setup I2C object and pass into the superclass
171+
setCommunicationBus(_i2cBus, deviceAddress);
172+
173+
// Give the I2C port provided by the user to the I2C bus class.
174+
_i2cBus.init(wirePort, true);
175+
176+
// Initialize the system - return results
177+
return this->QwDevKX134::init();
178+
}
179+
180+
private:
181+
182+
//I2C bus class
183+
QwI2C _i2cBus;
184+
185+
};
186+
187+
class SparkFun_KX134_SPI : public QwDevKX134
188+
{
189+
public:
190+
191+
SparkFun_KX134_SPI() {};
192+
193+
///////////////////////////////////////////////////////////////////////
194+
// begin()
195+
//
196+
// This method is called to initialize the ISM330DHCX library and connect to
197+
// the ISM330DHCX device. This method must be called before calling any other method
198+
// that interacts with the device.
199+
//
200+
// This method follows the standard startup pattern in SparkFun Arduino
201+
// libraries.
202+
//
203+
// Parameter Description
204+
// --------- ----------------------------
205+
// spiPort optional. The SPI port. If not provided, the default port is used
206+
// SPISettings optional. SPI "transaction" settings are need for every data transfer.
207+
// Default used if not provided.
208+
// Chip Select mandatory. The chip select pin ("CS") can't be guessed, so must be provided.
209+
// retval true on success, false on startup failure
210+
//
211+
// This methond is overridden, implementing two versions.
212+
//
213+
// Version 1:
214+
// User skips passing in an SPI object which then defaults to SPI.
215+
216+
bool begin(uint8_t cs)
217+
{
218+
// Setup a SPI object and pass into the superclass
219+
setCommunicationBus(_spiBus);
220+
221+
// Initialize the SPI bus class with the chip select pin, SPI port defaults to SPI,
222+
// and SPI settings are set to class defaults.
223+
_spiBus.init(cs, true);
224+
225+
// Initialize the system - return results
226+
return this->QwDevKX134::init();
227+
}
228+
229+
bool begin(SPIClass &spiPort, SPISettings ismSettings, uint8_t cs)
230+
{
231+
// Setup a SPI object and pass into the superclass
232+
setCommunicationBus(_spiBus);
233+
234+
// Initialize the SPI bus class with provided SPI port, SPI setttings, and chip select pin.
235+
_spiBus.init(spiPort, ismSettings, cs, true);
236+
237+
// Initialize the system - return results
238+
return this->QwDevKX134::init();
119239
}
120240

121241
private:

src/SparkFun_KX13X_regs.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
12
#define SFE_KX13X_MAN_ID 0x00// Retuns "KION" in ASCII
3+
//
24
typedef struct
35
{
46
uint8_t man_id : 8;
5-
} sfe_kx13x_man_id_t
7+
} sfe_kx13x_man_id_t;
68

79
#define SFE_KX13X_PART_ID 0x01// Retuns "KION" in ASCII
810
typedef struct
@@ -505,7 +507,7 @@ typedef struct
505507
{
506508
uint8_t reserved_one : 1;
507509
uint8_t avc : 3;
508-
uint8_t reserved_one : 4;
510+
uint8_t reserved_two : 4;
509511
} sfe_kx13x_lp_cntl1_t;
510512

511513
#define SFE_KX13X_LP_CNTL2 0x3B
@@ -769,8 +771,8 @@ typedef struct
769771

770772
typedef struct
771773
{
772-
uint8_t SFE_KX13X_SUCCESS = 0x00,
773-
uint8_t SFE_KX13X_GENERAL_ERROR = 0x01,
774-
uint8_t SFE_KX13X_I2C_ERROR = 0x02
774+
uint8_t SFE_KX13X_SUCCESS = 0x00;
775+
uint8_t SFE_KX13X_GENERAL_ERROR = 0x01;
776+
uint8_t SFE_KX13X_I2C_ERROR = 0x03;
775777
} sfe_error_code_t;
776778

0 commit comments

Comments
 (0)