mbed.h library with any bug fixes AV finds.
Dependents: micromouse4_encoder_testing PID_Test Lab1_Test WorkingPID ... more
common/CallChain.cpp@0:ba7650f404af, 2015-11-02 (annotated)
- Committer:
- aravindsv
- Date:
- Mon Nov 02 02:26:59 2015 +0000
- Revision:
- 0:ba7650f404af
Reduced HSE_STARTUP_TIMEOUT to 500 ms, fixed some compiler warnings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aravindsv | 0:ba7650f404af | 1 | #include "CallChain.h" |
aravindsv | 0:ba7650f404af | 2 | #include "cmsis.h" |
aravindsv | 0:ba7650f404af | 3 | |
aravindsv | 0:ba7650f404af | 4 | namespace mbed { |
aravindsv | 0:ba7650f404af | 5 | |
aravindsv | 0:ba7650f404af | 6 | CallChain::CallChain(int size) : _chain(), _size(size), _elements(0) { |
aravindsv | 0:ba7650f404af | 7 | _chain = new pFunctionPointer_t[size](); |
aravindsv | 0:ba7650f404af | 8 | } |
aravindsv | 0:ba7650f404af | 9 | |
aravindsv | 0:ba7650f404af | 10 | CallChain::~CallChain() { |
aravindsv | 0:ba7650f404af | 11 | clear(); |
aravindsv | 0:ba7650f404af | 12 | delete _chain; |
aravindsv | 0:ba7650f404af | 13 | } |
aravindsv | 0:ba7650f404af | 14 | |
aravindsv | 0:ba7650f404af | 15 | pFunctionPointer_t CallChain::add(void (*function)(void)) { |
aravindsv | 0:ba7650f404af | 16 | return common_add(new FunctionPointer(function)); |
aravindsv | 0:ba7650f404af | 17 | } |
aravindsv | 0:ba7650f404af | 18 | |
aravindsv | 0:ba7650f404af | 19 | pFunctionPointer_t CallChain::add_front(void (*function)(void)) { |
aravindsv | 0:ba7650f404af | 20 | return common_add_front(new FunctionPointer(function)); |
aravindsv | 0:ba7650f404af | 21 | } |
aravindsv | 0:ba7650f404af | 22 | |
aravindsv | 0:ba7650f404af | 23 | int CallChain::size() const { |
aravindsv | 0:ba7650f404af | 24 | return _elements; |
aravindsv | 0:ba7650f404af | 25 | } |
aravindsv | 0:ba7650f404af | 26 | |
aravindsv | 0:ba7650f404af | 27 | pFunctionPointer_t CallChain::get(int i) const { |
aravindsv | 0:ba7650f404af | 28 | if (i < 0 || i >= _elements) |
aravindsv | 0:ba7650f404af | 29 | return NULL; |
aravindsv | 0:ba7650f404af | 30 | return _chain[i]; |
aravindsv | 0:ba7650f404af | 31 | } |
aravindsv | 0:ba7650f404af | 32 | |
aravindsv | 0:ba7650f404af | 33 | int CallChain::find(pFunctionPointer_t f) const { |
aravindsv | 0:ba7650f404af | 34 | for (int i = 0; i < _elements; i++) |
aravindsv | 0:ba7650f404af | 35 | if (f == _chain[i]) |
aravindsv | 0:ba7650f404af | 36 | return i; |
aravindsv | 0:ba7650f404af | 37 | return -1; |
aravindsv | 0:ba7650f404af | 38 | } |
aravindsv | 0:ba7650f404af | 39 | |
aravindsv | 0:ba7650f404af | 40 | void CallChain::clear() { |
aravindsv | 0:ba7650f404af | 41 | for(int i = 0; i < _elements; i ++) { |
aravindsv | 0:ba7650f404af | 42 | delete _chain[i]; |
aravindsv | 0:ba7650f404af | 43 | _chain[i] = NULL; |
aravindsv | 0:ba7650f404af | 44 | } |
aravindsv | 0:ba7650f404af | 45 | _elements = 0; |
aravindsv | 0:ba7650f404af | 46 | } |
aravindsv | 0:ba7650f404af | 47 | |
aravindsv | 0:ba7650f404af | 48 | bool CallChain::remove(pFunctionPointer_t f) { |
aravindsv | 0:ba7650f404af | 49 | int i; |
aravindsv | 0:ba7650f404af | 50 | |
aravindsv | 0:ba7650f404af | 51 | if ((i = find(f)) == -1) |
aravindsv | 0:ba7650f404af | 52 | return false; |
aravindsv | 0:ba7650f404af | 53 | if (i != _elements - 1) |
aravindsv | 0:ba7650f404af | 54 | memmove(_chain + i, _chain + i + 1, (_elements - i - 1) * sizeof(pFunctionPointer_t)); |
aravindsv | 0:ba7650f404af | 55 | delete f; |
aravindsv | 0:ba7650f404af | 56 | _elements --; |
aravindsv | 0:ba7650f404af | 57 | return true; |
aravindsv | 0:ba7650f404af | 58 | } |
aravindsv | 0:ba7650f404af | 59 | |
aravindsv | 0:ba7650f404af | 60 | void CallChain::call() { |
aravindsv | 0:ba7650f404af | 61 | for(int i = 0; i < _elements; i++) |
aravindsv | 0:ba7650f404af | 62 | _chain[i]->call(); |
aravindsv | 0:ba7650f404af | 63 | } |
aravindsv | 0:ba7650f404af | 64 | |
aravindsv | 0:ba7650f404af | 65 | void CallChain::_check_size() { |
aravindsv | 0:ba7650f404af | 66 | if (_elements < _size) |
aravindsv | 0:ba7650f404af | 67 | return; |
aravindsv | 0:ba7650f404af | 68 | _size = (_size < 4) ? 4 : _size + 4; |
aravindsv | 0:ba7650f404af | 69 | pFunctionPointer_t* new_chain = new pFunctionPointer_t[_size](); |
aravindsv | 0:ba7650f404af | 70 | memcpy(new_chain, _chain, _elements * sizeof(pFunctionPointer_t)); |
aravindsv | 0:ba7650f404af | 71 | delete _chain; |
aravindsv | 0:ba7650f404af | 72 | _chain = new_chain; |
aravindsv | 0:ba7650f404af | 73 | } |
aravindsv | 0:ba7650f404af | 74 | |
aravindsv | 0:ba7650f404af | 75 | pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) { |
aravindsv | 0:ba7650f404af | 76 | _check_size(); |
aravindsv | 0:ba7650f404af | 77 | _chain[_elements] = pf; |
aravindsv | 0:ba7650f404af | 78 | _elements ++; |
aravindsv | 0:ba7650f404af | 79 | return pf; |
aravindsv | 0:ba7650f404af | 80 | } |
aravindsv | 0:ba7650f404af | 81 | |
aravindsv | 0:ba7650f404af | 82 | pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) { |
aravindsv | 0:ba7650f404af | 83 | _check_size(); |
aravindsv | 0:ba7650f404af | 84 | memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); |
aravindsv | 0:ba7650f404af | 85 | _chain[0] = pf; |
aravindsv | 0:ba7650f404af | 86 | _elements ++; |
aravindsv | 0:ba7650f404af | 87 | return pf; |
aravindsv | 0:ba7650f404af | 88 | } |
aravindsv | 0:ba7650f404af | 89 | |
aravindsv | 0:ba7650f404af | 90 | } // namespace mbed |