MeterLogger
ringbuf.c
Go to the documentation of this file.
1 /**
2 * \file
3 * Ring Buffer library
4 */
5 
6 #include "ringbuf.h"
7 
8 
9 /**
10 * \brief init a RINGBUF object
11 * \param r pointer to a RINGBUF object
12 * \param buf pointer to a byte array
13 * \param size size of buf
14 * \return 0 if successfull, otherwise failed
15 */
17 {
18  if(r == NULL || buf == NULL || size < 2) return -1;
19 
20  r->p_o = r->p_r = r->p_w = buf;
21  r->fill_cnt = 0;
22  r->size = size;
23 
24  return 0;
25 }
26 /**
27 * \brief put a character into ring buffer
28 * \param r pointer to a ringbuf object
29 * \param c character to be put
30 * \return 0 if successfull, otherwise failed
31 */
33 {
34  if(r->fill_cnt>=r->size)return -1; // ring buffer is full, this should be atomic operation
35 
36 
37  r->fill_cnt++; // increase filled slots count, this should be atomic operation
38 
39 
40  *r->p_w++ = c; // put character into buffer
41 
42  if(r->p_w >= r->p_o + r->size) // rollback if write pointer go pass
43  r->p_w = r->p_o; // the physical boundary
44 
45  return 0;
46 }
47 /**
48 * \brief get a character from ring buffer
49 * \param r pointer to a ringbuf object
50 * \param c read character
51 * \return 0 if successfull, otherwise failed
52 */
54 {
55  if(r->fill_cnt<=0)return -1; // ring buffer is empty, this should be atomic operation
56 
57 
58  r->fill_cnt--; // decrease filled slots count
59 
60 
61  *c = *r->p_r++; // get the character out
62 
63  if(r->p_r >= r->p_o + r->size) // rollback if write pointer go pass
64  r->p_r = r->p_o; // the physical boundary
65 
66  return 0;
67 }
I16 ICACHE_FLASH_ATTR RINGBUF_Get(RINGBUF *r, U8 *c)
get a character from ring buffer
Definition: ringbuf.c:53
Definition: ringbuf.h:7
#define NULL
Definition: def.h:47
short I16
Definition: typedef.h:11
U8 * p_o
Definition: ringbuf.h:8
long I32
Definition: typedef.h:13
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
I16 ICACHE_FLASH_ATTR RINGBUF_Put(RINGBUF *r, U8 c)
put a character into ring buffer
Definition: ringbuf.c:32
unsigned char U8
Definition: typedef.h:10
I16 ICACHE_FLASH_ATTR RINGBUF_Init(RINGBUF *r, U8 *buf, I32 size)
init a RINGBUF object
Definition: ringbuf.c:16
U8 *volatile p_w
Definition: ringbuf.h:10
volatile I32 fill_cnt
Definition: ringbuf.h:11
I32 size
Definition: ringbuf.h:12
U8 *volatile p_r
Definition: ringbuf.h:9