Keisuke Sehara
/
STM32_Whisking
fast-feedback virtual target task code on STM Nucleo
pulse.h@20:4c06d3041337, 2018-07-02 (annotated)
- Committer:
- gwappa
- Date:
- Mon Jul 02 09:04:45 2018 +0000
- Revision:
- 20:4c06d3041337
- Parent:
- 15:20f7f737c256
try extending Pulse to allow tickering output
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gwappa | 12:06ea96546af1 | 1 | #ifndef PULSE_H_ |
gwappa | 12:06ea96546af1 | 2 | #define PULSE_H_ |
gwappa | 12:06ea96546af1 | 3 | |
gwappa | 12:06ea96546af1 | 4 | #include "mbed.h" |
gwappa | 12:06ea96546af1 | 5 | |
gwappa | 12:06ea96546af1 | 6 | |
gwappa | 12:06ea96546af1 | 7 | /** |
gwappa | 12:06ea96546af1 | 8 | * implementation for turning on the port |
gwappa | 12:06ea96546af1 | 9 | * for a specified duration (using a timer). |
gwappa | 12:06ea96546af1 | 10 | * |
gwappa | 12:06ea96546af1 | 11 | * the state of a Pulse instance follows the |
gwappa | 12:06ea96546af1 | 12 | * sequence below, after calling the run() method: |
gwappa | 12:06ea96546af1 | 13 | * |
gwappa | 12:06ea96546af1 | 14 | * Rest -(user calling: run)-> Armed (output=L) |
gwappa | 12:06ea96546af1 | 15 | * Armed -(after onset: start)-> Active (output=H) |
gwappa | 12:06ea96546af1 | 16 | * Active -(after dur: stop)-> Rest (output=L) |
gwappa | 12:06ea96546af1 | 17 | * |
gwappa | 12:06ea96546af1 | 18 | * Although you can generate a force-transition to the specified state |
gwappa | 12:06ea96546af1 | 19 | * by calling e.g. start() or stop(), it is not a recommended usage. |
gwappa | 12:06ea96546af1 | 20 | */ |
gwappa | 12:06ea96546af1 | 21 | class Pulse |
gwappa | 12:06ea96546af1 | 22 | { |
gwappa | 12:06ea96546af1 | 23 | public: |
gwappa | 12:06ea96546af1 | 24 | enum Status { |
gwappa | 12:06ea96546af1 | 25 | Armed, |
gwappa | 12:06ea96546af1 | 26 | Active, |
gwappa | 12:06ea96546af1 | 27 | Interrupted, |
gwappa | 12:06ea96546af1 | 28 | Rest |
gwappa | 12:06ea96546af1 | 29 | }; |
gwappa | 12:06ea96546af1 | 30 | |
gwappa | 20:4c06d3041337 | 31 | Pulse(PinName pin, const uint64_t& onset_us=0, const uint64_t& duration_us=1000, const uint64_t& blink_us=0); |
gwappa | 20:4c06d3041337 | 32 | |
gwappa | 15:20f7f737c256 | 33 | void setEnabled(const bool& value); |
gwappa | 15:20f7f737c256 | 34 | bool isEnabled(); |
gwappa | 12:06ea96546af1 | 35 | void setOnset(const uint64_t& value_us); |
gwappa | 12:06ea96546af1 | 36 | void setDuration(const uint64_t& value_us); |
gwappa | 20:4c06d3041337 | 37 | void setBlinkDuration(const uint64_t& vaue_us); |
gwappa | 20:4c06d3041337 | 38 | |
gwappa | 12:06ea96546af1 | 39 | void attachTurnOnCallback(Callback<void ()> cb=0); |
gwappa | 12:06ea96546af1 | 40 | void attachTurnOffCallback(Callback<void ()> cb=0); |
gwappa | 12:06ea96546af1 | 41 | void detachTurnOnCallback(); |
gwappa | 12:06ea96546af1 | 42 | void detachTurnOffCallback(); |
gwappa | 12:06ea96546af1 | 43 | |
gwappa | 12:06ea96546af1 | 44 | /** |
gwappa | 12:06ea96546af1 | 45 | * runs the pulse output. |
gwappa | 12:06ea96546af1 | 46 | * |
gwappa | 12:06ea96546af1 | 47 | * the state of this Pulse can be retrieved via getStatus(). |
gwappa | 12:06ea96546af1 | 48 | */ |
gwappa | 12:06ea96546af1 | 49 | void run(); |
gwappa | 12:06ea96546af1 | 50 | |
gwappa | 12:06ea96546af1 | 51 | /** |
gwappa | 12:06ea96546af1 | 52 | * force-start the output. |
gwappa | 12:06ea96546af1 | 53 | */ |
gwappa | 12:06ea96546af1 | 54 | void start(); |
gwappa | 12:06ea96546af1 | 55 | |
gwappa | 12:06ea96546af1 | 56 | /** |
gwappa | 12:06ea96546af1 | 57 | * aborts the output. |
gwappa | 12:06ea96546af1 | 58 | */ |
gwappa | 12:06ea96546af1 | 59 | void stop(); |
gwappa | 12:06ea96546af1 | 60 | |
gwappa | 12:06ea96546af1 | 61 | /** |
gwappa | 12:06ea96546af1 | 62 | * use this output as a normal DigitalOut |
gwappa | 12:06ea96546af1 | 63 | */ |
gwappa | 12:06ea96546af1 | 64 | void direct(const bool& value); |
gwappa | 12:06ea96546af1 | 65 | |
gwappa | 12:06ea96546af1 | 66 | Status getStatus(); |
gwappa | 12:06ea96546af1 | 67 | |
gwappa | 12:06ea96546af1 | 68 | /** |
gwappa | 12:06ea96546af1 | 69 | * busy-wait until its status becomes from Active to Rest. |
gwappa | 12:06ea96546af1 | 70 | * if the status is already at Rest, then it returns immediately. |
gwappa | 12:06ea96546af1 | 71 | */ |
gwappa | 12:06ea96546af1 | 72 | void wait(); |
gwappa | 12:06ea96546af1 | 73 | |
gwappa | 12:06ea96546af1 | 74 | private: |
gwappa | 20:4c06d3041337 | 75 | void blink(); |
gwappa | 12:06ea96546af1 | 76 | |
gwappa | 12:06ea96546af1 | 77 | DigitalOut out_; |
gwappa | 12:06ea96546af1 | 78 | Timeout timer_; |
gwappa | 20:4c06d3041337 | 79 | Ticker blinker_; |
gwappa | 15:20f7f737c256 | 80 | bool enabled_; |
gwappa | 12:06ea96546af1 | 81 | uint64_t onset_; |
gwappa | 12:06ea96546af1 | 82 | uint64_t dur_; |
gwappa | 20:4c06d3041337 | 83 | uint64_t blinkdur_; |
gwappa | 12:06ea96546af1 | 84 | Callback<void ()> turnon_; |
gwappa | 12:06ea96546af1 | 85 | Callback<void ()> turnoff_; |
gwappa | 12:06ea96546af1 | 86 | |
gwappa | 12:06ea96546af1 | 87 | volatile Status stat_; |
gwappa | 12:06ea96546af1 | 88 | }; |
gwappa | 12:06ea96546af1 | 89 | |
gwappa | 12:06ea96546af1 | 90 | #endif |