dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/hal/common/CallChain.cpp@9:6bb35cef007d, 2016-10-22 (annotated)
- Committer:
- cyberjoey
- Date:
- Sat Oct 22 01:31:58 2016 +0000
- Revision:
- 9:6bb35cef007d
- Parent:
- 1:55a6170b404f
WORKING
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | #include "CallChain.h" |
nexpaq | 1:55a6170b404f | 2 | #include "cmsis.h" |
nexpaq | 1:55a6170b404f | 3 | #include "critical.h" |
nexpaq | 1:55a6170b404f | 4 | |
nexpaq | 1:55a6170b404f | 5 | namespace mbed { |
nexpaq | 1:55a6170b404f | 6 | |
nexpaq | 1:55a6170b404f | 7 | class CallChainLink { |
nexpaq | 1:55a6170b404f | 8 | public: |
nexpaq | 1:55a6170b404f | 9 | CallChainLink(): cb(), next(NULL) { |
nexpaq | 1:55a6170b404f | 10 | // No work to do |
nexpaq | 1:55a6170b404f | 11 | } |
nexpaq | 1:55a6170b404f | 12 | |
nexpaq | 1:55a6170b404f | 13 | CallChainLink(Callback<void()> &callback): cb(callback), next(NULL) { |
nexpaq | 1:55a6170b404f | 14 | // No work to do |
nexpaq | 1:55a6170b404f | 15 | } |
nexpaq | 1:55a6170b404f | 16 | Callback<void()> cb; |
nexpaq | 1:55a6170b404f | 17 | CallChainLink * next; |
nexpaq | 1:55a6170b404f | 18 | }; |
nexpaq | 1:55a6170b404f | 19 | |
nexpaq | 1:55a6170b404f | 20 | CallChain::CallChain(int size) : _chain(NULL) { |
nexpaq | 1:55a6170b404f | 21 | // No work to do |
nexpaq | 1:55a6170b404f | 22 | } |
nexpaq | 1:55a6170b404f | 23 | |
nexpaq | 1:55a6170b404f | 24 | CallChain::~CallChain() { |
nexpaq | 1:55a6170b404f | 25 | clear(); |
nexpaq | 1:55a6170b404f | 26 | } |
nexpaq | 1:55a6170b404f | 27 | |
nexpaq | 1:55a6170b404f | 28 | pFunctionPointer_t CallChain::add(Callback<void()> func) { |
nexpaq | 1:55a6170b404f | 29 | CallChainLink *new_link = new CallChainLink(func); |
nexpaq | 1:55a6170b404f | 30 | if (NULL == _chain) { |
nexpaq | 1:55a6170b404f | 31 | _chain = new_link; |
nexpaq | 1:55a6170b404f | 32 | return &new_link->cb; |
nexpaq | 1:55a6170b404f | 33 | } |
nexpaq | 1:55a6170b404f | 34 | |
nexpaq | 1:55a6170b404f | 35 | CallChainLink *link = _chain; |
nexpaq | 1:55a6170b404f | 36 | while (true) { |
nexpaq | 1:55a6170b404f | 37 | if (NULL == link->next) { |
nexpaq | 1:55a6170b404f | 38 | link->next = new_link; |
nexpaq | 1:55a6170b404f | 39 | return &new_link->cb; |
nexpaq | 1:55a6170b404f | 40 | } |
nexpaq | 1:55a6170b404f | 41 | link = link->next; |
nexpaq | 1:55a6170b404f | 42 | } |
nexpaq | 1:55a6170b404f | 43 | } |
nexpaq | 1:55a6170b404f | 44 | |
nexpaq | 1:55a6170b404f | 45 | pFunctionPointer_t CallChain::add_front(Callback<void()> func) { |
nexpaq | 1:55a6170b404f | 46 | CallChainLink *link = new CallChainLink(func); |
nexpaq | 1:55a6170b404f | 47 | link->next = _chain; |
nexpaq | 1:55a6170b404f | 48 | _chain = link->next; |
nexpaq | 1:55a6170b404f | 49 | return &link->cb; |
nexpaq | 1:55a6170b404f | 50 | } |
nexpaq | 1:55a6170b404f | 51 | |
nexpaq | 1:55a6170b404f | 52 | int CallChain::size() const { |
nexpaq | 1:55a6170b404f | 53 | CallChainLink *link = _chain; |
nexpaq | 1:55a6170b404f | 54 | int elements = 0; |
nexpaq | 1:55a6170b404f | 55 | while (link != NULL) { |
nexpaq | 1:55a6170b404f | 56 | elements++; |
nexpaq | 1:55a6170b404f | 57 | link = link->next; |
nexpaq | 1:55a6170b404f | 58 | } |
nexpaq | 1:55a6170b404f | 59 | return elements; |
nexpaq | 1:55a6170b404f | 60 | } |
nexpaq | 1:55a6170b404f | 61 | |
nexpaq | 1:55a6170b404f | 62 | pFunctionPointer_t CallChain::get(int idx) const { |
nexpaq | 1:55a6170b404f | 63 | CallChainLink *link = _chain; |
nexpaq | 1:55a6170b404f | 64 | for (int i = 0; i < idx; i++) { |
nexpaq | 1:55a6170b404f | 65 | if (NULL == link) { |
nexpaq | 1:55a6170b404f | 66 | break; |
nexpaq | 1:55a6170b404f | 67 | } |
nexpaq | 1:55a6170b404f | 68 | link = link->next; |
nexpaq | 1:55a6170b404f | 69 | } |
nexpaq | 1:55a6170b404f | 70 | return &link->cb; |
nexpaq | 1:55a6170b404f | 71 | } |
nexpaq | 1:55a6170b404f | 72 | |
nexpaq | 1:55a6170b404f | 73 | int CallChain::find(pFunctionPointer_t f) const { |
nexpaq | 1:55a6170b404f | 74 | CallChainLink *link = _chain; |
nexpaq | 1:55a6170b404f | 75 | int i = 0; |
nexpaq | 1:55a6170b404f | 76 | while (link != NULL) { |
nexpaq | 1:55a6170b404f | 77 | if (f == &link->cb) { |
nexpaq | 1:55a6170b404f | 78 | return i; |
nexpaq | 1:55a6170b404f | 79 | } |
nexpaq | 1:55a6170b404f | 80 | i++; |
nexpaq | 1:55a6170b404f | 81 | link = link->next; |
nexpaq | 1:55a6170b404f | 82 | } |
nexpaq | 1:55a6170b404f | 83 | return -1; |
nexpaq | 1:55a6170b404f | 84 | } |
nexpaq | 1:55a6170b404f | 85 | |
nexpaq | 1:55a6170b404f | 86 | void CallChain::clear() { |
nexpaq | 1:55a6170b404f | 87 | CallChainLink *link = _chain; |
nexpaq | 1:55a6170b404f | 88 | _chain = NULL; |
nexpaq | 1:55a6170b404f | 89 | while (link != NULL) { |
nexpaq | 1:55a6170b404f | 90 | CallChainLink *temp = link->next; |
nexpaq | 1:55a6170b404f | 91 | delete link; |
nexpaq | 1:55a6170b404f | 92 | link = temp; |
nexpaq | 1:55a6170b404f | 93 | } |
nexpaq | 1:55a6170b404f | 94 | } |
nexpaq | 1:55a6170b404f | 95 | |
nexpaq | 1:55a6170b404f | 96 | bool CallChain::remove(pFunctionPointer_t f) { |
nexpaq | 1:55a6170b404f | 97 | CallChainLink *link = _chain; |
nexpaq | 1:55a6170b404f | 98 | while (link != NULL) { |
nexpaq | 1:55a6170b404f | 99 | if (f == &link->cb) { |
nexpaq | 1:55a6170b404f | 100 | delete link; |
nexpaq | 1:55a6170b404f | 101 | return true; |
nexpaq | 1:55a6170b404f | 102 | } |
nexpaq | 1:55a6170b404f | 103 | link = link->next; |
nexpaq | 1:55a6170b404f | 104 | } |
nexpaq | 1:55a6170b404f | 105 | return false; |
nexpaq | 1:55a6170b404f | 106 | } |
nexpaq | 1:55a6170b404f | 107 | |
nexpaq | 1:55a6170b404f | 108 | void CallChain::call() { |
nexpaq | 1:55a6170b404f | 109 | CallChainLink *link = _chain; |
nexpaq | 1:55a6170b404f | 110 | while (link != NULL) { |
nexpaq | 1:55a6170b404f | 111 | link->cb.call(); |
nexpaq | 1:55a6170b404f | 112 | link = link->next; |
nexpaq | 1:55a6170b404f | 113 | } |
nexpaq | 1:55a6170b404f | 114 | } |
nexpaq | 1:55a6170b404f | 115 | |
nexpaq | 1:55a6170b404f | 116 | } // namespace mbed |