From 925d770ce107f6d4e51d5d3b2b490a0f920b3239 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Fri, 22 Jan 2021 15:03:03 +0100 Subject: [PATCH] Add begin and end APIs to the base class --- README.md | 30 +++++----- keywords.txt | 2 + library.properties | 2 +- src/LSM6DSOXSensor.cpp | 132 +++++++++++++++++------------------------ src/LSM6DSOXSensor.h | 2 + 5 files changed, 76 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index f10f0a1..5600af0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/keywords.txt b/keywords.txt index a3f1787..b466c3c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -12,6 +12,8 @@ LSM6DSOXSensor KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### +begin KEYWORD2 +end KEYWORD2 ReadID KEYWORD2 Enable_X KEYWORD2 Disable_X KEYWORD2 diff --git a/library.properties b/library.properties index de200ac..df6e7e0 100644 --- a/library.properties +++ b/library.properties @@ -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. diff --git a/src/LSM6DSOXSensor.cpp b/src/LSM6DSOXSensor.cpp index d4417aa..f188b62 100644 --- a/src/LSM6DSOXSensor.cpp +++ b/src/LSM6DSOXSensor.cpp @@ -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(®_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(®_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK) - { - return; - } - - /* Enable BDU */ - if (lsm6dsox_block_data_update_set(®_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK) - { - return; - } - - /* FIFO mode selection */ - if (lsm6dsox_fifo_mode_set(®_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(®_ctx, LSM6DSOX_XL_ODR_OFF) != LSM6DSOX_OK) - { - return; - } - - /* Full scale selection. */ - if (lsm6dsox_xl_full_scale_set(®_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(®_ctx, LSM6DSOX_GY_ODR_OFF) != LSM6DSOX_OK) - { - return; - } - - /* Full scale selection. */ - if (lsm6dsox_gy_full_scale_set(®_ctx, LSM6DSOX_2000dps) != LSM6DSOX_OK) - { - return; - } - - acc_is_enabled = 0; - gyro_is_enabled = 0; - - - return; + acc_is_enabled = 0U; + gyro_is_enabled = 0U; } /** Constructor @@ -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(®_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(®_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK) { - return; + return LSM6DSOX_ERROR; } /* Enable BDU */ if (lsm6dsox_block_data_update_set(®_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK) { - return; + return LSM6DSOX_ERROR; } /* FIFO mode selection */ if (lsm6dsox_fifo_mode_set(®_ctx, LSM6DSOX_BYPASS_MODE) != LSM6DSOX_OK) { - return; + return LSM6DSOX_ERROR; } /* Select default output data rate. */ @@ -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(®_ctx, LSM6DSOX_XL_ODR_OFF) != LSM6DSOX_OK) { - return; + return LSM6DSOX_ERROR; } /* Full scale selection. */ if (lsm6dsox_xl_full_scale_set(®_ctx, LSM6DSOX_2g) != LSM6DSOX_OK) { - return; + return LSM6DSOX_ERROR; } /* Select default output data rate. */ @@ -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(®_ctx, LSM6DSOX_GY_ODR_OFF) != LSM6DSOX_OK) { - return; + return LSM6DSOX_ERROR; } /* Full scale selection. */ if (lsm6dsox_gy_full_scale_set(®_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; } diff --git a/src/LSM6DSOXSensor.h b/src/LSM6DSOXSensor.h index afb230e..2d5f71c 100644 --- a/src/LSM6DSOXSensor.h +++ b/src/LSM6DSOXSensor.h @@ -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();