the fish that looks like a jet

Dependencies:   ADXL345 ADXL345_I2C IMUfilter ITG3200 mbed Servo

PwmReader.cpp

Committer:
sandwich
Date:
2014-01-28
Revision:
5:090ef6275773
Child:
6:a4d6f3e4bf28

File content as of revision 5:090ef6275773:

#include "PwmReader.h"

//Constructors
PwmReader::PwmReader(PinName pwmInPort)
{
    di = new InterruptIn(pwmInPort);
    
    lastRise = 0;
    period = 0;
    duty = 0.0;
    di->rise(this,&PwmReader::pwmRise);  // attach the address of the flip function to the rising edge
    di->fall(this,&PwmReader::pwmFall);
}

PwmReader::~PwmReader()
{
    delete di;
}

// public methods
float PwmReader::getDuty()
{
    return duty;
}

// private methods
void PwmReader::pwmRise()
{
    int rise = t.read_us();
    if( (lastRise > 0) && (rise > lastRise) ) {
        period = rise - lastRise;
    }
    lastRise = rise;

}

void PwmReader::pwmFall()
{
    int fallTime = t.read_us();
    if(period > 0 ) {
        int delta = fallTime - lastRise;
        duty = float(delta)/float(period);
    }
}