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