Skip to content

Commit

Permalink
refactor change() functions (#6)
Browse files Browse the repository at this point in the history
- refactor getter constants
- add return value to **change()** functions.
- rename **change()** functions, more descriptive
- update readme.md
- update GitHub actions
- update license 2023
- minor edits
  • Loading branch information
RobTillaart authored Feb 24, 2023
1 parent 85472d1 commit 3e7ea20
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 70 deletions.
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.0] - 2023-02-23
- refactor getter constants
- add return value to **change()** functions.
- rename **change()** functions, more descriptive
- update readme.md
- update GitHub actions
- update license 2023
- minor edits

----

## [0.2.2] - 2022-11-22
- add changelog.md
- add RP2040 to build-CI
- minor edits


## [0.2.1] - 2021-12-23
## [0.2.1] - 2021-12-23
- update library.json
- update license
- add experimental gas law
Expand Down
95 changes: 57 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Arduino library for pressure conversion.

## Description

Simple library to convert between several pressure formats.
It consists of a number of setters and getters and internally it uses millibar.
In fact it just hides all conversion constants.
Arduino library to convert between several pressure units (formats).
The class consists of a number of setters and getters and internally it uses millibar.
In fact the class just hides all conversion constants to and frm millibar.

Pressure is implemented as a float so this limits the precision of the value.

Expand All @@ -27,8 +27,16 @@ than in a single conversion step as there are two multiplications involved.
Note: constants need to be verified.


#### Related

- https://github.com/RobTillaart/temperature (a bit)


## Interface

```cpp
#include "pressure.h"
```

#### Constructor

Expand Down Expand Up @@ -70,14 +78,15 @@ Note: constants need to be verified.
#### Gas law (experimental see below)

The **change()** function is applied to the current internal pressure.
All these functions return pressure in milliBar.

- **void change(float T1, float T2, float V1, float V2, float N1, float N2)**
- **float change(float T1, float T2, float V1, float V2, float N1, float N2)**
- apply changing temperature (**Kelvin**),
- volume (m3) and moles.
- If an parameter does not change fill in 1 for both before (T1,V1, N1) and after (T2,V2,n2).
- **void changeT(float T1, float T2)** only change temperature. T in **Kelvin**.
- **void changeV(float V1, float V2)** only change volume.
- **void changeN(float N1, float N2)** only change moles.
- **float changeTemperature(float T1, float T2)** only change temperature. T in **Kelvin**.
- **float changeVolume(float V1, float V2)** only change volume.
- **float changeMole(float N1, float N2)** only change moles.


#### Constants
Expand All @@ -86,6 +95,7 @@ The library has a number of constants to convert units. See the pressure.h file.
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.
The constants will multiply compile time!


```cpp
Expand All @@ -99,7 +109,7 @@ or
```cpp
#define PSI2MSW (PSI2MILLIBAR * MILLIBAR2MSW)
...
float out = in * (PSI2MSW);
float out = in * PSI2MSW;
```


Expand All @@ -117,33 +127,32 @@ Serial.print("TORR: ");
Serial.println(P.getTORR()); // 1000 Dynes in Torr
```

#### Obsolete
#### Experimental

Version 0.1.0 has incorrect setters. fixed in version 0.2.0.
Note: names changed in 0.3.0


#### Experimental 0.2.1
Apply the ideal gas law : **(P x V) / (n x T) = Constant**

Apply the ideal gas law : **P x V / n x T = Constant**

- **void change(float T1, float T2, float V1, float V2, float N1, float N2)**
- **float change(float T1, float T2, float V1, float V2, float N1, float N2)**
- T (temperature) in Kelvin,
- V (volume) in identical units,
- N (# atoms) in mole
- **void changeT(float T1, float T2)** only change temperature. T in Kelvin.
- **void changeV(float V1, float V2)** only change volume.
- **void changeN(float N1, float N2)** only change moles.
- wrapper around next three.
- **float changeTemperature(float T1, float T2)** only change temperature. T in Kelvin.
- **float changeVolume(float V1, float V2)** only change volume.
- **float changeMole(float N1, float N2)** only change moles.

in code
```cpp
pressure P;
pressure P;
P.setPressure(...);
P.change(T1, T2, V1, V2, N1, N2); // apply all changes.
P.change(T1, T2, V1, V2, N1, N2); // apply all changes.
x = P.getPressure()
```

- do we need a **changeTC(float T1, float T2)** only change temperature, T in Celsius
- should functions return bool true on success ?

Some temperature converters

```cpp
Kelvin = Celsius + 273.15;
Expand All @@ -154,29 +163,39 @@ Kelvin = Fahrenheit \* 5 / 9 + 290.93; // one operator less.

## Future

#### must
#### Must

- update documentation
- find a good reference for conversion formula constants.
- find a good reference for conversion constants.
- https://www.semicore.com/reference/pressure-conversion-reference


#### Should

#### should
- test with gas law.
- calculate getter constants from setter constants. 1.0 / XXX
- rename parameters so they make more sense?


#### Could

- **float AddMillibar(float value)** simple math with all units (12x)
- ```return _pressure += value * factor;```
- does **subtractMillibar()** make more sense?
- **float setMillibar(float value)** return pressure in millibar all setters
- ```return _pressure = value * factor;```


#### Wont (unless requested)

- **float changeTC(float T1, float T2)** only change temperature, T in Celsius.
- **float changeTF(float T1, float T2)** only change temperature, T in Fahrenheit.
- move code to .cpp file
- rename parameters so they make more sense? (simple enough)
```
void setMilliBar(float milliBar ) { _pressure = milliBar; };
void setBar(float Bar) { _pressure = Bar * BAR2MILLIBAR; };
void setPSI(float PSI) { _pressure = PSI * PSI2MILLIBAR; };
```



#### could
- defaults for functions? 0 like constructor?
- move code to .cpp file ?
- change could return int indicating
- 1: a change is made.
- 0: no change is made
- -1: parameter negative
- change could return a float indicating the new pressure in mBar?

- defaults for functions?
- none <<<<<<<<<<<<<<<<<<<<<<<<<<<
- 0 like constructor?
- 1 to get conversion constant?
12 changes: 6 additions & 6 deletions examples/pressure_gas_law/pressure_gas_law.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ void setup()
Serial.println();

P.setMilliBar(1019.1);
P.changeT(273.15 + 25, 273.15 + 50);
P.changeTemperature(273.15 + 25, 273.15 + 50);
Serial.print(1019.1);
Serial.print(" heated from 25 to 50C gives ");
Serial.println(P.getMilliBar());

P.setMilliBar(1019.1);
P.changeV(2, 1);
P.changeVolume(2, 1);
Serial.print(1019.1);
Serial.print(" with volume in half gives ");
Serial.println(P.getMilliBar());

P.setMilliBar(1019.1);
P.changeN(2, 4);
P.changeMole(2, 4);
Serial.print(1019.1);
Serial.print(" with atoms doubled gives ");
Serial.println(P.getMilliBar());
Expand All @@ -45,9 +45,9 @@ void setup()
Serial.println(P.getMilliBar());

P.setMilliBar(1019.1);
P.changeT(273.15 + 25, 273.15 + 50);
P.changeV(2, 1);
P.changeN(2, 4);
P.changeTemperature(273.15 + 25, 273.15 + 50);
P.changeVolume(2, 1);
P.changeMole(2, 4);
Serial.print(1019.1);
Serial.print(" check the last one: ");
Serial.println(P.getMilliBar());
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/pressure.git"
},
"version": "0.2.2",
"version": "0.3.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=pressure
version=0.2.2
version=0.3.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for pressure conversion
paragraph=
paragraph=
category=Signal Input/Output
url=https://github.com/RobTillaart/pressure.git
architectures=*
Expand Down
68 changes: 48 additions & 20 deletions pressure.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
//
// FILE: pressure.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.2
// VERSION: 0.3.0
// PURPOSE: Arduino library for pressure conversion
// URL: https://github.com/RobTillaart/pressure



#define PRESSURE_LIB_VERSION (F("0.2.2"))
#define PRESSURE_LIB_VERSION (F("0.3.0"))


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

// CONSTANTS SETTERS
// CONSTANTS SETTERS
#define BAR2MILLIBAR 1000
#define ATM2MILLIBAR 1013.25
#define PSI2MILLIBAR 68.9475729318
Expand All @@ -27,7 +27,21 @@
#define CMH2O2MILLIBAR 0.980665
#define MSW2MILLIBAR 0.01

// CONSTANTS GETTERS
// CONSTANTS GETTERS
#define MILLIBAR2BAR (1.0 / BAR2MILLIBAR)
#define MILLIBAR2ATM (1.0 / ATM2MILLIBAR)
#define MILLIBAR2PSI (1.0 / PSI2MILLIBAR)
#define MILLIBAR2DYNES (1.0 / DYNES2MILLIBAR)
#define MILLIBAR2INHG (1.0 / INHG2MILLIBAR)
#define MILLIBAR2INH2O (1.0 / INH2O2MILLIBAR)
#define MILLIBAR2PASCAL (1.0 / PASCAL2MILLIBAR)
#define MILLIBAR2TORR (1.0 / TORR2MILLIBAR)
#define MILLIBAR2CMHG (1.0 / CMHG2MILLIBAR)
#define MILLIBAR2CMH2O (1.0 / CMH2O2MILLIBAR)
#define MILLIBAR2MSW (1.0 / MSW2MILLIBAR)

/*
// was
#define MILLIBAR2BAR 0.001
#define MILLIBAR2ATM 9.86923267e-4
#define MILLIBAR2PSI 0.0145037738
Expand All @@ -37,8 +51,10 @@
#define MILLIBAR2PASCAL 100
#define MILLIBAR2TORR 0.750061683
#define MILLIBAR2CMHG 0.0750061683
#define MILLIBAR2CMH2O 1.0197162129779
#define MILLIBAR2CMH2O 1.0197162129779
#define MILLIBAR2MSW 100
*/



class pressure
Expand Down Expand Up @@ -78,36 +94,48 @@ class pressure

// EXPERIMENTAL
// temperature in KELVIN !
void change(float T1, float T2, float V1, float V2, float N1, float N2)
float change(float T1, float T2, float V1, float V2, float N1, float N2)
{
changeT(T1, T2);
changeV(V1, V2);
changeN(N1, N2);
changeTemperature(T1, T2);
changeVolume(V1, V2);
changeMole(N1, N2);
return _pressure;
}

// temperature in KELVIN!
void changeT(float T1, float T2)
// temperature must be in KELVIN!
float changeTemperature(float T1, float T2)
{
if ((T1 != T2) && (T1 > 0) && (T2 > 0)) _pressure *= (T2 / T1);
if ((T1 != T2) && (T1 > 0) && (T2 > 0))
{
_pressure *= (T2 / T1);
}
return _pressure;
}


void changeV(float V1, float V2)
// volume must be in same units
float changeVolume(float V1, float V2)
{
if ((V1 != V2) && (V1 > 0) && (V2 > 0)) _pressure *= (V1 / V2);
if ((V1 != V2) && (V1 > 0) && (V2 > 0))
{
_pressure *= (V1 / V2);
}
return _pressure;
}


void changeN(float N1, float N2)
// moles must be in same units.
float changeMole(float N1, float N2)
{
if ((N1 != N2) && (N1 > 0) && (N2 > 0)) _pressure *= (N2 / N1);
if ((N1 != N2) && (N1 > 0) && (N2 > 0))
{
_pressure *= (N2 / N1);
}
return _pressure;
}


private:
float _pressure; // millibar.
};


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

5 changes: 4 additions & 1 deletion test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ unittest_setup()
fprintf(stderr, "PRESSURE_LIB_VERSION: %s\n", (char *) PRESSURE_LIB_VERSION);
}


unittest_teardown()
{
}
Expand Down Expand Up @@ -156,4 +157,6 @@ unittest(test_get_set)

unittest_main()

// --------

// -- END OF FILE --

0 comments on commit 3e7ea20

Please sign in to comment.