Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Committer:
Michael J. Spencer
Date:
Fri Feb 28 18:52:52 2014 -0800
Revision:
2:1df0b61d3b5a
Parent:
0:31e91bb0ef3c
Child:
3:f151d08d335c
Update to latest Smoothie.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michael J. Spencer 2:1df0b61d3b5a 1 /*
Michael J. Spencer 2:1df0b61d3b5a 2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
Michael J. Spencer 2:1df0b61d3b5a 3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Michael J. Spencer 2:1df0b61d3b5a 4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Michael J. Spencer 2:1df0b61d3b5a 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
Michael J. Spencer 2:1df0b61d3b5a 6
Michael J. Spencer 2:1df0b61d3b5a 7 With chucks taken from http://en.wikipedia.org/wiki/Circular_buffer, see licence there also
Michael J. Spencer 2:1df0b61d3b5a 8 */
Michael J. Spencer 2:1df0b61d3b5a 9
Michael J. Spencer 2:1df0b61d3b5a 10 #ifndef RINGBUFFER_H
Michael J. Spencer 2:1df0b61d3b5a 11 #define RINGBUFFER_H
Michael J. Spencer 2:1df0b61d3b5a 12
Michael J. Spencer 2:1df0b61d3b5a 13
Michael J. Spencer 2:1df0b61d3b5a 14 template<class kind, int length> class RingBuffer {
Michael J. Spencer 2:1df0b61d3b5a 15 public:
Michael J. Spencer 2:1df0b61d3b5a 16 RingBuffer();
Michael J. Spencer 2:1df0b61d3b5a 17 int size();
Michael J. Spencer 2:1df0b61d3b5a 18 int capacity();
Michael J. Spencer 2:1df0b61d3b5a 19 int next_block_index(int index);
Michael J. Spencer 2:1df0b61d3b5a 20 int prev_block_index(int index);
Michael J. Spencer 2:1df0b61d3b5a 21 void push_back(kind object);
Michael J. Spencer 2:1df0b61d3b5a 22 void pop_front(kind &object);
Michael J. Spencer 2:1df0b61d3b5a 23 kind* get_head_ref();
Michael J. Spencer 2:1df0b61d3b5a 24 kind* get_tail_ref();
Michael J. Spencer 2:1df0b61d3b5a 25 void get( int index, kind &object);
Michael J. Spencer 2:1df0b61d3b5a 26 kind* get_ref( int index);
Michael J. Spencer 2:1df0b61d3b5a 27 void delete_tail();
Michael J. Spencer 2:1df0b61d3b5a 28
Michael J. Spencer 2:1df0b61d3b5a 29 kind buffer[length];
Michael J. Spencer 2:1df0b61d3b5a 30 volatile int tail;
Michael J. Spencer 2:1df0b61d3b5a 31 volatile int head;
Michael J. Spencer 2:1df0b61d3b5a 32 };
Michael J. Spencer 2:1df0b61d3b5a 33
Michael J. Spencer 2:1df0b61d3b5a 34 #include "sLPC17xx.h"
Michael J. Spencer 2:1df0b61d3b5a 35
Michael J. Spencer 2:1df0b61d3b5a 36 template<class kind, int length> RingBuffer<kind, length>::RingBuffer(){
Michael J. Spencer 2:1df0b61d3b5a 37 this->tail = this->head = 0;
Michael J. Spencer 2:1df0b61d3b5a 38 }
Michael J. Spencer 2:1df0b61d3b5a 39
Michael J. Spencer 2:1df0b61d3b5a 40 template<class kind, int length> int RingBuffer<kind, length>::capacity(){
Michael J. Spencer 2:1df0b61d3b5a 41 return length-1;
Michael J. Spencer 2:1df0b61d3b5a 42 }
Michael J. Spencer 2:1df0b61d3b5a 43
Michael J. Spencer 2:1df0b61d3b5a 44 template<class kind, int length> int RingBuffer<kind, length>::size(){
Michael J. Spencer 2:1df0b61d3b5a 45 //return((this->tail>this->head)?length:0)+this->head-tail;
Michael J. Spencer 2:1df0b61d3b5a 46 __disable_irq();
Michael J. Spencer 2:1df0b61d3b5a 47 int i = head - tail + ((tail > head)?length:0);
Michael J. Spencer 2:1df0b61d3b5a 48 __enable_irq();
Michael J. Spencer 2:1df0b61d3b5a 49 return i;
Michael J. Spencer 2:1df0b61d3b5a 50 }
Michael J. Spencer 2:1df0b61d3b5a 51
Michael J. Spencer 2:1df0b61d3b5a 52 template<class kind, int length> int RingBuffer<kind, length>::next_block_index(int index){
Michael J. Spencer 2:1df0b61d3b5a 53 index++;
Michael J. Spencer 2:1df0b61d3b5a 54 if (index == length) { index = 0; }
Michael J. Spencer 2:1df0b61d3b5a 55 return(index);
Michael J. Spencer 2:1df0b61d3b5a 56 }
Michael J. Spencer 2:1df0b61d3b5a 57
Michael J. Spencer 2:1df0b61d3b5a 58 template<class kind, int length> int RingBuffer<kind, length>::prev_block_index(int index){
Michael J. Spencer 2:1df0b61d3b5a 59 if (index == 0) { index = length; }
Michael J. Spencer 2:1df0b61d3b5a 60 index--;
Michael J. Spencer 2:1df0b61d3b5a 61 return(index);
Michael J. Spencer 2:1df0b61d3b5a 62 }
Michael J. Spencer 2:1df0b61d3b5a 63
Michael J. Spencer 2:1df0b61d3b5a 64 template<class kind, int length> void RingBuffer<kind, length>::push_back(kind object){
Michael J. Spencer 2:1df0b61d3b5a 65 this->buffer[this->head] = object;
Michael J. Spencer 2:1df0b61d3b5a 66 this->head = (head+1)&(length-1);
Michael J. Spencer 2:1df0b61d3b5a 67 }
Michael J. Spencer 2:1df0b61d3b5a 68
Michael J. Spencer 2:1df0b61d3b5a 69 template<class kind, int length> kind* RingBuffer<kind, length>::get_head_ref(){
Michael J. Spencer 2:1df0b61d3b5a 70 return &(buffer[head]);
Michael J. Spencer 2:1df0b61d3b5a 71 }
Michael J. Spencer 2:1df0b61d3b5a 72
Michael J. Spencer 2:1df0b61d3b5a 73 template<class kind, int length> kind* RingBuffer<kind, length>::get_tail_ref(){
Michael J. Spencer 2:1df0b61d3b5a 74 return &(buffer[tail]);
Michael J. Spencer 2:1df0b61d3b5a 75 }
Michael J. Spencer 2:1df0b61d3b5a 76
Michael J. Spencer 2:1df0b61d3b5a 77 template<class kind, int length> void RingBuffer<kind, length>::get(int index, kind &object){
Michael J. Spencer 2:1df0b61d3b5a 78 int j= 0;
Michael J. Spencer 2:1df0b61d3b5a 79 int k= this->tail;
Michael J. Spencer 2:1df0b61d3b5a 80 while (k != this->head){
Michael J. Spencer 2:1df0b61d3b5a 81 if (j == index) break;
Michael J. Spencer 2:1df0b61d3b5a 82 j++;
Michael J. Spencer 2:1df0b61d3b5a 83 k= (k + 1) & (length - 1);
Michael J. Spencer 2:1df0b61d3b5a 84 }
Michael J. Spencer 2:1df0b61d3b5a 85 // TODO : this checks wether we are asked a value out of range
Michael J. Spencer 2:1df0b61d3b5a 86 //if (k == this->head){
Michael J. Spencer 2:1df0b61d3b5a 87 // return NULL;
Michael J. Spencer 2:1df0b61d3b5a 88 //}
Michael J. Spencer 2:1df0b61d3b5a 89 object = this->buffer[k];
Michael J. Spencer 2:1df0b61d3b5a 90 }
Michael J. Spencer 2:1df0b61d3b5a 91
Michael J. Spencer 2:1df0b61d3b5a 92
Michael J. Spencer 2:1df0b61d3b5a 93 template<class kind, int length> kind* RingBuffer<kind, length>::get_ref(int index){
Michael J. Spencer 2:1df0b61d3b5a 94 int j= 0;
Michael J. Spencer 2:1df0b61d3b5a 95 int k= this->tail;
Michael J. Spencer 2:1df0b61d3b5a 96 while (k != this->head){
Michael J. Spencer 2:1df0b61d3b5a 97 if (j == index) break;
Michael J. Spencer 2:1df0b61d3b5a 98 j++;
Michael J. Spencer 2:1df0b61d3b5a 99 k= (k + 1) & (length - 1);
Michael J. Spencer 2:1df0b61d3b5a 100 }
Michael J. Spencer 2:1df0b61d3b5a 101 // TODO : this checks wether we are asked a value out of range
Michael J. Spencer 2:1df0b61d3b5a 102 if (k == this->head){
Michael J. Spencer 2:1df0b61d3b5a 103 return NULL;
Michael J. Spencer 2:1df0b61d3b5a 104 }
Michael J. Spencer 2:1df0b61d3b5a 105 return &(this->buffer[k]);
Michael J. Spencer 2:1df0b61d3b5a 106 }
Michael J. Spencer 2:1df0b61d3b5a 107
Michael J. Spencer 2:1df0b61d3b5a 108 template<class kind, int length> void RingBuffer<kind, length>::pop_front(kind &object){
Michael J. Spencer 2:1df0b61d3b5a 109 object = this->buffer[this->tail];
Michael J. Spencer 2:1df0b61d3b5a 110 this->tail = (this->tail+1)&(length-1);
Michael J. Spencer 2:1df0b61d3b5a 111 }
Michael J. Spencer 2:1df0b61d3b5a 112
Michael J. Spencer 2:1df0b61d3b5a 113 template<class kind, int length> void RingBuffer<kind, length>::delete_tail(){
Michael J. Spencer 2:1df0b61d3b5a 114 //kind dummy;
Michael J. Spencer 2:1df0b61d3b5a 115 //this->pop_front(dummy);
Michael J. Spencer 2:1df0b61d3b5a 116 this->tail = (this->tail+1)&(length-1);
Michael J. Spencer 2:1df0b61d3b5a 117 }
Michael J. Spencer 2:1df0b61d3b5a 118
Michael J. Spencer 2:1df0b61d3b5a 119
Michael J. Spencer 2:1df0b61d3b5a 120 #endif