Skip to content

Commit

Permalink
Add begin and end APIs and SPI support
Browse files Browse the repository at this point in the history
  • Loading branch information
cparata committed Jan 26, 2021
1 parent 95d362d commit f2dcb9b
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 180 deletions.
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,39 @@ Arduino library to support the LSM6DSL 3D accelerometer and 3D gyroscope

## API

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

dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
dev_i2c->begin();
TwoWire dev_i2c(I2C_SDA, I2C_SCL);
dev_i2c.begin();

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

AccGyr = new LSM6DSLSensor(dev_i2c);
AccGyr->Enable_X();
AccGyr->Enable_G();
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
dev_spi.begin();

An instance can be created and enabled when the I2C bus is used following the procedure below:

LSM6DSLSensor AccGyr(&dev_i2c);
AccGyr.begin();
AccGyr.Enable_X();
AccGyr.Enable_G();

An instance can be created and enabled when the SPI bus is used following the procedure below:

LSM6DSLSensor AccGyr(&dev_spi, CS_PIN);
AccGyr.begin();
AccGyr.Enable_X();
AccGyr.Enable_G();

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

Read accelerometer and gyroscope.

AccGyr->Get_X_Axes(accelerometer);
AccGyr->Get_G_Axes(gyroscope);
int32_t accelerometer[3];
int32_t gyroscope[3];
AccGyr.Get_X_Axes(accelerometer);
AccGyr.Get_G_Axes(gyroscope);

## Documentation

Expand Down
27 changes: 13 additions & 14 deletions examples/DISCO_IOT_6DOrientation/DISCO_IOT_6DOrientation.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
#define INT1 PD11

// Components.
LSM6DSLSensor *AccGyr;
TwoWire *dev_i2c;
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);

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

// Initialize I2C bus.
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
dev_i2c->begin();
dev_i2c.begin();

//Interrupts.
attachInterrupt(INT1, INT1Event_cb, RISING);

// Initlialize Components.
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
AccGyr->Enable_X();
AccGyr.begin();
AccGyr.Enable_X();

// Enable 6D Orientation.
AccGyr->Enable_6D_Orientation();
AccGyr.Enable_6D_Orientation();
}

void loop() {
if (mems_event)
{
mems_event = 0;
LSM6DSL_Event_Status_t status;
AccGyr->Get_Event_Status(&status);
AccGyr.Get_Event_Status(&status);
if (status.D6DOrientationStatus)
{
// Send 6D Orientation
Expand Down Expand Up @@ -115,12 +114,12 @@ void sendOrientation()
uint8_t zl = 0;
uint8_t zh = 0;

AccGyr->Get_6D_Orientation_XL(&xl);
AccGyr->Get_6D_Orientation_XH(&xh);
AccGyr->Get_6D_Orientation_YL(&yl);
AccGyr->Get_6D_Orientation_YH(&yh);
AccGyr->Get_6D_Orientation_ZL(&zl);
AccGyr->Get_6D_Orientation_ZH(&zh);
AccGyr.Get_6D_Orientation_XL(&xl);
AccGyr.Get_6D_Orientation_XH(&xh);
AccGyr.Get_6D_Orientation_YL(&yl);
AccGyr.Get_6D_Orientation_YH(&yh);
AccGyr.Get_6D_Orientation_ZL(&zl);
AccGyr.Get_6D_Orientation_ZH(&zh);

if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
{
Expand Down
15 changes: 7 additions & 8 deletions examples/DISCO_IOT_DoubleTap/DISCO_IOT_DoubleTap.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
#define INT1 PD11

// Components.
LSM6DSLSensor *AccGyr;
TwoWire *dev_i2c;
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);

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

// Initialize I2C bus.
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
dev_i2c->begin();
dev_i2c.begin();

//Interrupts.
attachInterrupt(INT1, INT1Event_cb, RISING);

// Initlialize Components.
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
AccGyr->Enable_X();
AccGyr.begin();
AccGyr.Enable_X();

// Enable Double Tap Detection.
AccGyr->Enable_Double_Tap_Detection();
AccGyr.Enable_Double_Tap_Detection();
}

void loop() {
if (mems_event) {
mems_event = 0;
LSM6DSL_Event_Status_t status;
AccGyr->Get_Event_Status(&status);
AccGyr.Get_Event_Status(&status);
if (status.DoubleTapStatus)
{
// Led blinking.
Expand Down
15 changes: 7 additions & 8 deletions examples/DISCO_IOT_FreeFall/DISCO_IOT_FreeFall.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
#define INT1 PD11

// Components.
LSM6DSLSensor *AccGyr;
TwoWire *dev_i2c;
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);

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

// Initialize I2C bus.
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
dev_i2c->begin();
dev_i2c.begin();

//Interrupts.
attachInterrupt(INT1, INT1Event_cb, RISING);

// Initlialize Components.
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
AccGyr->Enable_X();
AccGyr.begin();
AccGyr.Enable_X();

// Enable Free Fall Detection.
AccGyr->Enable_Free_Fall_Detection();
AccGyr.Enable_Free_Fall_Detection();
}

void loop() {
if (mems_event) {
mems_event = 0;
LSM6DSL_Event_Status_t status;
AccGyr->Get_Event_Status(&status);
AccGyr.Get_Event_Status(&status);
if (status.FreeFallStatus)
{
// Led blinking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
#define I2C2_SDA PB11

// Components.
LSM6DSLSensor *AccGyr;
TwoWire *dev_i2c;
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);

void setup() {
// Led.
Expand All @@ -57,13 +57,12 @@ void setup() {
SerialPort.begin(9600);

// Initialize I2C bus.
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
dev_i2c->begin();
dev_i2c.begin();

// Initlialize components.
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
AccGyr->Enable_X();
AccGyr->Enable_G();
AccGyr.begin();
AccGyr.Enable_X();
AccGyr.Enable_G();
}

void loop() {
Expand All @@ -76,8 +75,8 @@ void loop() {
// Read accelerometer and gyroscope.
int32_t accelerometer[3];
int32_t gyroscope[3];
AccGyr->Get_X_Axes(accelerometer);
AccGyr->Get_G_Axes(gyroscope);
AccGyr.Get_X_Axes(accelerometer);
AccGyr.Get_G_Axes(gyroscope);

// Output data.
SerialPort.print("Acc[mg]: ");
Expand Down
39 changes: 19 additions & 20 deletions examples/DISCO_IOT_MultiEvent/DISCO_IOT_MultiEvent.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
#define INT1 PD11

// Components.
LSM6DSLSensor *AccGyr;
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);

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

void INT1Event_cb();
void sendOrientation();
TwoWire *dev_i2c;

void setup() {
// Initialize serial for output.
SerialPort.begin(9600);

// Initialize I2C bus.
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
dev_i2c->begin();
dev_i2c.begin();

//Interrupts.
attachInterrupt(INT1, INT1Event_cb, RISING);

// Initlialize Components.
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
AccGyr->Enable_X();
AccGyr.begin();
AccGyr.Enable_X();

// Enable all HW events.
AccGyr->Enable_Pedometer();
AccGyr->Enable_Tilt_Detection();
AccGyr->Enable_Free_Fall_Detection();
AccGyr->Enable_Single_Tap_Detection();
AccGyr->Enable_Double_Tap_Detection();
AccGyr->Enable_6D_Orientation();
AccGyr.Enable_Pedometer();
AccGyr.Enable_Tilt_Detection();
AccGyr.Enable_Free_Fall_Detection();
AccGyr.Enable_Single_Tap_Detection();
AccGyr.Enable_Double_Tap_Detection();
AccGyr.Enable_6D_Orientation();
}

void loop() {
if (mems_event)
{
mems_event = 0;
LSM6DSL_Event_Status_t status;
AccGyr->Get_Event_Status(&status);
AccGyr.Get_Event_Status(&status);

if (status.StepStatus)
{
// New step detected, so print the step counter
AccGyr->Get_Step_Counter(&step_count);
AccGyr.Get_Step_Counter(&step_count);
snprintf(report, sizeof(report), "Step counter: %d", step_count);
SerialPort.println(report);
}
Expand Down Expand Up @@ -153,12 +152,12 @@ void sendOrientation()
uint8_t zl = 0;
uint8_t zh = 0;

AccGyr->Get_6D_Orientation_XL(&xl);
AccGyr->Get_6D_Orientation_XH(&xh);
AccGyr->Get_6D_Orientation_YL(&yl);
AccGyr->Get_6D_Orientation_YH(&yh);
AccGyr->Get_6D_Orientation_ZL(&zl);
AccGyr->Get_6D_Orientation_ZH(&zh);
AccGyr.Get_6D_Orientation_XL(&xl);
AccGyr.Get_6D_Orientation_XH(&xh);
AccGyr.Get_6D_Orientation_YL(&yl);
AccGyr.Get_6D_Orientation_YH(&yh);
AccGyr.Get_6D_Orientation_ZL(&zl);
AccGyr.Get_6D_Orientation_ZH(&zh);

if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
{
Expand Down
19 changes: 9 additions & 10 deletions examples/DISCO_IOT_Pedometer/DISCO_IOT_Pedometer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
#define INT1 PD11

// Components.
LSM6DSLSensor *AccGyr;
TwoWire *dev_i2c;
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
LSM6DSLSensor AccGyr(&dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);

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

// Initialize I2C bus.
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
dev_i2c->begin();
dev_i2c.begin();

//Interrupts.
attachInterrupt(INT1, INT1Event_cb, RISING);

// Initlialize Components.
AccGyr = new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW);
AccGyr->Enable_X();
AccGyr.begin();
AccGyr.Enable_X();

// Enable Pedometer.
AccGyr->Enable_Pedometer();
AccGyr.Enable_Pedometer();

previous_tick = millis();
}
Expand All @@ -91,11 +90,11 @@ void loop() {
{
mems_event = 0;
LSM6DSL_Event_Status_t status;
AccGyr->Get_Event_Status(&status);
AccGyr.Get_Event_Status(&status);
if (status.StepStatus)
{
// New step detected, so print the step counter
AccGyr->Get_Step_Counter(&step_count);
AccGyr.Get_Step_Counter(&step_count);
snprintf(report, sizeof(report), "Step counter: %d", step_count);
SerialPort.println(report);

Expand All @@ -110,7 +109,7 @@ void loop() {
current_tick = millis();
if((current_tick - previous_tick) >= 3000)
{
AccGyr->Get_Step_Counter(&step_count);
AccGyr.Get_Step_Counter(&step_count);
snprintf(report, sizeof(report), "Step counter: %d", step_count);
SerialPort.println(report);
previous_tick = millis();
Expand Down
Loading

0 comments on commit f2dcb9b

Please sign in to comment.