Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@ Arduino library to support the LPS25HB 260-1260 hPa absolute digital output baro

## API

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

dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
dev_i2c->begin();
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
dev_i2c.begin();

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

PressTemp = new LPS25HBSensor(dev_i2c);
PressTemp->Enable();
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
dev_spi.begin();

An instance can be created and enabled when the I2C bus is used following the procedure below:

LPS25HBSensor PressTemp(&dev_i2c);
PressTemp.begin();
PressTemp.Enable();

An instance can be created and enabled when the SPI bus is used following the procedure below:

LPS25HBSensor PressTemp(&dev_spi, CS_PIN);
PressTemp.begin();
PressTemp.Enable();

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

Read pressure and temperature.

PressTemp->GetPressure(&pressure);
PressTemp->GetTemperature(&temperature);
float pressure;
float temperature;
PressTemp.GetPressure(&pressure);
PressTemp.GetTemperature(&temperature);

## Documentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#endif

// Components.
LPS25HBSensor *PressTemp;
LPS25HBSensor PressTemp(&DEV_I2C);

void setup() {
// Led.
Expand All @@ -63,9 +63,9 @@ void setup() {
// Initialize I2C bus.
DEV_I2C.begin();

// Initlialize components.
PressTemp = new LPS25HBSensor (&DEV_I2C);
PressTemp->Enable();
// Initialize components.
PressTemp.begin();
PressTemp.Enable();
}

void loop() {
Expand All @@ -77,8 +77,8 @@ void loop() {

// Read pressure and temperature.
float pressure, temperature;
PressTemp->GetPressure(&pressure);
PressTemp->GetTemperature(&temperature);
PressTemp.GetPressure(&pressure);
PressTemp.GetTemperature(&temperature);

// Output data.
SerialPort.print("| Pres[hPa]: ");
Expand Down
26 changes: 14 additions & 12 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ LPS25HBSensor KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################

Enable KEYWORD2
Disable KEYWORD2
ReadID KEYWORD2
Reset KEYWORD2
GetTemperature KEYWORD2
GetODR KEYWORD2
SetODR KEYWORD2
ReadReg KEYWORD2
WriteReg KEYWORD2
IO_Read KEYWORD2
IO_Write KEYWORD2
GetPressure KEYWORD2
begin KEYWORD2
end KEYWORD2
Enable KEYWORD2
Disable KEYWORD2
ReadID KEYWORD2
Reset KEYWORD2
GetTemperature KEYWORD2
GetODR KEYWORD2
SetODR KEYWORD2
ReadReg KEYWORD2
WriteReg KEYWORD2
IO_Read KEYWORD2
IO_Write KEYWORD2
GetPressure KEYWORD2


#######################################
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=STM32duino LPS25HB
version=1.0.3
version=2.0.0
author=AST
maintainer=stm32duino
sentence=260-1260 hPa absolute digital output barometer.
paragraph=This library provides Arduino support for the 260-1260 hPa absolute digital output barometer LPS25HB for STM32 boards.
category=Sensors
url=https://github.com/stm32duino/LPS25HB
architectures=stm32
architectures=stm32, avr, sam
93 changes: 48 additions & 45 deletions src/LPS25HBSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,99 +39,102 @@
/* Includes ------------------------------------------------------------------*/

#include "Arduino.h"
#include "Wire.h"
#include "LPS25HBSensor.h"


/* Class Implementation ------------------------------------------------------*/

/** Constructor
* @param i2c object of an helper class which handles the I2C peripheral
* @param address the address of the component's instance
*/
LPS25HBSensor::LPS25HBSensor(TwoWire *i2c) : dev_i2c(i2c)
LPS25HBSensor::LPS25HBSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
{
dev_spi = NULL;
}

/** Constructor
* @param spi object of an helper class which handles the SPI peripheral
* @param cs_pin the chip select pin
* @param spi_speed the SPI speed
*/
LPS25HBSensor::LPS25HBSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : dev_spi(spi), cs_pin(cs_pin), spi_speed(spi_speed)
{
address = LPS25HB_ADDRESS_HIGH;
dev_i2c = NULL;
address = 0;
}

/**
* @brief Configure the sensor in order to be used
* @retval 0 in case of success, an error code otherwise
*/
LPS25HBStatusTypeDef LPS25HBSensor::begin(void)
{
if(dev_spi)
{
// Configure CS pin
pinMode(cs_pin, OUTPUT);
digitalWrite(cs_pin, HIGH);
}

/* Power down the device */
if ( LPS25HB_DeActivate( (void *)this ) == LPS25HB_ERROR )
{
return;
return LPS25HB_STATUS_ERROR;
}

if ( SetODR( 1.0f ) == LPS25HB_STATUS_ERROR )
{
return;
return LPS25HB_STATUS_ERROR;
}

/* Enable interrupt circuit */
if ( LPS25HB_Set_InterruptCircuitEnable( (void *)this, LPS25HB_ENABLE ) == LPS25HB_ERROR )
{
return;
return LPS25HB_STATUS_ERROR;
}

/* Set block data update mode */
if ( LPS25HB_Set_Bdu( (void *)this, LPS25HB_BDU_NO_UPDATE ) == LPS25HB_ERROR )
{
return;
return LPS25HB_STATUS_ERROR;
}

/* Set SPI mode */
if ( LPS25HB_Set_SpiInterface( (void *)this, LPS25HB_SPI_4_WIRE ) == LPS25HB_ERROR )
{
return;
return LPS25HB_STATUS_ERROR;
}

/* Set internal averaging sample counts for pressure and temperature */
if ( LPS25HB_Set_Avg( (void *)this, LPS25HB_AVGP_32, LPS25HB_AVGT_16 ) == LPS25HB_ERROR )
{
return;
return LPS25HB_STATUS_ERROR;
}
};

return LPS25HB_STATUS_OK;
}

/** Constructor
* @param i2c object of an helper class which handles the I2C peripheral
* @param address the address of the component's instance
/**
* @brief Disable the sensor and relative resources
* @retval 0 in case of success, an error code otherwise
*/
LPS25HBSensor::LPS25HBSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
LPS25HBStatusTypeDef LPS25HBSensor::end(void)
{
/* Power down the device */
if ( LPS25HB_DeActivate( (void *)this ) == LPS25HB_ERROR )
{
return;
}

if ( SetODR( 1.0f ) == LPS25HB_STATUS_ERROR )
{
return;
}

/* Enable interrupt circuit */
if ( LPS25HB_Set_InterruptCircuitEnable( (void *)this, LPS25HB_ENABLE ) == LPS25HB_ERROR )
{
return;
}

/* Set block data update mode */
if ( LPS25HB_Set_Bdu( (void *)this, LPS25HB_BDU_NO_UPDATE ) == LPS25HB_ERROR )
/* Disable pressure and temperature sensor */
if (Disable() != LPS25HB_STATUS_OK)
{
return;
}

/* Set SPI mode */
if ( LPS25HB_Set_SpiInterface( (void *)this, LPS25HB_SPI_4_WIRE ) == LPS25HB_ERROR )
{
return;
return LPS25HB_STATUS_ERROR;
}

/* Set internal averaging sample counts for pressure and temperature */
if ( LPS25HB_Set_Avg( (void *)this, LPS25HB_AVGP_32, LPS25HB_AVGT_16 ) == LPS25HB_ERROR )
/* Reset CS configuration */
if(dev_spi)
{
return;
// Configure CS pin
pinMode(cs_pin, INPUT);
}
};

return LPS25HB_STATUS_OK;
}

/**
* @brief Enable LPS25HB
Expand Down
Loading