Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
hdw-mic.h File Reference

Detailed Description

Design Philosophy

The microphone uses the Analog to Digital Converter (ADC) Continuous Mode Driver.

The microphone code is based on the ADC DMA Example.

The microphone is continuously sampled at 8KHz.

Warning
The battery monitor (hdw-battmon.h) and microphone cannot be used at the same time! Each mode can either continuously sample the microphone or measure the battery voltage, not both.
Warning
Note that the DAC peripheral (hdw-dac.h) and the ADC peripheral use the same DMA controller, so they cannot both be used at the same time. One must be deinitialize before initializing the other.

Usage

You don't need to call initMic() or deinitMic(). The system does at the appropriate times.

The system will also automatically call startMic(), though the Swadge mode can later call stopMic() or startMic() when the microphone needs to be used. Stopping the microphone when not in use can save some processing cycles.

loopMic() is called automatically by the system while the microphone is started and samples are delivered to the Swadge mode through a callback, swadgeMode_t.fnAudioCallback. The Swadge mode can do what it wants with the samples from there.

If swadgeMode_t.fnAudioCallback is left NULL, then the microphone will not be initialized or sampled.

Example

static void demoAudioCallback(uint16_t* samples, uint32_t sampleCnt);
swadgeMode_t demoMode = {
...
.fnAudioCallback = demoAudioCallback,
...
};
...
// Start the mic
startMic();
// Stop the mic
...
static void demoAudioCallback(uint16_t* samples, uint32_t sampleCnt)
{
; // Do something with the audio samples, when the mic is started
}
void stopMic(void)
Stop sampling the microphone's ADC.
Definition hdw-mic.c:119
A struct of all the function pointers necessary for a swadge mode. If a mode does not need a particul...
Definition swadge2024.h:233
void(* fnAudioCallback)(uint16_t *samples, uint32_t sampleCnt)
This function is called whenever audio samples are read from the microphone (ADC) and are ready for p...
Definition swadge2024.h:296

Go to the source code of this file.

Macros

#define ADC_READ_LEN   512
 The maximum number of bytes read by the ADC in one go.
 
#define MAX_MIC_GAIN   7
 
#define ADC_SAMPLE_RATE_HZ   8000
 

Functions

void initMic (gpio_num_t gpio)
 Initialize the ADC which continuously samples the microphone.
 
void startMic (void)
 Start sampling the microphone's ADC.
 
uint32_t loopMic (uint16_t *outSamples, uint32_t outSamplesMax)
 Attempt to read a block of 12-bit samples from the ADC in continuous mode. This may return fewer than expected samples (or zero samples) if the task rate is faster than the sampling rate.
 
void stopMic (void)
 Stop sampling the microphone's ADC.
 
void deinitMic (void)
 Deinitialize the ADC which continuously samples the microphone.
 

Macro Definition Documentation

◆ ADC_READ_LEN

#define ADC_READ_LEN   512

The maximum number of bytes read by the ADC in one go.

◆ MAX_MIC_GAIN

#define MAX_MIC_GAIN   7

◆ ADC_SAMPLE_RATE_HZ

#define ADC_SAMPLE_RATE_HZ   8000

Function Documentation

◆ initMic()

void initMic ( gpio_num_t gpio)

Initialize the ADC which continuously samples the microphone.

This does not start sampling, so startMic() must be called afterwards.

Parameters
gpioThe GPIO the microphone is attached to

◆ startMic()

void startMic ( void )

Start sampling the microphone's ADC.

◆ loopMic()

uint32_t loopMic ( uint16_t * outSamples,
uint32_t outSamplesMax )

Attempt to read a block of 12-bit samples from the ADC in continuous mode. This may return fewer than expected samples (or zero samples) if the task rate is faster than the sampling rate.

Parameters
[out]outSamplesA pointer to write 12-bit samples from the ADC
[in]outSamplesMaxThe maximum number of samples that can be written to outSamples
Returns
The number of samples which were actually written to outSamples

◆ stopMic()

void stopMic ( void )

Stop sampling the microphone's ADC.

◆ deinitMic()

void deinitMic ( void )

Deinitialize the ADC which continuously samples the microphone.