Microphone_PDM
All Data Structures Functions Variables Enumerations Enumerator Pages
Static Public Attributes | Protected Member Functions | Protected Attributes
Microphone_PDM_RTL872x Class Reference

#include <Microphone_PDM_RTL872x.h>

Inheritance diagram for Microphone_PDM_RTL872x:
Microphone_PDM_Base Microphone_PDM_MCU Microphone_PDM

Static Public Attributes

static const size_t BUFFER_SIZE_SAMPLES = SP_DMA_PAGE_SIZE / 2
 
static const size_t NUM_BUFFERS = SP_DMA_PAGE_NUM
 

Protected Member Functions

 Microphone_PDM_RTL872x ()
 
virtual ~Microphone_PDM_RTL872x ()
 
virtual int init ()
 
virtual int uninit ()
 
virtual int start ()
 
virtual int stop ()
 
virtual bool samplesAvailable () const
 
virtual bool copySamples (void *pSamples)
 
virtual bool noCopySamples (std::function< void(void *pSamples, size_t numSamples)>callback)
 
size_t getNumberOfSamples () const
 
- Protected Member Functions inherited from Microphone_PDM_Base
 Microphone_PDM_Base (size_t numSamples)
 
virtual ~Microphone_PDM_Base ()
 
void copySamplesInternal (const int16_t *src, uint8_t *dst) const
 
virtual size_t copySrcIncrement () const
 

Protected Attributes

bool running = false
 
- Protected Attributes inherited from Microphone_PDM_Base
pin_t clkPin = A0
 
pin_t datPin = A1
 
bool stereoMode = false
 
int sampleRate
 
OutputSize outputSize = OutputSize::SIGNED_16
 
Range range = Range::RANGE_2048
 
size_t numSamples
 

Additional Inherited Members

- Public Types inherited from Microphone_PDM_Base
enum class  OutputSize { UNSIGNED_8 , SIGNED_16 , RAW_SIGNED_16 }
 
enum class  Range {
  RANGE_128 = 0 , RANGE_256 , RANGE_512 , RANGE_1024 ,
  RANGE_2048 , RANGE_4096 , RANGE_8192 , RANGE_16384 ,
  RANGE_32768
}
 
- Public Member Functions inherited from Microphone_PDM_Base
int getSampleRate () const
 

Detailed Description

MCU-specific implementation of PDM for the RTL872x.

This class is used on the P2 and Photon 2.

You do not instantiate this class directly; it's automatically created when you use the Microphone_PDM singleton.

Member Function Documentation

◆ copySamples()

virtual bool Microphone_PDM_RTL872x::copySamples ( void *  pSamples)
protectedvirtual

Copy samples from the DMA buffer to your buffer.

Parameters
pSamplesPointer to buffer to copy samples to. It must be at least getNumberOfSamples() samples in length.
Returns
true There were samples available and they were copied.
false There were no samples available. Your buffer is unmodified.

The size of the buffer in bytes will depend on the outputSize. If UNSIGNED_8, then it's getNumberOfSamples() bytes. If SIGNED_16 or RAW_SIGNED_16, then it's 2 * getNumberOfSamples().

You can skip calling samplesAvailable() and just call copySamples which will return false in the same cases where samplesAvailable() would have returned false.

Reimplemented in Microphone_PDM.

◆ getNumberOfSamples()

size_t Microphone_PDM_RTL872x::getNumberOfSamples ( ) const
inlineprotected

Return the number of int16_t samples that copySamples will copy.

Returns
size_t Number of uint16_t samples. Number of bytes is twice that value

You will never get a partial buffer of data. The number of samples is a constant that is determined by the MCU type at compile time and does not change.

On the RTL872x, it's 256 samples (512 bytes). It's smaller because the are 4 buffers instead of the 2 buffers used on the nRF52, and the optimal DMA size on the RTL872x is 512 bytes.

◆ init()

virtual int Microphone_PDM_RTL872x::init ( )
protectedvirtual

Initialize the PDM module.

This is often done from setup(). You can defer it until you're ready to sample if desired, calling right before start().

Reimplemented in Microphone_PDM.

◆ noCopySamples()

virtual bool Microphone_PDM_RTL872x::noCopySamples ( std::function< void(void *pSamples, size_t numSamples)>  callback)
protectedvirtual

Alternative API to get samples.

Parameters
callbackCallback function or lambda
Returns
true
false

Alternative API that does not require a buffer to be passed in. You should only use this if you can consume the buffer immediately without blocking.

The callback function or lamba has this prototype:

void callback(void *pSamples, size_t numSamples)

It will be called with a pointer to the samples (in the DMA buffer) and the number of samples (not bytes!) of data. The number of bytes will vary depending on the outputSize.

You can skip calling samplesAvailable() and just call noCopySamples which will return false in the same cases where samplesAvailable() would have returned false.

Reimplemented in Microphone_PDM.

◆ samplesAvailable()

virtual bool Microphone_PDM_RTL872x::samplesAvailable ( ) const
protectedvirtual

Return true if there is data available to be copied using copySamples.

Returns
true
false

Reimplemented in Microphone_PDM.

◆ start()

virtual int Microphone_PDM_RTL872x::start ( )
protectedvirtual

Start sampling.

Reimplemented in Microphone_PDM.

◆ stop()

virtual int Microphone_PDM_RTL872x::stop ( )
protectedvirtual

Stop sampling.

Reimplemented in Microphone_PDM.

◆ uninit()

virtual int Microphone_PDM_RTL872x::uninit ( )
inlineprotectedvirtual

Uninitialize the PDM module.

You normally will just initialize it once and only start and stop it as necessary, however you can completely uninitialize it if desired. The clkPin will be reset to INPUT mode.

Reimplemented in Microphone_PDM.


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