Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/RingBuffer.h@0:31e91bb0ef3c, 2012-07-31 (annotated)
- Committer:
- scachat
- Date:
- Tue Jul 31 21:11:18 2012 +0000
- Revision:
- 0:31e91bb0ef3c
- Child:
- 2:1df0b61d3b5a
smoothie port to mbed online compiler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scachat | 0:31e91bb0ef3c | 1 | /* |
scachat | 0:31e91bb0ef3c | 2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
scachat | 0:31e91bb0ef3c | 6 | |
scachat | 0:31e91bb0ef3c | 7 | With chucks taken from http://en.wikipedia.org/wiki/Circular_buffer, see licence there also |
scachat | 0:31e91bb0ef3c | 8 | */ |
scachat | 0:31e91bb0ef3c | 9 | |
scachat | 0:31e91bb0ef3c | 10 | #ifndef RINGBUFFER_H |
scachat | 0:31e91bb0ef3c | 11 | #define RINGBUFFER_H |
scachat | 0:31e91bb0ef3c | 12 | |
scachat | 0:31e91bb0ef3c | 13 | |
scachat | 0:31e91bb0ef3c | 14 | template<class kind, int length> class RingBuffer { |
scachat | 0:31e91bb0ef3c | 15 | public: |
scachat | 0:31e91bb0ef3c | 16 | RingBuffer(); |
scachat | 0:31e91bb0ef3c | 17 | int size(); |
scachat | 0:31e91bb0ef3c | 18 | int capacity(); |
scachat | 0:31e91bb0ef3c | 19 | int next_block_index(int index); |
scachat | 0:31e91bb0ef3c | 20 | int prev_block_index(int index); |
scachat | 0:31e91bb0ef3c | 21 | void push_back(kind object); |
scachat | 0:31e91bb0ef3c | 22 | void pop_front(kind &object); |
scachat | 0:31e91bb0ef3c | 23 | void get( int index, kind &object); |
scachat | 0:31e91bb0ef3c | 24 | kind* get_ref( int index); |
scachat | 0:31e91bb0ef3c | 25 | void delete_first(); |
scachat | 0:31e91bb0ef3c | 26 | |
scachat | 0:31e91bb0ef3c | 27 | kind buffer[length]; |
scachat | 0:31e91bb0ef3c | 28 | int head; |
scachat | 0:31e91bb0ef3c | 29 | int tail; |
scachat | 0:31e91bb0ef3c | 30 | }; |
scachat | 0:31e91bb0ef3c | 31 | |
scachat | 0:31e91bb0ef3c | 32 | |
scachat | 0:31e91bb0ef3c | 33 | template<class kind, int length> RingBuffer<kind, length>::RingBuffer(){ |
scachat | 0:31e91bb0ef3c | 34 | this->head = this->tail = 0; |
scachat | 0:31e91bb0ef3c | 35 | } |
scachat | 0:31e91bb0ef3c | 36 | |
scachat | 0:31e91bb0ef3c | 37 | template<class kind, int length> int RingBuffer<kind, length>::capacity(){ |
scachat | 0:31e91bb0ef3c | 38 | return length-1; |
scachat | 0:31e91bb0ef3c | 39 | } |
scachat | 0:31e91bb0ef3c | 40 | |
scachat | 0:31e91bb0ef3c | 41 | template<class kind, int length> int RingBuffer<kind, length>::size(){ |
scachat | 0:31e91bb0ef3c | 42 | return((this->head>this->tail)?length:0)+this->tail-head; |
scachat | 0:31e91bb0ef3c | 43 | } |
scachat | 0:31e91bb0ef3c | 44 | |
scachat | 0:31e91bb0ef3c | 45 | template<class kind, int length> int RingBuffer<kind, length>::next_block_index(int index){ |
scachat | 0:31e91bb0ef3c | 46 | index++; |
scachat | 0:31e91bb0ef3c | 47 | if (index == length) { index = 0; } |
scachat | 0:31e91bb0ef3c | 48 | return(index); |
scachat | 0:31e91bb0ef3c | 49 | } |
scachat | 0:31e91bb0ef3c | 50 | |
scachat | 0:31e91bb0ef3c | 51 | template<class kind, int length> int RingBuffer<kind, length>::prev_block_index(int index){ |
scachat | 0:31e91bb0ef3c | 52 | if (index == 0) { index = length; } |
scachat | 0:31e91bb0ef3c | 53 | index--; |
scachat | 0:31e91bb0ef3c | 54 | return(index); |
scachat | 0:31e91bb0ef3c | 55 | } |
scachat | 0:31e91bb0ef3c | 56 | |
scachat | 0:31e91bb0ef3c | 57 | template<class kind, int length> void RingBuffer<kind, length>::push_back(kind object){ |
scachat | 0:31e91bb0ef3c | 58 | this->buffer[this->tail] = object; |
scachat | 0:31e91bb0ef3c | 59 | this->tail = (tail+1)&(length-1); |
scachat | 0:31e91bb0ef3c | 60 | } |
scachat | 0:31e91bb0ef3c | 61 | |
scachat | 0:31e91bb0ef3c | 62 | template<class kind, int length> void RingBuffer<kind, length>::get(int index, kind &object){ |
scachat | 0:31e91bb0ef3c | 63 | int j= 0; |
scachat | 0:31e91bb0ef3c | 64 | int k= this->head; |
scachat | 0:31e91bb0ef3c | 65 | while (k != this->tail){ |
scachat | 0:31e91bb0ef3c | 66 | if (j == index) break; |
scachat | 0:31e91bb0ef3c | 67 | j++; |
scachat | 0:31e91bb0ef3c | 68 | k= (k + 1) & (length - 1); |
scachat | 0:31e91bb0ef3c | 69 | } |
scachat | 0:31e91bb0ef3c | 70 | if (k == this->tail){ |
scachat | 0:31e91bb0ef3c | 71 | //return NULL; |
scachat | 0:31e91bb0ef3c | 72 | } |
scachat | 0:31e91bb0ef3c | 73 | object = this->buffer[k]; |
scachat | 0:31e91bb0ef3c | 74 | } |
scachat | 0:31e91bb0ef3c | 75 | |
scachat | 0:31e91bb0ef3c | 76 | |
scachat | 0:31e91bb0ef3c | 77 | template<class kind, int length> kind* RingBuffer<kind, length>::get_ref(int index){ |
scachat | 0:31e91bb0ef3c | 78 | int j= 0; |
scachat | 0:31e91bb0ef3c | 79 | int k= this->head; |
scachat | 0:31e91bb0ef3c | 80 | while (k != this->tail){ |
scachat | 0:31e91bb0ef3c | 81 | if (j == index) break; |
scachat | 0:31e91bb0ef3c | 82 | j++; |
scachat | 0:31e91bb0ef3c | 83 | k= (k + 1) & (length - 1); |
scachat | 0:31e91bb0ef3c | 84 | } |
scachat | 0:31e91bb0ef3c | 85 | if (k == this->tail){ |
scachat | 0:31e91bb0ef3c | 86 | return NULL; |
scachat | 0:31e91bb0ef3c | 87 | } |
scachat | 0:31e91bb0ef3c | 88 | return &(this->buffer[k]); |
scachat | 0:31e91bb0ef3c | 89 | } |
scachat | 0:31e91bb0ef3c | 90 | |
scachat | 0:31e91bb0ef3c | 91 | template<class kind, int length> void RingBuffer<kind, length>::pop_front(kind &object){ |
scachat | 0:31e91bb0ef3c | 92 | object = this->buffer[this->head]; |
scachat | 0:31e91bb0ef3c | 93 | this->head = (this->head+1)&(length-1); |
scachat | 0:31e91bb0ef3c | 94 | } |
scachat | 0:31e91bb0ef3c | 95 | |
scachat | 0:31e91bb0ef3c | 96 | template<class kind, int length> void RingBuffer<kind, length>::delete_first(){ |
scachat | 0:31e91bb0ef3c | 97 | //kind dummy; |
scachat | 0:31e91bb0ef3c | 98 | //this->pop_front(dummy); |
scachat | 0:31e91bb0ef3c | 99 | this->head = (this->head+1)&(length-1); |
scachat | 0:31e91bb0ef3c | 100 | } |
scachat | 0:31e91bb0ef3c | 101 | |
scachat | 0:31e91bb0ef3c | 102 | |
scachat | 0:31e91bb0ef3c | 103 | #endif |