Skip to content

Commit

Permalink
Merge pull request #2 from cparata/master
Browse files Browse the repository at this point in the history
Add begin and end APIs
  • Loading branch information
cparata authored Jan 27, 2021
2 parents 0b42933 + 960fe20 commit 2f35e31
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 35 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ This sensor uses SPI to communicate.

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 enabled when the SPI bus is used following the procedure below:

Accelero = new LIS3DHHSensor(dev_spi, CS_PIN);
Accelero->Enable_X();
LIS3DHHSensor Accelero(&dev_spi, CS_PIN);
Accelero.begin();
Accelero.Enable_X();

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

Read accelerometer.

Accelero->Get_X_Axes(&accelerometer);
int32_t accelerometer[3];
Accelero.Get_X_Axes(&accelerometer);

## Documentation

Expand Down
40 changes: 21 additions & 19 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ LIS3DHHSensor KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################

Enable_X KEYWORD2
Disable_X KEYWORD2
ReadID KEYWORD2
Get_X_Axes KEYWORD2
Get_X_Sensitivity KEYWORD2
Get_X_AxesRaw KEYWORD2
Get_X_ODR KEYWORD2
Set_X_ODR KEYWORD2
Get_X_FS KEYWORD2
Set_X_FS KEYWORD2
Enable_DRDY_Interrupt KEYWORD2
Disable_DRDY_Interrupt KEYWORD2
Set_Filter_Mode KEYWORD2
Get_DRDY_Status KEYWORD2
Get_FIFO_Num_Samples KEYWORD2
Set_FIFO_Mode KEYWORD2
ReadReg KEYWORD2
WriteReg KEYWORD2
begin KEYWORD2
end KEYWORD2
Enable_X KEYWORD2
Disable_X KEYWORD2
ReadID KEYWORD2
Get_X_Axes KEYWORD2
Get_X_Sensitivity KEYWORD2
Get_X_AxesRaw KEYWORD2
Get_X_ODR KEYWORD2
Set_X_ODR KEYWORD2
Get_X_FS KEYWORD2
Set_X_FS KEYWORD2
Enable_DRDY_Interrupt KEYWORD2
Disable_DRDY_Interrupt KEYWORD2
Set_Filter_Mode KEYWORD2
Get_DRDY_Status KEYWORD2
Get_FIFO_Num_Samples KEYWORD2
Set_FIFO_Mode KEYWORD2
ReadReg KEYWORD2
WriteReg KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

LIS3DHH_ACC_SENSITIVITY LITERAL1
LIS3DHH_ACC_SENSITIVITY LITERAL1
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=STM32duino LIS3DHH
version=1.0.0
version=2.0.0
author=SRA
maintainer=stm32duino
sentence=Ultra High Resolution 3D accelerometer.
Expand Down
46 changes: 36 additions & 10 deletions src/LIS3DHHSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,65 @@ LIS3DHHSensor::LIS3DHHSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
reg_ctx.write_reg = LIS3DHH_io_write;
reg_ctx.read_reg = LIS3DHH_io_read;
reg_ctx.handle = (void *)this;
X_isEnabled = 0U;
}

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

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

/* Enable BDU */
if (lis3dhh_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != 0)
{
return;
return LIS3DHH_STATUS_ERROR;
}

/* FIFO mode selection */
if (lis3dhh_fifo_mode_set(&reg_ctx, LIS3DHH_BYPASS_MODE) != 0)
{
return;
return LIS3DHH_STATUS_ERROR;
}

/* Output data rate selection - power down. */
if (lis3dhh_data_rate_set(&reg_ctx, LIS3DHH_POWER_DOWN) != 0)
{
return;
return LIS3DHH_STATUS_ERROR;
}

X_isEnabled = 0;

return;
return LIS3DHH_STATUS_OK;
}

/**
* @brief Disable the sensor and relative resources
* @retval 0 in case of success, an error code otherwise
*/
LIS3DHHStatusTypeDef LIS3DHHSensor::end()
{
/* Disable acc */
if (Disable_X() != LIS3DHH_STATUS_OK)
{
return LIS3DHH_STATUS_ERROR;
}

/* Reset CS configuration */
pinMode(cs_pin, INPUT);

return LIS3DHH_STATUS_OK;
}

/**
Expand All @@ -96,7 +122,7 @@ LIS3DHHSensor::LIS3DHHSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
LIS3DHHStatusTypeDef LIS3DHHSensor::Enable_X(void)
{
/* Check if the component is already enabled */
if (X_isEnabled == 1)
if (X_isEnabled == 1U)
{
return LIS3DHH_STATUS_OK;
}
Expand All @@ -107,7 +133,7 @@ LIS3DHHStatusTypeDef LIS3DHHSensor::Enable_X(void)
return LIS3DHH_STATUS_ERROR;
}

X_isEnabled = 1;
X_isEnabled = 1U;

return LIS3DHH_STATUS_OK;
}
Expand All @@ -119,7 +145,7 @@ LIS3DHHStatusTypeDef LIS3DHHSensor::Enable_X(void)
LIS3DHHStatusTypeDef LIS3DHHSensor::Disable_X(void)
{
/* Check if the component is already disabled */
if (X_isEnabled == 0)
if (X_isEnabled == 0U)
{
return LIS3DHH_STATUS_OK;
}
Expand All @@ -130,7 +156,7 @@ LIS3DHHStatusTypeDef LIS3DHHSensor::Disable_X(void)
return LIS3DHH_STATUS_ERROR;
}

X_isEnabled = 0;
X_isEnabled = 0U;

return LIS3DHH_STATUS_OK;
}
Expand Down
2 changes: 2 additions & 0 deletions src/LIS3DHHSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class LIS3DHHSensor
{
public:
LIS3DHHSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
LIS3DHHStatusTypeDef begin(void);
LIS3DHHStatusTypeDef end(void);
LIS3DHHStatusTypeDef Enable_X(void);
LIS3DHHStatusTypeDef Disable_X(void);
LIS3DHHStatusTypeDef ReadID(uint8_t *id);
Expand Down

0 comments on commit 2f35e31

Please sign in to comment.