Skip to content

Commit 6124908

Browse files
authored
Merge pull request #13 from cparata/master
Add begin and end APIs and SPI support
2 parents 95d362d + f2dcb9b commit 6124908

File tree

14 files changed

+240
-180
lines changed

14 files changed

+240
-180
lines changed

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,39 @@ Arduino library to support the LSM6DSL 3D accelerometer and 3D gyroscope
33

44
## API
55

6-
This sensor uses I2C to communicate. It is then required to create a TwoWire interface before accessing to the sensors:
6+
This sensor uses I2C or SPI to communicate.
7+
For I2C it is then required to create a TwoWire interface before accessing to the sensors:
78

8-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
9-
dev_i2c->begin();
9+
TwoWire dev_i2c(I2C_SDA, I2C_SCL);
10+
dev_i2c.begin();
1011

11-
An instance can be created and enabled following the procedure below:
12+
For SPI it is then required to create a SPI interface before accessing to the sensors:
1213

13-
AccGyr = new LSM6DSLSensor(dev_i2c);
14-
AccGyr->Enable_X();
15-
AccGyr->Enable_G();
14+
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
15+
dev_spi.begin();
16+
17+
An instance can be created and enabled when the I2C bus is used following the procedure below:
18+
19+
LSM6DSLSensor AccGyr(&dev_i2c);
20+
AccGyr.begin();
21+
AccGyr.Enable_X();
22+
AccGyr.Enable_G();
23+
24+
An instance can be created and enabled when the SPI bus is used following the procedure below:
25+
26+
LSM6DSLSensor AccGyr(&dev_spi, CS_PIN);
27+
AccGyr.begin();
28+
AccGyr.Enable_X();
29+
AccGyr.Enable_G();
1630

1731
The access to the sensor values is done as explained below:
1832

1933
Read accelerometer and gyroscope.
2034

21-
AccGyr->Get_X_Axes(accelerometer);
22-
AccGyr->Get_G_Axes(gyroscope);
35+
int32_t accelerometer[3];
36+
int32_t gyroscope[3];
37+
AccGyr.Get_X_Axes(accelerometer);
38+
AccGyr.Get_G_Axes(gyroscope);
2339

2440
## Documentation
2541

examples/DISCO_IOT_6DOrientation/DISCO_IOT_6DOrientation.ino

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#define INT1 PD11
5050

5151
// Components.
52-
LSM6DSLSensor *AccGyr;
53-
TwoWire *dev_i2c;
52+
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
53+
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
5454

5555
//Interrupts.
5656
volatile int mems_event = 0;
@@ -68,26 +68,25 @@ void setup() {
6868
SerialPort.begin(9600);
6969

7070
// Initialize I2C bus.
71-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
72-
dev_i2c->begin();
71+
dev_i2c.begin();
7372

7473
//Interrupts.
7574
attachInterrupt(INT1, INT1Event_cb, RISING);
7675

7776
// Initlialize Components.
78-
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
79-
AccGyr->Enable_X();
77+
AccGyr.begin();
78+
AccGyr.Enable_X();
8079

8180
// Enable 6D Orientation.
82-
AccGyr->Enable_6D_Orientation();
81+
AccGyr.Enable_6D_Orientation();
8382
}
8483

8584
void loop() {
8685
if (mems_event)
8786
{
8887
mems_event = 0;
8988
LSM6DSL_Event_Status_t status;
90-
AccGyr->Get_Event_Status(&status);
89+
AccGyr.Get_Event_Status(&status);
9190
if (status.D6DOrientationStatus)
9291
{
9392
// Send 6D Orientation
@@ -115,12 +114,12 @@ void sendOrientation()
115114
uint8_t zl = 0;
116115
uint8_t zh = 0;
117116

118-
AccGyr->Get_6D_Orientation_XL(&xl);
119-
AccGyr->Get_6D_Orientation_XH(&xh);
120-
AccGyr->Get_6D_Orientation_YL(&yl);
121-
AccGyr->Get_6D_Orientation_YH(&yh);
122-
AccGyr->Get_6D_Orientation_ZL(&zl);
123-
AccGyr->Get_6D_Orientation_ZH(&zh);
117+
AccGyr.Get_6D_Orientation_XL(&xl);
118+
AccGyr.Get_6D_Orientation_XH(&xh);
119+
AccGyr.Get_6D_Orientation_YL(&yl);
120+
AccGyr.Get_6D_Orientation_YH(&yh);
121+
AccGyr.Get_6D_Orientation_ZL(&zl);
122+
AccGyr.Get_6D_Orientation_ZH(&zh);
124123

125124
if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
126125
{

examples/DISCO_IOT_DoubleTap/DISCO_IOT_DoubleTap.ino

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#define INT1 PD11
5050

5151
// Components.
52-
LSM6DSLSensor *AccGyr;
53-
TwoWire *dev_i2c;
52+
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
53+
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
5454

5555
//Interrupts.
5656
volatile int mems_event = 0;
@@ -65,25 +65,24 @@ void setup() {
6565
SerialPort.begin(9600);
6666

6767
// Initialize I2C bus.
68-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
69-
dev_i2c->begin();
68+
dev_i2c.begin();
7069

7170
//Interrupts.
7271
attachInterrupt(INT1, INT1Event_cb, RISING);
7372

7473
// Initlialize Components.
75-
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
76-
AccGyr->Enable_X();
74+
AccGyr.begin();
75+
AccGyr.Enable_X();
7776

7877
// Enable Double Tap Detection.
79-
AccGyr->Enable_Double_Tap_Detection();
78+
AccGyr.Enable_Double_Tap_Detection();
8079
}
8180

8281
void loop() {
8382
if (mems_event) {
8483
mems_event = 0;
8584
LSM6DSL_Event_Status_t status;
86-
AccGyr->Get_Event_Status(&status);
85+
AccGyr.Get_Event_Status(&status);
8786
if (status.DoubleTapStatus)
8887
{
8988
// Led blinking.

examples/DISCO_IOT_FreeFall/DISCO_IOT_FreeFall.ino

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#define INT1 PD11
5050

5151
// Components.
52-
LSM6DSLSensor *AccGyr;
53-
TwoWire *dev_i2c;
52+
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
53+
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
5454

5555
//Interrupts.
5656
volatile int mems_event = 0;
@@ -65,25 +65,24 @@ void setup() {
6565
SerialPort.begin(9600);
6666

6767
// Initialize I2C bus.
68-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
69-
dev_i2c->begin();
68+
dev_i2c.begin();
7069

7170
//Interrupts.
7271
attachInterrupt(INT1, INT1Event_cb, RISING);
7372

7473
// Initlialize Components.
75-
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
76-
AccGyr->Enable_X();
74+
AccGyr.begin();
75+
AccGyr.Enable_X();
7776

7877
// Enable Free Fall Detection.
79-
AccGyr->Enable_Free_Fall_Detection();
78+
AccGyr.Enable_Free_Fall_Detection();
8079
}
8180

8281
void loop() {
8382
if (mems_event) {
8483
mems_event = 0;
8584
LSM6DSL_Event_Status_t status;
86-
AccGyr->Get_Event_Status(&status);
85+
AccGyr.Get_Event_Status(&status);
8786
if (status.FreeFallStatus)
8887
{
8988
// Led blinking.

examples/DISCO_IOT_LSM6DSL_DataLog_Terminal/DISCO_IOT_LSM6DSL_DataLog_Terminal.ino

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
#define I2C2_SDA PB11
4848

4949
// Components.
50-
LSM6DSLSensor *AccGyr;
51-
TwoWire *dev_i2c;
50+
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
51+
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
5252

5353
void setup() {
5454
// Led.
@@ -57,13 +57,12 @@ void setup() {
5757
SerialPort.begin(9600);
5858

5959
// Initialize I2C bus.
60-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
61-
dev_i2c->begin();
60+
dev_i2c.begin();
6261

6362
// Initlialize components.
64-
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
65-
AccGyr->Enable_X();
66-
AccGyr->Enable_G();
63+
AccGyr.begin();
64+
AccGyr.Enable_X();
65+
AccGyr.Enable_G();
6766
}
6867

6968
void loop() {
@@ -76,8 +75,8 @@ void loop() {
7675
// Read accelerometer and gyroscope.
7776
int32_t accelerometer[3];
7877
int32_t gyroscope[3];
79-
AccGyr->Get_X_Axes(accelerometer);
80-
AccGyr->Get_G_Axes(gyroscope);
78+
AccGyr.Get_X_Axes(accelerometer);
79+
AccGyr.Get_G_Axes(gyroscope);
8180

8281
// Output data.
8382
SerialPort.print("Acc[mg]: ");

examples/DISCO_IOT_MultiEvent/DISCO_IOT_MultiEvent.ino

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
#define INT1 PD11
5757

5858
// Components.
59-
LSM6DSLSensor *AccGyr;
59+
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
60+
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
6061

6162
//Interrupts.
6263
volatile int mems_event = 0;
@@ -66,43 +67,41 @@ char report[256];
6667

6768
void INT1Event_cb();
6869
void sendOrientation();
69-
TwoWire *dev_i2c;
7070

7171
void setup() {
7272
// Initialize serial for output.
7373
SerialPort.begin(9600);
7474

7575
// Initialize I2C bus.
76-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
77-
dev_i2c->begin();
76+
dev_i2c.begin();
7877

7978
//Interrupts.
8079
attachInterrupt(INT1, INT1Event_cb, RISING);
8180

8281
// Initlialize Components.
83-
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
84-
AccGyr->Enable_X();
82+
AccGyr.begin();
83+
AccGyr.Enable_X();
8584

8685
// Enable all HW events.
87-
AccGyr->Enable_Pedometer();
88-
AccGyr->Enable_Tilt_Detection();
89-
AccGyr->Enable_Free_Fall_Detection();
90-
AccGyr->Enable_Single_Tap_Detection();
91-
AccGyr->Enable_Double_Tap_Detection();
92-
AccGyr->Enable_6D_Orientation();
86+
AccGyr.Enable_Pedometer();
87+
AccGyr.Enable_Tilt_Detection();
88+
AccGyr.Enable_Free_Fall_Detection();
89+
AccGyr.Enable_Single_Tap_Detection();
90+
AccGyr.Enable_Double_Tap_Detection();
91+
AccGyr.Enable_6D_Orientation();
9392
}
9493

9594
void loop() {
9695
if (mems_event)
9796
{
9897
mems_event = 0;
9998
LSM6DSL_Event_Status_t status;
100-
AccGyr->Get_Event_Status(&status);
99+
AccGyr.Get_Event_Status(&status);
101100

102101
if (status.StepStatus)
103102
{
104103
// New step detected, so print the step counter
105-
AccGyr->Get_Step_Counter(&step_count);
104+
AccGyr.Get_Step_Counter(&step_count);
106105
snprintf(report, sizeof(report), "Step counter: %d", step_count);
107106
SerialPort.println(report);
108107
}
@@ -153,12 +152,12 @@ void sendOrientation()
153152
uint8_t zl = 0;
154153
uint8_t zh = 0;
155154

156-
AccGyr->Get_6D_Orientation_XL(&xl);
157-
AccGyr->Get_6D_Orientation_XH(&xh);
158-
AccGyr->Get_6D_Orientation_YL(&yl);
159-
AccGyr->Get_6D_Orientation_YH(&yh);
160-
AccGyr->Get_6D_Orientation_ZL(&zl);
161-
AccGyr->Get_6D_Orientation_ZH(&zh);
155+
AccGyr.Get_6D_Orientation_XL(&xl);
156+
AccGyr.Get_6D_Orientation_XH(&xh);
157+
AccGyr.Get_6D_Orientation_YL(&yl);
158+
AccGyr.Get_6D_Orientation_YH(&yh);
159+
AccGyr.Get_6D_Orientation_ZL(&zl);
160+
AccGyr.Get_6D_Orientation_ZH(&zh);
162161

163162
if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
164163
{

examples/DISCO_IOT_Pedometer/DISCO_IOT_Pedometer.ino

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#define INT1 PD11
5050

5151
// Components.
52-
LSM6DSLSensor *AccGyr;
53-
TwoWire *dev_i2c;
52+
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
53+
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
5454

5555
//Interrupts.
5656
volatile int mems_event = 0;
@@ -70,18 +70,17 @@ void setup() {
7070
SerialPort.begin(9600);
7171

7272
// Initialize I2C bus.
73-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
74-
dev_i2c->begin();
73+
dev_i2c.begin();
7574

7675
//Interrupts.
7776
attachInterrupt(INT1, INT1Event_cb, RISING);
7877

7978
// Initlialize Components.
80-
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
81-
AccGyr->Enable_X();
79+
AccGyr.begin();
80+
AccGyr.Enable_X();
8281

8382
// Enable Pedometer.
84-
AccGyr->Enable_Pedometer();
83+
AccGyr.Enable_Pedometer();
8584

8685
previous_tick = millis();
8786
}
@@ -91,11 +90,11 @@ void loop() {
9190
{
9291
mems_event = 0;
9392
LSM6DSL_Event_Status_t status;
94-
AccGyr->Get_Event_Status(&status);
93+
AccGyr.Get_Event_Status(&status);
9594
if (status.StepStatus)
9695
{
9796
// New step detected, so print the step counter
98-
AccGyr->Get_Step_Counter(&step_count);
97+
AccGyr.Get_Step_Counter(&step_count);
9998
snprintf(report, sizeof(report), "Step counter: %d", step_count);
10099
SerialPort.println(report);
101100

@@ -110,7 +109,7 @@ void loop() {
110109
current_tick = millis();
111110
if((current_tick - previous_tick) >= 3000)
112111
{
113-
AccGyr->Get_Step_Counter(&step_count);
112+
AccGyr.Get_Step_Counter(&step_count);
114113
snprintf(report, sizeof(report), "Step counter: %d", step_count);
115114
SerialPort.println(report);
116115
previous_tick = millis();

0 commit comments

Comments
 (0)