ECE 4180 - Final Project Team / Mbed 2 deprecated WalkieTalkie

Dependencies:   mbed 4DGL-uLCD-SE mbed-rtos nRF24L01P

Microphone.cpp

Committer:
Nurchu
Date:
2018-04-25
Revision:
33:5d86c111d9bc
Parent:
8:0222df74596e

File content as of revision 33:5d86c111d9bc:

#include  "Microphone.h"
#define UINT8_MAX 255

Microphone::Microphone (PinName pin):
    _pin(pin), dc(.67 / 3.3)
{}

float Microphone::read()
{
    return _pin.read();
}

inline Microphone::operator float ()
{
    return _pin.read();
}

uint8_t Microphone::getData()
{
    // This can be better but this should work for now
    const static float alpha = 0.99999f;
    float sample = (float)read();
    
    dc = alpha*dc + (1.0f - alpha)*sample;
    
    sample = 0.5f + (sample - dc)* 33.0f;
    return sample * UINT8_MAX;
}