upm  1.3.0
Sensor/Actuator repository for libmraa (v1.7.0)
 All Data Structures Namespaces Files Functions Variables Typedefs 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

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);
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

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

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

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

Prints a running average of the threshold context

ctxThreshold context

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