Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1
lypinator 0:bb348c97df44 2 // Suppress deprecation warnings since this whole
lypinator 0:bb348c97df44 3 // class is deprecated already
lypinator 0:bb348c97df44 4 #include "mbed_toolchain.h"
lypinator 0:bb348c97df44 5 #undef MBED_DEPRECATED_SINCE
lypinator 0:bb348c97df44 6 #define MBED_DEPRECATED_SINCE(...)
lypinator 0:bb348c97df44 7
lypinator 0:bb348c97df44 8 #include "platform/CallChain.h"
lypinator 0:bb348c97df44 9 #include "cmsis.h"
lypinator 0:bb348c97df44 10 #include "platform/mbed_critical.h"
lypinator 0:bb348c97df44 11
lypinator 0:bb348c97df44 12 namespace mbed {
lypinator 0:bb348c97df44 13
lypinator 0:bb348c97df44 14 class CallChainLink {
lypinator 0:bb348c97df44 15 public:
lypinator 0:bb348c97df44 16 CallChainLink(): cb(), next(NULL)
lypinator 0:bb348c97df44 17 {
lypinator 0:bb348c97df44 18 // No work to do
lypinator 0:bb348c97df44 19 }
lypinator 0:bb348c97df44 20
lypinator 0:bb348c97df44 21 CallChainLink(Callback<void()> &callback): cb(callback), next(NULL)
lypinator 0:bb348c97df44 22 {
lypinator 0:bb348c97df44 23 // No work to do
lypinator 0:bb348c97df44 24 }
lypinator 0:bb348c97df44 25 Callback<void()> cb;
lypinator 0:bb348c97df44 26 CallChainLink *next;
lypinator 0:bb348c97df44 27 };
lypinator 0:bb348c97df44 28
lypinator 0:bb348c97df44 29 CallChain::CallChain(int size) : _chain(NULL)
lypinator 0:bb348c97df44 30 {
lypinator 0:bb348c97df44 31 // No work to do
lypinator 0:bb348c97df44 32 }
lypinator 0:bb348c97df44 33
lypinator 0:bb348c97df44 34 CallChain::~CallChain()
lypinator 0:bb348c97df44 35 {
lypinator 0:bb348c97df44 36 clear();
lypinator 0:bb348c97df44 37 }
lypinator 0:bb348c97df44 38
lypinator 0:bb348c97df44 39 pFunctionPointer_t CallChain::add(Callback<void()> func)
lypinator 0:bb348c97df44 40 {
lypinator 0:bb348c97df44 41 CallChainLink *new_link = new CallChainLink(func);
lypinator 0:bb348c97df44 42 if (NULL == _chain) {
lypinator 0:bb348c97df44 43 _chain = new_link;
lypinator 0:bb348c97df44 44 return &new_link->cb;
lypinator 0:bb348c97df44 45 }
lypinator 0:bb348c97df44 46
lypinator 0:bb348c97df44 47 CallChainLink *link = _chain;
lypinator 0:bb348c97df44 48 while (true) {
lypinator 0:bb348c97df44 49 if (NULL == link->next) {
lypinator 0:bb348c97df44 50 link->next = new_link;
lypinator 0:bb348c97df44 51 return &new_link->cb;
lypinator 0:bb348c97df44 52 }
lypinator 0:bb348c97df44 53 link = link->next;
lypinator 0:bb348c97df44 54 }
lypinator 0:bb348c97df44 55 }
lypinator 0:bb348c97df44 56
lypinator 0:bb348c97df44 57 pFunctionPointer_t CallChain::add_front(Callback<void()> func)
lypinator 0:bb348c97df44 58 {
lypinator 0:bb348c97df44 59 CallChainLink *link = new CallChainLink(func);
lypinator 0:bb348c97df44 60 link->next = _chain;
lypinator 0:bb348c97df44 61 _chain = link;
lypinator 0:bb348c97df44 62 return &link->cb;
lypinator 0:bb348c97df44 63 }
lypinator 0:bb348c97df44 64
lypinator 0:bb348c97df44 65 int CallChain::size() const
lypinator 0:bb348c97df44 66 {
lypinator 0:bb348c97df44 67 CallChainLink *link = _chain;
lypinator 0:bb348c97df44 68 int elements = 0;
lypinator 0:bb348c97df44 69 while (link != NULL) {
lypinator 0:bb348c97df44 70 elements++;
lypinator 0:bb348c97df44 71 link = link->next;
lypinator 0:bb348c97df44 72 }
lypinator 0:bb348c97df44 73 return elements;
lypinator 0:bb348c97df44 74 }
lypinator 0:bb348c97df44 75
lypinator 0:bb348c97df44 76 pFunctionPointer_t CallChain::get(int idx) const
lypinator 0:bb348c97df44 77 {
lypinator 0:bb348c97df44 78 CallChainLink *link = _chain;
lypinator 0:bb348c97df44 79 for (int i = 0; i < idx; i++) {
lypinator 0:bb348c97df44 80 if (NULL == link) {
lypinator 0:bb348c97df44 81 break;
lypinator 0:bb348c97df44 82 }
lypinator 0:bb348c97df44 83 link = link->next;
lypinator 0:bb348c97df44 84 }
lypinator 0:bb348c97df44 85 return &link->cb;
lypinator 0:bb348c97df44 86 }
lypinator 0:bb348c97df44 87
lypinator 0:bb348c97df44 88 int CallChain::find(pFunctionPointer_t f) const
lypinator 0:bb348c97df44 89 {
lypinator 0:bb348c97df44 90 CallChainLink *link = _chain;
lypinator 0:bb348c97df44 91 int i = 0;
lypinator 0:bb348c97df44 92 while (link != NULL) {
lypinator 0:bb348c97df44 93 if (f == &link->cb) {
lypinator 0:bb348c97df44 94 return i;
lypinator 0:bb348c97df44 95 }
lypinator 0:bb348c97df44 96 i++;
lypinator 0:bb348c97df44 97 link = link->next;
lypinator 0:bb348c97df44 98 }
lypinator 0:bb348c97df44 99 return -1;
lypinator 0:bb348c97df44 100 }
lypinator 0:bb348c97df44 101
lypinator 0:bb348c97df44 102 void CallChain::clear()
lypinator 0:bb348c97df44 103 {
lypinator 0:bb348c97df44 104 CallChainLink *link = _chain;
lypinator 0:bb348c97df44 105 _chain = NULL;
lypinator 0:bb348c97df44 106 while (link != NULL) {
lypinator 0:bb348c97df44 107 CallChainLink *temp = link->next;
lypinator 0:bb348c97df44 108 delete link;
lypinator 0:bb348c97df44 109 link = temp;
lypinator 0:bb348c97df44 110 }
lypinator 0:bb348c97df44 111 }
lypinator 0:bb348c97df44 112
lypinator 0:bb348c97df44 113 bool CallChain::remove(pFunctionPointer_t f)
lypinator 0:bb348c97df44 114 {
lypinator 0:bb348c97df44 115 CallChainLink *link = _chain;
lypinator 0:bb348c97df44 116 while (link != NULL) {
lypinator 0:bb348c97df44 117 if (f == &link->cb) {
lypinator 0:bb348c97df44 118 delete link;
lypinator 0:bb348c97df44 119 return true;
lypinator 0:bb348c97df44 120 }
lypinator 0:bb348c97df44 121 link = link->next;
lypinator 0:bb348c97df44 122 }
lypinator 0:bb348c97df44 123 return false;
lypinator 0:bb348c97df44 124 }
lypinator 0:bb348c97df44 125
lypinator 0:bb348c97df44 126 void CallChain::call()
lypinator 0:bb348c97df44 127 {
lypinator 0:bb348c97df44 128 CallChainLink *link = _chain;
lypinator 0:bb348c97df44 129 while (link != NULL) {
lypinator 0:bb348c97df44 130 link->cb.call();
lypinator 0:bb348c97df44 131 link = link->next;
lypinator 0:bb348c97df44 132 }
lypinator 0:bb348c97df44 133 }
lypinator 0:bb348c97df44 134
lypinator 0:bb348c97df44 135 } // namespace mbed