Skip to content

Commit be02f2e

Browse files
authored
Merge pull request #7 from cparata/master
Add begin and end APIs and SPI support
2 parents 1bd4751 + 7d98c7b commit be02f2e

File tree

6 files changed

+164
-92
lines changed

6 files changed

+164
-92
lines changed

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,37 @@ Arduino library to support the LPS25HB 260-1260 hPa absolute digital output baro
33

44
## API
55

6-
This sensor uses I2C to communicate. It is then required to create a TwoWire interface before accessing to the sensors:
6+
This sensor uses I2C or SPI to communicate.
7+
For I2C it is then required to create a TwoWire interface before accessing to the sensors:
78

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

11-
An instance can be created and enbaled following the procedure below:
12+
For SPI it is then required to create a SPI interface before accessing to the sensors:
1213

13-
PressTemp = new LPS25HBSensor(dev_i2c);
14-
PressTemp->Enable();
14+
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
15+
dev_spi.begin();
16+
17+
An instance can be created and enabled when the I2C bus is used following the procedure below:
18+
19+
LPS25HBSensor PressTemp(&dev_i2c);
20+
PressTemp.begin();
21+
PressTemp.Enable();
22+
23+
An instance can be created and enabled when the SPI bus is used following the procedure below:
24+
25+
LPS25HBSensor PressTemp(&dev_spi, CS_PIN);
26+
PressTemp.begin();
27+
PressTemp.Enable();
1528

1629
The access to the sensor values is done as explained below:
1730

1831
Read pressure and temperature.
1932

20-
PressTemp->GetPressure(&pressure);
21-
PressTemp->GetTemperature(&temperature);
33+
float pressure;
34+
float temperature;
35+
PressTemp.GetPressure(&pressure);
36+
PressTemp.GetTemperature(&temperature);
2237

2338
## Documentation
2439

examples/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#endif
5252

5353
// Components.
54-
LPS25HBSensor *PressTemp;
54+
LPS25HBSensor PressTemp(&DEV_I2C);
5555

5656
void setup() {
5757
// Led.
@@ -63,9 +63,9 @@ void setup() {
6363
// Initialize I2C bus.
6464
DEV_I2C.begin();
6565

66-
// Initlialize components.
67-
PressTemp = new LPS25HBSensor (&DEV_I2C);
68-
PressTemp->Enable();
66+
// Initialize components.
67+
PressTemp.begin();
68+
PressTemp.Enable();
6969
}
7070

7171
void loop() {
@@ -77,8 +77,8 @@ void loop() {
7777

7878
// Read pressure and temperature.
7979
float pressure, temperature;
80-
PressTemp->GetPressure(&pressure);
81-
PressTemp->GetTemperature(&temperature);
80+
PressTemp.GetPressure(&pressure);
81+
PressTemp.GetTemperature(&temperature);
8282

8383
// Output data.
8484
SerialPort.print("| Pres[hPa]: ");

keywords.txt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ LPS25HBSensor KEYWORD1
1212
# Methods and Functions (KEYWORD2)
1313
#######################################
1414

15-
Enable KEYWORD2
16-
Disable KEYWORD2
17-
ReadID KEYWORD2
18-
Reset KEYWORD2
19-
GetTemperature KEYWORD2
20-
GetODR KEYWORD2
21-
SetODR KEYWORD2
22-
ReadReg KEYWORD2
23-
WriteReg KEYWORD2
24-
IO_Read KEYWORD2
25-
IO_Write KEYWORD2
26-
GetPressure KEYWORD2
15+
begin KEYWORD2
16+
end KEYWORD2
17+
Enable KEYWORD2
18+
Disable KEYWORD2
19+
ReadID KEYWORD2
20+
Reset KEYWORD2
21+
GetTemperature KEYWORD2
22+
GetODR KEYWORD2
23+
SetODR KEYWORD2
24+
ReadReg KEYWORD2
25+
WriteReg KEYWORD2
26+
IO_Read KEYWORD2
27+
IO_Write KEYWORD2
28+
GetPressure KEYWORD2
2729

2830

2931
#######################################

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=STM32duino LPS25HB
2-
version=1.0.3
2+
version=2.0.0
33
author=AST
44
maintainer=stm32duino
55
sentence=260-1260 hPa absolute digital output barometer.
66
paragraph=This library provides Arduino support for the 260-1260 hPa absolute digital output barometer LPS25HB for STM32 boards.
77
category=Sensors
88
url=https://github.com/stm32duino/LPS25HB
9-
architectures=stm32
9+
architectures=stm32, avr, sam

src/LPS25HBSensor.cpp

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,99 +39,102 @@
3939
/* Includes ------------------------------------------------------------------*/
4040

4141
#include "Arduino.h"
42-
#include "Wire.h"
4342
#include "LPS25HBSensor.h"
4443

4544

4645
/* Class Implementation ------------------------------------------------------*/
47-
4846
/** Constructor
4947
* @param i2c object of an helper class which handles the I2C peripheral
5048
* @param address the address of the component's instance
5149
*/
52-
LPS25HBSensor::LPS25HBSensor(TwoWire *i2c) : dev_i2c(i2c)
50+
LPS25HBSensor::LPS25HBSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
51+
{
52+
dev_spi = NULL;
53+
}
54+
55+
/** Constructor
56+
* @param spi object of an helper class which handles the SPI peripheral
57+
* @param cs_pin the chip select pin
58+
* @param spi_speed the SPI speed
59+
*/
60+
LPS25HBSensor::LPS25HBSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : dev_spi(spi), cs_pin(cs_pin), spi_speed(spi_speed)
5361
{
54-
address = LPS25HB_ADDRESS_HIGH;
62+
dev_i2c = NULL;
63+
address = 0;
64+
}
65+
66+
/**
67+
* @brief Configure the sensor in order to be used
68+
* @retval 0 in case of success, an error code otherwise
69+
*/
70+
LPS25HBStatusTypeDef LPS25HBSensor::begin(void)
71+
{
72+
if(dev_spi)
73+
{
74+
// Configure CS pin
75+
pinMode(cs_pin, OUTPUT);
76+
digitalWrite(cs_pin, HIGH);
77+
}
5578

5679
/* Power down the device */
5780
if ( LPS25HB_DeActivate( (void *)this ) == LPS25HB_ERROR )
5881
{
59-
return;
82+
return LPS25HB_STATUS_ERROR;
6083
}
6184

6285
if ( SetODR( 1.0f ) == LPS25HB_STATUS_ERROR )
6386
{
64-
return;
87+
return LPS25HB_STATUS_ERROR;
6588
}
6689

6790
/* Enable interrupt circuit */
6891
if ( LPS25HB_Set_InterruptCircuitEnable( (void *)this, LPS25HB_ENABLE ) == LPS25HB_ERROR )
6992
{
70-
return;
93+
return LPS25HB_STATUS_ERROR;
7194
}
7295

7396
/* Set block data update mode */
7497
if ( LPS25HB_Set_Bdu( (void *)this, LPS25HB_BDU_NO_UPDATE ) == LPS25HB_ERROR )
7598
{
76-
return;
99+
return LPS25HB_STATUS_ERROR;
77100
}
78101

79102
/* Set SPI mode */
80103
if ( LPS25HB_Set_SpiInterface( (void *)this, LPS25HB_SPI_4_WIRE ) == LPS25HB_ERROR )
81104
{
82-
return;
105+
return LPS25HB_STATUS_ERROR;
83106
}
84107

85108
/* Set internal averaging sample counts for pressure and temperature */
86109
if ( LPS25HB_Set_Avg( (void *)this, LPS25HB_AVGP_32, LPS25HB_AVGT_16 ) == LPS25HB_ERROR )
87110
{
88-
return;
111+
return LPS25HB_STATUS_ERROR;
89112
}
90-
};
91113

114+
return LPS25HB_STATUS_OK;
115+
}
92116

93-
/** Constructor
94-
* @param i2c object of an helper class which handles the I2C peripheral
95-
* @param address the address of the component's instance
117+
/**
118+
* @brief Disable the sensor and relative resources
119+
* @retval 0 in case of success, an error code otherwise
96120
*/
97-
LPS25HBSensor::LPS25HBSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
121+
LPS25HBStatusTypeDef LPS25HBSensor::end(void)
98122
{
99-
/* Power down the device */
100-
if ( LPS25HB_DeActivate( (void *)this ) == LPS25HB_ERROR )
101-
{
102-
return;
103-
}
104-
105-
if ( SetODR( 1.0f ) == LPS25HB_STATUS_ERROR )
106-
{
107-
return;
108-
}
109-
110-
/* Enable interrupt circuit */
111-
if ( LPS25HB_Set_InterruptCircuitEnable( (void *)this, LPS25HB_ENABLE ) == LPS25HB_ERROR )
112-
{
113-
return;
114-
}
115-
116-
/* Set block data update mode */
117-
if ( LPS25HB_Set_Bdu( (void *)this, LPS25HB_BDU_NO_UPDATE ) == LPS25HB_ERROR )
123+
/* Disable pressure and temperature sensor */
124+
if (Disable() != LPS25HB_STATUS_OK)
118125
{
119-
return;
120-
}
121-
122-
/* Set SPI mode */
123-
if ( LPS25HB_Set_SpiInterface( (void *)this, LPS25HB_SPI_4_WIRE ) == LPS25HB_ERROR )
124-
{
125-
return;
126+
return LPS25HB_STATUS_ERROR;
126127
}
127128

128-
/* Set internal averaging sample counts for pressure and temperature */
129-
if ( LPS25HB_Set_Avg( (void *)this, LPS25HB_AVGP_32, LPS25HB_AVGT_16 ) == LPS25HB_ERROR )
129+
/* Reset CS configuration */
130+
if(dev_spi)
130131
{
131-
return;
132+
// Configure CS pin
133+
pinMode(cs_pin, INPUT);
132134
}
133-
};
134135

136+
return LPS25HB_STATUS_OK;
137+
}
135138

136139
/**
137140
* @brief Enable LPS25HB

0 commit comments

Comments
 (0)