Skip to content

Commit

Permalink
refactor API (#4)
Browse files Browse the repository at this point in the history
- refactor API, more in line with other Wire based libs.
- update readme.md
- added **isConnected()**
- added asynchronous interface.
- rewrote the synchronous interface.
- add examples.
  • Loading branch information
RobTillaart authored Dec 5, 2023
1 parent 1204650 commit 62ddc8e
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 39 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ 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-04
- refactor API, more in line with other Wire based libs.
- update readme.md
- added **isConnected()**
- added asynchronous interface.
- rewrote the synchronous interface.
- add examples.

----

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


## [0.1.2] - 2022-11-26
- Add RP2040 support to build-CI.
- Add CHANGELOG.md
Expand Down
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ Arduino library specific for AVR tiny processors.

## Description

**Experimental**
The tinySHT2x is an **Experimental** Arduino library to read the SHT2X sensor.
This sensor provides temperature and humidity.

The library is meant for AVR only, tiny platform, so it is minimal (not?) portable.


#### 0.2.0 Breaking change

Version 0.2.0 introduced a breaking change.
Wire parameter has moved from **begin()** to the Constructor.
The user has to call **Wire.begin()** before calling **begin()**.

Not portable, AVR only

#### Related

Based upon https://github.com/RobTillaart/SHT2x
- https://github.com/RobTillaart/SHT2x (based upon).


## Interface
Expand All @@ -31,6 +40,33 @@ Based upon https://github.com/RobTillaart/SHT2x
#include "tinySHT2x.h"
```

#### Functions

- **tinySHT2x(TwoWire \* wire = &Wire)** Constructor
- **bool begin()** initializes internals. Returns true if device can be found.
- **bool isConnected()** Returns true if device can be found.
- **bool reset()** sends SOFT_RESET command to sensor.
returns false if I2C failed to send it.


#### Async interface

With the Async interface the user must watch keep track of the appropriate
delay between the request and read.
For temperature this is typical around 70 millis and for humidity 30 millis().

- **void requestTemperature()** sends GET_TEMPERATURE command to sensor.
- **void requestHumidity()** sends GET_HUMIDITY command to sensor.
- **float readTemperature()** fetches data from the sensor.
- **float readHumidity()** fetches data from the sensor.

#### Sync interface

- **float getTemperature(uint8_t del = 70)** requests and read the temperature.
It uses a delay of 70 milliseconds which can be tuned by the user.
- **float getHumidity(uint8_t del = 30)** requests and read the humidity.
It uses a delay of30 milliseconds which can be tuned by the user.

- see https://github.com/RobTillaart/SHT2x


Expand All @@ -45,6 +81,9 @@ Based upon https://github.com/RobTillaart/SHT2x

#### Could

- investigate async interface
- splitting requestHumidity() and getHumidity().
- idem for temperature.
- Can a tiny have another Wire than Wire?
- Check the status bit (temperature / humidity flag)
- datasheet page 8, LSB bit 1 - bit 0 not used)
Expand Down
2 changes: 2 additions & 0 deletions documents/Download Center - Sensirion.url
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[InternetShortcut]
URL=https://www.sensirion.com/en/download-center/humidity-sensors/humidity-temperature-sensor-sht2x-digital-i2c-accurate/
Binary file added documents/SHT20_datasheet.pdf
Binary file not shown.
Binary file added documents/SHT21_datasheet.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 2 additions & 1 deletion examples/tinySHT2x_demo/tinySHT2x_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void setup()
Serial.begin(115200);
Serial.println(__FILE__);

Wire.begin();
sht.begin();
}

Expand All @@ -29,5 +30,5 @@ void loop()
}


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

12 changes: 12 additions & 0 deletions examples/tinySHT2x_demo_async/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
- leonardo
# - m4
#- esp32
# - esp8266
- mega2560
# - trinket
38 changes: 38 additions & 0 deletions examples/tinySHT2x_demo_async/tinySHT2x_demo_async.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// FILE: tinySHT2x_demo_async.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/tinySHT2x


#include "Wire.h"
#include "tinySHT2x.h"

tinySHT2x sht;


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);

Wire.begin();
sht.begin();
}


void loop()
{
sht.requestTemperature();
delay(65); // tune to work
Serial.print(sht.readTemperature());
Serial.print("\t");

sht.requestHumidity();
delay(28); // tune to work
Serial.println(sht.getHumidity());
delay(1000);
}


// -- END OF FILE --
12 changes: 12 additions & 0 deletions examples/tinySHT2x_demo_delay/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
- leonardo
# - m4
#- esp32
# - esp8266
- mega2560
# - trinket
33 changes: 33 additions & 0 deletions examples/tinySHT2x_demo_delay/tinySHT2x_demo_delay.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// FILE: tinySHT2x_demo_delay.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/tinySHT2x


#include "Wire.h"
#include "tinySHT2x.h"

tinySHT2x sht;


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);

Wire.begin();
sht.begin();
}


void loop()
{
Serial.print(sht.getTemperature(65)); // adjust delay if needed
Serial.print("\t");
Serial.println(sht.getHumidity(28)); // adjust delay if needed
delay(1000);
}


// -- END OF FILE --
9 changes: 8 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ tinySHT2x KEYWORD1

# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
reset KEYWORD2

requestTemperature KEYWORD2
requestHumidity KEYWORD2
readTemperature KEYWORD2
readHumidity KEYWORD2

getTemperature KEYWORD2
getHumidity KEYWORD2
reset KEYWORD2


# Instances (KEYWORD2)
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/tinySHT2x.git"
},
"version": "0.1.3",
"version": "0.2.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "avr",
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=tinySHT2x
version=0.1.3
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the SHT20, SHT21 and SHT25 temperature and humidity sensor. Optimized for AVR tiny.
Expand Down
13 changes: 8 additions & 5 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ unittest(test_constant)
unittest(test_constructor)
{
tinySHT2x sht;


Wire.begin();

sht.begin();
assertTrue(sht.reset());
// need godmode for these
// assertEqualFloat(TINY_SHT2x_NO_VALUE, sht.getTemperature(), 0.01);
// assertEqualFloat(TINY_SHT2x_NO_VALUE, sht.getHumidity(), 0.01);
// need godmode for these
// assertEqualFloat(TINY_SHT2x_NO_VALUE, sht.getTemperature(), 0.01);
// assertEqualFloat(TINY_SHT2x_NO_VALUE, sht.getHumidity(), 0.01);
}


unittest_main()


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

Loading

0 comments on commit 62ddc8e

Please sign in to comment.