roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
temperature.cpp
Go to the documentation of this file.
2
3ROO_FLAG(char, roo_quantity_default_temperature_unit, 'C');
4
5namespace roo_quantity {
6namespace {
7
8void TemperatureToString(const Temperature& t, char* out, int maxlen) {
9 switch (GET_ROO_FLAG(roo_quantity_default_temperature_unit)) {
10 case 'F': {
11 if (t.isUnknown()) {
12 snprintf(out, maxlen, "?°F");
13 } else {
14 snprintf(out, maxlen, "%g°F", t.degFahrenheit());
15 }
16 break;
17 }
18 case 'K': {
19 if (t.isUnknown()) {
20 snprintf(out, maxlen, "?°K");
21 } else {
22 snprintf(out, maxlen, "%g°K", t.degKelvin());
23 }
24 break;
25 }
26 default: {
27 if (t.isUnknown()) {
28 snprintf(out, maxlen, "?°C");
29 } else {
30 snprintf(out, maxlen, "%g°C", t.degCelcius());
31 }
32 }
33 }
34}
35
36void TemperatureDeltaToString(const TemperatureDelta& dt, char* out, int maxlen) {
37 switch (GET_ROO_FLAG(roo_quantity_default_temperature_unit)) {
38 case 'F': {
39 if (dt.isUnknown()) {
40 snprintf(out, maxlen, "?°F");
41 } else {
42 snprintf(out, maxlen, "%g°F", dt.degFahrenheit());
43 }
44 break;
45 }
46 case 'K': {
47 if (dt.isUnknown()) {
48 snprintf(out, maxlen, "?°K");
49 } else {
50 snprintf(out, maxlen, "%g°K", dt.degKelvin());
51 }
52 break;
53 }
54 default: {
55 if (dt.isUnknown()) {
56 snprintf(out, maxlen, "?°C");
57 } else {
58 snprintf(out, maxlen, "%g°C", dt.degCelcius());
59 }
60 }
61 }
62}
63} // namespace
64
65#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
66
67std::string Temperature::asString() const {
68 char out[16];
69 TemperatureToString(*this, out, 16);
70 return out;
71}
72
73std::string TemperatureDelta::asString() const {
74 char out[16];
75 TemperatureDeltaToString(*this, out, 16);
76 return out;
77}
78
79#endif
80
81#if defined(ARDUINO)
82
83String Temperature::asArduinoString() const {
84 char out[16];
85 TemperatureToString(*this, out, 16);
86 return out;
87}
88
89String TemperatureDelta::asArduinoString() const {
90 char out[16];
91 TemperatureDeltaToString(*this, out, 16);
92 return out;
93}
94
95#endif
96
97roo_logging::Stream& operator<<(roo_logging::Stream& os, const Temperature& t) {
98 char out[16];
99 TemperatureToString(t, out, 16);
100 os << out;
101 return os;
102}
103
104roo_logging::Stream& operator<<(roo_logging::Stream& os, const TemperatureDelta& dt) {
105 char out[16];
106 TemperatureDeltaToString(dt, out, 16);
107 os << out;
108 return os;
109}
110
111} // namespace roo_quantity
Representation of a temperature, internally stored as floating-point Celsius degrees.
Definition temperature.h:26
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
ROO_FLAG(char, roo_quantity_default_temperature_unit, 'C')