roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
voltage.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"
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 voltage, internally stored as floating-point Volts.
21class Voltage {
22 public:
23 /// Creates a voltage object representing an 'unknown' voltage.
24 Voltage() : voltage_(std::nanf("")) {}
25
26 /// Returns the voltage in kiloVolts.
27 float inKiloVolts() const { return voltage_ * 0.001f; }
28
29 /// Returns the voltage in Volts.
30 float inVolts() const { return voltage_; }
31
32 /// Returns the voltage in milliVolts.
33 float inMilliVolts() const { return voltage_ * 1000.0f; }
34
35 /// Returns the voltage in microVolts.
36 float inMicroVolts() const { return voltage_ * 1000000.0f; }
37
38 /// Returns whether the object represents an unknown voltage.
39 bool isUnknown() const { return std::isnan(voltage_); }
40
41 bool operator<(const Voltage& other) const {
42 return voltage_ < other.voltage_;
43 }
44
45 bool operator==(const Voltage& other) const {
46 return voltage_ == other.voltage_;
47 }
48
49 bool operator>(const Voltage& other) const {
50 return other.voltage_ < voltage_;
51 }
52
53 bool operator<=(const Voltage& other) const {
54 return !(other.voltage_ < voltage_);
55 }
56
57 bool operator>=(const Voltage& other) const {
58 return !(voltage_ < other.voltage_);
59 }
60
61 bool operator!=(const Voltage& other) const {
62 return !(voltage_ == other.voltage_);
63 }
64
65 inline Voltage& operator+=(const Voltage& other) {
66 voltage_ += other.inVolts();
67 return *this;
68 }
69
70 inline Voltage& operator-=(const Voltage& other) {
71 voltage_ -= other.inVolts();
72 return *this;
73 }
74
75 inline Voltage& operator*=(float multi) {
76 voltage_ *= multi;
77 return *this;
78 }
79
80 inline Voltage& operator/=(float div) {
81 voltage_ /= div;
82 return *this;
83 }
84
85#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
86 /// Returns the string representation of the voltage.
87 std::string asString() const;
88#endif
89
90#if defined(ARDUINO)
91 String asArduinoString() const;
92#endif
93
94 private:
95 friend Voltage UnknownVoltage();
96
97 friend Voltage VoltageInVolts(float);
98
99 explicit Voltage(float voltage) : voltage_(voltage) {}
100
101 /// Stored in Volts.
102 float voltage_;
103};
104
105inline Voltage VoltageInVolts(float voltage);
106
107/// Returns a voltage object representing an unknown voltage.
108inline Voltage UnknownVoltage() { return Voltage(); }
109
110/// Returns a voltage object equivalent to the specified voltage
111/// expressed in kiloVolts.
112inline Voltage VoltageInKiloVolts(float voltage) {
113 return VoltageInVolts(voltage * 1000.0f);
114}
115
116/// Returns a voltage object equivalent to the specified voltage
117/// expressed in Volts.
118inline Voltage VoltageInVolts(float voltage) { return Voltage(voltage); }
119
120/// Returns a voltage object equivalent to the specified voltage
121/// expressed in milliVolts.
122inline Voltage VoltageInMilliVolts(float voltage) {
123 return VoltageInVolts(voltage * 0.001f);
124}
125
126/// Returns a voltage object equivalent to the specified voltage
127/// expressed in microVolts.
128inline Voltage VoltageInMicroVolts(float voltage) {
129 return VoltageInVolts(voltage * 0.000001f);
130}
131
133 return VoltageInVolts(a.inVolts() + b.inVolts());
134}
135
137 return VoltageInVolts(a.inVolts() - b.inVolts());
138}
139
140inline Voltage operator-(Voltage a) { return VoltageInVolts(-a.inVolts()); }
141
142inline Voltage operator*(Voltage a, float b) {
143 return VoltageInVolts(a.inVolts() * b);
144}
145
146inline Voltage operator*(float a, Voltage b) {
147 return VoltageInVolts(a * b.inVolts());
148}
149
150inline Voltage operator/(Voltage a, float b) {
151 return VoltageInVolts(a.inVolts() / b);
152}
153
154inline float operator/(Voltage a, Voltage b) {
155 return a.inVolts() / b.inVolts();
156}
157
158/// Vs power.
159
161 return PowerInWatts(a.inVolts() * b.inAmperes());
162}
163
165 return PowerInWatts(a.inAmperes() * b.inVolts());
166}
167
169 return VoltageInVolts(a.inWatts() / b.inAmperes());
170}
171
173 return CurrentInAmperes(a.inWatts() / b.inVolts());
174}
175
176/// Vs work.
177
179 return WorkInJoules(a.inVolts() * b.inCoulombs());
180}
181
183 return WorkInJoules(a.inCoulombs() * b.inVolts());
184}
185
187 return VoltageInVolts(a.inJoules() / b.inCoulombs());
188}
189
191 return ChargeInCoulombs(a.inJoules() / b.inVolts());
192}
193
194roo_logging::Stream& operator<<(roo_logging::Stream& os, const Voltage& val);
195
196} // namespace roo_quantity
Representation of charge, internally stored as floating-point Coulombs.
Definition charge.h:19
float inCoulombs() const
Returns the charge in Coulombs.
Definition charge.h:28
Representation of current, internally stored as floating-point Amperes.
Definition current.h:21
float inAmperes() const
Returns the current in Amperes.
Definition current.h:33
Representation of power, internally stored as floating-point Watts.
Definition power.h:21
float inWatts() const
Returns the power in Watts.
Definition power.h:36
Representation of voltage, internally stored as floating-point Volts.
Definition voltage.h:21
bool operator<(const Voltage &other) const
Definition voltage.h:41
friend Voltage UnknownVoltage()
Returns a voltage object representing an unknown voltage.
Definition voltage.h:108
float inMilliVolts() const
Returns the voltage in milliVolts.
Definition voltage.h:33
bool operator>(const Voltage &other) const
Definition voltage.h:49
float inMicroVolts() const
Returns the voltage in microVolts.
Definition voltage.h:36
Voltage & operator*=(float multi)
Definition voltage.h:75
float inVolts() const
Returns the voltage in Volts.
Definition voltage.h:30
friend Voltage VoltageInVolts(float)
Returns a voltage object equivalent to the specified voltage expressed in Volts.
Definition voltage.h:118
Voltage & operator+=(const Voltage &other)
Definition voltage.h:65
float inKiloVolts() const
Returns the voltage in kiloVolts.
Definition voltage.h:27
bool operator<=(const Voltage &other) const
Definition voltage.h:53
Voltage & operator-=(const Voltage &other)
Definition voltage.h:70
Voltage & operator/=(float div)
Definition voltage.h:80
Voltage()
Creates a voltage object representing an 'unknown' voltage.
Definition voltage.h:24
bool operator!=(const Voltage &other) const
Definition voltage.h:61
bool operator>=(const Voltage &other) const
Definition voltage.h:57
bool isUnknown() const
Returns whether the object represents an unknown voltage.
Definition voltage.h:39
bool operator==(const Voltage &other) const
Definition voltage.h:45
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
Current CurrentInAmperes(float current)
Returns a current object equivalent to the specified current expressed in Amperes.
Definition current.h:127
Voltage VoltageInVolts(float voltage)
Returns a voltage object equivalent to the specified voltage expressed in Volts.
Definition voltage.h:118
Area operator+(Area a, Area b)
Definition area.h:171
Voltage VoltageInMicroVolts(float voltage)
Returns a voltage object equivalent to the specified voltage expressed in microVolts.
Definition voltage.h:128
Power PowerInWatts(float power)
Returns a power object equivalent to the specified power expressed in Watts.
Definition power.h:126
Area operator-(Area a, Area b)
Definition area.h:175
Work WorkInJoules(float work)
Returns a work object equivalent to the specified work expressed in Joules.
Definition work.h:116
Voltage VoltageInMilliVolts(float voltage)
Returns a voltage object equivalent to the specified voltage expressed in milliVolts.
Definition voltage.h:122
Voltage UnknownVoltage()
Returns a voltage object representing an unknown voltage.
Definition voltage.h:108
Area operator/(Area a, float b)
Definition area.h:191
Area operator*(Area a, float b)
Definition area.h:183
Voltage VoltageInKiloVolts(float voltage)
Returns a voltage object equivalent to the specified voltage expressed in kiloVolts.
Definition voltage.h:112
Charge ChargeInCoulombs(float charge)
Returns a charge object equivalent to the specified charge expressed in Coulombs.
Definition charge.h:112