From 25e2e91c5ce133e1d447d9e30a2630e5aeff3b26 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Wed, 27 Jan 2021 17:18:37 +0100 Subject: [PATCH] Add begin and end APIs --- README.md | 14 ++++++++------ keywords.txt | 2 ++ library.properties | 2 +- src/STTS22HSensor.cpp | 39 +++++++++++++++++++++++++++++++-------- src/STTS22HSensor.h | 2 ++ 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b6b6984..6101d33 100644 --- a/README.md +++ b/README.md @@ -6,19 +6,21 @@ Arduino library to support the STTS22H digital temperature sensor This sensor uses I2C to communicate. For I2C it is then required to create a TwoWire interface before accessing to the sensors: - dev_i2c = new TwoWire(I2C_SDA, I2C_SCL); - dev_i2c->begin(); + TwoWire dev_i2c(I2C_SDA, I2C_SCL); + dev_i2c.begin(); -An instance can be created and enbaled when the I2C bus is used following the procedure below: +An instance can be created and enabled when the I2C bus is used following the procedure below: - Temp = new STTS22HSensor(dev_i2c); - Temp->Enable(); + STTS22HSensor Temp(&dev_i2c); + Temp.begin(); + Temp.Enable(); The access to the sensor values is done as explained below: Read temperature. - Temp->GetTemperature(&temperature); + float temperature; + Temp.GetTemperature(&temperature); ## Documentation diff --git a/keywords.txt b/keywords.txt index a5a3845..9b38d62 100644 --- a/keywords.txt +++ b/keywords.txt @@ -12,6 +12,8 @@ STTS22HSensor KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### +begin KEYWORD2 +end KEYWORD2 ReadID KEYWORD2 Enable KEYWORD2 Disable KEYWORD2 diff --git a/library.properties b/library.properties index e9ebd90..b319f7f 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=STM32duino STTS22H -version=1.0.1 +version=2.0.0 author=SRA maintainer=stm32duino sentence=digital temperature sensor. diff --git a/src/STTS22HSensor.cpp b/src/STTS22HSensor.cpp index 9059231..a1c225e 100644 --- a/src/STTS22HSensor.cpp +++ b/src/STTS22HSensor.cpp @@ -52,31 +52,54 @@ STTS22HSensor::STTS22HSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), addr reg_ctx.write_reg = STTS22H_io_write; reg_ctx.read_reg = STTS22H_io_read; reg_ctx.handle = (void *)this; + temp_is_enabled = 0U; +} +/** + * @brief Configure the sensor in order to be used + * @retval 0 in case of success, an error code otherwise + */ +STTS22HStatusTypeDef STTS22HSensor::begin() +{ /* Set default ODR */ temp_odr = 1.0f; /* Enable BDU */ if(stts22h_block_data_update_set(®_ctx, PROPERTY_ENABLE) != STTS22H_OK) { - return; + return STTS22H_ERROR; } /* Enable Automatic Address Increment */ if(stts22h_auto_increment_set(®_ctx, PROPERTY_ENABLE) != STTS22H_OK) { - return; + return STTS22H_ERROR; } /* Put the component in standby mode. */ if (stts22h_temp_data_rate_set(®_ctx, STTS22H_POWER_DOWN) != STTS22H_OK) { - return; + return STTS22H_ERROR; } - temp_is_enabled = 0; - - return; + temp_is_enabled = 0U; + + return STTS22H_OK; +} + +/** + * @brief Disable the sensor and relative resources + * @retval 0 in case of success, an error code otherwise + */ +STTS22HStatusTypeDef STTS22HSensor::end() +{ + /* Disable temperature sensor */ + if (Disable() != STTS22H_OK) + { + return STTS22H_ERROR; + } + + return STTS22H_OK; } /** @@ -116,7 +139,7 @@ STTS22HStatusTypeDef STTS22HSensor::Enable() return STTS22H_ERROR; } - temp_is_enabled = 1; + temp_is_enabled = 1U; return STTS22H_OK; } @@ -145,7 +168,7 @@ STTS22HStatusTypeDef STTS22HSensor::Disable() return STTS22H_ERROR; } - temp_is_enabled = 0; + temp_is_enabled = 0U; return STTS22H_OK; } diff --git a/src/STTS22HSensor.h b/src/STTS22HSensor.h index 1c9fec6..543a3e3 100644 --- a/src/STTS22HSensor.h +++ b/src/STTS22HSensor.h @@ -69,6 +69,8 @@ class STTS22HSensor { public: STTS22HSensor(TwoWire *i2c, uint8_t address=STTS22H_I2C_ADD_H); + STTS22HStatusTypeDef begin(); + STTS22HStatusTypeDef end(); STTS22HStatusTypeDef ReadID(uint8_t *Id); STTS22HStatusTypeDef Enable(); STTS22HStatusTypeDef Disable();