roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
velocity.cpp
Go to the documentation of this file.
2
3namespace roo_quantity {
4namespace {
5
6void VelocityToString(Velocity val, char* out, int maxlen) {
7 if (val.isUnknown()) {
8 strncpy(out, "? m/s", maxlen);
9 } else {
10 const char* format;
11 const char* sign = "";
12 float num;
13 if (val.inMetersPerSecond() == 0.0f) {
14 snprintf(out, maxlen, "0 m/s");
15 return;
16 }
17 if (val.inMetersPerSecond() < 0) {
18 sign = "-";
19 val = -val;
20 }
21 if (val >= VelocityInKilometersPerSecond(1.0f)) {
22 format = "%s%g km/s";
23 num = val.inKilometersPerSecond();
24 } else if (val >= VelocityInMetersPerSecond(1.0f)) {
25 format = "%s%g m/s";
26 num = val.inMetersPerSecond();
27 } else if (val >= VelocityInMillimetersPerSecond(1.0f)) {
28 format = "%s%g mm/s";
29 num = val.inMillimetersPerSecond();
30 } else {
31 format = "%s%g µm/s";
32 num = val.inMicrometersPerSecond();
33 }
34 snprintf(out, maxlen, format, sign, num);
35 }
36}
37
38} // namespace
39
40#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
41
42std::string Velocity::asString() const {
43 char out[16];
44 VelocityToString(*this, out, 16);
45 return out;
46}
47
48#endif
49
50#if defined(ARDUINO)
51
52String Velocity::asArduinoString() const {
53 char out[16];
54 VelocityToString(*this, out, 16);
55 return out;
56}
57
58#endif
59
60roo_logging::Stream& operator<<(roo_logging::Stream& os, const Velocity& val) {
61 char out[16];
62 VelocityToString(val, out, 16);
63 os << out;
64 return os;
65}
66
67} // namespace roo_quantity
Representation of velocity, internally stored as floating-point meters per second.
Definition velocity.h:22
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
Velocity VelocityInMillimetersPerSecond(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in millimeters per second.
Definition velocity.h:139
Velocity VelocityInMetersPerSecond(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in meters per second.
Definition velocity.h:133
Velocity VelocityInKilometersPerSecond(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in kilometers per second.
Definition velocity.h:127