Skip to content

Commit

Permalink
refactor API, begin + constructor (#10)
Browse files Browse the repository at this point in the history
- refactor API, follow SHT31
  • Loading branch information
RobTillaart committed Dec 9, 2023
1 parent c9e70f8 commit febab9e
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 57 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.2.0] - 2023-12-09
- refactor API, follow SHT31

----

## [0.1.3] - 2023-11-22
- update readme.md


## [0.1.2] - 2023-07-23
- update documentation
- remove commented **SoftwareWire** version => own repo.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ Test on UNO
| SHT85 | ~0.2 | 1.5 | no | See SHT31_SWW


#### 0.2.0 Breaking change

Version 0.2.0 introduced a breaking change in constructor and begin().
Parameters have moved from begin() to the constructor.


#### Related

These libraries need to be installed to get SHT31_SW working:
Expand Down
22 changes: 7 additions & 15 deletions SHT31_SW.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: SHT31_SW.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// AUTHOR: Rob Tillaart, Gunter Haug
// VERSION: 0.2.0
// DATE: 2019-02-08 (base SHT31 lib)
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
// to be used with the SoftWire library instead of (hardware) Wire.
Expand All @@ -14,10 +14,10 @@
#include "SHT31_SW.h"


SHT31_SW::SHT31_SW()
SHT31_SW::SHT31_SW(const uint8_t address, SoftWire *softWire)
{
_softWire = NULL;
_address = 0;
_address = address;
_softWire = softWire;
_lastRead = 0;
_rawTemperature = 0;
_rawHumidity = 0;
Expand All @@ -29,25 +29,17 @@ SHT31_SW::SHT31_SW()
}


bool SHT31_SW::begin(const uint8_t address, SoftWire *softWire)
bool SHT31_SW::begin()
{
if ((address != 0x44) && (address != 0x45))
if ((_address != 0x44) && (_address != 0x45))
{
return false;
}
_address = address;
_softWire = softWire;
_softWire->begin();
return reset();
}


bool SHT31_SW::begin(SoftWire *softWire)
{
return begin(SHT_DEFAULT_ADDRESS, softWire);
}


bool SHT31_SW::isConnected()
{
_softWire->beginTransmission(_address);
Expand Down
11 changes: 4 additions & 7 deletions SHT31_SW.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: SHT31_SW.h
// AUTHOR: Rob Tillaart, Gunter Haug
// VERSION: 0.1.3
// VERSION: 0.2.0
// DATE: 2019-02-08 (base SHT31 lib)
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
// to be used with the SoftWire library instead of (hardware) Wire.
Expand All @@ -12,7 +12,7 @@
// https://github.com/RobTillaart/SHT31


#define SHT31_SW_LIB_VERSION (F("0.1.3"))
#define SHT31_SW_LIB_VERSION (F("0.2.0"))


#include "Arduino.h"
Expand All @@ -23,11 +23,8 @@
class SHT31_SW : public SHT31
{
public:
SHT31_SW();

// use SHT_DEFAULT_ADDRESS
bool begin(const uint8_t address, SoftWire *wire);
bool begin(SoftWire *wire);
SHT31_SW(uint8_t address, SoftWire *wire);
bool begin();

// check if sensor is reachable over I2C
bool isConnected();
Expand Down
6 changes: 3 additions & 3 deletions examples/SHT31_I2Cspeed/SHT31_I2Cspeed.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SoftWire sw(6, 7);
uint32_t start;
uint32_t stop;

SHT31_SW sht;
SHT31_SW sht(SHT31_ADDRESS, &sw);


void setup()
Expand All @@ -27,8 +27,8 @@ void setup()
Serial.println(SHT31_SW_LIB_VERSION);

sw.begin();
sht.begin(SHT31_ADDRESS, &sw);
sw.setClock(100000);
sht.begin();

uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
Expand Down Expand Up @@ -58,7 +58,7 @@ void test()
Serial.print(sht.getTemperature(), 1);
Serial.print("\t");
Serial.println(sht.getHumidity(), 1);
delay(100);
delay(1000);
}


Expand Down
12 changes: 6 additions & 6 deletions examples/SHT31_async/SHT31_async.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ uint32_t start;
uint32_t stop;
uint32_t cnt;

SHT31_SW sht;
SHT31_SW sht(SHT31_ADDRESS, &sw);


void setup()
Expand All @@ -28,8 +28,8 @@ void setup()
Serial.println(SHT31_SW_LIB_VERSION);

sw.begin();
sht.begin(SHT31_ADDRESS, &sw);
sw.setClock(100000);
sht.begin();

uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
Expand All @@ -45,9 +45,9 @@ void loop()
if (sht.dataReady())
{
start = micros();
bool success = sht.readData(); // default = true = fast
bool success = sht.readData(); // default = true = fast
stop = micros();
sht.requestData(); // request for next sample
sht.requestData(); // request for next sample

Serial.print("\t");
Serial.print(stop - start);
Expand All @@ -66,8 +66,8 @@ void loop()
cnt = 0;
}
}
cnt++; // simulate other activity
cnt++; // simulate other activity
}


// -- END OF FILE --
// -- END OF FILE --
4 changes: 2 additions & 2 deletions examples/SHT31_demo/SHT31_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SoftWire sw(6, 7);
uint32_t start;
uint32_t stop;

SHT31_SW sht;
SHT31_SW sht(SHT31_ADDRESS, &sw);


void setup()
Expand All @@ -28,7 +28,7 @@ void setup()

sw.begin();
sw.setClock(100000);
sht.begin(SHT31_ADDRESS, &sw);
sht.begin();

Serial.print("CON:\t");
Serial.println(sht.isConnected());
Expand Down
8 changes: 4 additions & 4 deletions examples/SHT31_heater/SHT31_heater.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SoftWire sw(6, 7);
uint32_t start;
uint32_t stop;

SHT31_SW sht;
SHT31_SW sht(SHT31_ADDRESS, &sw);
uint16_t status;


Expand All @@ -28,10 +28,10 @@ void setup()
Serial.println(SHT31_SW_LIB_VERSION);

sw.begin();
sht.begin(SHT31_ADDRESS, &sw);
sw.setClock(100000);
sht.begin();

sht.setHeatTimeout(30); // heater timeout 30 seconds, just for demo.
sht.setHeatTimeout(30); // heater timeout 30 seconds, just for demo.

status = sht.readStatus();
printHeaterStatus(status);
Expand Down Expand Up @@ -71,4 +71,4 @@ void printHeaterStatus(uint16_t status)
}


// -- END OF FILE --
// -- END OF FILE --
4 changes: 2 additions & 2 deletions examples/SHT31_isConnected/SHT31_isConnected.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ uint32_t start;
uint32_t stop;
uint32_t connectionFails = 0;

SHT31_SW sht;
SHT31_SW sht(SHT31_ADDRESS, &sw);


void setup()
Expand All @@ -28,8 +28,8 @@ void setup()
Serial.println(SHT31_SW_LIB_VERSION);

sw.begin();
sht.begin(SHT31_ADDRESS, &sw);
sw.setClock(100000);
sht.begin();

uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
Expand Down
8 changes: 4 additions & 4 deletions examples/SHT31_lastRead/SHT31_lastRead.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SoftWire sw(6, 7);
uint32_t start;
uint32_t stop;

SHT31_SW sht;
SHT31_SW sht(SHT31_ADDRESS, &sw);


void setup()
Expand All @@ -27,8 +27,8 @@ void setup()
Serial.println(SHT31_SW_LIB_VERSION);

sw.begin();
sht.begin(SHT31_ADDRESS, &sw);
sw.setClock(100000);
sht.begin();


uint16_t stat = sht.readStatus();
Expand All @@ -39,7 +39,7 @@ void setup()

void loop()
{
sht.read(); // default = true/fast slow = false
sht.read(); // default = true/fast slow = false
Serial.print("\t");
Serial.print(sht.lastRead());
Serial.print("\t");
Expand All @@ -50,5 +50,5 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --

20 changes: 12 additions & 8 deletions examples/SHT31_rawValues/SHT31_rawValues.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ uint32_t start;
uint32_t stop;
uint32_t cnt;

SHT31_SW sht;
SHT31_SW sht(SHT31_ADDRESS, &sw);


void setup()
Expand All @@ -28,8 +28,8 @@ void setup()
Serial.println(SHT31_SW_LIB_VERSION);

sw.begin();
sht.begin(SHT31_ADDRESS, &sw);
sw.setClock(100000);
sht.begin();


uint16_t stat = sht.readStatus();
Expand All @@ -49,9 +49,9 @@ void loop()
if (sht.dataReady())
{
start = micros();
bool success = sht.readData(); // default = true = fast
bool success = sht.readData(); // default = true = fast
stop = micros();
sht.requestData(); // request for next sample
sht.requestData(); // request for next sample

Serial.print("\t");
Serial.print(stop - start);
Expand All @@ -66,19 +66,23 @@ void loop()
rawHumidity = sht.getRawHumidity();
Serial.print(rawTemperature, HEX);
Serial.print(" = ");
Serial.print(rawTemperature * (175.0 / 65535) - 45, 1); // This formula comes from page 14 of the SHT31 datasheet

// This formula comes from page 14 of the SHT31 datasheet
Serial.print(rawTemperature * (175.0 / 65535) - 45, 1);
Serial.print("°C\t");
Serial.print(sht.getRawHumidity(), HEX);
Serial.print(" = ");
Serial.print(rawHumidity * (100.0 / 65535), 1); // This formula comes from page 14 of the SHT31 datasheet

// This formula comes from page 14 of the SHT31 datasheet
Serial.print(rawHumidity * (100.0 / 65535), 1);
Serial.print("%\t");
Serial.println(cnt);
cnt = 0;
}
}
cnt++; // simulate other activity
cnt++; // simulate other activity
}


// -- END OF FILE --
// -- END OF FILE --

4 changes: 2 additions & 2 deletions examples/SHT31_slowRead/SHT31_slowRead.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SoftWire sw(6, 7);
uint32_t start;
uint32_t stop;

SHT31_SW sht;
SHT31_SW sht(SHT31_ADDRESS, &sw);


void setup()
Expand All @@ -27,8 +27,8 @@ void setup()
Serial.println(SHT31_SW_LIB_VERSION);

sw.begin();
sht.begin(SHT31_ADDRESS, &sw);
sw.setClock(100000);
sht.begin();

uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"version": "^1.1.2"
}
],
"version": "0.1.3",
"version": "0.2.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SHT31_SW
version=0.1.3
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>, Gunter Haug
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the I2C SHT31 temperature and humidity sensor
Expand Down
2 changes: 1 addition & 1 deletion test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "SHT31_SW.h"


int expect; // TODO needed as there seems a problem with 8 bit comparisons (char?)
int expect; // TODO needed as there seems a problem with 8 bit comparisons (char?)

uint32_t start, stop;

Expand Down

0 comments on commit febab9e

Please sign in to comment.