source: rtos_arduino/trunk/arduino_lib/libraries/Ciao/src/lib/ringbuf.c@ 175

Last change on this file since 175 was 175, checked in by ertl-honda, 8 years ago

ライブラリを Arduino IDE 1.7.9 にupdate

File size: 1.5 KB
Line 
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*/
16int16_t RINGBUF_Init(RINGBUF *r, uint8_t* buf, int32_t size)
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*/
32int16_t RINGBUF_Put(RINGBUF *r, uint8_t c)
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*/
53int16_t RINGBUF_Get(RINGBUF *r, uint8_t* c)
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}
Note: See TracBrowser for help on using the repository browser.