Skip to content

Commit 2526b58

Browse files
authored
Merge pull request #10 from cparata/master
Add begin and end APIs to the base class
2 parents 5b72a34 + 925d770 commit 2526b58

File tree

5 files changed

+76
-92
lines changed

5 files changed

+76
-92
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,34 @@ Arduino library to support the LSM6DSOX 3D accelerometer and 3D gyroscope
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 LSM6DSOXSensor(dev_i2c);
20-
AccGyr->Enable_X();
21-
AccGyr->Enable_G();
19+
LSM6DSOXSensor 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 LSM6DSOXSensor(dev_spi, CS_PIN);
26-
AccGyr->Enable_X();
27-
AccGyr->Enable_G();
26+
LSM6DSOXSensor 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

33-
AccGyr->Get_X_Axes(accelerometer);
34-
AccGyr->Get_G_Axes(gyroscope);
35+
AccGyr.Get_X_Axes(accelerometer);
36+
AccGyr.Get_G_Axes(gyroscope);
3537

3638
## Documentation
3739

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ LSM6DSOXSensor 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 LSM6DSOX
2-
version=1.1.1
2+
version=2.0.0
33
author=SRA
44
maintainer=stm32duino
55
sentence=Ultra Low Power inertial measurement unit.

src/LSM6DSOXSensor.cpp

Lines changed: 55 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -54,67 +54,8 @@ LSM6DSOXSensor::LSM6DSOXSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), ad
5454
reg_ctx.write_reg = LSM6DSOX_io_write;
5555
reg_ctx.read_reg = LSM6DSOX_io_read;
5656
reg_ctx.handle = (void *)this;
57-
58-
/* Disable I3C */
59-
if (lsm6dsox_i3c_disable_set(&reg_ctx, LSM6DSOX_I3C_DISABLE) != LSM6DSOX_OK)
60-
{
61-
return;
62-
}
63-
64-
/* Enable register address automatically incremented during a multiple byte
65-
access with a serial interface. */
66-
if (lsm6dsox_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
67-
{
68-
return;
69-
}
70-
71-
/* Enable BDU */
72-
if (lsm6dsox_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
73-
{
74-
return;
75-
}
76-
77-
/* FIFO mode selection */
78-
if (lsm6dsox_fifo_mode_set(&reg_ctx, LSM6DSOX_BYPASS_MODE) != LSM6DSOX_OK)
79-
{
80-
return;
81-
}
82-
83-
/* Select default output data rate. */
84-
acc_odr = LSM6DSOX_XL_ODR_104Hz;
85-
86-
/* Output data rate selection - power down. */
87-
if (lsm6dsox_xl_data_rate_set(&reg_ctx, LSM6DSOX_XL_ODR_OFF) != LSM6DSOX_OK)
88-
{
89-
return;
90-
}
91-
92-
/* Full scale selection. */
93-
if (lsm6dsox_xl_full_scale_set(&reg_ctx, LSM6DSOX_2g) != LSM6DSOX_OK)
94-
{
95-
return;
96-
}
97-
98-
/* Select default output data rate. */
99-
gyro_odr = LSM6DSOX_GY_ODR_104Hz;
100-
101-
/* Output data rate selection - power down. */
102-
if (lsm6dsox_gy_data_rate_set(&reg_ctx, LSM6DSOX_GY_ODR_OFF) != LSM6DSOX_OK)
103-
{
104-
return;
105-
}
106-
107-
/* Full scale selection. */
108-
if (lsm6dsox_gy_full_scale_set(&reg_ctx, LSM6DSOX_2000dps) != LSM6DSOX_OK)
109-
{
110-
return;
111-
}
112-
113-
acc_is_enabled = 0;
114-
gyro_is_enabled = 0;
115-
116-
117-
return;
57+
acc_is_enabled = 0U;
58+
gyro_is_enabled = 0U;
11859
}
11960

12061
/** Constructor
@@ -127,36 +68,48 @@ LSM6DSOXSensor::LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
12768
reg_ctx.write_reg = LSM6DSOX_io_write;
12869
reg_ctx.read_reg = LSM6DSOX_io_read;
12970
reg_ctx.handle = (void *)this;
130-
131-
// Configure CS pin
132-
pinMode(cs_pin, OUTPUT);
133-
digitalWrite(cs_pin, HIGH);
13471
dev_i2c = NULL;
135-
address = 0;
72+
address = 0;
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+
LSM6DSOXStatusTypeDef LSM6DSOXSensor::begin()
82+
{
83+
if(dev_spi)
84+
{
85+
// Configure CS pin
86+
pinMode(cs_pin, OUTPUT);
87+
digitalWrite(cs_pin, HIGH);
88+
}
13689

13790
/* Disable I3C */
13891
if (lsm6dsox_i3c_disable_set(&reg_ctx, LSM6DSOX_I3C_DISABLE) != LSM6DSOX_OK)
13992
{
140-
return;
93+
return LSM6DSOX_ERROR;
14194
}
14295

14396
/* Enable register address automatically incremented during a multiple byte
14497
access with a serial interface. */
14598
if (lsm6dsox_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
14699
{
147-
return;
100+
return LSM6DSOX_ERROR;
148101
}
149102

150103
/* Enable BDU */
151104
if (lsm6dsox_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
152105
{
153-
return;
106+
return LSM6DSOX_ERROR;
154107
}
155108

156109
/* FIFO mode selection */
157110
if (lsm6dsox_fifo_mode_set(&reg_ctx, LSM6DSOX_BYPASS_MODE) != LSM6DSOX_OK)
158111
{
159-
return;
112+
return LSM6DSOX_ERROR;
160113
}
161114

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

171124
/* Full scale selection. */
172125
if (lsm6dsox_xl_full_scale_set(&reg_ctx, LSM6DSOX_2g) != LSM6DSOX_OK)
173126
{
174-
return;
127+
return LSM6DSOX_ERROR;
175128
}
176129

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

186139
/* Full scale selection. */
187140
if (lsm6dsox_gy_full_scale_set(&reg_ctx, LSM6DSOX_2000dps) != LSM6DSOX_OK)
188141
{
189-
return;
142+
return LSM6DSOX_ERROR;
190143
}
191144

192145
acc_is_enabled = 0;
193146
gyro_is_enabled = 0;
194147

195-
196-
return;
197-
148+
return LSM6DSOX_OK;
149+
}
150+
151+
/**
152+
* @brief Disable the sensor and relative resources
153+
* @retval 0 in case of success, an error code otherwise
154+
*/
155+
LSM6DSOXStatusTypeDef LSM6DSOXSensor::end()
156+
{
157+
/* Disable both acc and gyro */
158+
if (Disable_X() != LSM6DSOX_OK)
159+
{
160+
return LSM6DSOX_ERROR;
161+
}
162+
163+
if (Disable_G() != LSM6DSOX_OK)
164+
{
165+
return LSM6DSOX_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 LSM6DSOX_OK;
198176
}
199177

200178

src/LSM6DSOXSensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class LSM6DSOXSensor
125125
public:
126126
LSM6DSOXSensor(TwoWire *i2c, uint8_t address=LSM6DSOX_I2C_ADD_H);
127127
LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
128+
LSM6DSOXStatusTypeDef begin();
129+
LSM6DSOXStatusTypeDef end();
128130
LSM6DSOXStatusTypeDef ReadID(uint8_t *Id);
129131
LSM6DSOXStatusTypeDef Enable_X();
130132
LSM6DSOXStatusTypeDef Disable_X();

0 commit comments

Comments
 (0)