upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
Public Member Functions
Microphone Class Reference

API for the Analog Microphone. More...

Detailed Description

This module defines the Analog Microphone sensor

mic.jpg
int
main(int argc, char **argv)
{
// Attach microphone to analog port A0
sensor = new upm::Microphone(0);
signal(SIGINT, sig_handler);
ctx.averageReading = 0;
ctx.runningAverage = 0;
ctx.averagedOver = 2;
// Infinite loop, ends when script is cancelled
// Repeatedly, take a sample every 2 microseconds;
// find the average of 128 samples; and
// print a running graph of the averages
while (!is_running) {
int len = sensor->getSampledWindow (2, 128, buffer);
if (len) {
int thresh = sensor->findThreshold (&ctx, 30, buffer, len);
sensor->printGraph(&ctx);
if (thresh) {
// do something ....
}
}
}
std::cout << "exiting application" << std::endl;
delete sensor;
return 0;
}

Public Member Functions

 Microphone (int micPin)
 
 ~Microphone ()
 
int getSampledWindow (unsigned int freqMS, int numberOfSamples, uint16_t *buffer)
 
int findThreshold (thresholdContext *ctx, unsigned int threshold, uint16_t *buffer, int len)
 
void printGraph (thresholdContext *ctx)
 

Constructor & Destructor Documentation

Microphone ( int  micPin)

Instantiates a Microphone object

Parameters
micPinPin where the microphone is connected
~Microphone ( )

Microphone object destructor

Member Function Documentation

int getSampledWindow ( unsigned int  freqMS,
int  numberOfSamples,
uint16_t *  buffer 
)

Gets samples from the microphone according to the provided window and number of samples

Parameters
freqMSTime between each sample (in microseconds)
numberOfSamplesNumber of sample to sample for this window
bufferBuffer with sampled data
int findThreshold ( thresholdContext ctx,
unsigned int  threshold,
uint16_t *  buffer,
int  len 
)

Given the sampled buffer, this method returns TRUE/FALSE if threshold is reached

Parameters
ctxThreshold context
thresholdSample threshold
bufferBuffer with samples
lenBuffer length
void printGraph ( thresholdContext ctx)

Prints a running average of the threshold context

Parameters
ctxThreshold context

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