Skip to content

Commit

Permalink
Merge pull request #10 from cparata/master
Browse files Browse the repository at this point in the history
Add begin and end APIs to the base class
  • Loading branch information
cparata authored Jan 22, 2021
2 parents 5b72a34 + 925d770 commit 2526b58
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 92 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@ Arduino library to support the LSM6DSOX 3D accelerometer and 3D gyroscope
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(I2C_SDA, I2C_SCL);
dev_i2c->begin();
TwoWire dev_i2c(I2C_SDA, I2C_SCL);
dev_i2c.begin();

For SPI it is then required to create a SPI interface before accessing to the sensors:

dev_spi = new SPIClass(SPI_MOSI, SPI_MISO, SPI_SCK);
dev_spi->begin();
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
dev_spi.begin();

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

AccGyr = new LSM6DSOXSensor(dev_i2c);
AccGyr->Enable_X();
AccGyr->Enable_G();
LSM6DSOXSensor AccGyr(&dev_i2c);
AccGyr.begin();
AccGyr.Enable_X();
AccGyr.Enable_G();

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

AccGyr = new LSM6DSOXSensor(dev_spi, CS_PIN);
AccGyr->Enable_X();
AccGyr->Enable_G();
LSM6DSOXSensor 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);
AccGyr.Get_X_Axes(accelerometer);
AccGyr.Get_G_Axes(gyroscope);

## Documentation

Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ LSM6DSOXSensor KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################

begin KEYWORD2
end KEYWORD2
ReadID KEYWORD2
Enable_X KEYWORD2
Disable_X KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=STM32duino LSM6DSOX
version=1.1.1
version=2.0.0
author=SRA
maintainer=stm32duino
sentence=Ultra Low Power inertial measurement unit.
Expand Down
132 changes: 55 additions & 77 deletions src/LSM6DSOXSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,67 +54,8 @@ LSM6DSOXSensor::LSM6DSOXSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), ad
reg_ctx.write_reg = LSM6DSOX_io_write;
reg_ctx.read_reg = LSM6DSOX_io_read;
reg_ctx.handle = (void *)this;

/* Disable I3C */
if (lsm6dsox_i3c_disable_set(&reg_ctx, LSM6DSOX_I3C_DISABLE) != LSM6DSOX_OK)
{
return;
}

/* Enable register address automatically incremented during a multiple byte
access with a serial interface. */
if (lsm6dsox_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
{
return;
}

/* Enable BDU */
if (lsm6dsox_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
{
return;
}

/* FIFO mode selection */
if (lsm6dsox_fifo_mode_set(&reg_ctx, LSM6DSOX_BYPASS_MODE) != LSM6DSOX_OK)
{
return;
}

/* Select default output data rate. */
acc_odr = LSM6DSOX_XL_ODR_104Hz;

/* Output data rate selection - power down. */
if (lsm6dsox_xl_data_rate_set(&reg_ctx, LSM6DSOX_XL_ODR_OFF) != LSM6DSOX_OK)
{
return;
}

/* Full scale selection. */
if (lsm6dsox_xl_full_scale_set(&reg_ctx, LSM6DSOX_2g) != LSM6DSOX_OK)
{
return;
}

/* Select default output data rate. */
gyro_odr = LSM6DSOX_GY_ODR_104Hz;

/* Output data rate selection - power down. */
if (lsm6dsox_gy_data_rate_set(&reg_ctx, LSM6DSOX_GY_ODR_OFF) != LSM6DSOX_OK)
{
return;
}

/* Full scale selection. */
if (lsm6dsox_gy_full_scale_set(&reg_ctx, LSM6DSOX_2000dps) != LSM6DSOX_OK)
{
return;
}

acc_is_enabled = 0;
gyro_is_enabled = 0;


return;
acc_is_enabled = 0U;
gyro_is_enabled = 0U;
}

/** Constructor
Expand All @@ -127,36 +68,48 @@ LSM6DSOXSensor::LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
reg_ctx.write_reg = LSM6DSOX_io_write;
reg_ctx.read_reg = LSM6DSOX_io_read;
reg_ctx.handle = (void *)this;

// Configure CS pin
pinMode(cs_pin, OUTPUT);
digitalWrite(cs_pin, HIGH);
dev_i2c = NULL;
address = 0;
address = 0;
acc_is_enabled = 0U;
gyro_is_enabled = 0U;
}

/**
* @brief Configure the sensor in order to be used
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOXStatusTypeDef LSM6DSOXSensor::begin()
{
if(dev_spi)
{
// Configure CS pin
pinMode(cs_pin, OUTPUT);
digitalWrite(cs_pin, HIGH);
}

/* Disable I3C */
if (lsm6dsox_i3c_disable_set(&reg_ctx, LSM6DSOX_I3C_DISABLE) != LSM6DSOX_OK)
{
return;
return LSM6DSOX_ERROR;
}

/* Enable register address automatically incremented during a multiple byte
access with a serial interface. */
if (lsm6dsox_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
{
return;
return LSM6DSOX_ERROR;
}

/* Enable BDU */
if (lsm6dsox_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
{
return;
return LSM6DSOX_ERROR;
}

/* FIFO mode selection */
if (lsm6dsox_fifo_mode_set(&reg_ctx, LSM6DSOX_BYPASS_MODE) != LSM6DSOX_OK)
{
return;
return LSM6DSOX_ERROR;
}

/* Select default output data rate. */
Expand All @@ -165,13 +118,13 @@ LSM6DSOXSensor::LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
/* Output data rate selection - power down. */
if (lsm6dsox_xl_data_rate_set(&reg_ctx, LSM6DSOX_XL_ODR_OFF) != LSM6DSOX_OK)
{
return;
return LSM6DSOX_ERROR;
}

/* Full scale selection. */
if (lsm6dsox_xl_full_scale_set(&reg_ctx, LSM6DSOX_2g) != LSM6DSOX_OK)
{
return;
return LSM6DSOX_ERROR;
}

/* Select default output data rate. */
Expand All @@ -180,21 +133,46 @@ LSM6DSOXSensor::LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
/* Output data rate selection - power down. */
if (lsm6dsox_gy_data_rate_set(&reg_ctx, LSM6DSOX_GY_ODR_OFF) != LSM6DSOX_OK)
{
return;
return LSM6DSOX_ERROR;
}

/* Full scale selection. */
if (lsm6dsox_gy_full_scale_set(&reg_ctx, LSM6DSOX_2000dps) != LSM6DSOX_OK)
{
return;
return LSM6DSOX_ERROR;
}

acc_is_enabled = 0;
gyro_is_enabled = 0;


return;

return LSM6DSOX_OK;
}

/**
* @brief Disable the sensor and relative resources
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOXStatusTypeDef LSM6DSOXSensor::end()
{
/* Disable both acc and gyro */
if (Disable_X() != LSM6DSOX_OK)
{
return LSM6DSOX_ERROR;
}

if (Disable_G() != LSM6DSOX_OK)
{
return LSM6DSOX_ERROR;
}

/* Reset CS configuration */
if(dev_spi)
{
// Configure CS pin
pinMode(cs_pin, INPUT);
}

return LSM6DSOX_OK;
}


Expand Down
2 changes: 2 additions & 0 deletions src/LSM6DSOXSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class LSM6DSOXSensor
public:
LSM6DSOXSensor(TwoWire *i2c, uint8_t address=LSM6DSOX_I2C_ADD_H);
LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
LSM6DSOXStatusTypeDef begin();
LSM6DSOXStatusTypeDef end();
LSM6DSOXStatusTypeDef ReadID(uint8_t *Id);
LSM6DSOXStatusTypeDef Enable_X();
LSM6DSOXStatusTypeDef Disable_X();
Expand Down

0 comments on commit 2526b58

Please sign in to comment.