Usada bajo permiso del autor tony63

Dependents:   IR-NEC

Fork of Pulse1 by Gustavo Ramirez

Pulse1.cpp

Committer:
tony63
Date:
2013-11-07
Revision:
1:48651f86a80c
Child:
2:a91e76505e52

File content as of revision 1:48651f86a80c:

 
#include "Pulse1.h"

PulseInOut::PulseInOut(PinName pin):
        startval(0), pulsetime(), runtime(), io(pin)    {
}


PulseInOut::~PulseInOut() {
}

void PulseInOut::write(int val) {
    io.output();
    io = val;
}

void PulseInOut::write_us(int val, int time)   {
    io.output();
    io = val;
    wait_us(time);
    io = !val;
}

int PulseInOut::read_high_us()  {
    pulsetime.reset();
    io.input();
    while (io == 1) {
    }
    while (io == 0) {
    }
    pulsetime.start();
    while (io == 1) {
    }
    pulsetime.stop();
    return pulsetime.read_us();
}

int PulseInOut::read_high_us(int timeout)  {
    runtime.reset();
    runtime.start();
    pulsetime.reset();
    io.input();
    while (io == 1) {
        if (runtime.read_us() > timeout)   return -1;
    }
    while (io == 0) {
        if (runtime.read_us() > timeout)   return -1;
    }
    pulsetime.start();
    while (io == 1) {
        if (runtime.read_us() > timeout)   return -1;
    }
    pulsetime.stop();
    return pulsetime.read_us();
}

int PulseInOut::read_low_us()   {
    pulsetime.reset();
    io.input();
    while (io == 0) {
    }
    while (io == 1) {
    }
    pulsetime.start();
    while (io == 0) {
    }
    pulsetime.stop();
    return pulsetime.read_us();
}

int PulseInOut::read_low_us(int timeout)   {
    runtime.reset();
    runtime.start();
    pulsetime.reset();
    io.input();
    while (io == 0) {
        if (runtime.read_us() > timeout)   return -1;
    }
    while (io == 1) {
        if (runtime.read_us() > timeout)   return -1;
    }
    pulsetime.start();
    while (io == 0) {
        if (runtime.read_us() > timeout)   return -1;
    }
    pulsetime.stop();
    return pulsetime.read_us();
}

int PulseInOut::read_us()  {
    pulsetime.reset();
    io.input();
    startval = io;
    while (io == startval)   {
    }
    pulsetime.start();
    while (io != startval)  {
    }
    pulsetime.stop();
    return pulsetime.read_us();
}

int PulseInOut::read_us(int timeout)   {
    runtime.reset();
    runtime.start();
    pulsetime.reset();
    io.input();
    startval = io;
    while (io == startval)  {
        if (runtime.read_us() > timeout)   return -1;
    }
    pulsetime.start();
    while (io != startval)   {
        if (runtime.read_us() > timeout)   return -1;
    }
    pulsetime.stop();
    return pulsetime.read_us();
}