Skip to content

Commit 092a2b4

Browse files
authored
Merge pull request #5 from cparata/master
Add begin and end APIs
2 parents 32eccbd + b827e72 commit 092a2b4

7 files changed

Lines changed: 89 additions & 102 deletions

File tree

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,34 @@ Arduino library to support the LSM6DSR automotive 3D accelerometer and 3D gyrosc
66
This sensor uses I2C or SPI to communicate.
77
For I2C it is then required to create a TwoWire interface before accessing to the sensors:
88

9-
dev_i2c = new TwoWire(I2C_SDA, I2C_SCL);
10-
dev_i2c->begin();
9+
TwoWire dev_i2c(I2C_SDA, I2C_SCL);
10+
dev_i2c.begin();
1111

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

14-
dev_spi = new SPIClass(SPI_MOSI, SPI_MISO, SPI_SCK);
15-
dev_spi->begin();
14+
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
15+
dev_spi.begin();
1616

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

19-
AccGyr = new LSM6DSRSensor(dev_i2c);
20-
AccGyr->Enable_X();
21-
AccGyr->Enable_G();
19+
LSM6DSRSensor AccGyr(&dev_i2c);
20+
AccGyr.begin();
21+
AccGyr.Enable_X();
22+
AccGyr.Enable_G();
2223

23-
An instance can be created and enbaled when the SPI bus is used following the procedure below:
24+
An instance can be created and enabled when the SPI bus is used following the procedure below:
2425

25-
AccGyr = new LSM6DSRSensor(dev_spi, CS_PIN);
26-
AccGyr->Enable_X();
27-
AccGyr->Enable_G();
26+
LSM6DSRSensor AccGyr(&dev_spi, CS_PIN);
27+
AccGyr.begin();
28+
AccGyr.Enable_X();
29+
AccGyr.Enable_G();
2830

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

3133
Read accelerometer and gyroscope.
3234

35+
int32_t accelerometer[3];
36+
int32_t gyroscope[3];
3337
AccGyr->Get_X_Axes(accelerometer);
3438
AccGyr->Get_G_Axes(gyroscope);
3539

examples/LSM6DSR_I2C_HelloWorld/LSM6DSR_I2C_HelloWorld.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#define INT_1 A5
5757

5858
// Components
59-
LSM6DSRSensor *AccGyr;
59+
LSM6DSRSensor AccGyr(&DEV_I2C, LSM6DSR_I2C_ADD_L);
6060

6161
void setup() {
6262
// Led.
@@ -75,9 +75,9 @@ void setup() {
7575
// Initialize I2C bus.
7676
DEV_I2C.begin();
7777

78-
AccGyr = new LSM6DSRSensor (&DEV_I2C, LSM6DSR_I2C_ADD_L);
79-
AccGyr->Enable_X();
80-
AccGyr->Enable_G();
78+
AccGyr.begin();
79+
AccGyr.Enable_X();
80+
AccGyr.Enable_G();
8181
}
8282

8383
void loop() {
@@ -90,8 +90,8 @@ void loop() {
9090
// Read accelerometer and gyroscope.
9191
int32_t accelerometer[3];
9292
int32_t gyroscope[3];
93-
AccGyr->Get_X_Axes(accelerometer);
94-
AccGyr->Get_G_Axes(gyroscope);
93+
AccGyr.Get_X_Axes(accelerometer);
94+
AccGyr.Get_G_Axes(gyroscope);
9595

9696
// Output data.
9797
SerialPort.print("LSM6DSR: | Acc[mg]: ");

examples/LSM6DSR_SPI_HelloWorld/LSM6DSR_SPI_HelloWorld.ino

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
#define SerialPort Serial
4747

4848
// SPI
49-
SPIClass *dev_spi;
49+
SPIClass dev_spi(D11, D12, D3);
5050

5151
// Components
52-
LSM6DSRSensor *AccGyr;
52+
LSM6DSRSensor AccGyr(&dev_spi, D10);
5353

5454
void setup() {
5555
// Led.
@@ -59,12 +59,11 @@ void setup() {
5959
SerialPort.begin(115200);
6060

6161
// Initialize SPI bus.
62-
dev_spi = new SPIClass(D11, D12, D3);
63-
dev_spi->begin();
62+
dev_spi.begin();
6463

65-
AccGyr = new LSM6DSRSensor (dev_spi, D10);
66-
AccGyr->Enable_X();
67-
AccGyr->Enable_G();
64+
AccGyr.begin();
65+
AccGyr.Enable_X();
66+
AccGyr.Enable_G();
6867
}
6968

7069
void loop() {
@@ -77,8 +76,8 @@ void loop() {
7776
// Read accelerometer and gyroscope.
7877
int32_t accelerometer[3];
7978
int32_t gyroscope[3];
80-
AccGyr->Get_X_Axes(accelerometer);
81-
AccGyr->Get_G_Axes(gyroscope);
79+
AccGyr.Get_X_Axes(accelerometer);
80+
AccGyr.Get_G_Axes(gyroscope);
8281

8382
// Output data.
8483
SerialPort.print("LSM6DSR: | Acc[mg]: ");

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ LSM6DSRSensor KEYWORD1
1212
# Methods and Functions (KEYWORD2)
1313
#######################################
1414

15+
begin KEYWORD2
16+
end KEYWORD2
1517
ReadID KEYWORD2
1618
Enable_X KEYWORD2
1719
Disable_X KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=STM32duino LSM6DSR
2-
version=1.0.1
2+
version=2.0.0
33
author=SRA
44
maintainer=stm32duino
55
sentence=iNEMO inertial measurement unit.

src/LSM6DSRSensor.cpp

Lines changed: 54 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -54,66 +54,8 @@ LSM6DSRSensor::LSM6DSRSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), addr
5454
reg_ctx.write_reg = LSM6DSR_io_write;
5555
reg_ctx.read_reg = LSM6DSR_io_read;
5656
reg_ctx.handle = (void *)this;
57-
58-
/* Disable I3C */
59-
if (lsm6dsr_i3c_disable_set(&reg_ctx, LSM6DSR_I3C_DISABLE) != LSM6DSR_OK)
60-
{
61-
return;
62-
}
63-
64-
/* Enable register address automatically incremented during a multiple byte
65-
access with a serial interface. */
66-
if (lsm6dsr_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSR_OK)
67-
{
68-
return;
69-
}
70-
71-
/* Enable BDU */
72-
if (lsm6dsr_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSR_OK)
73-
{
74-
return;
75-
}
76-
77-
/* FIFO mode selection */
78-
if (lsm6dsr_fifo_mode_set(&reg_ctx, LSM6DSR_BYPASS_MODE) != LSM6DSR_OK)
79-
{
80-
return;
81-
}
82-
83-
/* Select default output data rate. */
84-
acc_odr = LSM6DSR_XL_ODR_104Hz;
85-
86-
/* Output data rate selection - power down. */
87-
if (lsm6dsr_xl_data_rate_set(&reg_ctx, LSM6DSR_XL_ODR_OFF) != LSM6DSR_OK)
88-
{
89-
return;
90-
}
91-
92-
/* Full scale selection. */
93-
if (lsm6dsr_xl_full_scale_set(&reg_ctx, LSM6DSR_2g) != LSM6DSR_OK)
94-
{
95-
return;
96-
}
97-
98-
/* Select default output data rate. */
99-
gyro_odr = LSM6DSR_GY_ODR_104Hz;
100-
101-
/* Output data rate selection - power down. */
102-
if (lsm6dsr_gy_data_rate_set(&reg_ctx, LSM6DSR_GY_ODR_OFF) != LSM6DSR_OK)
103-
{
104-
return;
105-
}
106-
107-
/* Full scale selection. */
108-
if (lsm6dsr_gy_full_scale_set(&reg_ctx, LSM6DSR_2000dps) != LSM6DSR_OK)
109-
{
110-
return;
111-
}
112-
113-
acc_is_enabled = 0;
114-
gyro_is_enabled = 0;
115-
116-
return;
57+
acc_is_enabled = 0U;
58+
gyro_is_enabled = 0U;
11759
}
11860

11961
/** Constructor
@@ -126,36 +68,48 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
12668
reg_ctx.write_reg = LSM6DSR_io_write;
12769
reg_ctx.read_reg = LSM6DSR_io_read;
12870
reg_ctx.handle = (void *)this;
129-
130-
// Configure CS pin
131-
pinMode(cs_pin, OUTPUT);
132-
digitalWrite(cs_pin, HIGH);
13371
dev_i2c = NULL;
134-
address = 0;
72+
address = 0U;
73+
acc_is_enabled = 0U;
74+
gyro_is_enabled = 0U;
75+
}
76+
77+
/**
78+
* @brief Configure the sensor in order to be used
79+
* @retval 0 in case of success, an error code otherwise
80+
*/
81+
LSM6DSRStatusTypeDef LSM6DSRSensor::begin()
82+
{
83+
if(dev_spi)
84+
{
85+
// Configure CS pin
86+
pinMode(cs_pin, OUTPUT);
87+
digitalWrite(cs_pin, HIGH);
88+
}
13589

13690
/* Disable I3C */
13791
if (lsm6dsr_i3c_disable_set(&reg_ctx, LSM6DSR_I3C_DISABLE) != LSM6DSR_OK)
13892
{
139-
return;
93+
return LSM6DSR_ERROR;
14094
}
14195

14296
/* Enable register address automatically incremented during a multiple byte
14397
access with a serial interface. */
14498
if (lsm6dsr_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSR_OK)
14599
{
146-
return;
100+
return LSM6DSR_ERROR;
147101
}
148102

149103
/* Enable BDU */
150104
if (lsm6dsr_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSR_OK)
151105
{
152-
return;
106+
return LSM6DSR_ERROR;
153107
}
154108

155109
/* FIFO mode selection */
156110
if (lsm6dsr_fifo_mode_set(&reg_ctx, LSM6DSR_BYPASS_MODE) != LSM6DSR_OK)
157111
{
158-
return;
112+
return LSM6DSR_ERROR;
159113
}
160114

161115
/* Select default output data rate. */
@@ -164,13 +118,13 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
164118
/* Output data rate selection - power down. */
165119
if (lsm6dsr_xl_data_rate_set(&reg_ctx, LSM6DSR_XL_ODR_OFF) != LSM6DSR_OK)
166120
{
167-
return;
121+
return LSM6DSR_ERROR;
168122
}
169123

170124
/* Full scale selection. */
171125
if (lsm6dsr_xl_full_scale_set(&reg_ctx, LSM6DSR_2g) != LSM6DSR_OK)
172126
{
173-
return;
127+
return LSM6DSR_ERROR;
174128
}
175129

176130
/* Select default output data rate. */
@@ -179,21 +133,47 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
179133
/* Output data rate selection - power down. */
180134
if (lsm6dsr_gy_data_rate_set(&reg_ctx, LSM6DSR_GY_ODR_OFF) != LSM6DSR_OK)
181135
{
182-
return;
136+
return LSM6DSR_ERROR;
183137
}
184138

185139
/* Full scale selection. */
186140
if (lsm6dsr_gy_full_scale_set(&reg_ctx, LSM6DSR_2000dps) != LSM6DSR_OK)
187141
{
188-
return;
142+
return LSM6DSR_ERROR;
189143
}
190144

191145
acc_is_enabled = 0;
192146
gyro_is_enabled = 0;
193147

194-
return;
148+
return LSM6DSR_OK;
195149
}
196150

151+
/**
152+
* @brief Disable the sensor and relative resources
153+
* @retval 0 in case of success, an error code otherwise
154+
*/
155+
LSM6DSRStatusTypeDef LSM6DSRSensor::end()
156+
{
157+
/* Disable both acc and gyro */
158+
if (Disable_X() != LSM6DSR_OK)
159+
{
160+
return LSM6DSR_ERROR;
161+
}
162+
163+
if (Disable_G() != LSM6DSR_OK)
164+
{
165+
return LSM6DSR_ERROR;
166+
}
167+
168+
/* Reset CS configuration */
169+
if(dev_spi)
170+
{
171+
// Configure CS pin
172+
pinMode(cs_pin, INPUT);
173+
}
174+
175+
return LSM6DSR_OK;
176+
}
197177

198178
/**
199179
* @brief Read component ID

src/LSM6DSRSensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class LSM6DSRSensor
9696
public:
9797
LSM6DSRSensor(TwoWire *i2c, uint8_t address=LSM6DSR_I2C_ADD_H);
9898
LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
99+
LSM6DSRStatusTypeDef begin();
100+
LSM6DSRStatusTypeDef end();
99101
LSM6DSRStatusTypeDef ReadID(uint8_t *Id);
100102
LSM6DSRStatusTypeDef Enable_X();
101103
LSM6DSRStatusTypeDef Disable_X();

0 commit comments

Comments
 (0)