Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Buffer.cpp
00001 #include "Buffer.h" 00002 #include "mbed.h" 00003 00004 int circular_buf_reset(circular_buf_t * cbuf) 00005 { 00006 int r = -1; 00007 00008 if(cbuf) { 00009 cbuf->head = 0; 00010 cbuf->tail = 0; 00011 cbuf->size = 0; 00012 r = 0; 00013 } 00014 00015 return r; 00016 00017 } 00018 00019 bool circular_buf_empty(circular_buf_t cbuf) 00020 { 00021 // We define empty as head == tail 00022 return (cbuf.head == cbuf.tail); 00023 } 00024 00025 bool circular_buf_full(circular_buf_t cbuf) 00026 { 00027 // We determine "full" case by head being one position behind the tail 00028 // Note that this means we are wasting one space in the buffer! 00029 // Instead, you could have an "empty" flag and determine buffer full that way 00030 return ((cbuf.head + 1) % cbuf.size) == cbuf.tail; 00031 } 00032 00033 int circular_buf_put(circular_buf_t * cbuf, uint8_t data) 00034 { 00035 int r = -1; 00036 00037 if(cbuf) { 00038 cbuf->buffer[cbuf->head] = data; 00039 cbuf->head = (cbuf->head + 1) % cbuf->size; 00040 00041 if(cbuf->head == cbuf->tail) { 00042 cbuf->tail = (cbuf->tail + 1) % cbuf->size; 00043 } 00044 00045 r = 0; 00046 } 00047 00048 return r; 00049 } 00050 00051 int circular_buf_get(circular_buf_t * cbuf, uint8_t * data) 00052 { 00053 int r = -1; 00054 00055 if(cbuf && data && !circular_buf_empty(*cbuf)) { 00056 *data = cbuf->buffer[cbuf->tail]; 00057 cbuf->tail = (cbuf->tail + 1) % cbuf->size; 00058 00059 r = 0; 00060 } 00061 00062 return r; 00063 }
Generated on Fri Jul 29 2022 01:24:56 by
1.7.2