roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
pressure.cpp
Go to the documentation of this file.
2
3namespace roo_quantity {
4namespace {
5
6void PressureToString(Pressure val, char* out, int maxlen) {
7 if (val.isUnknown()) {
8 strncpy(out, "? Pa", maxlen);
9 } else {
10 const char* format;
11 const char* sign = "";
12 float num;
13 if (val.inPascals() == 0.0f) {
14 snprintf(out, maxlen, "0 Pa");
15 return;
16 }
17 if (val.inPascals() < 0) {
18 sign = "-";
19 val = -val;
20 }
21 if (val >= PressureInGigaPascals(1.0f)) {
22 format = "%s%g GPa";
23 num = val.inGigaPascals();
24 } else if (val >= PressureInMegaPascals(2.0f)) {
25 format = "%s%g MPa";
26 num = val.inMegaPascals();
27 } else if (val >= PressureInBars(0.1f)) {
28 format = "%s%g bar";
29 num = val.inBars();
30 // } else if (val >= PressureInHectoPascals(1100.0f)) {
31 // format = "%s%g kPa";
32 // num = val.inKiloPascals();
33 // } else if (val >= PressureInHectoPascals(800.0f)) {
34 // format = "%s%g hPa";
35 // num = val.inHectoPascals();
36 } else if (val >= PressureInKiloPascals(1.0f)) {
37 format = "%s%g kPa";
38 num = val.inKiloPascals();
39 } else if (val >= PressureInPascals(1.0f)) {
40 format = "%s%g Pa";
41 num = val.inPascals();
42 } else if (val >= PressureInMilliPascals(1.0f)) {
43 format = "%s%g mPa";
44 num = val.inMilliPascals();
45 } else {
46 format = "%s%g µPa";
47 num = val.inMicroPascals();
48 }
49 snprintf(out, maxlen, format, sign, num);
50 }
51}
52
53} // namespace
54
55#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
56
57std::string Pressure::asString() const {
58 char out[16];
59 PressureToString(*this, out, 16);
60 return out;
61}
62
63#endif
64
65#if defined(ARDUINO)
66
67String Pressure::asArduinoString() const {
68 char out[16];
69 PressureToString(*this, out, 16);
70 return out;
71}
72
73#endif
74
75roo_logging::Stream& operator<<(roo_logging::Stream& os, const Pressure& val) {
76 char out[16];
77 PressureToString(val, out, 16);
78 os << out;
79 return os;
80}
81
82} // namespace roo_quantity
Representation of pressure, internally stored as floating-point Pascals.
Definition pressure.h:22
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
Pressure PressureInKiloPascals(float pressure)
Returns a pressure object equivalent to the specified pressure expressed in kiloPascals.
Definition pressure.h:146
roo_logging::Stream & operator<<(roo_logging::Stream &os, const Area &val)
Definition area.cpp:57
Pressure PressureInGigaPascals(float pressure)
Returns a pressure object equivalent to the specified pressure expressed in GigaPascals.
Definition pressure.h:128
Pressure PressureInBars(float pressure)
Returns a pressure object equivalent to the specified pressure expressed in bars.
Definition pressure.h:140
Pressure PressureInPascals(float pressure)
Returns a pressure object equivalent to the specified pressure expressed in Pascals.
Definition pressure.h:158
Pressure PressureInMegaPascals(float pressure)
Returns a pressure object equivalent to the specified pressure expressed in MegaPascals.
Definition pressure.h:134
Pressure PressureInMilliPascals(float pressure)
Returns a pressure object equivalent to the specified pressure expressed in milliPascals.
Definition pressure.h:162