,.
Buffer.cpp@0:9375e70ace0a, 2018-07-27 (annotated)
- Committer:
- alejo5214416
- Date:
- Fri Jul 27 00:31:37 2018 +0000
- Revision:
- 0:9375e70ace0a
Esclavo Maestro
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alejo5214416 | 0:9375e70ace0a | 1 | #include "Buffer.h" |
alejo5214416 | 0:9375e70ace0a | 2 | #include "mbed.h" |
alejo5214416 | 0:9375e70ace0a | 3 | |
alejo5214416 | 0:9375e70ace0a | 4 | int circular_buf_reset(circular_buf_t * cbuf) |
alejo5214416 | 0:9375e70ace0a | 5 | { |
alejo5214416 | 0:9375e70ace0a | 6 | int r = -1; |
alejo5214416 | 0:9375e70ace0a | 7 | |
alejo5214416 | 0:9375e70ace0a | 8 | if(cbuf) { |
alejo5214416 | 0:9375e70ace0a | 9 | cbuf->head = 0; |
alejo5214416 | 0:9375e70ace0a | 10 | cbuf->tail = 0; |
alejo5214416 | 0:9375e70ace0a | 11 | cbuf->size = 0; |
alejo5214416 | 0:9375e70ace0a | 12 | r = 0; |
alejo5214416 | 0:9375e70ace0a | 13 | } |
alejo5214416 | 0:9375e70ace0a | 14 | |
alejo5214416 | 0:9375e70ace0a | 15 | return r; |
alejo5214416 | 0:9375e70ace0a | 16 | |
alejo5214416 | 0:9375e70ace0a | 17 | } |
alejo5214416 | 0:9375e70ace0a | 18 | |
alejo5214416 | 0:9375e70ace0a | 19 | bool circular_buf_empty(circular_buf_t cbuf) |
alejo5214416 | 0:9375e70ace0a | 20 | { |
alejo5214416 | 0:9375e70ace0a | 21 | // We define empty as head == tail |
alejo5214416 | 0:9375e70ace0a | 22 | return (cbuf.head == cbuf.tail); |
alejo5214416 | 0:9375e70ace0a | 23 | } |
alejo5214416 | 0:9375e70ace0a | 24 | |
alejo5214416 | 0:9375e70ace0a | 25 | bool circular_buf_full(circular_buf_t cbuf) |
alejo5214416 | 0:9375e70ace0a | 26 | { |
alejo5214416 | 0:9375e70ace0a | 27 | // We determine "full" case by head being one position behind the tail |
alejo5214416 | 0:9375e70ace0a | 28 | // Note that this means we are wasting one space in the buffer! |
alejo5214416 | 0:9375e70ace0a | 29 | // Instead, you could have an "empty" flag and determine buffer full that way |
alejo5214416 | 0:9375e70ace0a | 30 | return ((cbuf.head + 1) % cbuf.size) == cbuf.tail; |
alejo5214416 | 0:9375e70ace0a | 31 | } |
alejo5214416 | 0:9375e70ace0a | 32 | |
alejo5214416 | 0:9375e70ace0a | 33 | int circular_buf_put(circular_buf_t * cbuf, uint8_t data) |
alejo5214416 | 0:9375e70ace0a | 34 | { |
alejo5214416 | 0:9375e70ace0a | 35 | int r = -1; |
alejo5214416 | 0:9375e70ace0a | 36 | |
alejo5214416 | 0:9375e70ace0a | 37 | if(cbuf) { |
alejo5214416 | 0:9375e70ace0a | 38 | cbuf->buffer[cbuf->head] = data; |
alejo5214416 | 0:9375e70ace0a | 39 | cbuf->head = (cbuf->head + 1) % cbuf->size; |
alejo5214416 | 0:9375e70ace0a | 40 | |
alejo5214416 | 0:9375e70ace0a | 41 | if(cbuf->head == cbuf->tail) { |
alejo5214416 | 0:9375e70ace0a | 42 | cbuf->tail = (cbuf->tail + 1) % cbuf->size; |
alejo5214416 | 0:9375e70ace0a | 43 | } |
alejo5214416 | 0:9375e70ace0a | 44 | |
alejo5214416 | 0:9375e70ace0a | 45 | r = 0; |
alejo5214416 | 0:9375e70ace0a | 46 | } |
alejo5214416 | 0:9375e70ace0a | 47 | |
alejo5214416 | 0:9375e70ace0a | 48 | return r; |
alejo5214416 | 0:9375e70ace0a | 49 | } |
alejo5214416 | 0:9375e70ace0a | 50 | |
alejo5214416 | 0:9375e70ace0a | 51 | int circular_buf_get(circular_buf_t * cbuf, uint8_t * data) |
alejo5214416 | 0:9375e70ace0a | 52 | { |
alejo5214416 | 0:9375e70ace0a | 53 | int r = -1; |
alejo5214416 | 0:9375e70ace0a | 54 | |
alejo5214416 | 0:9375e70ace0a | 55 | if(cbuf && data && !circular_buf_empty(*cbuf)) { |
alejo5214416 | 0:9375e70ace0a | 56 | *data = cbuf->buffer[cbuf->tail]; |
alejo5214416 | 0:9375e70ace0a | 57 | cbuf->tail = (cbuf->tail + 1) % cbuf->size; |
alejo5214416 | 0:9375e70ace0a | 58 | |
alejo5214416 | 0:9375e70ace0a | 59 | r = 0; |
alejo5214416 | 0:9375e70ace0a | 60 | } |
alejo5214416 | 0:9375e70ace0a | 61 | |
alejo5214416 | 0:9375e70ace0a | 62 | return r; |
alejo5214416 | 0:9375e70ace0a | 63 | } |