roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
length.cpp
Go to the documentation of this file.
2
3namespace roo_quantity {
4namespace {
5
6void LengthToString(Length val, char* out, int maxlen) {
7 if (val.isUnknown()) {
8 strncpy(out, "? m", maxlen);
9 } else {
10 const char* format;
11 const char* sign = "";
12 float num;
13 if (val.inMeters() == 0.0f) {
14 snprintf(out, maxlen, "0 m");
15 return;
16 }
17 if (val.inMeters() < 0) {
18 sign = "-";
19 val = -val;
20 }
21 if (val >= LengthInMeters(1000.0f)) {
22 format = "%s%g km";
23 num = val.inKilometers();
24 } else if (val >= LengthInMeters(1.0f)) {
25 format = "%s%g m";
26 num = val.inMeters();
27 } else if (val >= LengthInCentimeters(10.0f)) {
28 format = "%s%g cm";
29 num = val.inCentimeters();
30 } else if (val >= LengthInMillimeters(1.0f)) {
31 format = "%s%g mm";
32 num = val.inMillimeters();
33 } else if (val >= LengthInMicrometers(1.0f)) {
34 format = "%s%g µm";
35 num = val.inMicrometers();
36 } else {
37 format = "%s%g nm";
38 num = val.inNanometers();
39 }
40 snprintf(out, maxlen, format, sign, num);
41 }
42}
43
44} // namespace
45
46#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
47
48std::string Length::asString() const {
49 char out[16];
50 LengthToString(*this, out, 16);
51 return out;
52}
53
54#endif
55
56#if defined(ARDUINO)
57
58String Length::asArduinoString() const {
59 char out[16];
60 LengthToString(*this, out, 16);
61 return out;
62}
63
64#endif
65
66roo_logging::Stream& operator<<(roo_logging::Stream& os, const Length& val) {
67 char out[16];
68 LengthToString(val, out, 16);
69 os << out;
70 return os;
71}
72
73} // namespace roo_quantity
Representation of length, internally stored as floating-point meters.
Definition length.h:19
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
Length LengthInMeters(float length)
Returns a length object equivalent to the specified length expressed in meters.
Definition length.h:133
roo_logging::Stream & operator<<(roo_logging::Stream &os, const Area &val)
Definition area.cpp:57
Length LengthInMillimeters(float length)
Returns a length object equivalent to the specified length expressed in millimeters.
Definition length.h:149
Length LengthInMicrometers(float length)
Returns a length object equivalent to the specified length expressed in micrometers.
Definition length.h:155
Length LengthInCentimeters(float length)
Returns a length object equivalent to the specified length expressed in centimeters.
Definition length.h:143