roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
volume.cpp
Go to the documentation of this file.
2
3namespace roo_quantity {
4namespace {
5
6void VolumeToString(const Volume& val, char* out, int maxlen) {
7 if (val.isUnknown()) {
8 strncpy(out, "? m³", maxlen);
9 } else {
10 const char* format;
11 float num;
12 if (val > VolumeInCubicKilometers(1.0f)) {
13 format = "%g km³";
14 num = val.inCubicKilometers();
15 } else if (val > VolumeInCubicMeters(1.0f)) {
16 format = "%g m³";
17 num = val.inCubicMeters();
18 } else if (val > VolumeInCubicDecimeters(1.0f)) {
19 format = "%g dm³";
20 num = val.inCubicDecimeters();
21 } else if (val > VolumeInMilliliters(1.0f)) {
22 format = "%g ml";
23 num = val.inMilliliters();
24 } else if (val > VolumeInCubicMillimeters(1.0f)) {
25 format = "%g mm³";
26 num = val.inCubicMillimeters();
27 } else {
28 format = "%g µm³";
29 num = val.inCubicMicrometers();
30 }
31 snprintf(out, maxlen, format, num);
32 }
33}
34
35} // namespace
36
37#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
38
39std::string Volume::asString() const {
40 char out[16];
41 VolumeToString(*this, out, 16);
42 return out;
43}
44
45#endif
46
47#if defined(ARDUINO)
48
49String Volume::asArduinoString() const {
50 char out[16];
51 VolumeToString(*this, out, 16);
52 return out;
53}
54
55#endif
56
57roo_logging::Stream& operator<<(roo_logging::Stream& os, const Volume& val) {
58 char out[16];
59 VolumeToString(val, out, 16);
60 os << out;
61 return os;
62}
63
64} // namespace roo_quantity
Representation of volume, internally stored as floating-point cubic meters.
Definition volume.h:21
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
roo_logging::Stream & operator<<(roo_logging::Stream &os, const Area &val)
Definition area.cpp:57
Volume VolumeInCubicMeters(float volume)
Returns a volume object equivalent to the specified volume expressed in cubic meters.
Definition volume.h:132
Volume VolumeInCubicKilometers(float volume)
Returns a volume object equivalent to the specified volume expressed in cubic kilometers.
Definition volume.h:126
Volume VolumeInCubicDecimeters(float volume)
Returns a volume object equivalent to the specified volume expressed in liters.
Definition volume.h:142
Volume VolumeInMilliliters(float volume)
Returns a volume object equivalent to the specified volume expressed in milliliters.
Definition volume.h:148
Volume VolumeInCubicMillimeters(float volume)
Returns a volume object equivalent to the specified volume expressed in cubic millimeters.
Definition volume.h:160