PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
6:ea7377f3d1af
direct lcd stuff used by sensitive

Who changed what in which revision?

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