fast-feedback virtual target task code on STM Nucleo

Dependencies:   mbed

pulse.cpp

Committer:
gwappa
Date:
2018-06-25
Revision:
12:06ea96546af1
Parent:
duration.cpp@ 11:897ecd5413e0
Child:
13:8ea85a33e37a

File content as of revision 12:06ea96546af1:

#include "pulse.h"

Pulse::Pulse(PinName pin, const uint64_t& onset_us, const uint64_t& duration_us):
    out_(pin, 0),
    stat_(Rest)
{
    setOnset(onset_us);
    setDuration(duration_us);
}

void Pulse::setOnset(const uint64_t& value_us)
{
    onset_ = value_us;
}

void Pulse::setDuration(const uint64_t& value_us)
{
    dur_ = value_us;
}

void Pulse::attachTurnOnCallback(Callback<void ()> cb)
{
    turnon_ = cb;
}

void Pulse::attachTurnOffCallback(Callback<void ()> cb)
{
    turnoff_ = cb;
}

void Pulse::detachTurnOnCallback()
{
    turnon_ = 0;
}

void Pulse::detachTurnOffCallback()
{
    turnoff_ = 0;
}

void Pulse::run()
{
    if (onset_ < 10) {
        // do not wait for the onset timeout
        start();
    } else {
        stat_ = Armed;
        timer_.attach_us(callback(this, &Pulse::start), onset_);
    }
}

void Pulse::start()
{
    stat_ = Active;
    out_.write(1);
    timer_.attach_us(callback(this, &Pulse::stop), dur_);
    if (turnon_) {
        turnon_();
    }
}

void Pulse::stop()
{
    timer_.detach();
    out_.write(0);
    stat_ = Rest;
    if (turnoff_) {
        turnoff_();
    }
}

void Pulse::direct(const bool& value) {
    out_.write(value? 1:0);
}

Pulse::Status Pulse::getStatus()
{
    return stat_;
}

void Pulse::wait()
{
    while(stat_ != Rest);
}