Skip to content

Commit 2f35e31

Browse files
authored
Merge pull request #2 from cparata/master
Add begin and end APIs
2 parents 0b42933 + 960fe20 commit 2f35e31

File tree

5 files changed

+67
-35
lines changed

5 files changed

+67
-35
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ This sensor uses SPI to communicate.
77

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

10-
dev_spi = new SPIClass(SPI_MOSI, SPI_MISO, SPI_SCK);
11-
dev_spi->begin();
10+
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
11+
dev_spi.begin();
1212

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

15-
Accelero = new LIS3DHHSensor(dev_spi, CS_PIN);
16-
Accelero->Enable_X();
15+
LIS3DHHSensor Accelero(&dev_spi, CS_PIN);
16+
Accelero.begin();
17+
Accelero.Enable_X();
1718

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

2021
Read accelerometer.
2122

22-
Accelero->Get_X_Axes(&accelerometer);
23+
int32_t accelerometer[3];
24+
Accelero.Get_X_Axes(&accelerometer);
2325

2426
## Documentation
2527

keywords.txt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,29 @@ LIS3DHHSensor KEYWORD1
1212
# Methods and Functions (KEYWORD2)
1313
#######################################
1414

15-
Enable_X KEYWORD2
16-
Disable_X KEYWORD2
17-
ReadID KEYWORD2
18-
Get_X_Axes KEYWORD2
19-
Get_X_Sensitivity KEYWORD2
20-
Get_X_AxesRaw KEYWORD2
21-
Get_X_ODR KEYWORD2
22-
Set_X_ODR KEYWORD2
23-
Get_X_FS KEYWORD2
24-
Set_X_FS KEYWORD2
25-
Enable_DRDY_Interrupt KEYWORD2
26-
Disable_DRDY_Interrupt KEYWORD2
27-
Set_Filter_Mode KEYWORD2
28-
Get_DRDY_Status KEYWORD2
29-
Get_FIFO_Num_Samples KEYWORD2
30-
Set_FIFO_Mode KEYWORD2
31-
ReadReg KEYWORD2
32-
WriteReg KEYWORD2
15+
begin KEYWORD2
16+
end KEYWORD2
17+
Enable_X KEYWORD2
18+
Disable_X KEYWORD2
19+
ReadID KEYWORD2
20+
Get_X_Axes KEYWORD2
21+
Get_X_Sensitivity KEYWORD2
22+
Get_X_AxesRaw KEYWORD2
23+
Get_X_ODR KEYWORD2
24+
Set_X_ODR KEYWORD2
25+
Get_X_FS KEYWORD2
26+
Set_X_FS KEYWORD2
27+
Enable_DRDY_Interrupt KEYWORD2
28+
Disable_DRDY_Interrupt KEYWORD2
29+
Set_Filter_Mode KEYWORD2
30+
Get_DRDY_Status KEYWORD2
31+
Get_FIFO_Num_Samples KEYWORD2
32+
Set_FIFO_Mode KEYWORD2
33+
ReadReg KEYWORD2
34+
WriteReg KEYWORD2
3335

3436
#######################################
3537
# Constants (LITERAL1)
3638
#######################################
3739

38-
LIS3DHH_ACC_SENSITIVITY LITERAL1
40+
LIS3DHH_ACC_SENSITIVITY LITERAL1

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=STM32duino LIS3DHH
2-
version=1.0.0
2+
version=2.0.0
33
author=SRA
44
maintainer=stm32duino
55
sentence=Ultra High Resolution 3D accelerometer.

src/LIS3DHHSensor.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,39 +54,65 @@ LIS3DHHSensor::LIS3DHHSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
5454
reg_ctx.write_reg = LIS3DHH_io_write;
5555
reg_ctx.read_reg = LIS3DHH_io_read;
5656
reg_ctx.handle = (void *)this;
57+
X_isEnabled = 0U;
58+
}
5759

60+
/**
61+
* @brief Configure the sensor in order to be used
62+
* @retval 0 in case of success, an error code otherwise
63+
*/
64+
LIS3DHHStatusTypeDef LIS3DHHSensor::begin()
65+
{
5866
// Configure CS pin
5967
pinMode(cs_pin, OUTPUT);
60-
digitalWrite(cs_pin, HIGH);
68+
digitalWrite(cs_pin, HIGH);
6169

6270
/* Enable register address automatically incremented during a multiple byte
6371
access with a serial interface. */
6472
if (lis3dhh_auto_add_inc_set(&reg_ctx, PROPERTY_ENABLE) != 0)
6573
{
66-
return;
74+
return LIS3DHH_STATUS_ERROR;
6775
}
6876

6977
/* Enable BDU */
7078
if (lis3dhh_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != 0)
7179
{
72-
return;
80+
return LIS3DHH_STATUS_ERROR;
7381
}
7482

7583
/* FIFO mode selection */
7684
if (lis3dhh_fifo_mode_set(&reg_ctx, LIS3DHH_BYPASS_MODE) != 0)
7785
{
78-
return;
86+
return LIS3DHH_STATUS_ERROR;
7987
}
8088

8189
/* Output data rate selection - power down. */
8290
if (lis3dhh_data_rate_set(&reg_ctx, LIS3DHH_POWER_DOWN) != 0)
8391
{
84-
return;
92+
return LIS3DHH_STATUS_ERROR;
8593
}
8694

8795
X_isEnabled = 0;
8896

89-
return;
97+
return LIS3DHH_STATUS_OK;
98+
}
99+
100+
/**
101+
* @brief Disable the sensor and relative resources
102+
* @retval 0 in case of success, an error code otherwise
103+
*/
104+
LIS3DHHStatusTypeDef LIS3DHHSensor::end()
105+
{
106+
/* Disable acc */
107+
if (Disable_X() != LIS3DHH_STATUS_OK)
108+
{
109+
return LIS3DHH_STATUS_ERROR;
110+
}
111+
112+
/* Reset CS configuration */
113+
pinMode(cs_pin, INPUT);
114+
115+
return LIS3DHH_STATUS_OK;
90116
}
91117

92118
/**
@@ -96,7 +122,7 @@ LIS3DHHSensor::LIS3DHHSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
96122
LIS3DHHStatusTypeDef LIS3DHHSensor::Enable_X(void)
97123
{
98124
/* Check if the component is already enabled */
99-
if (X_isEnabled == 1)
125+
if (X_isEnabled == 1U)
100126
{
101127
return LIS3DHH_STATUS_OK;
102128
}
@@ -107,7 +133,7 @@ LIS3DHHStatusTypeDef LIS3DHHSensor::Enable_X(void)
107133
return LIS3DHH_STATUS_ERROR;
108134
}
109135

110-
X_isEnabled = 1;
136+
X_isEnabled = 1U;
111137

112138
return LIS3DHH_STATUS_OK;
113139
}
@@ -119,7 +145,7 @@ LIS3DHHStatusTypeDef LIS3DHHSensor::Enable_X(void)
119145
LIS3DHHStatusTypeDef LIS3DHHSensor::Disable_X(void)
120146
{
121147
/* Check if the component is already disabled */
122-
if (X_isEnabled == 0)
148+
if (X_isEnabled == 0U)
123149
{
124150
return LIS3DHH_STATUS_OK;
125151
}
@@ -130,7 +156,7 @@ LIS3DHHStatusTypeDef LIS3DHHSensor::Disable_X(void)
130156
return LIS3DHH_STATUS_ERROR;
131157
}
132158

133-
X_isEnabled = 0;
159+
X_isEnabled = 0U;
134160

135161
return LIS3DHH_STATUS_OK;
136162
}

src/LIS3DHHSensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class LIS3DHHSensor
7575
{
7676
public:
7777
LIS3DHHSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
78+
LIS3DHHStatusTypeDef begin(void);
79+
LIS3DHHStatusTypeDef end(void);
7880
LIS3DHHStatusTypeDef Enable_X(void);
7981
LIS3DHHStatusTypeDef Disable_X(void);
8082
LIS3DHHStatusTypeDef ReadID(uint8_t *id);

0 commit comments

Comments
 (0)