roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
current.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"
8#include "roo_quantity/time.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 current, internally stored as floating-point Amperes.
21class Current {
22 public:
23 /// Creates a current object representing an 'unknown' current.
24 Current() : current_(std::nanf("")) {}
25
26 /// Returns the current in megaAmperes.
27 float inMegaAmperes() const { return current_ / 1000000.0f; }
28
29 /// Returns the current in kiloAmperes.
30 float inKiloAmperes() const { return current_ / 1000.0f; }
31
32 /// Returns the current in Amperes.
33 float inAmperes() const { return current_; }
34
35 /// Returns the current in milliAmperes.
36 float inMilliAmperes() const { return current_ * 1000.0f; }
37
38 /// Returns the current in microAmperes.
39 float inMicroAmperes() const { return current_ * 1000000.0f; }
40
41 /// Returns whether the object represents an unknown current.
42 bool isUnknown() const { return std::isnan(current_); }
43
44 bool operator<(const Current& other) const {
45 return current_ < other.current_;
46 }
47
48 bool operator==(const Current& other) const {
49 return current_ == other.current_;
50 }
51
52 bool operator>(const Current& other) const {
53 return other.current_ < current_;
54 }
55
56 bool operator<=(const Current& other) const {
57 return !(other.current_ < current_);
58 }
59
60 bool operator>=(const Current& other) const {
61 return !(current_ < other.current_);
62 }
63
64 bool operator!=(const Current& other) const {
65 return !(current_ == other.current_);
66 }
67
68 inline Current& operator+=(const Current& other) {
69 current_ += other.inAmperes();
70 return *this;
71 }
72
73 inline Current& operator-=(const Current& other) {
74 current_ -= other.inAmperes();
75 return *this;
76 }
77
78 inline Current& operator*=(float multi) {
79 current_ *= multi;
80 return *this;
81 }
82
83 inline Current& operator/=(float div) {
84 current_ /= div;
85 return *this;
86 }
87
88#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
89 /// Returns the string representation of the current.
90 std::string asString() const;
91#endif
92
93#if defined(ARDUINO)
94 String asArduinoString() const;
95#endif
96
97 private:
98 friend Current UnknownCurrent();
99
100 friend Current CurrentInAmperes(float);
101
102 explicit Current(float current) : current_(current) {}
103
104 /// Stored in Amperes.
105 float current_;
106};
107
108inline Current CurrentInAmperes(float current);
109
110/// Returns a current object representing an unknown current.
111inline Current UnknownCurrent() { return Current(); }
112
113/// Returns a current object equivalent to the specified current
114/// expressed in megaAmperes.
115inline Current CurrentInMegaAmperes(float current) {
116 return CurrentInAmperes(current * 1000000.0f);
117}
118
119/// Returns a current object equivalent to the specified current
120/// expressed in kiloAmperes.
121inline Current CurrentInKiloAmperes(float current) {
122 return CurrentInAmperes(current * 1000.0f);
123}
124
125/// Returns a current object equivalent to the specified current
126/// expressed in Amperes.
127inline Current CurrentInAmperes(float current) { return Current(current); }
128
129/// Returns a current object equivalent to the specified current
130/// expressed in milliAmperes.
131inline Current CurrentInMilliAmperes(float current) {
132 return CurrentInAmperes(current * 0.001f);
133}
134
135/// Returns a current object equivalent to the specified current
136/// expressed in microAmperes.
137inline Current CurrentInMicroAmperes(float current) {
138 return CurrentInAmperes(current * 0.000001f);
139}
140
142 return CurrentInAmperes(a.inAmperes() + b.inAmperes());
143}
144
146 return CurrentInAmperes(a.inAmperes() - b.inAmperes());
147}
148
150
151inline Current operator*(Current a, float b) {
152 return CurrentInAmperes(a.inAmperes() * b);
153}
154
155inline Current operator*(float a, Current b) {
156 return CurrentInAmperes(a * b.inAmperes());
157}
158
159inline Current operator/(Current a, float b) {
160 return CurrentInAmperes(a.inAmperes() / b);
161}
162
163inline float operator/(Current a, Current b) {
164 return a.inAmperes() / b.inAmperes();
165}
166
167/// Vs charge.
168
170 return ChargeInCoulombs(a.inAmperes() * b.inSeconds());
171}
172
174 return ChargeInCoulombs(a.inSeconds() * b.inAmperes());
175}
176
178 return CurrentInAmperes(a.inCoulombs() / b.inSeconds());
179}
180
182 return TimeInSeconds(a.inCoulombs() / b.inAmperes());
183}
184
185roo_logging::Stream& operator<<(roo_logging::Stream& os, const Current& val);
186
187} // 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
Current & operator-=(const Current &other)
Definition current.h:73
bool operator<(const Current &other) const
Definition current.h:44
float inKiloAmperes() const
Returns the current in kiloAmperes.
Definition current.h:30
float inMegaAmperes() const
Returns the current in megaAmperes.
Definition current.h:27
Current & operator/=(float div)
Definition current.h:83
bool isUnknown() const
Returns whether the object represents an unknown current.
Definition current.h:42
bool operator!=(const Current &other) const
Definition current.h:64
friend Current UnknownCurrent()
Returns a current object representing an unknown current.
Definition current.h:111
bool operator==(const Current &other) const
Definition current.h:48
Current & operator*=(float multi)
Definition current.h:78
float inMicroAmperes() const
Returns the current in microAmperes.
Definition current.h:39
float inAmperes() const
Returns the current in Amperes.
Definition current.h:33
friend Current CurrentInAmperes(float)
Returns a current object equivalent to the specified current expressed in Amperes.
Definition current.h:127
Current()
Creates a current object representing an 'unknown' current.
Definition current.h:24
bool operator>(const Current &other) const
Definition current.h:52
Current & operator+=(const Current &other)
Definition current.h:68
bool operator>=(const Current &other) const
Definition current.h:60
bool operator<=(const Current &other) const
Definition current.h:56
float inMilliAmperes() const
Returns the current in milliAmperes.
Definition current.h:36
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
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
Current CurrentInKiloAmperes(float current)
Returns a current object equivalent to the specified current expressed in kiloAmperes.
Definition current.h:121
Area operator+(Area a, Area b)
Definition area.h:171
Time TimeInSeconds(float time)
Returns a time object equivalent to the specified time expressed in seconds.
Definition time.h:111
Current CurrentInMicroAmperes(float current)
Returns a current object equivalent to the specified current expressed in microAmperes.
Definition current.h:137
Current CurrentInMilliAmperes(float current)
Returns a current object equivalent to the specified current expressed in milliAmperes.
Definition current.h:131
Area operator-(Area a, Area b)
Definition area.h:175
Area operator/(Area a, float b)
Definition area.h:191
Current CurrentInMegaAmperes(float current)
Returns a current object equivalent to the specified current expressed in megaAmperes.
Definition current.h:115
Area operator*(Area a, float b)
Definition area.h:183
Current UnknownCurrent()
Returns a current object representing an unknown current.
Definition current.h:111
Charge ChargeInCoulombs(float charge)
Returns a charge object equivalent to the specified charge expressed in Coulombs.
Definition charge.h:112