Skip to content

Commit d308623

Browse files
authored
Merge pull request #11 from cparata/master
Add begin and end APIs
2 parents d81b871 + 96f0e07 commit d308623

File tree

6 files changed

+59
-53
lines changed

6 files changed

+59
-53
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ Arduino library to support the HTS221 capacitive digital sensor for relative hum
55

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

8-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
9-
dev_i2c->begin();
8+
TwoWire dev_i2c(I2C_SDA, I2C_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

1313
For the humidity sensor:
1414

15-
HumTemp = new HTS221Sensor (dev_i2c);
16-
HumTemp->Enable();
15+
HTS221Sensor HumTemp(&dev_i2c);
16+
HumTemp.begin();
17+
HumTemp.Enable();
1718

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

2021
Read humidity and temperature.
2122

22-
HumTemp->GetHumidity(&humidity);
23-
HumTemp->GetTemperature(&temperature);
23+
float humidity;
24+
float temperature;
25+
HumTemp.GetHumidity(&humidity);
26+
HumTemp.GetTemperature(&temperature);
2427

2528
## Documentation
2629

examples/DISCO_IOT_HTS221_DataLog_Terminal/DISCO_IOT_HTS221_DataLog_Terminal.ino

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
#define I2C2_SCL PB10
4646
#define I2C2_SDA PB11
4747

48+
// I2C
49+
TwoWire dev_i2c(I2C2_SDA, I2C2_SCL);
50+
4851
// Components.
49-
HTS221Sensor *HumTemp;
50-
TwoWire *dev_i2c;
52+
HTS221Sensor HumTemp(&dev_i2c);
5153

5254
void setup() {
5355
// Led.
@@ -56,12 +58,11 @@ void setup() {
5658
Serial.begin(9600);
5759

5860
// Initialize I2C bus.
59-
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
60-
dev_i2c->begin();
61+
dev_i2c.begin();
6162

6263
// Initlialize components.
63-
HumTemp = new HTS221Sensor (dev_i2c);
64-
HumTemp->Enable();
64+
HumTemp.begin();
65+
HumTemp.Enable();
6566
}
6667

6768
void loop() {
@@ -73,8 +74,8 @@ void loop() {
7374

7475
// Read humidity and temperature.
7576
float humidity, temperature;
76-
HumTemp->GetHumidity(&humidity);
77-
HumTemp->GetTemperature(&temperature);
77+
HumTemp.GetHumidity(&humidity);
78+
HumTemp.GetTemperature(&temperature);
7879

7980
// Output data.
8081
Serial.print("Hum[%]: ");

keywords.txt

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

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

2830
#######################################
2931
# Constants (LITERAL1)

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=STM32duino HTS221
2-
version=1.0.4
2+
version=2.0.0
33
author=AST, Wi6Labs
44
maintainer=stm32duino
55
sentence=Capacitive digital sensor for relative humidity and temperature.
66
paragraph=This library provides Arduino support for the capacitive digital sensor for relative humidity and temperature HTS221 for STM32 boards.
77
category=Sensors
88
url=https://github.com/stm32duino/HTS221
9-
architectures=stm32
9+
architectures=stm32, avr, sam

src/HTS221Sensor.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,49 +52,48 @@
5252
HTS221Sensor::HTS221Sensor(TwoWire *i2c) : dev_i2c(i2c)
5353
{
5454
address = HTS221_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+
HTS221StatusTypeDef HTS221Sensor::begin()
62+
{
5663
/* Power down the device */
5764
if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
5865
{
59-
return;
66+
return HTS221_STATUS_ERROR;
6067
}
6168

6269
/* Enable BDU */
6370
if ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR )
6471
{
65-
return;
72+
return HTS221_STATUS_ERROR;
6673
}
6774

6875
if(SetODR(1.0f) == HTS221_STATUS_ERROR)
6976
{
70-
return;
77+
return HTS221_STATUS_ERROR;
7178
}
72-
};
7379

80+
return HTS221_STATUS_OK;
81+
}
7482

75-
/** Constructor
76-
* @param i2c object of an helper class which handles the I2C peripheral
77-
* @param address the address of the component's instance
83+
/**
84+
* @brief Disable the sensor and relative resources
85+
* @retval 0 in case of success, an error code otherwise
7886
*/
79-
HTS221Sensor::HTS221Sensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
87+
HTS221StatusTypeDef HTS221Sensor::end()
8088
{
81-
/* Power down the device */
82-
if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
89+
/* Disable humidity and temperature sensor */
90+
if ( Disable() != HTS221_STATUS_OK )
8391
{
84-
return;
85-
}
86-
87-
/* Enable BDU */
88-
if ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR )
89-
{
90-
return;
92+
return HTS221_STATUS_ERROR;
9193
}
9294

93-
if(SetODR(1.0f) == HTS221_STATUS_ERROR)
94-
{
95-
return;
96-
}
97-
};
95+
return HTS221_STATUS_OK;
96+
}
9897

9998
/**
10099
* @brief Enable HTS221
@@ -117,7 +116,7 @@ HTS221StatusTypeDef HTS221Sensor::Enable(void)
117116
*/
118117
HTS221StatusTypeDef HTS221Sensor::Disable(void)
119118
{
120-
/* Power up the device */
119+
/* Power off the device */
121120
if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
122121
{
123122
return HTS221_STATUS_ERROR;

src/HTS221Sensor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class HTS221Sensor
6666
{
6767
public:
6868
HTS221Sensor (TwoWire *i2c);
69-
HTS221Sensor (TwoWire *i2c, uint8_t address);
69+
HTS221StatusTypeDef begin (void);
70+
HTS221StatusTypeDef end (void);
7071
HTS221StatusTypeDef Enable (void);
7172
HTS221StatusTypeDef Disable (void);
7273
HTS221StatusTypeDef ReadID (uint8_t *ht_id);

0 commit comments

Comments
 (0)