Skip to content

Commit f40cd87

Browse files
authored
Merge pull request #18 from cparata/master
Add begin and end APIs
2 parents e6c116a + 0ee6977 commit f40cd87

File tree

8 files changed

+82
-113
lines changed

8 files changed

+82
-113
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@ Arduino library to support the LSM303AGR 3D accelerometer and 3D magnetometer
55

66
This sensor uses I2C to communicate. It is then required to create a TwoWire interface before accessing the sensors:
77

8-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
9-
dev_i2c->begin();
8+
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
9+
dev_i2c.begin();
1010

11-
An instance can be created and enbaled following the procedure below:
11+
An instance can be created and enabled following the procedure below:
1212

13-
Acc = new LSM303AGR_ACC_Sensor(dev_i2c);
14-
Acc->Enable();
15-
Mag = new LSM303AGR_MAG_Sensor(dev_i2c);
16-
Mag->Enable();
13+
LSM303AGR_ACC_Sensor Acc(&dev_i2c);
14+
Acc.begin();
15+
Acc.Enable();
16+
LSM303AGR_MAG_Sensor Mag(&dev_i2c);
17+
Mag.begin();
18+
Mag.Enable();
1719

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

2022
Read accelerometer and magnetometer.
2123

22-
Acc->GetAxes(accelerometer);
23-
Mag->GetAxes(magnetometer);
24+
int32_t accelerometer[3];
25+
int32_t magnetometer[3];
26+
Acc.GetAxes(accelerometer);
27+
Mag.GetAxes(magnetometer);
2428

2529
## Documentation
2630

examples/X_NUCLEO_IKS01A2_LSM303AGR_DataLog_Terminal/X_NUCLEO_IKS01A2_LSM303AGR_DataLog_Terminal.ino

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
#endif
5353

5454
// Components.
55-
LSM303AGR_ACC_Sensor *Acc;
56-
LSM303AGR_MAG_Sensor *Mag;
55+
LSM303AGR_ACC_Sensor Acc(&DEV_I2C);
56+
LSM303AGR_MAG_Sensor Mag(&DEV_I2C);
5757

5858
void setup() {
5959
// Led.
@@ -66,11 +66,11 @@ void setup() {
6666
DEV_I2C.begin();
6767

6868
// Initlialize components.
69-
Acc = new LSM303AGR_ACC_Sensor(&DEV_I2C);
70-
Acc->Enable();
71-
Acc->EnableTemperatureSensor();
72-
Mag = new LSM303AGR_MAG_Sensor(&DEV_I2C);
73-
Mag->Enable();
69+
Acc.begin();
70+
Acc.Enable();
71+
Acc.EnableTemperatureSensor();
72+
Mag.begin();
73+
Mag.Enable();
7474
}
7575

7676
void loop() {
@@ -82,15 +82,15 @@ void loop() {
8282

8383
// Read accelerometer LSM303AGR.
8484
int32_t accelerometer[3];
85-
Acc->GetAxes(accelerometer);
85+
Acc.GetAxes(accelerometer);
8686

8787
// Read temperature LSM303AGR.
8888
float temperature;
89-
Acc->GetTemperature(&temperature);
89+
Acc.GetTemperature(&temperature);
9090

9191
// Read magnetometer LSM303AGR.
9292
int32_t magnetometer[3];
93-
Mag->GetAxes(magnetometer);
93+
Mag.GetAxes(magnetometer);
9494

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

keywords.txt

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

16+
begin KEYWORD2
17+
end KEYWORD2
1618
Enable KEYWORD2
1719
Disable KEYWORD2
1820
ReadID KEYWORD2

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=STM32duino LSM303AGR
2-
version=1.1.0
2+
version=2.0.0
33
author=AST
44
maintainer=stm32duino
55
sentence=3D accelerometer and 3D magnetometer.
66
paragraph=This library provides Arduino support for the 3D accelerometer and 3D magnetometer LSM303AGR for STM32 boards.
77
category=Sensors
88
url=https://github.com/stm32duino/LSM303AGR
9-
architectures=stm32
9+
architectures=stm32, avr, sam

src/LSM303AGR_ACC_Sensor.cpp

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,104 +51,76 @@
5151
LSM303AGR_ACC_Sensor::LSM303AGR_ACC_Sensor(TwoWire *i2c) : dev_i2c(i2c)
5252
{
5353
address = LSM303AGR_ACC_I2C_ADDRESS;
54-
54+
isEnabled = 0;
55+
}
56+
57+
/**
58+
* @brief Configure the sensor in order to be used
59+
* @retval 0 in case of success, an error code otherwise
60+
*/
61+
LSM303AGR_ACC_StatusTypeDef LSM303AGR_ACC_Sensor::begin(void)
62+
{
5563
/* Enable BDU */
5664
if ( LSM303AGR_ACC_W_BlockDataUpdate( (void *)this, LSM303AGR_ACC_BDU_ENABLED ) == MEMS_ERROR )
5765
{
58-
return;
66+
return LSM303AGR_ACC_STATUS_ERROR;
5967
}
6068

6169
/* FIFO mode selection */
6270
if ( LSM303AGR_ACC_W_FifoMode( (void *)this, LSM303AGR_ACC_FM_BYPASS ) == MEMS_ERROR )
6371
{
64-
return;
72+
return LSM303AGR_ACC_STATUS_ERROR;
6573
}
6674

6775
/* Output data rate selection - power down. */
6876
if ( LSM303AGR_ACC_W_ODR( (void *)this, LSM303AGR_ACC_ODR_DO_PWR_DOWN ) == MEMS_ERROR )
6977
{
70-
return;
78+
return LSM303AGR_ACC_STATUS_ERROR;
7179
}
7280

7381
/* Full scale selection. */
7482
if ( SetFS( 2.0f ) == LSM303AGR_ACC_STATUS_ERROR )
7583
{
76-
return;
84+
return LSM303AGR_ACC_STATUS_ERROR;
7785
}
7886

7987
/* Enable axes. */
8088
if ( LSM303AGR_ACC_W_XEN( (void *)this, LSM303AGR_ACC_XEN_ENABLED ) == MEMS_ERROR )
8189
{
82-
return;
90+
return LSM303AGR_ACC_STATUS_ERROR;
8391
}
8492

8593
if ( LSM303AGR_ACC_W_YEN ( (void *)this, LSM303AGR_ACC_YEN_ENABLED ) == MEMS_ERROR )
8694
{
87-
return;
95+
return LSM303AGR_ACC_STATUS_ERROR;
8896
}
8997

9098
if ( LSM303AGR_ACC_W_ZEN ( (void *)this, LSM303AGR_ACC_ZEN_ENABLED ) == MEMS_ERROR )
9199
{
92-
return;
100+
return LSM303AGR_ACC_STATUS_ERROR;
93101
}
94102

95103
/* Select default output data rate. */
96104
Last_ODR = 100.0f;
97105

98106
isEnabled = 0;
99-
};
100107

101-
/** Constructor
102-
* @param i2c object of an helper class which handles the I2C peripheral
103-
* @param address the address of the component's instance
108+
return LSM303AGR_ACC_STATUS_OK;
109+
}
110+
111+
/**
112+
* @brief Disable the sensor and relative resources
113+
* @retval 0 in case of success, an error code otherwise
104114
*/
105-
LSM303AGR_ACC_Sensor::LSM303AGR_ACC_Sensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
115+
LSM303AGR_ACC_StatusTypeDef LSM303AGR_ACC_Sensor::end(void)
106116
{
107-
/* Enable BDU */
108-
if ( LSM303AGR_ACC_W_BlockDataUpdate( (void *)this, LSM303AGR_ACC_BDU_ENABLED ) == MEMS_ERROR )
109-
{
110-
return;
111-
}
112-
113-
/* FIFO mode selection */
114-
if ( LSM303AGR_ACC_W_FifoMode( (void *)this, LSM303AGR_ACC_FM_BYPASS ) == MEMS_ERROR )
115-
{
116-
return;
117-
}
118-
119-
/* Output data rate selection - power down. */
120-
if ( LSM303AGR_ACC_W_ODR( (void *)this, LSM303AGR_ACC_ODR_DO_PWR_DOWN ) == MEMS_ERROR )
121-
{
122-
return;
123-
}
124-
125-
/* Full scale selection. */
126-
if ( SetFS( 2.0f ) == LSM303AGR_ACC_STATUS_ERROR )
127-
{
128-
return;
129-
}
130-
131-
/* Enable axes. */
132-
if ( LSM303AGR_ACC_W_XEN( (void *)this, LSM303AGR_ACC_XEN_ENABLED ) == MEMS_ERROR )
133-
{
134-
return;
135-
}
136-
137-
if ( LSM303AGR_ACC_W_YEN ( (void *)this, LSM303AGR_ACC_YEN_ENABLED ) == MEMS_ERROR )
138-
{
139-
return;
140-
}
141-
142-
if ( LSM303AGR_ACC_W_ZEN ( (void *)this, LSM303AGR_ACC_ZEN_ENABLED ) == MEMS_ERROR )
117+
if(Disable() != LSM303AGR_ACC_STATUS_OK)
143118
{
144-
return;
119+
return LSM303AGR_ACC_STATUS_ERROR;
145120
}
146-
147-
/* Select default output data rate. */
148-
Last_ODR = 100.0f;
149-
150-
isEnabled = 0;
151-
};
121+
122+
return LSM303AGR_ACC_STATUS_OK;
123+
}
152124

153125
/**
154126
* @brief Enable LSM303AGR Accelerator

src/LSM303AGR_ACC_Sensor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ class LSM303AGR_ACC_Sensor
8282
{
8383
public:
8484
LSM303AGR_ACC_Sensor (TwoWire *i2c);
85-
LSM303AGR_ACC_Sensor (TwoWire *i2c, uint8_t address);
85+
LSM303AGR_ACC_StatusTypeDef begin (void);
86+
LSM303AGR_ACC_StatusTypeDef end (void);
8687
LSM303AGR_ACC_StatusTypeDef Enable (void);
8788
LSM303AGR_ACC_StatusTypeDef Disable (void);
8889
LSM303AGR_ACC_StatusTypeDef ReadID (uint8_t *p_id);

src/LSM303AGR_MAG_Sensor.cpp

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,68 +52,57 @@
5252
LSM303AGR_MAG_Sensor::LSM303AGR_MAG_Sensor(TwoWire *i2c) : dev_i2c(i2c)
5353
{
5454
address = LSM303AGR_MAG_I2C_ADDRESS;
55+
}
5556

57+
/**
58+
* @brief Configure the sensor in order to be used
59+
* @retval 0 in case of success, an error code otherwise
60+
*/
61+
LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::begin(void)
62+
{
5663
/* Operating mode selection - power down */
5764
if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR )
5865
{
59-
return;
66+
return LSM303AGR_MAG_STATUS_ERROR;
6067
}
6168

6269
/* Enable BDU */
6370
if ( LSM303AGR_MAG_W_BDU( (void *)this, LSM303AGR_MAG_BDU_ENABLED ) == MEMS_ERROR )
6471
{
65-
return;
72+
return LSM303AGR_MAG_STATUS_ERROR;
6673
}
6774

6875
if ( SetODR( 100.0f ) == LSM303AGR_MAG_STATUS_ERROR )
6976
{
70-
return;
77+
return LSM303AGR_MAG_STATUS_ERROR;
7178
}
7279

7380
if ( SetFS( 50.0f ) == LSM303AGR_MAG_STATUS_ERROR )
7481
{
75-
return;
82+
return LSM303AGR_MAG_STATUS_ERROR;
7683
}
7784

7885
if ( LSM303AGR_MAG_W_ST( (void *)this, LSM303AGR_MAG_ST_DISABLED ) == MEMS_ERROR )
7986
{
80-
return;
87+
return LSM303AGR_MAG_STATUS_ERROR;
8188
}
82-
};
8389

84-
/** Constructor
85-
* @param i2c object of an helper class which handles the I2C peripheral
86-
* @param address the address of the component's instance
90+
return LSM303AGR_MAG_STATUS_OK;
91+
}
92+
93+
/**
94+
* @brief Disable the sensor and relative resources
95+
* @retval 0 in case of success, an error code otherwise
8796
*/
88-
LSM303AGR_MAG_Sensor::LSM303AGR_MAG_Sensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
97+
LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::end(void)
8998
{
90-
/* Operating mode selection - power down */
91-
if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR )
99+
if(Disable() != LSM303AGR_MAG_STATUS_OK)
92100
{
93-
return;
94-
}
95-
96-
/* Enable BDU */
97-
if ( LSM303AGR_MAG_W_BDU( (void *)this, LSM303AGR_MAG_BDU_ENABLED ) == MEMS_ERROR )
98-
{
99-
return;
100-
}
101-
102-
if ( SetODR( 100.0f ) == LSM303AGR_MAG_STATUS_ERROR )
103-
{
104-
return;
105-
}
106-
107-
if ( SetFS( 50.0f ) == LSM303AGR_MAG_STATUS_ERROR )
108-
{
109-
return;
101+
return LSM303AGR_MAG_STATUS_ERROR;
110102
}
111103

112-
if ( LSM303AGR_MAG_W_ST( (void *)this, LSM303AGR_MAG_ST_DISABLED ) == MEMS_ERROR )
113-
{
114-
return;
115-
}
116-
};
104+
return LSM303AGR_MAG_STATUS_OK;
105+
}
117106

118107
/**
119108
* @brief Enable LSM303AGR magnetometer

src/LSM303AGR_MAG_Sensor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class LSM303AGR_MAG_Sensor
6767
{
6868
public:
6969
LSM303AGR_MAG_Sensor (TwoWire *i2c);
70-
LSM303AGR_MAG_Sensor (TwoWire *i2c, uint8_t address);
70+
LSM303AGR_MAG_StatusTypeDef begin (void);
71+
LSM303AGR_MAG_StatusTypeDef end (void);
7172
LSM303AGR_MAG_StatusTypeDef Enable (void);
7273
LSM303AGR_MAG_StatusTypeDef Disable (void);
7374
LSM303AGR_MAG_StatusTypeDef ReadID (uint8_t *p_id);

0 commit comments

Comments
 (0)