roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
resistance.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 resistance, internally stored as floating-point Ohms.
22 public:
23 /// Creates a resistance object representing an 'unknown' resistance.
24 Resistance() : resistance_(std::nanf("")) {}
25
26 /// Returns the resistance in GigaOhms.
27 float inGigaOhms() const { return resistance_ * 0.000000001f; }
28
29 /// Returns the resistance in MegaOhms.
30 float inMegaOhms() const { return resistance_ * 0.000001f; }
31
32 /// Returns the resistance in kiloOhms.
33 float inKiloOhms() const { return resistance_ * 0.001f; }
34
35 /// Returns the resistance in Ohms.
36 float inOhms() const { return resistance_; }
37
38 /// Returns the resistance in milliOhms.
39 float inMilliOhms() const { return resistance_ * 1000.0f; }
40
41 /// Returns the resistance in microOhms.
42 float inMicroOhms() const { return resistance_ * 1000000.0f; }
43
44 /// Returns the resistance in nanoOhms.
45 float inNanoOhms() const { return resistance_ * 1000000000.0f; }
46
47 /// Returns whether the object represents an unknown resistance.
48 bool isUnknown() const { return std::isnan(resistance_); }
49
50 bool operator<(const Resistance& other) const {
51 return resistance_ < other.resistance_;
52 }
53
54 bool operator==(const Resistance& other) const {
55 return resistance_ == other.resistance_;
56 }
57
58 bool operator>(const Resistance& other) const {
59 return other.resistance_ < resistance_;
60 }
61
62 bool operator<=(const Resistance& other) const {
63 return !(other.resistance_ < resistance_);
64 }
65
66 bool operator>=(const Resistance& other) const {
67 return !(resistance_ < other.resistance_);
68 }
69
70 bool operator!=(const Resistance& other) const {
71 return !(resistance_ == other.resistance_);
72 }
73
74 inline Resistance& operator+=(const Resistance& other) {
75 resistance_ += other.inOhms();
76 return *this;
77 }
78
79 inline Resistance& operator-=(const Resistance& other) {
80 resistance_ -= other.inOhms();
81 return *this;
82 }
83
84 inline Resistance& operator*=(float multi) {
85 resistance_ *= multi;
86 return *this;
87 }
88
89 inline Resistance& operator/=(float div) {
90 resistance_ /= div;
91 return *this;
92 }
93
94#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
95 /// Returns the string representation of the resistance.
96 std::string asString() const;
97#endif
98
99#if defined(ARDUINO)
100 String asArduinoString() const;
101#endif
102
103 private:
105
106 friend Resistance ResistanceInOhms(float);
107
108 explicit Resistance(float resistance) : resistance_(resistance) {}
109
110 /// Stored in Ohms.
111 float resistance_;
112};
113
114inline Resistance ResistanceInOhms(float resistance);
115
116/// Returns a resistance object representing an unknown resistance.
118
119/// Returns a resistance object equivalent to the specified resistance
120/// expressed in GigaOhms.
121inline Resistance ResistanceInGigaOhms(float resistance) {
122 return ResistanceInOhms(resistance * 1000000000.0f);
123}
124
125/// Returns a resistance object equivalent to the specified resistance
126/// expressed in MegaOhms.
127inline Resistance ResistanceInMegaOhms(float resistance) {
128 return ResistanceInOhms(resistance * 1000000.0f);
129}
130
131/// Returns a resistance object equivalent to the specified resistance
132/// expressed in kiloOhms.
133inline Resistance ResistanceInKiloOhms(float resistance) {
134 return ResistanceInOhms(resistance * 1000.0f);
135}
136
137/// Returns a resistance object equivalent to the specified resistance
138/// expressed in Ohms.
139inline Resistance ResistanceInOhms(float resistance) {
140 return Resistance(resistance);
141}
142
143/// Returns a resistance object equivalent to the specified resistance
144/// expressed in milliOhms.
145inline Resistance ResistanceInMilliOhms(float resistance) {
146 return ResistanceInOhms(resistance * 0.001f);
147}
148
149/// Returns a resistance object equivalent to the specified resistance
150/// expressed in microOhms.
151inline Resistance ResistanceInMicroOhms(float resistance) {
152 return ResistanceInOhms(resistance * 0.000001f);
153}
154
155/// Returns a resistance object equivalent to the specified resistance
156/// expressed in microOhms.
157inline Resistance ResistanceInNanoOhms(float resistance) {
158 return ResistanceInOhms(resistance * 0.000000001f);
159}
160
162 return ResistanceInOhms(a.inOhms() + b.inOhms());
163}
164
166 return ResistanceInOhms(a.inOhms() - b.inOhms());
167}
168
170 return ResistanceInOhms(-a.inOhms());
171}
172
173inline Resistance operator*(Resistance a, float b) {
174 return ResistanceInOhms(a.inOhms() * b);
175}
176
177inline Resistance operator*(float a, Resistance b) {
178 return ResistanceInOhms(a * b.inOhms());
179}
180
181inline Resistance operator/(Resistance a, float b) {
182 return ResistanceInOhms(a.inOhms() / b);
183}
184
185inline float operator/(Resistance a, Resistance b) {
186 return a.inOhms() / b.inOhms();
187}
188
189/// Vs Ohm's law.
190
192 return VoltageInVolts(a.inOhms() * b.inAmperes());
193}
194
196 return VoltageInVolts(a.inAmperes() * b.inOhms());
197}
198
200 return ResistanceInOhms(a.inVolts() / b.inAmperes());
201}
202
204 return CurrentInAmperes(a.inVolts() / b.inOhms());
205}
206
207roo_logging::Stream& operator<<(roo_logging::Stream& os, const Resistance& val);
208
209} // namespace roo_quantity
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 resistance, internally stored as floating-point Ohms.
Definition resistance.h:21
friend Resistance ResistanceInOhms(float)
Returns a resistance object equivalent to the specified resistance expressed in Ohms.
Definition resistance.h:139
float inNanoOhms() const
Returns the resistance in nanoOhms.
Definition resistance.h:45
float inGigaOhms() const
Returns the resistance in GigaOhms.
Definition resistance.h:27
float inMicroOhms() const
Returns the resistance in microOhms.
Definition resistance.h:42
bool operator<(const Resistance &other) const
Definition resistance.h:50
float inMilliOhms() const
Returns the resistance in milliOhms.
Definition resistance.h:39
bool operator<=(const Resistance &other) const
Definition resistance.h:62
bool operator>(const Resistance &other) const
Definition resistance.h:58
bool isUnknown() const
Returns whether the object represents an unknown resistance.
Definition resistance.h:48
Resistance & operator+=(const Resistance &other)
Definition resistance.h:74
Resistance()
Creates a resistance object representing an 'unknown' resistance.
Definition resistance.h:24
float inKiloOhms() const
Returns the resistance in kiloOhms.
Definition resistance.h:33
float inMegaOhms() const
Returns the resistance in MegaOhms.
Definition resistance.h:30
bool operator!=(const Resistance &other) const
Definition resistance.h:70
float inOhms() const
Returns the resistance in Ohms.
Definition resistance.h:36
Resistance & operator-=(const Resistance &other)
Definition resistance.h:79
bool operator>=(const Resistance &other) const
Definition resistance.h:66
bool operator==(const Resistance &other) const
Definition resistance.h:54
friend Resistance UnknownResistance()
Returns a resistance object representing an unknown resistance.
Definition resistance.h:117
Resistance & operator/=(float div)
Definition resistance.h:89
Resistance & operator*=(float multi)
Definition resistance.h:84
Representation of voltage, internally stored as floating-point Volts.
Definition voltage.h:21
float inVolts() const
Returns the voltage in Volts.
Definition voltage.h:30
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
Resistance ResistanceInNanoOhms(float resistance)
Returns a resistance object equivalent to the specified resistance expressed in microOhms.
Definition resistance.h:157
Area operator+(Area a, Area b)
Definition area.h:171
Resistance ResistanceInMegaOhms(float resistance)
Returns a resistance object equivalent to the specified resistance expressed in MegaOhms.
Definition resistance.h:127
Resistance ResistanceInOhms(float resistance)
Returns a resistance object equivalent to the specified resistance expressed in Ohms.
Definition resistance.h:139
Area operator-(Area a, Area b)
Definition area.h:175
Resistance ResistanceInMilliOhms(float resistance)
Returns a resistance object equivalent to the specified resistance expressed in milliOhms.
Definition resistance.h:145
Resistance ResistanceInMicroOhms(float resistance)
Returns a resistance object equivalent to the specified resistance expressed in microOhms.
Definition resistance.h:151
Area operator/(Area a, float b)
Definition area.h:191
Resistance UnknownResistance()
Returns a resistance object representing an unknown resistance.
Definition resistance.h:117
Area operator*(Area a, float b)
Definition area.h:183
Resistance ResistanceInKiloOhms(float resistance)
Returns a resistance object equivalent to the specified resistance expressed in kiloOhms.
Definition resistance.h:133
Resistance ResistanceInGigaOhms(float resistance)
Returns a resistance object equivalent to the specified resistance expressed in GigaOhms.
Definition resistance.h:121