fast-feedback virtual target task code on STM Nucleo

Dependencies:   mbed

Revision:
26:b4421d1ee57a
Parent:
20:4c06d3041337
Child:
28:797536a42b9f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/pulse.cpp	Thu Jul 05 20:15:37 2018 +0000
@@ -0,0 +1,122 @@
+#include "pulse.h"
+
+Pulse::Pulse(PinName pin,
+             const uint64_t& onset_us,
+             const uint64_t& duration_us,
+             const uint64_t& blink_us):
+    out_(pin, 0),
+    enabled_(true),
+    stat_(Rest)
+{
+    setOnset(onset_us);
+    setDuration(duration_us);
+    setBlinkDuration(blink_us);
+}
+
+void Pulse::setEnabled(const bool& value)
+{
+    enabled_ = value;
+}
+
+void Pulse::setOnset(const uint64_t& value_us)
+{
+    onset_ = value_us;
+}
+
+bool Pulse::isEnabled()
+{
+    return enabled_;
+}
+
+void Pulse::setDuration(const uint64_t& value_us)
+{
+    dur_ = value_us;
+}
+
+void Pulse::setBlinkDuration(const uint64_t& value_us)
+{
+    blinkdur_ = 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 (!enabled_) return;
+    
+    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()
+{
+    if (!enabled_) return;
+    
+    stat_ = Active;
+    out_.write(1);
+    timer_.attach_us(callback(this, &Pulse::stop), dur_);
+    if (blinkdur_ > 0) {
+        blinker_.attach_us(callback(this, &Pulse::blink), blinkdur_);
+    }
+    if (turnon_) {
+        turnon_();
+    }
+}
+
+void Pulse::stop()
+{
+    if (!enabled_) return;
+    
+    timer_.detach();
+    blinker_.detach();
+    
+    out_.write(0);
+    stat_ = Rest;
+    if (turnoff_) {
+        turnoff_();
+    }
+}
+
+void Pulse::blink()
+{
+    out_ = !out_;
+}
+
+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