Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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