roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
power.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_quantity/time.h"
8#include "roo_quantity/work.h"
9
10#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
11#include <string>
12#endif
13
14#if defined(ARDUINO)
15#include <Arduino.h>
16#endif
17
18namespace roo_quantity {
19
20/// Representation of power, internally stored as floating-point Watts.
21class Power {
22 public:
23 /// Creates a power object representing an 'unknown' power.
24 Power() : power_(std::nanf("")) {}
25
26 /// Returns the power in gigaWatts.
27 float inGigaWatts() const { return power_ / 1000000000.0f; }
28
29 /// Returns the power in megaWatts.
30 float inMegaWatts() const { return power_ / 1000000.0f; }
31
32 /// Returns the power in kiloWatts.
33 float inKiloWatts() const { return power_ / 1000.0f; }
34
35 /// Returns the power in Watts.
36 float inWatts() const { return power_; }
37
38 /// Returns the power in milliWatts.
39 float inMilliWatts() const { return power_ * 1000.0f; }
40
41 /// Returns the power in microWatts.
42 float inMicroWatts() const { return power_ * 1000000.0f; }
43
44 /// Returns whether the object represents an unknown power.
45 bool isUnknown() const { return std::isnan(power_); }
46
47 bool operator<(const Power& other) const { return power_ < other.power_; }
48
49 bool operator==(const Power& other) const { return power_ == other.power_; }
50
51 bool operator>(const Power& other) const { return other.power_ < power_; }
52
53 bool operator<=(const Power& other) const { return !(other.power_ < power_); }
54
55 bool operator>=(const Power& other) const { return !(power_ < other.power_); }
56
57 bool operator!=(const Power& other) const {
58 return !(power_ == other.power_);
59 }
60
61 inline Power& operator+=(const Power& other) {
62 power_ += other.inWatts();
63 return *this;
64 }
65
66 inline Power& operator-=(const Power& other) {
67 power_ -= other.inWatts();
68 return *this;
69 }
70
71 inline Power& operator*=(float multi) {
72 power_ *= multi;
73 return *this;
74 }
75
76 inline Power& operator/=(float div) {
77 power_ /= div;
78 return *this;
79 }
80
81#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
82 /// Returns the string representation of the power.
83 std::string asString() const;
84#endif
85
86#if defined(ARDUINO)
87 String asArduinoString() const;
88#endif
89
90 private:
91 friend Power UnknownPower();
92
93 friend Power PowerInWatts(float);
94
95 explicit Power(float power) : power_(power) {}
96
97 /// Stored in Watts.
98 float power_;
99};
100
101inline Power PowerInWatts(float power);
102
103/// Returns a power object representing an unknown power.
104inline Power UnknownPower() { return Power(); }
105
106/// Returns a power object equivalent to the specified power
107/// expressed in gigaWatts.
108inline Power PowerInGigaWatts(float power) {
109 return PowerInWatts(power * 1000000000.0f);
110}
111
112/// Returns a power object equivalent to the specified power
113/// expressed in megaWatts.
114inline Power PowerInMegaWatts(float power) {
115 return PowerInWatts(power * 1000000.0f);
116}
117
118/// Returns a power object equivalent to the specified power
119/// expressed in kiloWatts.
120inline Power PowerInKiloWatts(float power) {
121 return PowerInWatts(power * 1000.0f);
122}
123
124/// Returns a power object equivalent to the specified power
125/// expressed in Watts.
126inline Power PowerInWatts(float power) { return Power(power); }
127
128/// Returns a power object equivalent to the specified power
129/// expressed in milliWatts.
130inline Power PowerInMilliWatts(float power) {
131 return PowerInWatts(power * 0.001f);
132}
133
134/// Returns a power object equivalent to the specified power
135/// expressed in microWatts.
136inline Power PowerInMicroWatts(float power) {
137 return PowerInWatts(power * 0.000001f);
138}
139
141 return PowerInWatts(a.inWatts() + b.inWatts());
142}
143
145 return PowerInWatts(a.inWatts() - b.inWatts());
146}
147
148inline Power operator-(Power a) { return PowerInWatts(-a.inWatts()); }
149
150inline Power operator*(Power a, float b) {
151 return PowerInWatts(a.inWatts() * b);
152}
153
154inline Power operator*(float a, Power b) {
155 return PowerInWatts(a * b.inWatts());
156}
157
158inline Power operator/(Power a, float b) {
159 return PowerInWatts(a.inWatts() / b);
160}
161
162inline float operator/(Power a, Power b) { return a.inWatts() / b.inWatts(); }
163
164/// Power vs work.
165
166inline Work operator*(Power a, Time b) {
167 return WorkInJoules(a.inWatts() * b.inSeconds());
168}
169
170inline Work operator*(Time a, Power b) {
171 return WorkInJoules(a.inSeconds() * b.inWatts());
172}
173
174inline Power operator/(Work w, Time b) {
175 return PowerInWatts(w.inJoules() / b.inSeconds());
176}
177
178inline Time operator/(Work w, Power b) {
179 return TimeInSeconds(w.inJoules() / b.inWatts());
180}
181
182roo_logging::Stream& operator<<(roo_logging::Stream& os, const Power& val);
183
184} // namespace roo_quantity
Representation of power, internally stored as floating-point Watts.
Definition power.h:21
friend Power UnknownPower()
Returns a power object representing an unknown power.
Definition power.h:104
bool operator==(const Power &other) const
Definition power.h:49
float inMicroWatts() const
Returns the power in microWatts.
Definition power.h:42
Power & operator/=(float div)
Definition power.h:76
bool operator>(const Power &other) const
Definition power.h:51
Power()
Creates a power object representing an 'unknown' power.
Definition power.h:24
float inMegaWatts() const
Returns the power in megaWatts.
Definition power.h:30
bool operator<=(const Power &other) const
Definition power.h:53
friend Power PowerInWatts(float)
Returns a power object equivalent to the specified power expressed in Watts.
Definition power.h:126
bool operator<(const Power &other) const
Definition power.h:47
float inGigaWatts() const
Returns the power in gigaWatts.
Definition power.h:27
Power & operator-=(const Power &other)
Definition power.h:66
bool isUnknown() const
Returns whether the object represents an unknown power.
Definition power.h:45
float inMilliWatts() const
Returns the power in milliWatts.
Definition power.h:39
Power & operator+=(const Power &other)
Definition power.h:61
bool operator>=(const Power &other) const
Definition power.h:55
float inWatts() const
Returns the power in Watts.
Definition power.h:36
bool operator!=(const Power &other) const
Definition power.h:57
Power & operator*=(float multi)
Definition power.h:71
float inKiloWatts() const
Returns the power in kiloWatts.
Definition power.h:33
Representation of elapsed, internally stored as floating-point seconds.
Definition time.h:22
float inSeconds() const
Returns the time in seconds.
Definition time.h:30
Representation of work, internally stored as floating-point Joules.
Definition work.h:19
float inJoules() const
Returns the work in joules.
Definition work.h:28
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
Area operator+(Area a, Area b)
Definition area.h:171
Power PowerInMicroWatts(float power)
Returns a power object equivalent to the specified power expressed in microWatts.
Definition power.h:136
Power UnknownPower()
Returns a power object representing an unknown power.
Definition power.h:104
Power PowerInMegaWatts(float power)
Returns a power object equivalent to the specified power expressed in megaWatts.
Definition power.h:114
Time TimeInSeconds(float time)
Returns a time object equivalent to the specified time expressed in seconds.
Definition time.h:111
Power PowerInWatts(float power)
Returns a power object equivalent to the specified power expressed in Watts.
Definition power.h:126
Power PowerInKiloWatts(float power)
Returns a power object equivalent to the specified power expressed in kiloWatts.
Definition power.h:120
Area operator-(Area a, Area b)
Definition area.h:175
Power PowerInGigaWatts(float power)
Returns a power object equivalent to the specified power expressed in gigaWatts.
Definition power.h:108
Work WorkInJoules(float work)
Returns a work object equivalent to the specified work expressed in Joules.
Definition work.h:116
Area operator/(Area a, float b)
Definition area.h:191
Area operator*(Area a, float b)
Definition area.h:183
Power PowerInMilliWatts(float power)
Returns a power object equivalent to the specified power expressed in milliWatts.
Definition power.h:130