Keisuke Sehara
/
STM32_Whisking
fast-feedback virtual target task code on STM Nucleo
core/pulse.cpp@32:1416e015016c, 2018-12-13 (annotated)
- Committer:
- gwappa
- Date:
- Thu Dec 13 07:18:43 2018 +0000
- Revision:
- 32:1416e015016c
- Parent:
- 29:1fb060aab1f8
change to use the Staged state
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gwappa | 12:06ea96546af1 | 1 | #include "pulse.h" |
gwappa | 29:1fb060aab1f8 | 2 | #include "IO.h" |
gwappa | 11:897ecd5413e0 | 3 | |
gwappa | 20:4c06d3041337 | 4 | Pulse::Pulse(PinName pin, |
gwappa | 20:4c06d3041337 | 5 | const uint64_t& onset_us, |
gwappa | 20:4c06d3041337 | 6 | const uint64_t& duration_us, |
gwappa | 20:4c06d3041337 | 7 | const uint64_t& blink_us): |
gwappa | 11:897ecd5413e0 | 8 | out_(pin, 0), |
gwappa | 15:20f7f737c256 | 9 | enabled_(true), |
gwappa | 11:897ecd5413e0 | 10 | stat_(Rest) |
gwappa | 11:897ecd5413e0 | 11 | { |
gwappa | 12:06ea96546af1 | 12 | setOnset(onset_us); |
gwappa | 11:897ecd5413e0 | 13 | setDuration(duration_us); |
gwappa | 20:4c06d3041337 | 14 | setBlinkDuration(blink_us); |
gwappa | 11:897ecd5413e0 | 15 | } |
gwappa | 11:897ecd5413e0 | 16 | |
gwappa | 15:20f7f737c256 | 17 | void Pulse::setEnabled(const bool& value) |
gwappa | 15:20f7f737c256 | 18 | { |
gwappa | 15:20f7f737c256 | 19 | enabled_ = value; |
gwappa | 15:20f7f737c256 | 20 | } |
gwappa | 15:20f7f737c256 | 21 | |
gwappa | 12:06ea96546af1 | 22 | void Pulse::setOnset(const uint64_t& value_us) |
gwappa | 12:06ea96546af1 | 23 | { |
gwappa | 12:06ea96546af1 | 24 | onset_ = value_us; |
gwappa | 12:06ea96546af1 | 25 | } |
gwappa | 12:06ea96546af1 | 26 | |
gwappa | 15:20f7f737c256 | 27 | bool Pulse::isEnabled() |
gwappa | 15:20f7f737c256 | 28 | { |
gwappa | 15:20f7f737c256 | 29 | return enabled_; |
gwappa | 15:20f7f737c256 | 30 | } |
gwappa | 15:20f7f737c256 | 31 | |
gwappa | 12:06ea96546af1 | 32 | void Pulse::setDuration(const uint64_t& value_us) |
gwappa | 11:897ecd5413e0 | 33 | { |
gwappa | 11:897ecd5413e0 | 34 | dur_ = value_us; |
gwappa | 11:897ecd5413e0 | 35 | } |
gwappa | 11:897ecd5413e0 | 36 | |
gwappa | 20:4c06d3041337 | 37 | void Pulse::setBlinkDuration(const uint64_t& value_us) |
gwappa | 20:4c06d3041337 | 38 | { |
gwappa | 20:4c06d3041337 | 39 | blinkdur_ = value_us; |
gwappa | 20:4c06d3041337 | 40 | } |
gwappa | 20:4c06d3041337 | 41 | |
gwappa | 29:1fb060aab1f8 | 42 | void Pulse::loginfo() { |
gwappa | 29:1fb060aab1f8 | 43 | const uint32_t onset_ms = onset_/1000; |
gwappa | 29:1fb060aab1f8 | 44 | const uint32_t duration_ms = dur_/1000; |
gwappa | 29:1fb060aab1f8 | 45 | IO::debug("pulse: enabled=%d, onset=%d ms, duration=%d ms", enabled_, onset_ms, duration_ms); |
gwappa | 29:1fb060aab1f8 | 46 | } |
gwappa | 29:1fb060aab1f8 | 47 | |
gwappa | 12:06ea96546af1 | 48 | void Pulse::attachTurnOnCallback(Callback<void ()> cb) |
gwappa | 12:06ea96546af1 | 49 | { |
gwappa | 12:06ea96546af1 | 50 | turnon_ = cb; |
gwappa | 12:06ea96546af1 | 51 | } |
gwappa | 12:06ea96546af1 | 52 | |
gwappa | 12:06ea96546af1 | 53 | void Pulse::attachTurnOffCallback(Callback<void ()> cb) |
gwappa | 12:06ea96546af1 | 54 | { |
gwappa | 12:06ea96546af1 | 55 | turnoff_ = cb; |
gwappa | 12:06ea96546af1 | 56 | } |
gwappa | 12:06ea96546af1 | 57 | |
gwappa | 12:06ea96546af1 | 58 | void Pulse::detachTurnOnCallback() |
gwappa | 12:06ea96546af1 | 59 | { |
gwappa | 12:06ea96546af1 | 60 | turnon_ = 0; |
gwappa | 12:06ea96546af1 | 61 | } |
gwappa | 12:06ea96546af1 | 62 | |
gwappa | 12:06ea96546af1 | 63 | void Pulse::detachTurnOffCallback() |
gwappa | 12:06ea96546af1 | 64 | { |
gwappa | 12:06ea96546af1 | 65 | turnoff_ = 0; |
gwappa | 12:06ea96546af1 | 66 | } |
gwappa | 12:06ea96546af1 | 67 | |
gwappa | 12:06ea96546af1 | 68 | void Pulse::run() |
gwappa | 12:06ea96546af1 | 69 | { |
gwappa | 15:20f7f737c256 | 70 | if (!enabled_) return; |
gwappa | 15:20f7f737c256 | 71 | |
gwappa | 28:797536a42b9f | 72 | if (onset_ < 500) { |
gwappa | 12:06ea96546af1 | 73 | // do not wait for the onset timeout |
gwappa | 12:06ea96546af1 | 74 | start(); |
gwappa | 12:06ea96546af1 | 75 | } else { |
gwappa | 12:06ea96546af1 | 76 | stat_ = Armed; |
gwappa | 12:06ea96546af1 | 77 | timer_.attach_us(callback(this, &Pulse::start), onset_); |
gwappa | 12:06ea96546af1 | 78 | } |
gwappa | 12:06ea96546af1 | 79 | } |
gwappa | 12:06ea96546af1 | 80 | |
gwappa | 12:06ea96546af1 | 81 | void Pulse::start() |
gwappa | 11:897ecd5413e0 | 82 | { |
gwappa | 15:20f7f737c256 | 83 | if (!enabled_) return; |
gwappa | 15:20f7f737c256 | 84 | |
gwappa | 11:897ecd5413e0 | 85 | stat_ = Active; |
gwappa | 11:897ecd5413e0 | 86 | out_.write(1); |
gwappa | 12:06ea96546af1 | 87 | timer_.attach_us(callback(this, &Pulse::stop), dur_); |
gwappa | 20:4c06d3041337 | 88 | if (blinkdur_ > 0) { |
gwappa | 20:4c06d3041337 | 89 | blinker_.attach_us(callback(this, &Pulse::blink), blinkdur_); |
gwappa | 20:4c06d3041337 | 90 | } |
gwappa | 12:06ea96546af1 | 91 | if (turnon_) { |
gwappa | 12:06ea96546af1 | 92 | turnon_(); |
gwappa | 12:06ea96546af1 | 93 | } |
gwappa | 11:897ecd5413e0 | 94 | } |
gwappa | 11:897ecd5413e0 | 95 | |
gwappa | 12:06ea96546af1 | 96 | void Pulse::stop() |
gwappa | 11:897ecd5413e0 | 97 | { |
gwappa | 15:20f7f737c256 | 98 | if (!enabled_) return; |
gwappa | 15:20f7f737c256 | 99 | |
gwappa | 11:897ecd5413e0 | 100 | timer_.detach(); |
gwappa | 20:4c06d3041337 | 101 | blinker_.detach(); |
gwappa | 20:4c06d3041337 | 102 | |
gwappa | 11:897ecd5413e0 | 103 | out_.write(0); |
gwappa | 11:897ecd5413e0 | 104 | stat_ = Rest; |
gwappa | 12:06ea96546af1 | 105 | if (turnoff_) { |
gwappa | 12:06ea96546af1 | 106 | turnoff_(); |
gwappa | 12:06ea96546af1 | 107 | } |
gwappa | 11:897ecd5413e0 | 108 | } |
gwappa | 11:897ecd5413e0 | 109 | |
gwappa | 20:4c06d3041337 | 110 | void Pulse::blink() |
gwappa | 20:4c06d3041337 | 111 | { |
gwappa | 20:4c06d3041337 | 112 | out_ = !out_; |
gwappa | 20:4c06d3041337 | 113 | } |
gwappa | 20:4c06d3041337 | 114 | |
gwappa | 12:06ea96546af1 | 115 | void Pulse::direct(const bool& value) { |
gwappa | 12:06ea96546af1 | 116 | out_.write(value? 1:0); |
gwappa | 12:06ea96546af1 | 117 | } |
gwappa | 12:06ea96546af1 | 118 | |
gwappa | 12:06ea96546af1 | 119 | Pulse::Status Pulse::getStatus() |
gwappa | 11:897ecd5413e0 | 120 | { |
gwappa | 11:897ecd5413e0 | 121 | return stat_; |
gwappa | 11:897ecd5413e0 | 122 | } |
gwappa | 11:897ecd5413e0 | 123 | |
gwappa | 12:06ea96546af1 | 124 | void Pulse::wait() |
gwappa | 11:897ecd5413e0 | 125 | { |
gwappa | 11:897ecd5413e0 | 126 | while(stat_ != Rest); |
gwappa | 11:897ecd5413e0 | 127 | } |
gwappa | 13:8ea85a33e37a | 128 | |
gwappa | 11:897ecd5413e0 | 129 |