MeterLogger
Data Structures | Functions
ringbuf.h File Reference
#include <esp8266.h>
#include "typedef.h"
Include dependency graph for ringbuf.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RINGBUF
 

Functions

I16 ICACHE_FLASH_ATTR RINGBUF_Init (RINGBUF *r, U8 *buf, I32 size)
 init a RINGBUF object More...
 
I16 ICACHE_FLASH_ATTR RINGBUF_Put (RINGBUF *r, U8 c)
 put a character into ring buffer More...
 
I16 ICACHE_FLASH_ATTR RINGBUF_Get (RINGBUF *r, U8 *c)
 get a character from ring buffer More...
 

Function Documentation

◆ RINGBUF_Get()

I16 ICACHE_FLASH_ATTR RINGBUF_Get ( RINGBUF r,
U8 c 
)

get a character from ring buffer

Parameters
rpointer to a ringbuf object
cread character
Returns
0 if successfull, otherwise failed

Definition at line 53 of file ringbuf.c.

References RINGBUF::fill_cnt, RINGBUF::p_o, RINGBUF::p_r, and RINGBUF::size.

Referenced by PROTO_ParseRb().

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 }
U8 * p_o
Definition: ringbuf.h:8
volatile I32 fill_cnt
Definition: ringbuf.h:11
I32 size
Definition: ringbuf.h:12
U8 *volatile p_r
Definition: ringbuf.h:9
Here is the caller graph for this function:

◆ RINGBUF_Init()

I16 ICACHE_FLASH_ATTR RINGBUF_Init ( RINGBUF r,
U8 buf,
I32  size 
)

init a RINGBUF object

Parameters
rpointer to a RINGBUF object
bufpointer to a byte array
sizesize of buf
Returns
0 if successfull, otherwise failed

Definition at line 16 of file ringbuf.c.

References RINGBUF::fill_cnt, NULL, RINGBUF::p_o, RINGBUF::p_r, RINGBUF::p_w, and RINGBUF::size.

Referenced by QUEUE_Init().

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 }
#define NULL
Definition: def.h:47
U8 * p_o
Definition: ringbuf.h:8
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
Here is the caller graph for this function:

◆ RINGBUF_Put()

I16 ICACHE_FLASH_ATTR RINGBUF_Put ( RINGBUF r,
U8  c 
)

put a character into ring buffer

Parameters
rpointer to a ringbuf object
ccharacter to be put
Returns
0 if successfull, otherwise failed

Definition at line 32 of file ringbuf.c.

References RINGBUF::fill_cnt, RINGBUF::p_o, RINGBUF::p_w, and RINGBUF::size.

Referenced by PROTO_AddRb().

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 }
U8 * p_o
Definition: ringbuf.h:8
U8 *volatile p_w
Definition: ringbuf.h:10
volatile I32 fill_cnt
Definition: ringbuf.h:11
I32 size
Definition: ringbuf.h:12
Here is the caller graph for this function: