roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
temperature.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
5#include "roo_flags.h"
6#include "roo_logging.h"
7#include "roo_time.h"
8
9#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
10#include <string>
11#endif
12
13#if defined(ARDUINO)
14#include <Arduino.h>
15#endif
16
17ROO_DECLARE_FLAG(char, roo_quantity_default_temperature_unit);
18
19namespace roo_quantity {
20
21class Temperature;
22class TemperatureDelta;
23
24/// Representation of a temperature, internally stored as floating-point Celsius
25/// degrees.
27 public:
28 /// Creates a temperature object representing an 'unknown' temperature.
29 Temperature() : tempC_(std::nanf("")) {}
30
31 /// Returns the temperature in degrees Celcius.
32 float degCelcius() const { return tempC_; }
33
34 /// Returns the temperature in degrees Kelvin.
35 float degKelvin() const { return tempC_ + 273.15; }
36
37 /// Returns the temperature in degrees Fahrenheit.
38 float degFahrenheit() const { return tempC_ * 1.8 + 32.0; }
39
40 /// Returns whether the object represents an unknown temperature.
41 bool isUnknown() const { return std::isnan(tempC_); }
42
43 bool operator<(const Temperature &other) const {
44 return tempC_ < other.tempC_;
45 }
46
47 bool operator==(const Temperature &other) const {
48 return tempC_ == other.tempC_;
49 }
50
51 bool operator>(const Temperature &other) const {
52 return other.tempC_ < tempC_;
53 }
54
55 bool operator<=(const Temperature &other) const {
56 return !(other.tempC_ < tempC_);
57 }
58
59 bool operator>=(const Temperature &other) const {
60 return !(tempC_ < other.tempC_);
61 }
62
63 bool operator!=(const Temperature &other) const {
64 return !(tempC_ == other.tempC_);
65 }
66
67 inline Temperature &operator+=(const TemperatureDelta &other);
68 inline Temperature &operator-=(const TemperatureDelta &other);
69
70#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
71 /// Returns the string representation of the temperature, using the unit
72 /// defined by the 'roo_temperature_default_unit' flag.
73 std::string asString() const;
74#endif
75
76#if defined(ARDUINO)
77 String asArduinoString() const;
78#endif
79
80 private:
82
86
87 explicit Temperature(float tempC) : tempC_(tempC) {}
88
89 /// Using Celsius for the internal representation, so that integer C
90 /// temperatures (particularly, zero) behave well when compared for equality.
91 float tempC_;
92};
93
95 public:
96 /// Creates a temperature delta object representing an 'unknown' temperature
97 /// delta.
98 TemperatureDelta() : tempC_(std::nanf("")) {}
99
100 /// Returns the temperature delta in degrees Celcius.
101 float degCelcius() const { return tempC_; }
102
103 /// Returns the temperature delta in degrees Kelvin.
104 float degKelvin() const { return tempC_; }
105
106 /// Returns the temperature delta in degrees Fahrenheit.
107 float degFahrenheit() const { return tempC_ * 1.8; }
108
109 /// Returns whether the object represents an unknown temperature delta.
110 bool isUnknown() const { return std::isnan(tempC_); }
111
112 bool operator<(const TemperatureDelta &other) const {
113 return tempC_ < other.tempC_;
114 }
115
116 bool operator==(const TemperatureDelta &other) const {
117 return tempC_ == other.tempC_;
118 }
119
120 bool operator>(const TemperatureDelta &other) const {
121 return other.tempC_ < tempC_;
122 }
123
124 bool operator<=(const TemperatureDelta &other) const {
125 return !(other.tempC_ < tempC_);
126 }
127
128 bool operator>=(const TemperatureDelta &other) const {
129 return !(tempC_ < other.tempC_);
130 }
131
132 bool operator!=(const TemperatureDelta &other) const {
133 return !(tempC_ == other.tempC_);
134 }
135
137 tempC_ += other.degCelcius();
138 return *this;
139 }
140
142 tempC_ -= other.degCelcius();
143 return *this;
144 }
145
146 inline TemperatureDelta &operator*=(float multi) {
147 tempC_ *= multi;
148 return *this;
149 }
150
151 inline TemperatureDelta &operator/=(float div) {
152 tempC_ /= div;
153 return *this;
154 }
155
156#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
157 /// Returns the string representation of the temperature delta, using the unit
158 /// defined by the 'roo_temperature_default_unit' flag.
159 std::string asString() const;
160#endif
161
162#if defined(ARDUINO)
163 String asArduinoString() const;
164#endif
165
166 private:
168
172
173 explicit TemperatureDelta(float tempC) : tempC_(tempC) {}
174
175 /// Using Celsius (equivalent to Kelvin) for the internal delta
176 /// representation.
177 float tempC_;
178};
179
180roo_logging::Stream &operator<<(roo_logging::Stream &os, const Temperature &t);
181roo_logging::Stream &operator<<(roo_logging::Stream &os,
182 const TemperatureDelta &t);
183
184/// Returns a temperature object representing an unknown temperature.
186
187/// Returns a temperature object equivalent to the specified temperature
188/// expressed in Celcius degrees.
190 return Temperature(tempC);
191}
192
193/// Returns a temperature object equivalent to the specified temperature
194/// expressed in Kelvin degrees.
195///
196/// Due to floating-point rounding errors, and since the temperature is
197/// internally stored in Celcius degrees, generally,
198/// DegKelvin(x).degKelvin() != x.
200 return Temperature(tempK - 273.15);
201}
202
203/// Returns a temperature object approximately equal to the specified
204/// temperature expressed in Fahrenheit degrees.
205///
206/// Due to floating-point rounding errors, and since the temperature is
207/// internally stored in Celcius degrees, generally,
208/// DegFahrenheit(x).degFahrenheit() != x.
210 return TemperatureDegCelcius((tempF - 32.0) / 1.8);
211}
212
213/// Returns a temperature object representing an unknown temperature delta.
215
216/// Returns a temperature delta object equivalent to the specified temperature
217/// expressed in Celcius degrees.
219 return TemperatureDelta(tempC);
220}
221
222/// Returns a temperature delta object equivalent to the specified temperature
223/// delta expressed in Kelvin degrees. It is in fact equivalent to
224/// TemperatureDeltaDegCelcius.
226 return TemperatureDelta(tempK);
227}
228
229/// Returns a temperature delta object approximately equal to the specified
230/// temperature delta expressed in Fahrenheit degrees.
231///
232/// Due to floating-point rounding errors, and since the temperature is
233/// internally stored in Celcius degrees, generally,
234/// DegFahrenheit(x).degFahrenheit() != x.
236 return TemperatureDeltaDegCelcius(tempF / 1.8);
237}
238
240 tempC_ += other.degCelcius();
241 return *this;
242}
243
245 tempC_ -= other.degCelcius();
246 return *this;
247}
248
252
256
260
264
268
272
276
280
284
288
289} // namespace roo_quantity
bool operator<=(const TemperatureDelta &other) const
TemperatureDelta & operator-=(const TemperatureDelta &other)
float degKelvin() const
Returns the temperature delta in degrees Kelvin.
float degFahrenheit() const
Returns the temperature delta in degrees Fahrenheit.
TemperatureDelta()
Creates a temperature delta object representing an 'unknown' temperature delta.
Definition temperature.h:98
bool operator!=(const TemperatureDelta &other) const
TemperatureDelta & operator*=(float multi)
bool operator==(const TemperatureDelta &other) const
bool operator>(const TemperatureDelta &other) const
TemperatureDelta & operator+=(const TemperatureDelta &other)
bool operator>=(const TemperatureDelta &other) const
friend TemperatureDelta TemperatureDeltaDegKelvin(float)
Returns a temperature delta object equivalent to the specified temperature delta expressed in Kelvin ...
bool isUnknown() const
Returns whether the object represents an unknown temperature delta.
friend TemperatureDelta TemperatureDeltaDegCelcius(float)
Returns a temperature delta object equivalent to the specified temperature expressed in Celcius degre...
float degCelcius() const
Returns the temperature delta in degrees Celcius.
TemperatureDelta & operator/=(float div)
bool operator<(const TemperatureDelta &other) const
friend TemperatureDelta UnknownTemperatureDelta()
Returns a temperature object representing an unknown temperature delta.
friend TemperatureDelta TemperatureDeltaDegFahrenheit(float)
Returns a temperature delta object approximately equal to the specified temperature delta expressed i...
Representation of a temperature, internally stored as floating-point Celsius degrees.
Definition temperature.h:26
bool operator<(const Temperature &other) const
Definition temperature.h:43
float degCelcius() const
Returns the temperature in degrees Celcius.
Definition temperature.h:32
friend Temperature TemperatureDegFahrenheit(float)
Returns a temperature object approximately equal to the specified temperature expressed in Fahrenheit...
friend Temperature TemperatureDegCelcius(float)
Returns a temperature object equivalent to the specified temperature expressed in Celcius degrees.
bool operator>=(const Temperature &other) const
Definition temperature.h:59
friend Temperature TemperatureDegKelvin(float)
Returns a temperature object equivalent to the specified temperature expressed in Kelvin degrees.
bool isUnknown() const
Returns whether the object represents an unknown temperature.
Definition temperature.h:41
bool operator!=(const Temperature &other) const
Definition temperature.h:63
float degFahrenheit() const
Returns the temperature in degrees Fahrenheit.
Definition temperature.h:38
bool operator<=(const Temperature &other) const
Definition temperature.h:55
friend Temperature UnknownTemperature()
Returns a temperature object representing an unknown temperature.
Temperature & operator+=(const TemperatureDelta &other)
float degKelvin() const
Returns the temperature in degrees Kelvin.
Definition temperature.h:35
Temperature & operator-=(const TemperatureDelta &other)
bool operator>(const Temperature &other) const
Definition temperature.h:51
bool operator==(const Temperature &other) const
Definition temperature.h:47
Temperature()
Creates a temperature object representing an 'unknown' temperature.
Definition temperature.h:29
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
Temperature TemperatureDegFahrenheit(float tempF)
Returns a temperature object approximately equal to the specified temperature expressed in Fahrenheit...
TemperatureDelta TemperatureDeltaDegCelcius(float tempC)
Returns a temperature delta object equivalent to the specified temperature expressed in Celcius degre...
TemperatureDelta TemperatureDeltaDegKelvin(float tempK)
Returns a temperature delta object equivalent to the specified temperature delta expressed in Kelvin ...
roo_logging::Stream & operator<<(roo_logging::Stream &os, const Area &val)
Definition area.cpp:57
Temperature TemperatureDegCelcius(float tempC)
Returns a temperature object equivalent to the specified temperature expressed in Celcius degrees.
Area operator+(Area a, Area b)
Definition area.h:171
TemperatureDelta TemperatureDeltaDegFahrenheit(float tempF)
Returns a temperature delta object approximately equal to the specified temperature delta expressed i...
Temperature TemperatureDegKelvin(float tempK)
Returns a temperature object equivalent to the specified temperature expressed in Kelvin degrees.
Area operator-(Area a, Area b)
Definition area.h:175
TemperatureDelta UnknownTemperatureDelta()
Returns a temperature object representing an unknown temperature delta.
Area operator/(Area a, float b)
Definition area.h:191
Area operator*(Area a, float b)
Definition area.h:183
Temperature UnknownTemperature()
Returns a temperature object representing an unknown temperature.
ROO_DECLARE_FLAG(char, roo_quantity_default_temperature_unit)