FDO2 Driver 1.0.0
API reference
Loading...
Searching...
No Matches
fdo2_types.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include <cmath>
16#include <cstdint>
17#include <string_view>
18
19namespace fdo2 {
20
22constexpr uint32_t kFdo2G2DefaultBaud = 19200U;
23
25constexpr uint32_t kFdo2G2PowerUpSettleMs = 1100U;
26
28enum class DriverError : uint8_t {
29 None = 0,
31 Timeout,
36};
37
38constexpr std::string_view ToString(DriverError e) noexcept {
39 switch (e) {
40 case DriverError::None: return "None";
41 case DriverError::InvalidParameter: return "InvalidParameter";
42 case DriverError::Timeout: return "Timeout";
43 case DriverError::BufferOverflow: return "BufferOverflow";
44 case DriverError::ProtocolError: return "ProtocolError";
45 case DriverError::DeviceError: return "DeviceError";
46 case DriverError::CrcMismatch: return "CrcMismatch";
47 }
48 return "?";
49}
50
51template <typename T>
53 T value{};
55
56 constexpr bool ok() const noexcept { return error == DriverError::None; }
57 constexpr explicit operator bool() const noexcept { return ok(); }
58
59 static constexpr DriverResult success(T v) noexcept { return {v, DriverError::None}; }
60 static constexpr DriverResult failure(DriverError e) noexcept { return {T{}, e}; }
61};
62
63template <>
64struct DriverResult<void> {
66 constexpr bool ok() const noexcept { return error == DriverError::None; }
67 constexpr explicit operator bool() const noexcept { return ok(); }
68 static constexpr DriverResult success() noexcept { return {DriverError::None}; }
69 static constexpr DriverResult failure(DriverError e) noexcept { return {e}; }
70};
71
79 int32_t device_id{};
80 int32_t num_channels{};
82 int32_t sensor_types{};
83};
84
86constexpr double FirmwareRevisionToDouble(int32_t r) noexcept {
87 return static_cast<double>(r) * 0.01;
88}
89
97 int32_t o_raw{};
98 int32_t t_raw{};
99 uint32_t status_u32{};
100
101 double p_o2_hpa{};
102 double temp_c{};
103};
104
112 int32_t d_raw{};
113 int32_t i_raw{};
114 int32_t a_raw{};
115 int32_t p_raw{};
116 int32_t h_raw{};
117
118 double dphi_deg{};
121 double pressure_mbar{};
123};
124
125namespace moxy_status {
126
127constexpr uint32_t kWarnAutoAmpReduced = 1U << 0;
128constexpr uint32_t kFatalSignalTooLow = 1U << 1;
129constexpr uint32_t kFatalSignalTooHigh = 1U << 2;
130constexpr uint32_t kFatalRefTooLow = 1U << 3;
131constexpr uint32_t kFatalRefTooHigh = 1U << 4;
132constexpr uint32_t kFatalTempSensor = 1U << 5;
133constexpr uint32_t kWarnHumidityHigh = 1U << 7;
134constexpr uint32_t kErrPressureSensor = 1U << 9;
135constexpr uint32_t kErrHumiditySensor = 1U << 10;
136
137} // namespace moxy_status
138
140constexpr bool MoxyStatusOkForOxygen(uint32_t s) noexcept { return s <= 1U; }
141
142inline MoxyReading DecodeMoxy(int32_t o_raw, int32_t t_raw, uint32_t s) noexcept {
143 MoxyReading m{};
144 m.o_raw = o_raw;
145 m.t_raw = t_raw;
146 m.status_u32 = s;
147 m.p_o2_hpa = static_cast<double>(o_raw) * 1e-3;
148 m.temp_c = static_cast<double>(t_raw) * 1e-3;
149 return m;
150}
151
152inline MrawReading DecodeMraw(int32_t o_raw, int32_t t_raw, uint32_t s, int32_t d_raw,
153 int32_t i_raw, int32_t a_raw, int32_t p_raw,
154 int32_t h_raw) noexcept {
155 MrawReading r{};
156 static_cast<MoxyReading&>(r) = DecodeMoxy(o_raw, t_raw, s);
157 r.d_raw = d_raw;
158 r.i_raw = i_raw;
159 r.a_raw = a_raw;
160 r.p_raw = p_raw;
161 r.h_raw = h_raw;
162 r.dphi_deg = static_cast<double>(d_raw) * 1e-3;
163 r.signal_intensity_mv = static_cast<double>(i_raw) * 1e-3;
164 r.ambient_light_mv = static_cast<double>(a_raw) * 1e-3;
165 r.pressure_mbar = static_cast<double>(p_raw) * 1e-3; /* µbar → mbar */
166 r.rh_in_housing_pct = static_cast<double>(h_raw) * 1e-3;
167 return r;
168}
169
177inline double VolumePercentO2(double p_o2_hpa, double p_air_mbar) noexcept {
178 if (p_air_mbar <= 0.0 || !std::isfinite(p_air_mbar) || !std::isfinite(p_o2_hpa)) {
179 return 0.0;
180 }
181 return 100.0 * p_o2_hpa / p_air_mbar;
182}
183
184} // namespace fdo2
constexpr uint32_t kFatalRefTooHigh
Definition fdo2_types.hpp:131
constexpr uint32_t kWarnHumidityHigh
Definition fdo2_types.hpp:133
constexpr uint32_t kErrHumiditySensor
Definition fdo2_types.hpp:135
constexpr uint32_t kFatalRefTooLow
Definition fdo2_types.hpp:130
constexpr uint32_t kWarnAutoAmpReduced
Definition fdo2_types.hpp:127
constexpr uint32_t kFatalSignalTooHigh
Definition fdo2_types.hpp:129
constexpr uint32_t kFatalSignalTooLow
Definition fdo2_types.hpp:128
constexpr uint32_t kErrPressureSensor
Definition fdo2_types.hpp:134
constexpr uint32_t kFatalTempSensor
Definition fdo2_types.hpp:132
Definition fdo2.hpp:19
constexpr double FirmwareRevisionToDouble(int32_t r) noexcept
Definition fdo2_types.hpp:86
constexpr bool MoxyStatusOkForOxygen(uint32_t s) noexcept
Data sheet: normal operation implies S is 0 or 1 only.
Definition fdo2_types.hpp:140
MrawReading DecodeMraw(int32_t o_raw, int32_t t_raw, uint32_t s, int32_t d_raw, int32_t i_raw, int32_t a_raw, int32_t p_raw, int32_t h_raw) noexcept
Definition fdo2_types.hpp:152
constexpr std::string_view ToString(DriverError e) noexcept
Definition fdo2_types.hpp:38
constexpr uint32_t kFdo2G2DefaultBaud
Default UART speed after power-up per FDO2-G2 data sheet §4.
Definition fdo2_types.hpp:22
DriverError
Driver-level error codes (stable for logging / telemetry).
Definition fdo2_types.hpp:28
@ CrcMismatch
#CRCE 1 checksum suffix did not match the reply, or was required and missing.
double VolumePercentO2(double p_o2_hpa, double p_air_mbar) noexcept
Volume % O₂ when p_air at the sensing membrane equals back-side pressure P.
Definition fdo2_types.hpp:177
MoxyReading DecodeMoxy(int32_t o_raw, int32_t t_raw, uint32_t s) noexcept
Definition fdo2_types.hpp:142
constexpr uint32_t kFdo2G2PowerUpSettleMs
Typical boot delay before the module accepts commands (data sheet: ~1 s).
Definition fdo2_types.hpp:25
static constexpr DriverResult success() noexcept
Definition fdo2_types.hpp:68
static constexpr DriverResult failure(DriverError e) noexcept
Definition fdo2_types.hpp:69
constexpr bool ok() const noexcept
Definition fdo2_types.hpp:66
Definition fdo2_types.hpp:52
constexpr bool ok() const noexcept
Definition fdo2_types.hpp:56
static constexpr DriverResult success(T v) noexcept
Definition fdo2_types.hpp:59
T value
Definition fdo2_types.hpp:53
static constexpr DriverResult failure(DriverError e) noexcept
Definition fdo2_types.hpp:60
DriverError error
Definition fdo2_types.hpp:54
Decoded #MOXY O T S sample (data sheet §4.3).
Definition fdo2_types.hpp:96
double p_o2_hpa
Partial pressure O₂ [hPa] = O × 10⁻³.
Definition fdo2_types.hpp:101
uint32_t status_u32
S as received.
Definition fdo2_types.hpp:99
int32_t t_raw
T as received (m°C).
Definition fdo2_types.hpp:98
int32_t o_raw
O as received (1e-3 hPa).
Definition fdo2_types.hpp:97
double temp_c
Temperature [°C] = T × 10⁻³.
Definition fdo2_types.hpp:102
Decoded #MRAW O T S D I A P H (data sheet §4.3).
Definition fdo2_types.hpp:111
int32_t i_raw
I (µV).
Definition fdo2_types.hpp:113
int32_t h_raw
H (mRH).
Definition fdo2_types.hpp:116
double dphi_deg
D × 10⁻³ degrees.
Definition fdo2_types.hpp:118
double rh_in_housing_pct
H × 10⁻³ RH.
Definition fdo2_types.hpp:122
double signal_intensity_mv
I × 10⁻³ mV.
Definition fdo2_types.hpp:119
double ambient_light_mv
A × 10⁻³ mV.
Definition fdo2_types.hpp:120
int32_t a_raw
A (µV).
Definition fdo2_types.hpp:114
int32_t d_raw
D (m°).
Definition fdo2_types.hpp:112
int32_t p_raw
P (µbar at connector / back side).
Definition fdo2_types.hpp:115
double pressure_mbar
P × 10⁻³ mbar (≈ hPa).
Definition fdo2_types.hpp:121
#VERS D N R S fields (FDO2-G2 data sheet §4.3).
Definition fdo2_types.hpp:78
int32_t num_channels
N (1 for FDO2-G2).
Definition fdo2_types.hpp:80
int32_t device_id
D (8 for FDO2-G2).
Definition fdo2_types.hpp:79
int32_t sensor_types
S bit mask: bit0 O2, bit1 temp housing, bit2 pressure, bit3 RH.
Definition fdo2_types.hpp:82
int32_t firmware_revision
R (e.g. 328 → 3.28).
Definition fdo2_types.hpp:81