HardFOC Vortex V1 HAL
Unified HAL API for the HardFOC Vortex motor-control platform (ESP32-C6, TMC9660)
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 Vortex 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 Deinitialize () noexcept
 Release all ADC resources and reset to uninitialised state.
 
bool IsInitialized () const noexcept
 Check whether the ADC subsystem has been initialised.
 
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, hf_u8_t samples=1, hf_time_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, hf_u8_t samples=1, hf_time_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_value, hf_u8_t numOfSamplesToAvg=1, hf_time_t timeBetweenSamples=0) noexcept
 Read raw ADC value from a channel by name.
 
hf_adc_err_t ReadRaw (HfFunctionalAdcChannel channel, uint32_t &raw_value, hf_u8_t numOfSamplesToAvg=1, hf_time_t timeBetweenSamples=0) noexcept
 Read raw ADC value from a channel by functional enum.
 
hf_adc_err_t GetSystemDiagnostics (AdcSystemDiagnostics &diagnostics) const noexcept
 Populate an AdcSystemDiagnostics snapshot.
 
hf_adc_err_t GetLastError () const noexcept
 Get the most recent error code from any ADC operation.
 
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 ()
 
bool Initialize () noexcept
 
size_t FindByName (std::string_view name) const noexcept
 Find entry index by string name. Returns kInvalidIndex if not found.
 
size_t FindByEnum (HfFunctionalAdcChannel channel) const noexcept
 Find entry index by functional enum. Returns kInvalidIndex if not found.
 

Private Attributes

std::array< AdcEntry, kMaxChannels > entries_ {}
 
size_t registered_count_ {0}
 
std::optional< Tmc9660AdcWrapper > tmc9660_adc_
 Single TMC9660 ADC wrapper — all channels route through this.
 
MotorController * motor_controller_ {nullptr}
 MotorController dependency (non-owning pointer, set at init).
 
std::atomic< bool > initialized_ {false}
 
std::atomic< hf_adc_err_t > last_error_ {hf_adc_err_t::ADC_SUCCESS}
 
std::atomic< bool > warned_motor_controller_not_ready_ {false}
 
std::atomic< bool > warned_tmc_handler_missing_ {false}
 
RtosMutex mutex_
 

Static Private Attributes

static constexpr size_t kMaxChannels
 
static constexpr size_t kInvalidIndex = SIZE_MAX
 

Detailed Description

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

Provides a uniform API over the ESP32 internal ADC and the TMC9660 motor-controller ADC. Channels are registered from the pincfg X-MACRO table at initialisation and looked up by name or by functional enum.

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 uninitialised state.

Returns
true on success.
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.

◆ FindByName()

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

Find entry index by string name. Returns kInvalidIndex if not found.

◆ 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 from any ADC operation.

Returns
Last hf_adc_err_t set by any API call

◆ GetSystemDiagnostics()

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

Populate an AdcSystemDiagnostics snapshot.

Parameters
[out]diagnosticsDiagnostics 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:

◆ IsInitialized()

bool AdcManager::IsInitialized ( ) const
inlinenoexcept

Check whether the ADC subsystem has been initialised.

Returns
true if initialised and ready for reads.

◆ 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_value,
hf_u8_t  numOfSamplesToAvg = 1,
hf_time_t  timeBetweenSamples = 0 
)
noexcept

Read raw ADC value from a channel by functional enum.

Parameters
channelFunctional ADC channel enum.
raw_valueOutput: raw ADC count.
numOfSamplesToAvgNumber 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_value,
hf_u8_t  numOfSamplesToAvg = 1,
hf_time_t  timeBetweenSamples = 0 
)
noexcept

Read raw ADC value from a channel by name.

Parameters
nameChannel name (linear scan).
raw_valueOutput: raw ADC count.
numOfSamplesToAvgNumber of samples to average (default 1).
timeBetweenSamplesDelay in ms between consecutive samples (default 0).
Returns
ADC error code.

◆ ReadVoltage() [1/2]

hf_adc_err_t AdcManager::ReadVoltage ( HfFunctionalAdcChannel  channel,
float &  voltage,
hf_u8_t  samples = 1,
hf_time_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,
hf_u8_t  samples = 1,
hf_time_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

◆ Size()

size_t AdcManager::Size ( ) const
noexcept

Get the number of registered ADC channels.

Returns
Channel count.

Member Data Documentation

◆ entries_

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

◆ initialized_

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

◆ kInvalidIndex

constexpr size_t AdcManager::kInvalidIndex = SIZE_MAX
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

◆ motor_controller_

MotorController* AdcManager::motor_controller_ {nullptr}
private

MotorController dependency (non-owning pointer, set at init).

◆ mutex_

RtosMutex AdcManager::mutex_
mutableprivate

◆ registered_count_

size_t AdcManager::registered_count_ {0}
private

◆ tmc9660_adc_

std::optional<Tmc9660AdcWrapper> AdcManager::tmc9660_adc_
private

Single TMC9660 ADC wrapper — all channels route through this.

In place; emplaced at init

◆ warned_motor_controller_not_ready_

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

Avoid repeating the same dependency warning when EnsureInitialized() is called from multiple subsystems (e.g. Vortex API then TemperatureManager) after a failed boot.

◆ warned_tmc_handler_missing_

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

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