Keisuke Sehara
/
STM32_Whisking
fast-feedback virtual target task code on STM Nucleo
Diff: pulse.cpp
- Revision:
- 12:06ea96546af1
- Parent:
- 11:897ecd5413e0
- Child:
- 13:8ea85a33e37a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pulse.cpp Mon Jun 25 12:10:31 2018 +0000 @@ -0,0 +1,85 @@ +#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); +} + \ No newline at end of file