Chrispy
High-performance multichannel ADC sampling and audio recording.
Loading...
Searching...
No Matches
adc Namespace Reference

Data Structures

struct  Adc
 Singleton instance of the ADC.
 
struct  AdcFrame
 Private/static data member for use by the ISR.
 
struct  Channel
 Single ADC channel (pin + metadata). More...
 
struct  State
 

Enumerations

enum struct  BitResolution : uint8_t { Eight = 8 , Ten = 10 }
 Supported bit resolutions for ADC sampling. More...
 
enum struct  AutotriggerSource : uint8_t {
  FreeRunning = 0b000 , AnalogComparator = 0b001 , ExternalIrq0 = 0b010 , TimCnt0CmpA = 0b011 ,
  TimCnt0Ovf = 0b100 , TimCnt1CmpB = 0b101 , TimCnt1Ovf = 0b110 , TimCnt1Cap = 0b111
}
 Potential sources for ADC interrupts to be triggered by. More...
 

Functions

 ISR (ADC_vect)
 Interrupt service routine responsible for reading samples from the ADC.
 
int8_t drain_buffer (uint8_t **buf, size_t &sz, size_t &ch_index)
 NOT SAFE TO USE WHEN THE ADC IS ENABLED!
 
int8_t swap_buffer (uint8_t **buf, size_t &sz, size_t &ch_index)
 Retrieve a buffer from the module belonging to a specific channel, writing the size and channel index to the reference arguments.
 
bool init (uint8_t _nchannels, Channel *_channels, uint8_t *_buf, size_t _sz)
 Initialize ADC module with these parameters for sampling.
 
void on ()
 Activate internal board's ADC.
 
void off ()
 Turn off ADC.
 
void sleep ()
 Turn off ADC and enter sleep mode.
 
int8_t start (BitResolution res, uint32_t sample_rate, size_t ch_window_sz=1, uint32_t warmup_ms=100)
 Start ADC sampling at a certain rate with a given bit resolution.
 
uint32_t collected ()
 
uint32_t stop ()
 Stops ADC sampling and performs cleanup on registers.
 
size_t bytes_per_sample (BitResolution res)
 

Variables

const size_t MAX_CHANNEL_COUNT = 16
 Number of channels supported by the ADC (0 - 15).
 

Enumeration Type Documentation

◆ AutotriggerSource

enum struct adc::AutotriggerSource : uint8_t
strong

Potential sources for ADC interrupts to be triggered by.

Maps to the bit patterns used to set source in ADCSRB register.

Enumerator
FreeRunning 

0b000

AnalogComparator 

0b001

ExternalIrq0 

0b010

TimCnt0CmpA 

0b011

TimCnt0Ovf 

0b100

TimCnt1CmpB 

0b101

TimCnt1Ovf 

0b110

TimCnt1Cap 

0b111

◆ BitResolution

enum struct adc::BitResolution : uint8_t
strong

Supported bit resolutions for ADC sampling.

Enumerator
Eight 
Ten 

Function Documentation

◆ bytes_per_sample()

size_t adc::bytes_per_sample ( BitResolution  res)

◆ collected()

uint32_t adc::collected ( )
Returns
(uint32_t): Number of samples collected in the current/previous round of sampling.

◆ drain_buffer()

int8_t adc::drain_buffer ( uint8_t **  buf,
size_t &  sz,
size_t &  ch_index 
)

NOT SAFE TO USE WHEN THE ADC IS ENABLED!

Identical API as swap_buffer but this will also give the active buffer. Used to drain any remaining samples from the buffer.

◆ init()

bool adc::init ( uint8_t  nchannels,
Channel channels,
uint8_t *  buf,
size_t  sz 
)

Initialize ADC module with these parameters for sampling.

◆ ISR()

adc::ISR ( ADC_vect  )

Interrupt service routine responsible for reading samples from the ADC.

◆ off()

void adc::off ( )

Turn off ADC.

◆ on()

void adc::on ( )

Activate internal board's ADC.

Wake up from sleep mode.

◆ sleep()

void adc::sleep ( )

Turn off ADC and enter sleep mode.

◆ start()

int8_t adc::start ( BitResolution  res,
uint32_t  sample_rate,
size_t  ch_window_sz = 1,
uint32_t  warmup_ms = 100 
)

Start ADC sampling at a certain rate with a given bit resolution.

Parameters
resBit resolution to use.
sample_rateSample rate in Hz to try and record at,
ch_window_szSize of each channel's window. Only checked when there are multiple channels being recorded from. Defaults to 8.
warmup_msMilliseconds to delay after starting ADC before ingesting samples. A small warmup helps prevent poor signal from channel switching noise.
Returns
(int8_t): Return code. 0 if all is good, negative otherwise.

◆ stop()

uint32_t adc::stop ( )

Stops ADC sampling and performs cleanup on registers.

Returns
(uint32_t): Number of samples collected.

◆ swap_buffer()

int8_t adc::swap_buffer ( uint8_t **  buf,
size_t &  sz,
size_t &  ch_index 
)

Retrieve a buffer from the module belonging to a specific channel, writing the size and channel index to the reference arguments.

Intent: Call this in a busy loop starting with a nullptr and write data out to the appropriate channel as it comes in.

Parameters
bufPointer to buffer being swapped. If it is a null-pointer, will try to swap it with a currently full buffer without marking it as usable by the ISR. If it is one of the double buffers, will free the buffer for use again and try to exchange it for the other buffer if possible.
szOut-parameter for the number of bytes in the buffer.
ch_indexOut-paramter for the channel index this data is from.
Returns
(int8_t): 0 if a new buffer is returned. Nonzero otherwise.

Variable Documentation

◆ MAX_CHANNEL_COUNT

const size_t adc::MAX_CHANNEL_COUNT = 16

Number of channels supported by the ADC (0 - 15).