HardFOC Flux V1 HAL
Unified HAL API for the HardFOC Flux valve-control platform (ESP32-S3)
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
AdcManager Class Reference

Singleton manager for all analogue-to-digital channels on Flux V1. More...

#include <AdcManager.h>

Collaboration diagram for AdcManager:
[legend]

Public Member Functions

 AdcManager (const AdcManager &)=delete
 
AdcManager & operator= (const AdcManager &)=delete
 
 AdcManager (AdcManager &&)=delete
 
AdcManager & operator= (AdcManager &&)=delete
 
bool EnsureInitialized () noexcept
 Initialise the ADC subsystem if not already initialised.
 
bool IsInitialized () const noexcept
 Check whether the ADC subsystem has been initialised.
 
flux::hal::ManagerError GetManagerError () const noexcept
 Lifecycle / bind error of the last EnsureInitialized() (shared catalog).
 
bool IsInternalAdcReady () const noexcept
 The MCU ADC unit came up during the last bring-up attempt.
 
bool IsAds7952Ready (uint8_t device_index) const noexcept
 ADS7952 device device_index came up during the last bring-up attempt.
 
bool Deinitialize () noexcept
 Release all ADC resources and reset to uninitialized state.
 
BaseAdc * Get (std::string_view name) noexcept
 Get the BaseAdc driver for a named channel.
 
BaseAdc * Get (HfFunctionalAdcChannel channel) noexcept
 Get the BaseAdc driver by functional enum — O(1) lookup.
 
bool Contains (std::string_view name) const noexcept
 Check whether a named ADC channel is registered.
 
size_t Size () const noexcept
 Get the number of registered ADC channels.
 
hf_adc_err_t ReadVoltage (std::string_view name, float &voltage, uint8_t samples=1, uint16_t timeBetweenSamples=0) noexcept
 Read voltage from a channel by name (already divided if divider > 1).
 
hf_adc_err_t ReadVoltage (HfFunctionalAdcChannel channel, float &voltage, uint8_t samples=1, uint16_t timeBetweenSamples=0) noexcept
 Read voltage from a channel by enum — O(1) lookup, preferred for real-time paths.
 
hf_adc_err_t ReadRaw (std::string_view name, uint32_t &raw_count, uint8_t samples=1, uint16_t timeBetweenSamples=0) noexcept
 Read raw ADC count from a channel by name.
 
hf_adc_err_t ReadRaw (HfFunctionalAdcChannel channel, uint32_t &raw_count, uint8_t samples=1, uint16_t timeBetweenSamples=0) noexcept
 Read raw ADC count from a channel by enum — O(1) lookup, preferred for real-time paths.
 
hf_adc_err_t GetLastError () const noexcept
 Get the most recent error code.
 
hf_adc_err_t GetSystemDiagnostics (AdcSystemDiagnostics &diag) const noexcept
 Populate an AdcSystemDiagnostics snapshot.
 
void DumpStatistics () const noexcept
 Log ADC channel statistics and health info to the console.
 

Static Public Member Functions

static AdcManager & GetInstance () noexcept
 Return the singleton AdcManager instance.
 

Private Member Functions

 AdcManager () noexcept=default
 
 ~AdcManager ()=default
 
bool Initialize () noexcept
 
void ReleaseLocked () noexcept
 Release the MCU ADC, every ADS7952 handler and the registry (mutex held).
 
size_t FindByName (std::string_view name) const noexcept
 Find entry index by string name. Returns kInvalidIndex if not found. O(N) linear scan.
 
size_t FindByEnum (HfFunctionalAdcChannel channel) const noexcept
 Find entry index by functional enum. Returns kInvalidIndex if not found. O(1) direct index.
 
hf_adc_err_t ReadVoltageByIndex (size_t idx, float &voltage, uint8_t samples, uint16_t timeBetweenSamples) noexcept
 Read from an entry by index — shared implementation for both name and enum overloads.
 
hf_adc_err_t ReadRawByIndex (size_t idx, uint32_t &raw_count, uint8_t samples, uint16_t timeBetweenSamples) noexcept
 

Private Attributes

std::array< AdcEntry, kMaxChannels > entries_ {}
 
std::array< size_t, kMaxChannels > enum_to_index_ {}
 O(1) enum → entries index lookup.
 
size_t registered_count_ {0}
 
uint64_t init_time_ms_ {0}
 Timestamp (ms) when Initialize() completed.
 
BaseAdc * internal_adc_ {nullptr}
 
std::array< std::optional< Ads7952Handler >, kMaxAds7952Devices > ads7952_handlers_ {}
 
size_t ads7952_device_count_ {0}
 
std::atomic< bool > initialized_ {false}
 
std::atomic< flux::hal::ManagerError > manager_error_ {flux::hal::ManagerError::NotInitialized}
 
std::atomic< hf_adc_err_t > last_error_ {hf_adc_err_t::ADC_SUCCESS}
 
RtosMutex mutex_
 

Static Private Attributes

static constexpr size_t kMaxChannels
 
static constexpr size_t kInvalidIndex = SIZE_MAX
 
static constexpr size_t kMaxAds7952Devices = 4
 

Detailed Description

Singleton manager for all analogue-to-digital channels on Flux V1.

Provides a uniform API over the internal ESP32 ADC and optional external ADS7952 ADC(s). Channels are registered from the pincfg X-MACRO table at initialisation and can be looked up by name (linear scan) or by functional enum (O(1) index).

Note
Call EnsureInitialized() once before any reads. All public methods are thread-safe (guarded by an internal RTOS mutex).

Constructor & Destructor Documentation

◆ AdcManager() [1/3]

AdcManager::AdcManager ( const AdcManager &  )
delete

◆ AdcManager() [2/3]

AdcManager::AdcManager ( AdcManager &&  )
delete

◆ AdcManager() [3/3]

AdcManager::AdcManager ( )
privatedefaultnoexcept

◆ ~AdcManager()

AdcManager::~AdcManager ( )
privatedefault

Member Function Documentation

◆ Contains()

bool AdcManager::Contains ( std::string_view  name) const
noexcept

Check whether a named ADC channel is registered.

Parameters
nameChannel name to look up.
Returns
true if the channel exists in the registry.

◆ Deinitialize()

bool AdcManager::Deinitialize ( )
noexcept

Release all ADC resources and reset to uninitialized state.

Returns
true on success
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DumpStatistics()

void AdcManager::DumpStatistics ( ) const
noexcept

Log ADC channel statistics and health info to the console.

Here is the caller graph for this function:

◆ EnsureInitialized()

bool AdcManager::EnsureInitialized ( )
noexcept

Initialise the ADC subsystem if not already initialised.

Returns
true on success or if already initialised.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindByEnum()

size_t AdcManager::FindByEnum ( HfFunctionalAdcChannel  channel) const
privatenoexcept

Find entry index by functional enum. Returns kInvalidIndex if not found. O(1) direct index.

◆ FindByName()

size_t AdcManager::FindByName ( std::string_view  name) const
privatenoexcept

Find entry index by string name. Returns kInvalidIndex if not found. O(N) linear scan.

◆ Get() [1/2]

BaseAdc * AdcManager::Get ( HfFunctionalAdcChannel  channel)
noexcept

Get the BaseAdc driver by functional enum — O(1) lookup.

Parameters
channelFunctional ADC channel enum.
Returns
Pointer to the BaseAdc driver, or nullptr if not found.

◆ Get() [2/2]

BaseAdc * AdcManager::Get ( std::string_view  name)
noexcept

Get the BaseAdc driver for a named channel.

Parameters
nameChannel name from the pincfg table (linear scan).
Returns
Pointer to the BaseAdc driver, or nullptr if not found.

◆ GetInstance()

AdcManager & AdcManager::GetInstance ( )
staticnoexcept

Return the singleton AdcManager instance.

Here is the caller graph for this function:

◆ GetLastError()

hf_adc_err_t AdcManager::GetLastError ( ) const
inlinenoexcept

Get the most recent error code.

Returns
Last hf_adc_err_t set by any API call

◆ GetManagerError()

flux::hal::ManagerError AdcManager::GetManagerError ( ) const
inlinenoexcept

Lifecycle / bind error of the last EnsureInitialized() (shared catalog).

Returns
ManagerError::None when initialised; otherwise why bring-up failed.

◆ GetSystemDiagnostics()

hf_adc_err_t AdcManager::GetSystemDiagnostics ( AdcSystemDiagnostics &  diag) const
noexcept

Populate an AdcSystemDiagnostics snapshot.

Parameters
[out]diagDiagnostics structure to fill.
Returns
ADC_SUCCESS on success.

◆ Initialize()

bool AdcManager::Initialize ( )
privatenoexcept
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsAds7952Ready()

bool AdcManager::IsAds7952Ready ( uint8_t  device_index) const
noexcept

ADS7952 device device_index came up during the last bring-up attempt.

Valid for diagnosis even when the manager failed closed.

◆ IsInitialized()

bool AdcManager::IsInitialized ( ) const
inlinenoexcept

Check whether the ADC subsystem has been initialised.

Returns
true if initialised and ready for reads.
Here is the caller graph for this function:

◆ IsInternalAdcReady()

bool AdcManager::IsInternalAdcReady ( ) const
inlinenoexcept

The MCU ADC unit came up during the last bring-up attempt.

◆ operator=() [1/2]

AdcManager & AdcManager::operator= ( AdcManager &&  )
delete

◆ operator=() [2/2]

AdcManager & AdcManager::operator= ( const AdcManager &  )
delete

◆ ReadRaw() [1/2]

hf_adc_err_t AdcManager::ReadRaw ( HfFunctionalAdcChannel  channel,
uint32_t &  raw_count,
uint8_t  samples = 1,
uint16_t  timeBetweenSamples = 0 
)
noexcept

Read raw ADC count from a channel by enum — O(1) lookup, preferred for real-time paths.

Parameters
channelFunctional ADC channel enum
raw_countOutput: raw ADC count
samplesNumber of samples to average (default 1)
timeBetweenSamplesDelay in ms between consecutive samples (default 0)
Returns
ADC error code

◆ ReadRaw() [2/2]

hf_adc_err_t AdcManager::ReadRaw ( std::string_view  name,
uint32_t &  raw_count,
uint8_t  samples = 1,
uint16_t  timeBetweenSamples = 0 
)
noexcept

Read raw ADC count from a channel by name.

Parameters
nameChannel name (linear string search — use enum overload in tight loops)
raw_countOutput: raw ADC count
samplesNumber of samples to average (default 1)
timeBetweenSamplesDelay in ms between consecutive samples (default 0)
Returns
ADC error code

◆ ReadRawByIndex()

hf_adc_err_t AdcManager::ReadRawByIndex ( size_t  idx,
uint32_t &  raw_count,
uint8_t  samples,
uint16_t  timeBetweenSamples 
)
privatenoexcept

◆ ReadVoltage() [1/2]

hf_adc_err_t AdcManager::ReadVoltage ( HfFunctionalAdcChannel  channel,
float &  voltage,
uint8_t  samples = 1,
uint16_t  timeBetweenSamples = 0 
)
noexcept

Read voltage from a channel by enum — O(1) lookup, preferred for real-time paths.

Parameters
channelFunctional ADC channel enum
voltageOutput: voltage at the input, if divider == 1, or bus voltage
samplesNumber of samples to average (default 1)
timeBetweenSamplesDelay in ms between consecutive samples (default 0)
Returns
ADC error code

◆ ReadVoltage() [2/2]

hf_adc_err_t AdcManager::ReadVoltage ( std::string_view  name,
float &  voltage,
uint8_t  samples = 1,
uint16_t  timeBetweenSamples = 0 
)
noexcept

Read voltage from a channel by name (already divided if divider > 1).

Parameters
nameChannel name (linear string search — use enum overload in tight loops)
voltageOutput: voltage at the input, if divider == 1, or bus voltage
samplesNumber of samples to average (default 1)
timeBetweenSamplesDelay in ms between consecutive samples (default 0)
Returns
ADC error code

◆ ReadVoltageByIndex()

hf_adc_err_t AdcManager::ReadVoltageByIndex ( size_t  idx,
float &  voltage,
uint8_t  samples,
uint16_t  timeBetweenSamples 
)
privatenoexcept

Read from an entry by index — shared implementation for both name and enum overloads.

◆ ReleaseLocked()

void AdcManager::ReleaseLocked ( )
privatenoexcept

Release the MCU ADC, every ADS7952 handler and the registry (mutex held).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Size()

size_t AdcManager::Size ( ) const
noexcept

Get the number of registered ADC channels.

Returns
Channel count.

Member Data Documentation

◆ ads7952_device_count_

size_t AdcManager::ads7952_device_count_ {0}
private

◆ ads7952_handlers_

std::array<std::optional<Ads7952Handler>, kMaxAds7952Devices> AdcManager::ads7952_handlers_ {}
private

◆ entries_

std::array<AdcEntry, kMaxChannels> AdcManager::entries_ {}
private

◆ enum_to_index_

std::array<size_t, kMaxChannels> AdcManager::enum_to_index_ {}
private

O(1) enum → entries index lookup.

◆ init_time_ms_

uint64_t AdcManager::init_time_ms_ {0}
private

Timestamp (ms) when Initialize() completed.

◆ initialized_

std::atomic<bool> AdcManager::initialized_ {false}
private

◆ internal_adc_

BaseAdc* AdcManager::internal_adc_ {nullptr}
private

◆ kInvalidIndex

constexpr size_t AdcManager::kInvalidIndex = SIZE_MAX
staticconstexprprivate

◆ kMaxAds7952Devices

constexpr size_t AdcManager::kMaxAds7952Devices = 4
staticconstexprprivate

◆ kMaxChannels

constexpr size_t AdcManager::kMaxChannels
staticconstexprprivate
Initial value:
=
static_cast<size_t>(HfFunctionalAdcChannel::HF_FUNCTIONAL_ADC_COUNT)

◆ last_error_

std::atomic<hf_adc_err_t> AdcManager::last_error_ {hf_adc_err_t::ADC_SUCCESS}
private

◆ manager_error_

std::atomic<flux::hal::ManagerError> AdcManager::manager_error_ {flux::hal::ManagerError::NotInitialized}
private

◆ mutex_

RtosMutex AdcManager::mutex_
mutableprivate

◆ registered_count_

size_t AdcManager::registered_count_ {0}
private

The documentation for this class was generated from the following files: