19#ifndef SFM_FLOW_METER_HPP
20#define SFM_FLOW_METER_HPP
33#include "sfm_flow_meter_version.h"
44template <
typename Port>
45concept DeclaresDelayUs =
requires(Port& port) { port.DelayUs(uint32_t{0}); };
47template <
typename Port>
48concept HasDelayUs =
requires { ExactHook<Port, void(uint32_t)
noexcept>::Take(&Port::DelayUs); };
51template <
typename Port>
52concept DeclaresNowUs =
requires(Port& port) { port.NowUs(); };
54template <
typename Port>
55concept HasNowUs =
requires { ExactHook<Port, uint32_t()
noexcept>::Take(&Port::NowUs); };
58template <
typename Port>
59concept DeclaresLog =
requires(Port& port) { port.Log(
LogLevel::Info,
""); };
61template <
typename Port>
63 requires { ExactHook<Port, void(
LogLevel,
const char*)
noexcept>::Take(&Port::Log); };
66template <
typename Port>
67concept DeclaresReadStretch =
68 requires(Port& port, uint8_t* data) { port.ReadStretch(uint8_t{0}, data, std::size_t{0}); };
71template <
typename Port>
72concept HasReadStretch =
requires {
73 ExactHook<Port, bool(uint8_t, uint8_t*, std::size_t)
noexcept>::Take(&Port::ReadStretch);
74} &&
requires {
requires Port::kClockStretch; };
77template <
typename Port>
78concept DeclaresGeneralCall =
requires(Port& port) { port.WriteGeneralCall(uint8_t{0}); };
80template <
typename Port>
81concept HasGeneralCall =
82 requires { ExactHook<Port, bool(uint8_t)
noexcept>::Take(&Port::WriteGeneralCall); };
85template <
typename Port>
86concept DeclaresPowerCycle =
requires(Port& port) { port.PowerCycle(); };
88template <
typename Port>
89concept HasPowerCycle =
requires { ExactHook<Port, bool()
noexcept>::Take(&Port::PowerCycle); };
92template <
typename Port>
93concept DeclaresResetPin =
requires(Port& port) { port.SetResetPin(
true); };
95template <
typename Port>
96concept HasResetPin =
requires { ExactHook<Port, bool(
bool)
noexcept>::Take(&Port::SetResetPin); };
101template <
typename Port>
102void DelayUs(Port& port, uint32_t us)
noexcept {
103 if constexpr (HasDelayUs<Port>) {
106 static_assert(kAlwaysFalse<Port>,
"sfm_flow_meter: the port must implement "
107 "`void DelayUs(uint32_t us) noexcept`");
112template <
typename Port>
113uint32_t NowUs(Port& port)
noexcept {
114 if constexpr (HasNowUs<Port>) {
124template <
typename Port>
125void Log(Port& port, [[maybe_unused]]
LogLevel level,
126 [[maybe_unused]]
const char* message)
noexcept {
127 if constexpr (HasLog<Port>) {
128 port.Log(level, message);
136constexpr uint8_t LastPhase(Op op)
noexcept {
139 case Op::ReadScaling:
146 case Op::SetAveraging:
151 case Op::ReadTemperature:
152 case Op::Concentration:
161inline constexpr uint32_t kMaxAwaitRounds = 10000U;
173template <I2cPort Port, u
int32_t FamilyMask = kAllFamilies>
175 static_assert((FamilyMask != 0U) && ((FamilyMask & ~
kAllFamilies) == 0U),
176 "sfm_flow_meter: bad family mask");
177 static_assert(!detail::DeclaresDelayUs<Port> || detail::HasDelayUs<Port>,
178 "sfm_flow_meter: `DelayUs` must be `void DelayUs(uint32_t) noexcept`");
179 static_assert(!detail::DeclaresNowUs<Port> || detail::HasNowUs<Port>,
180 "sfm_flow_meter: `NowUs` must be `uint32_t NowUs() noexcept`");
181 static_assert(!detail::DeclaresLog<Port> || detail::HasLog<Port>,
182 "sfm_flow_meter: `Log` must be `void Log(LogLevel, const "
184 static_assert(!detail::DeclaresReadStretch<Port> || detail::HasReadStretch<Port>,
185 "sfm_flow_meter: `ReadStretch` must be `bool "
186 "ReadStretch(uint8_t, uint8_t*, "
188 "with `static constexpr bool kClockStretch = true`");
189 static_assert(!detail::DeclaresGeneralCall<Port> || detail::HasGeneralCall<Port>,
190 "sfm_flow_meter: `WriteGeneralCall` must be `bool "
191 "WriteGeneralCall(uint8_t) noexcept`");
192 static_assert(!detail::DeclaresPowerCycle<Port> || detail::HasPowerCycle<Port>,
193 "sfm_flow_meter: `PowerCycle` must be `bool PowerCycle() noexcept`");
194 static_assert(!detail::DeclaresResetPin<Port> || detail::HasResetPin<Port>,
195 "sfm_flow_meter: `SetResetPin` must be `bool SetResetPin(bool) "
197 static_assert(FamilyMask !=
kFamilySf04 || detail::HasReadStretch<Port>,
198 "sfm_flow_meter: the SFM4100 family needs the port's "
199 "`ReadStretch()` hook");
209 : port_(port), config_(config),
212 address_(((config.address != 0U) || (part_ == nullptr)) ? config.address
213 : part_->addresses.list[0]),
395 return op_ != detail::Op::None;
404 return temperature_mdegc_;
444 Error execStep(const detail::Step& step, uint32_t& wait_us) noexcept;
445 Error writeStep(const detail::Step& step) noexcept;
446 Error doWrite(const detail::Step& step) noexcept;
447 Error doPoll(const detail::Step& step, uint32_t& wait_us) noexcept;
448 Error doRead(const detail::Step& step) noexcept;
449 Error finishPhase() noexcept;
450 Error finishInit() noexcept;
451 Error finishScale() noexcept;
452 Error finishTemperature() noexcept;
454 void loadPhase() noexcept;
455 bool nextPhase() noexcept;
456 void complete() noexcept;
457 [[nodiscard]] detail::Request makeRequest(detail::Op op) const noexcept;
458 Error gate(
bool allowed) const noexcept;
460 Error captureReply() noexcept;
461 Error fillSample(const detail::Frame& frame,
Sample& sample) noexcept;
465 void noteFrame() noexcept;
466 [[nodiscard]]
bool silenceExceeded() const noexcept;
467 Error escalateSilence() noexcept;
468 Error resetSupport() const noexcept;
469 [[nodiscard]] uint16_t resolvedAveraging() const noexcept;
470 Error driverConfigOk() const noexcept;
471 [[nodiscard]] std::span<const uint8_t> rx() const noexcept {
472 return {rx_.data(), rx_used_};
477 const PartInfo* part_{
nullptr};
478 const GasRow* running_{
nullptr};
479 detail::Op op_{detail::Op::None};
480 detail::Request req_{};
481 detail::Plan plan_{};
482 std::array<uint8_t, kMaxRead> rx_{};
484 int32_t temperature_mdegc_{0};
485 ScalingInfo scaling_{};
486 RawReply last_reply_{};
489 uint32_t wait_us_{0};
490 uint32_t wait_start_us_{0};
491 uint32_t started_us_{0};
492 uint32_t last_good_us_{0};
493 uint32_t last_frame_us_{0};
494 uint32_t quiet_start_us_{0};
495 uint32_t quiet_us_{0};
496 uint16_t o2_permille_{0};
497 uint16_t averaging_{0};
498 std::size_t rx_used_{0};
503 bool waiting_{
false};
504 bool skip_optional_read_{
false};
511#include "../src/sfm_flow_meter.ipp"
The driver.
Definition sfm_flow_meter.hpp:174
const ScalingInfo & GetScaling() const noexcept
Definition sfm_flow_meter.hpp:412
State GetState() const noexcept
Definition sfm_flow_meter.hpp:390
Capabilities GetCapabilities() const noexcept
Definition sfm_flow_meter.hpp:429
Result< Wait > BeginInitialize() noexcept
Start identification: wake the sensor, check the product identifier (or serial number),...
Result< void > EnsureInitialized() noexcept
BeginInitialize() and AwaitPending(): blocking, needs the port's DelayUs().
SfmFlowMeter(const SfmFlowMeter &)=delete
The driver is not copied: it holds a reference to the port.
Result< Wait > StartMeasurement(Gas gas, uint16_t o2_permille=0) noexcept
Start a measurement of a gas or gas mixture; reads and checks the part's scaling first.
Result< Wait > Sleep() noexcept
Enter sleep mode (idle only). Failure leaves StateUnknown.
SfmFlowMeter & operator=(const SfmFlowMeter &)=delete
The driver is not copied.
Result< Wait > SoftReset() noexcept
Reset by the part's soft-reset command (the general call needs allow_general_call_reset)....
bool HasPending() const noexcept
Definition sfm_flow_meter.hpp:394
Result< RawReply > ReadRawReply() noexcept
One raw read of the running measurement: the CRC-checked words, with no decoding, no status check and...
Result< void > Deinitialize() noexcept
Forget the initialisation. No bus traffic: call Stop() first if the sensor measures.
uint8_t GetAddress() const noexcept
Definition sfm_flow_meter.hpp:433
const Config & GetConfig() const noexcept
Definition sfm_flow_meter.hpp:420
Result< void > AwaitPending() noexcept
Wait with DelayUs() until no operation is pending. Needs the port's DelayUs().
const Stats & GetStats() const noexcept
Definition sfm_flow_meter.hpp:416
Result< Wait > ReadScaling(Gas gas) noexcept
Read and check the sensor's scaling of a gas while idle; see GetScaling().
Result< Wait > WakeUp() noexcept
Wake from sleep and write the averaging again.
const RawReply & GetLastReply() const noexcept
Definition sfm_flow_meter.hpp:408
Result< Wait > Stop() noexcept
Stop the measurement. A no-op when idle. Failure leaves StateUnknown.
Result< Sample > ReadSample() noexcept
Read one sample of the running measurement.
const ProductInfo & GetProductInfo() const noexcept
Definition sfm_flow_meter.hpp:398
Result< Wait > SetAveraging(uint16_t n) noexcept
Set the averaging while idle.
Result< Wait > HardReset() noexcept
Reset by the /RES pin (SFM4200) or by cycling the supply; needs the port hook.
SfmFlowMeter(Port &port, const Config &config=Config{}) noexcept
Bind a port and a configuration. No bus traffic.
Definition sfm_flow_meter.hpp:208
Result< Wait > ReadTemperature() noexcept
Read the sensor temperature by a pointer sequence while measuring (parts that offer it); the SF05 fam...
Result< Wait > Poll() noexcept
Poll() using the port's NowUs(); without a clock the whole wait counts as elapsed.
Result< Wait > UpdateConcentration(uint16_t o2_permille) noexcept
Change the oxygen fraction of a running mixture (no read in between).
int32_t GetTemperature() const noexcept
Definition sfm_flow_meter.hpp:403
const PartInfo * GetPart() const noexcept
Definition sfm_flow_meter.hpp:425
Gas
A calibrated gas or gas mixture. Which gases a part offers is in its table row.
Definition sfm_flow_meter_types.hpp:285
constexpr uint32_t kAllFamilies
Every family: the default mask.
Definition sfm_flow_meter_types.hpp:238
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition sfm_flow_meter_types.hpp:41
LogLevel
Severity passed to the optional Log() hook.
Definition sfm_flow_meter_types.hpp:196
constexpr uint32_t kFamilySf04
Family::Sf04.
Definition sfm_flow_meter_types.hpp:235
Variant
A part (one row of the table in sfm_flow_meter_parts.hpp). The names follow the vendor's ordering suf...
Definition sfm_flow_meter_types.hpp:245
State
The driver's shadow of the chip's mode.
Definition sfm_flow_meter_types.hpp:325
@ Info
Lifecycle events: initialised, reset.
@ Uninitialized
EnsureInitialized() has not succeeded.
The SFM flow meter driver: everything it declares lives here; internals in detail.
Definition sfm_flow_meter.hpp:35
constexpr Capabilities CapabilitiesOf(const PartInfo &part) noexcept
Definition sfm_flow_meter_parts.hpp:603
constexpr const PartInfo * FindPart(Variant variant) noexcept
The table row of a variant.
Definition sfm_flow_meter_parts.hpp:529
constexpr const PartInfo * FamilyRow(Family family) noexcept
The first row of a family: what Variant::Auto uses for waits before the part is known.
Definition sfm_flow_meter_parts.hpp:566
Compile-time helpers of the SFM_FLOW_METER driver: typed integer literals, exact hook matching,...
The I2C port of the SFM flow meter driver: the CRTP base, the I2cPort concept and the bus requirement...
The table of supported parts: one row of data per variant, the lookups, Validate() and the unit conve...
The six protocol policies: pure constexpr functions that turn an operation into a Plan of bus steps a...
Command codes, status-word helpers and the CRC-8 of the SFM flow meters.
Public types of the SFM flow meter driver: errors, Result<T>, the families, variants and gases,...
Which calls a part offers, from its table row.
Definition sfm_flow_meter_types.hpp:361
Everything EnsureInitialized() applies.
Definition sfm_flow_meter_types.hpp:437
One row of the part table.
Definition sfm_flow_meter_parts.hpp:112
The product identifier read at initialisation.
Definition sfm_flow_meter_types.hpp:341
The words of one reply, after their CRCs were checked and before anything was decoded.
Definition sfm_flow_meter_types.hpp:408
The value of a call, or why there is none.
Definition sfm_flow_meter_types.hpp:136
One measurement. Flow is in millilitres per minute at the part's reference conditions.
Definition sfm_flow_meter_types.hpp:380
What the sensor reports about the scaling of a gas, and whether it matches the table.
Definition sfm_flow_meter_types.hpp:352
Counters the driver keeps; none of them is cleared by a successful call.
Definition sfm_flow_meter_types.hpp:415
A wait the caller owes the driver: delay us, then call Poll(). 0 means nothing to wait.
Definition sfm_flow_meter_types.hpp:336