roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
charge.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
8#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
9#include <string>
10#endif
11
12#if defined(ARDUINO)
13#include <Arduino.h>
14#endif
15
16namespace roo_quantity {
17
18/// Representation of charge, internally stored as floating-point Coulombs.
19class Charge {
20 public:
21 /// Creates a charge object representing an 'unknown' charge.
22 Charge() : charge_(std::nanf("")) {}
23
24 /// Returns the charge in kiloCoulombs.
25 float inKiloCoulombs() const { return charge_ / 1000.0f; }
26
27 /// Returns the charge in Coulombs.
28 float inCoulombs() const { return charge_; }
29
30 /// Returns the charge in milliCoulombs.
31 float inMilliCoulombs() const { return charge_ * 1000.0f; }
32
33 /// Returns the charge in microCoulombs.
34 float inMicroCoulombs() const { return charge_ * 1000000.0f; }
35
36 /// Returns whether the object represents an unknown charge.
37 bool isUnknown() const { return std::isnan(charge_); }
38
39 bool operator<(const Charge& other) const { return charge_ < other.charge_; }
40
41 bool operator==(const Charge& other) const {
42 return charge_ == other.charge_;
43 }
44
45 bool operator>(const Charge& other) const { return other.charge_ < charge_; }
46
47 bool operator<=(const Charge& other) const {
48 return !(other.charge_ < charge_);
49 }
50
51 bool operator>=(const Charge& other) const {
52 return !(charge_ < other.charge_);
53 }
54
55 bool operator!=(const Charge& other) const {
56 return !(charge_ == other.charge_);
57 }
58
59 inline Charge& operator+=(const Charge& other) {
60 charge_ += other.inCoulombs();
61 return *this;
62 }
63
64 inline Charge& operator-=(const Charge& other) {
65 charge_ -= other.inCoulombs();
66 return *this;
67 }
68
69 inline Charge& operator*=(float multi) {
70 charge_ *= multi;
71 return *this;
72 }
73
74 inline Charge& operator/=(float div) {
75 charge_ /= div;
76 return *this;
77 }
78
79#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
80 /// Returns the string representation of the charge.
81 std::string asString() const;
82#endif
83
84#if defined(ARDUINO)
85 String asArduinoString() const;
86#endif
87
88 private:
89 friend Charge UnknownCharge();
90
91 friend Charge ChargeInCoulombs(float);
92
93 explicit Charge(float charge) : charge_(charge) {}
94
95 /// Stored in Coulombs.
96 float charge_;
97};
98
99inline Charge ChargeInCoulombs(float charge);
100
101/// Returns a charge object representing an unknown charge.
102inline Charge UnknownCharge() { return Charge(); }
103
104/// Returns a charge object equivalent to the specified charge
105/// expressed in kiloCoulombs.
106inline Charge ChargeInKiloCoulombs(float charge) {
107 return ChargeInCoulombs(charge * 1000.0f);
108}
109
110/// Returns a charge object equivalent to the specified charge
111/// expressed in Coulombs.
112inline Charge ChargeInCoulombs(float charge) { return Charge(charge); }
113
114/// Returns a charge object equivalent to the specified charge
115/// expressed in milliCoulombs.
116inline Charge ChargeInMilliCoulombs(float charge) {
117 return ChargeInCoulombs(charge * 0.001f);
118}
119
120/// Returns a charge object equivalent to the specified charge
121/// expressed in microCoulombs.
122inline Charge ChargeInMicroCoulombs(float charge) {
123 return ChargeInCoulombs(charge * 0.000001f);
124}
125
127 return ChargeInCoulombs(a.inCoulombs() + b.inCoulombs());
128}
129
131 return ChargeInCoulombs(a.inCoulombs() - b.inCoulombs());
132}
133
135
136inline Charge operator*(Charge a, float b) {
137 return ChargeInCoulombs(a.inCoulombs() * b);
138}
139
140inline Charge operator*(float a, Charge b) {
141 return ChargeInCoulombs(a * b.inCoulombs());
142}
143
144inline Charge operator/(Charge a, float b) {
145 return ChargeInCoulombs(a.inCoulombs() / b);
146}
147
148inline float operator/(Charge a, Charge b) {
149 return a.inCoulombs() / b.inCoulombs();
150}
151
152roo_logging::Stream& operator<<(roo_logging::Stream& os, const Charge& val);
153
154} // namespace roo_quantity
Representation of charge, internally stored as floating-point Coulombs.
Definition charge.h:19
bool operator>=(const Charge &other) const
Definition charge.h:51
bool operator!=(const Charge &other) const
Definition charge.h:55
bool isUnknown() const
Returns whether the object represents an unknown charge.
Definition charge.h:37
Charge & operator*=(float multi)
Definition charge.h:69
Charge & operator-=(const Charge &other)
Definition charge.h:64
float inMilliCoulombs() const
Returns the charge in milliCoulombs.
Definition charge.h:31
bool operator==(const Charge &other) const
Definition charge.h:41
Charge & operator+=(const Charge &other)
Definition charge.h:59
Charge & operator/=(float div)
Definition charge.h:74
float inCoulombs() const
Returns the charge in Coulombs.
Definition charge.h:28
Charge()
Creates a charge object representing an 'unknown' charge.
Definition charge.h:22
bool operator<=(const Charge &other) const
Definition charge.h:47
float inKiloCoulombs() const
Returns the charge in kiloCoulombs.
Definition charge.h:25
bool operator<(const Charge &other) const
Definition charge.h:39
friend Charge ChargeInCoulombs(float)
Returns a charge object equivalent to the specified charge expressed in Coulombs.
Definition charge.h:112
friend Charge UnknownCharge()
Returns a charge object representing an unknown charge.
Definition charge.h:102
bool operator>(const Charge &other) const
Definition charge.h:45
float inMicroCoulombs() const
Returns the charge in microCoulombs.
Definition charge.h:34
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
Charge ChargeInKiloCoulombs(float charge)
Returns a charge object equivalent to the specified charge expressed in kiloCoulombs.
Definition charge.h:106
Charge ChargeInMicroCoulombs(float charge)
Returns a charge object equivalent to the specified charge expressed in microCoulombs.
Definition charge.h:122
Area operator+(Area a, Area b)
Definition area.h:171
Charge ChargeInMilliCoulombs(float charge)
Returns a charge object equivalent to the specified charge expressed in milliCoulombs.
Definition charge.h:116
Area operator-(Area a, Area b)
Definition area.h:175
Area operator/(Area a, float b)
Definition area.h:191
Charge UnknownCharge()
Returns a charge object representing an unknown charge.
Definition charge.h:102
Area operator*(Area a, float b)
Definition area.h:183
Charge ChargeInCoulombs(float charge)
Returns a charge object equivalent to the specified charge expressed in Coulombs.
Definition charge.h:112