Skip to content

Commit

Permalink
Develop (#3)
Browse files Browse the repository at this point in the history
* fix setters and unit test
  • Loading branch information
RobTillaart committed Nov 26, 2021
1 parent 826fd9a commit b8ee289
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 19 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ Note: constants need to be verified.
- **float getMSW()** returns pressure in Meters of Sea Water. (under water pressure unit).


#### constants

The library has a number of constants to convert units.
These constants can be used to write specific convertors or define specific constants.

A dedicated conversion is faster as it has only one float multiplication runtime.


```cpp
inline float PSI2MSW(float value)
{
return value * (PSI2MILLIBAR * MILLIBAR2MSW);
}
```
or
```cpp
#define PSI2MSW (PSI2MILLIBAR * MILLIBAR2MSW)
...
float out = in * (PSI2MSW);
```



## Operation

```cpp
Expand All @@ -78,10 +102,15 @@ Serial.print("TORR: ");
Serial.println(P.getTORR()); // 1000 Dynes in Torr
```

#### Obsolete

Version 0.1.0 has incorrect setters. fixed in version 0.2.0.


## Future

- update documentation
- find a good reference for conversion formula constants.



76 changes: 76 additions & 0 deletions examples/pressure_specific/pressure_specific.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// FILE: pressure_specific.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// DATE: 2021-11-26
// URL: https://github.com/RobTillaart/pressure


#include "pressure.h"


pressure P;

uint32_t start, stop;
float x;

volatile float value = 1.234;

void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PRESSURE_LIB_VERSION: ");
Serial.println(PRESSURE_LIB_VERSION);

start = micros();
for (int i = 0; i < 1000; i++)
{
P.setPSI(value);
x = P.getMSW();
}
stop = micros();
Serial.print("TIME:\t");
Serial.println(stop - start);
Serial.print("MSW:\t");
Serial.println(x);
delay(10);

start = micros();
for (int i = 0; i < 1000; i++)
{
x = PSI2MSW(value);
}
stop = micros();
Serial.print("TIME:\t");
Serial.println(stop - start);
Serial.print("MSW:\t");
Serial.println(x);
delay(10);

start = micros();
for (int i = 0; i < 1000; i++)
{
x = value * (PSI2MILLIBAR * MILLIBAR2MSW);
}
stop = micros();
Serial.print("TIME:\t");
Serial.println(stop - start);
Serial.print("MSW:\t");
Serial.println(x);
delay(10);

}

void loop()
{
}

inline float PSI2MSW(float value)
{
return value * (PSI2MILLIBAR * MILLIBAR2MSW);
}


// -- END OF FILE --
23 changes: 23 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,27 @@ getMSW KEYWORD2


# Constants (LITERAL1)
BAR2MILLIBAR LITERAL1
ATM2MILLIBAR LITERAL1
PSI2MILLIBAR LITERAL1
DYNES2MILLIBAR LITERAL1
INHG2MILLIBAR LITERAL1
INH202MILLIBAR LITERAL1
PASCAL2MILLIBAR LITERAL1
TORR2MILLIBAR LITERAL1
CMHG2MILLIBAR LITERAL1
CMH2O2MILLIBAR LITERAL1
MSW2MILLIBAR LITERAL1

MILLIBAR2BAR LITERAL1
MILLIBAR2ATM LITERAL1
MILLIBAR2PSI LITERAL1
MILLIBAR2DYNES LITERAL1
MILLIBAR2INHG LITERAL1
MILLIBAR2INH2O LITERAL1
MILLIBAR2PASCAL LITERAL1
MILLIBAR2TORR LITERAL1
MILLIBAR2CMHG LITERAL1
MILLIBAR2CMH2O LITERAL1
MILLIBAR2MSW LITERAL1

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/pressure.git"
},
"version": "0.1.0",
"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=pressure
version=0.1.0
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for pressure conversion
Expand Down
30 changes: 16 additions & 14 deletions pressure.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
//
// FILE: pressure.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.2.0
// PURPOSE: Arduino library for pressure conversion
// URL: https://github.com/RobTillaart/pressure
//


#define PRESSURE_LIB_VERSION (F("0.2.0"))


// CONSTANTS NEED TO BE VERIFIED
// Temperature 25°C ?

Expand All @@ -17,7 +20,7 @@
#define PSI2MILLIBAR 68.9475729318
#define DYNES2MILLIBAR 0.001
#define INHG2MILLIBAR 33.85355
#define INH202MILLIBAR 2.4908890833333
#define INH2O2MILLIBAR 2.4908890833333
#define PASCAL2MILLIBAR 0.01
#define TORR2MILLIBAR 1.33322368
#define CMHG2MILLIBAR 13.3322368
Expand All @@ -38,7 +41,6 @@
#define MILLIBAR2MSW 100


#define PRESSURE_LIB_VERSION (F("0.1.0"))


class pressure
Expand All @@ -49,17 +51,17 @@ class pressure


void setMilliBar(float value) { _pressure = value; };
void setBar(float value) { _pressure = value * MILLIBAR2BAR; };
void setPSI(float value) { _pressure = value * MILLIBAR2PSI; };
void setATM(float value) { _pressure = value * MILLIBAR2ATM; }
void setDynes(float value) { _pressure = value * MILLIBAR2DYNES; }
void setInchHg(float value) { _pressure = value * MILLIBAR2INHG; }
void setInchH2O(float value) { _pressure = value * MILLIBAR2INH2O; }
void setPascal(float value) { _pressure = value * MILLIBAR2PASCAL; }
void setTORR(float value) { _pressure = value * MILLIBAR2TORR; }
void setCmHg(float value) { _pressure = value * MILLIBAR2CMHG; }
void setCmH2O(float value) { _pressure = value * MILLIBAR2CMH2O; }
void setMSW(float value) { _pressure = value * MILLIBAR2MSW; }
void setBar(float value) { _pressure = value * BAR2MILLIBAR; };
void setPSI(float value) { _pressure = value * PSI2MILLIBAR; };
void setATM(float value) { _pressure = value * ATM2MILLIBAR; }
void setDynes(float value) { _pressure = value * DYNES2MILLIBAR; }
void setInchHg(float value) { _pressure = value * INHG2MILLIBAR; }
void setInchH2O(float value) { _pressure = value * INH2O2MILLIBAR; }
void setPascal(float value) { _pressure = value * PASCAL2MILLIBAR; }
void setTORR(float value) { _pressure = value * TORR2MILLIBAR; }
void setCmHg(float value) { _pressure = value * CMHG2MILLIBAR; }
void setCmH2O(float value) { _pressure = value * CMH2O2MILLIBAR; }
void setMSW(float value) { _pressure = value * MSW2MILLIBAR; }


float getMilliBar() { return _pressure; };
Expand Down
41 changes: 38 additions & 3 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ unittest(test_constants_setter)
assertEqualFloat(68.9475729318, PSI2MILLIBAR, 1e-4);
assertEqualFloat(0.001, DYNES2MILLIBAR, 1e-7);
assertEqualFloat(33.85355, INHG2MILLIBAR, 1e-4);
assertEqualFloat(2.49088908333, INH202MILLIBAR, 1e-4);
assertEqualFloat(2.49088908333, INH2O2MILLIBAR, 1e-4);
assertEqualFloat(0.01, PASCAL2MILLIBAR, 1e-4);
assertEqualFloat(1.33322368, TORR2MILLIBAR, 1e-5);
assertEqualFloat(13.3322368, CMHG2MILLIBAR, 1e-5);
Expand Down Expand Up @@ -107,7 +107,6 @@ unittest(test_constants_getter)
}



unittest(test_constants_3)
{
fprintf(stderr, "PRESSURE_LIB_VERSION: %s\n", (char *) PRESSURE_LIB_VERSION);
Expand All @@ -119,7 +118,7 @@ unittest(test_constants_3)
assertEqualFloat(1.0, PSI2MILLIBAR * MILLIBAR2PSI, 1e-5);
assertEqualFloat(1.0, DYNES2MILLIBAR * MILLIBAR2DYNES, 1e-5);
assertEqualFloat(1.0, INHG2MILLIBAR * MILLIBAR2INHG, 1e-5);
assertEqualFloat(1.0, INH202MILLIBAR * MILLIBAR2INH2O, 1e-5);
assertEqualFloat(1.0, INH2O2MILLIBAR * MILLIBAR2INH2O, 1e-5);
assertEqualFloat(1.0, PASCAL2MILLIBAR * MILLIBAR2PASCAL, 1e-5);
assertEqualFloat(1.0, TORR2MILLIBAR * MILLIBAR2TORR, 1e-5);
assertEqualFloat(1.0, CMHG2MILLIBAR * MILLIBAR2CMHG, 1e-5);
Expand All @@ -128,6 +127,42 @@ unittest(test_constants_3)
}


unittest(test_get_set)
{
fprintf(stderr, "PRESSURE_LIB_VERSION: %s\n", (char *) PRESSURE_LIB_VERSION);

pressure P;

P.setMilliBar(2);
assertEqualFloat(2.0, P.getMilliBar(), 1e-4);
P.setBar(2);
assertEqualFloat(2.0, P.getBar(), 1e-4);
P.setPSI(2);
assertEqualFloat(2.0, P.getPSI(), 1e-4);

P.setATM(2);
assertEqualFloat(2.0, P.getATM(), 1e-4);
P.setDynes(2);
assertEqualFloat(2.0, P.getDynes(), 1e-4);
P.setInchHg(2);
assertEqualFloat(2.0, P.getInchHg(), 1e-4);

P.setInchH2O(2);
assertEqualFloat(2.0, P.getInchH2O(), 1e-4);
P.setPascal(2);
assertEqualFloat(2.0, P.getPascal(), 1e-4);
P.setTORR(2);
assertEqualFloat(2.0, P.getTORR(), 1e-4);

P.setCmHg(2);
assertEqualFloat(2.0, P.getCmHg(), 1e-4);
P.setCmH2O(2);
assertEqualFloat(2.0, P.getCmH2O(), 1e-4);
P.setMSW(2);
assertEqualFloat(2.0, P.getMSW(), 1e-4);
}


unittest_main()

// --------

0 comments on commit b8ee289

Please sign in to comment.