Skip to content

Commit 61e961f

Browse files
authored
Merge pull request #3 from cparata/master
Add begin and end APIs
2 parents 4dd4b53 + f330761 commit 61e961f

File tree

5 files changed

+60
-73
lines changed

5 files changed

+60
-73
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,32 @@ Arduino library to support the LIS2DW12 3D accelerometer
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-
Accelero = new LIS2DW12Sensor(dev_i2c);
20-
Accelero->Enable();
19+
LIS2DW12Sensor Accelero(&dev_i2c);
20+
Accelero.begin();
21+
Accelero.Enable_X();
2122

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

24-
Accelero = new LIS2DW12Sensor(dev_spi, CS_PIN);
25-
Accelero->Enable();
25+
LIS2DW12Sensor Accelero(&dev_spi, CS_PIN);
26+
Accelero.begin();
27+
Accelero.Enable_X();
2628

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

2931
Read accelerometer.
3032

31-
Accelero->GetAxes(&accelerometer);
33+
int32_t accelerometer[3];
34+
Accelero.Get_X_Axes(accelerometer);
3235

3336
## Documentation
3437

keywords.txt

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

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

library.properties

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

src/LIS2DW12Sensor.cpp

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -54,54 +54,7 @@ LIS2DW12Sensor::LIS2DW12Sensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), ad
5454
reg_ctx.write_reg = LIS2DW12_io_write;
5555
reg_ctx.read_reg = LIS2DW12_io_read;
5656
reg_ctx.handle = (void *)this;
57-
58-
/* Enable register address automatically incremented during a multiple byte
59-
access with a serial interface. */
60-
if (lis2dw12_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != 0)
61-
{
62-
return;
63-
}
64-
65-
/* Enable BDU */
66-
if (lis2dw12_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != 0)
67-
{
68-
return;
69-
}
70-
71-
/* FIFO mode selection */
72-
if (lis2dw12_fifo_mode_set(&reg_ctx, LIS2DW12_BYPASS_MODE) != 0)
73-
{
74-
return;
75-
}
76-
77-
/* Power mode selection */
78-
if (lis2dw12_power_mode_set(&reg_ctx, LIS2DW12_HIGH_PERFORMANCE) != 0)
79-
{
80-
return;
81-
}
82-
83-
/* Output data rate selection - power down. */
84-
if (lis2dw12_data_rate_set(&reg_ctx, LIS2DW12_XL_ODR_OFF) != 0)
85-
{
86-
return;
87-
}
88-
89-
/* Full scale selection. */
90-
if (lis2dw12_full_scale_set(&reg_ctx, LIS2DW12_2g) != 0)
91-
{
92-
return;
93-
}
94-
95-
/* Select default output data rate. */
96-
X_Last_ODR = 100.0f;
97-
98-
X_Last_Operating_Mode = LIS2DW12_HIGH_PERFORMANCE_MODE;
99-
100-
X_Last_Noise = LIS2DW12_LOW_NOISE_DISABLE;
101-
10257
X_isEnabled = 0;
103-
104-
return;
10558
}
10659

10760
/** Constructor
@@ -114,54 +67,59 @@ LIS2DW12Sensor::LIS2DW12Sensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
11467
reg_ctx.write_reg = LIS2DW12_io_write;
11568
reg_ctx.read_reg = LIS2DW12_io_read;
11669
reg_ctx.handle = (void *)this;
117-
118-
// Configure CS pin
119-
pinMode(cs_pin, OUTPUT);
120-
digitalWrite(cs_pin, HIGH);
12170
dev_i2c = NULL;
12271
address = 0;
72+
X_isEnabled = 0;
73+
}
12374

124-
/* Disable I2C on the component */
125-
if (lis2dw12_i2c_interface_set(&reg_ctx, LIS2DW12_I2C_DISABLE) != 0)
75+
/**
76+
* @brief Configure the sensor in order to be used
77+
* @retval 0 in case of success, an error code otherwise
78+
*/
79+
LIS2DW12StatusTypeDef LIS2DW12Sensor::begin()
80+
{
81+
if(dev_spi)
12682
{
127-
return;
83+
// Configure CS pin
84+
pinMode(cs_pin, OUTPUT);
85+
digitalWrite(cs_pin, HIGH);
12886
}
12987

13088
/* Enable register address automatically incremented during a multiple byte
13189
access with a serial interface. */
13290
if (lis2dw12_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != 0)
13391
{
134-
return;
92+
return LIS2DW12_STATUS_ERROR;
13593
}
13694

13795
/* Enable BDU */
13896
if (lis2dw12_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != 0)
13997
{
140-
return;
98+
return LIS2DW12_STATUS_ERROR;
14199
}
142100

143101
/* FIFO mode selection */
144102
if (lis2dw12_fifo_mode_set(&reg_ctx, LIS2DW12_BYPASS_MODE) != 0)
145103
{
146-
return;
104+
return LIS2DW12_STATUS_ERROR;
147105
}
148106

149107
/* Power mode selection */
150108
if (lis2dw12_power_mode_set(&reg_ctx, LIS2DW12_HIGH_PERFORMANCE) != 0)
151109
{
152-
return;
110+
return LIS2DW12_STATUS_ERROR;
153111
}
154112

155113
/* Output data rate selection - power down. */
156114
if (lis2dw12_data_rate_set(&reg_ctx, LIS2DW12_XL_ODR_OFF) != 0)
157115
{
158-
return;
116+
return LIS2DW12_STATUS_ERROR;
159117
}
160118

161119
/* Full scale selection. */
162120
if (lis2dw12_full_scale_set(&reg_ctx, LIS2DW12_2g) != 0)
163121
{
164-
return;
122+
return LIS2DW12_STATUS_ERROR;
165123
}
166124

167125
/* Select default output data rate. */
@@ -173,7 +131,29 @@ LIS2DW12Sensor::LIS2DW12Sensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
173131

174132
X_isEnabled = 0;
175133

176-
return;
134+
return LIS2DW12_STATUS_OK;
135+
}
136+
137+
/**
138+
* @brief Disable the sensor and relative resources
139+
* @retval 0 in case of success, an error code otherwise
140+
*/
141+
LIS2DW12StatusTypeDef LIS2DW12Sensor::end()
142+
{
143+
/* Disable acc */
144+
if (Disable_X() != LIS2DW12_STATUS_OK)
145+
{
146+
return LIS2DW12_STATUS_ERROR;
147+
}
148+
149+
/* Reset CS configuration */
150+
if(dev_spi)
151+
{
152+
// Configure CS pin
153+
pinMode(cs_pin, INPUT);
154+
}
155+
156+
return LIS2DW12_STATUS_OK;
177157
}
178158

179159
/**

src/LIS2DW12Sensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class LIS2DW12Sensor
103103
public:
104104
LIS2DW12Sensor(TwoWire *i2c, uint8_t address=LIS2DW12_I2C_ADD_H);
105105
LIS2DW12Sensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
106+
LIS2DW12StatusTypeDef begin();
107+
LIS2DW12StatusTypeDef end();
106108
LIS2DW12StatusTypeDef Enable_X(void);
107109
LIS2DW12StatusTypeDef Disable_X(void);
108110
LIS2DW12StatusTypeDef ReadID(uint8_t *id);

0 commit comments

Comments
 (0)