diff --git a/libraries/pressure/README.md b/libraries/pressure/README.md index ae7ccf5a..2a57d03b 100644 --- a/libraries/pressure/README.md +++ b/libraries/pressure/README.md @@ -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 @@ -78,6 +102,10 @@ 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 @@ -85,3 +113,4 @@ Serial.println(P.getTORR()); // 1000 Dynes in Torr - find a good reference for conversion formula constants. + diff --git a/libraries/pressure/examples/pressure_specific/pressure_specific.ino b/libraries/pressure/examples/pressure_specific/pressure_specific.ino new file mode 100644 index 00000000..c60dd7dc --- /dev/null +++ b/libraries/pressure/examples/pressure_specific/pressure_specific.ino @@ -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 -- diff --git a/libraries/pressure/keywords.txt b/libraries/pressure/keywords.txt index 70d9ee92..3c330035 100644 --- a/libraries/pressure/keywords.txt +++ b/libraries/pressure/keywords.txt @@ -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 diff --git a/libraries/pressure/library.json b/libraries/pressure/library.json index 47e9181b..5ba26f0d 100644 --- a/libraries/pressure/library.json +++ b/libraries/pressure/library.json @@ -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": "*", diff --git a/libraries/pressure/library.properties b/libraries/pressure/library.properties index 58f0a0c0..92710fd4 100644 --- a/libraries/pressure/library.properties +++ b/libraries/pressure/library.properties @@ -1,5 +1,5 @@ name=pressure -version=0.1.0 +version=0.2.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for pressure conversion diff --git a/libraries/pressure/pressure.h b/libraries/pressure/pressure.h index ae909efb..f55b71ad 100644 --- a/libraries/pressure/pressure.h +++ b/libraries/pressure/pressure.h @@ -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 ? @@ -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 @@ -38,7 +41,6 @@ #define MILLIBAR2MSW 100 -#define PRESSURE_LIB_VERSION (F("0.1.0")) class pressure @@ -49,17 +51,17 @@ public: 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; }; diff --git a/libraries/pressure/test/unit_test_001.cpp b/libraries/pressure/test/unit_test_001.cpp index 316ff568..0de7c938 100644 --- a/libraries/pressure/test/unit_test_001.cpp +++ b/libraries/pressure/test/unit_test_001.cpp @@ -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); @@ -107,7 +107,6 @@ unittest(test_constants_getter) } - unittest(test_constants_3) { fprintf(stderr, "PRESSURE_LIB_VERSION: %s\n", (char *) PRESSURE_LIB_VERSION); @@ -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); @@ -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() // --------