GPS Device
Loading...
Searching...
No Matches
Circular Buffer Public API

Public functions to interact with a circular buffer instance. More...

Collaboration diagram for Circular Buffer Public API:

Functions

ERR_te cbuf_read (CBUF_HANDLE_ts *cbuf_handle, uint8_t *output_buf_o)
 Reads all available data from the circular buffer into an output buffer.
ERR_te cbuf_write (CBUF_HANDLE_ts *cbuf_handle, uint8_t *input_buf, uint32_t input_len)
 Writes data from an input buffer into the circular buffer.
ERR_te cbuf_len (CBUF_HANDLE_ts const *cbuf_handle, uint8_t *len_o)
 Returns the number of bytes currently stored in the circular buffer.

Detailed Description

Public functions to interact with a circular buffer instance.

Function Documentation

◆ cbuf_read()

ERR_te cbuf_read ( CBUF_HANDLE_ts * cbuf_handle,
uint8_t * output_buf_o )

Reads all available data from the circular buffer into an output buffer.

Drains the circular buffer completely, copying each byte in order into output_buf_o and advancing the read position after each byte. Stops when the buffer is empty.

Parameters
[in,out]cbuf_handlePointer to the circular buffer to read from.
[out]output_buf_oPointer to the destination buffer. Must be large enough to hold all available bytes (up to size - 1).
Returns
  • ERR_OK on success

Reads all available data from the circular buffer into an output buffer.

See also
cbuf_read

Definition at line 22 of file cbuf.c.

22 {
23 uint8_t len = 0;
24
25 while(1) {
26 cbuf_len(cbuf_handle, &len);
27
28 if(len == 0) {
29 break;
30 }
31
32 *output_buf_o = cbuf_handle->ptr[cbuf_handle->read_position];
33 output_buf_o++;
34 cbuf_handle->read_position = (cbuf_handle->read_position + 1) & (cbuf_handle->size - 1);
35 }
36
37 return ERR_OK;
38}
ERR_te cbuf_len(CBUF_HANDLE_ts const *cbuf_handle, uint8_t *len_o)
Returns the number of bytes currently stored in the circular buffer.
Definition cbuf.c:61
@ ERR_OK
Definition err.h:36
uint8_t size
Definition cbuf.h:65
uint8_t * ptr
Definition cbuf.h:62
uint8_t read_position
Definition cbuf.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cbuf_write()

ERR_te cbuf_write ( CBUF_HANDLE_ts * cbuf_handle,
uint8_t * input_buf,
uint32_t input_len )

Writes data from an input buffer into the circular buffer.

Copies up to input_len bytes from input_buf into the circular buffer, advancing the write position after each byte.

Writing stops early and returns ERR_BUFFER_FULL if the buffer becomes full before all input_len bytes have been written.

Parameters
[in,out]cbuf_handlePointer to the circular buffer to write into.
[in]input_bufPointer to the source data buffer.
[in]input_lenNumber of bytes to write.
Returns
  • ERR_OK on success
  • ERR_BUFFER_FULL if the buffer is full before all bytes could be written
See also
cbuf_write

Definition at line 41 of file cbuf.c.

41 {
42 uint8_t len;
43
44 while(input_len != 0) {
45 cbuf_len(cbuf_handle, &len);
46
47 if(len == cbuf_handle->size - 1) {
48 return ERR_BUFFER_FULL;
49 }
50
51 cbuf_handle->ptr[cbuf_handle->write_position] = *input_buf;
52 input_len--;
53 input_buf++;
54 cbuf_handle->write_position = (cbuf_handle->write_position + 1) & (cbuf_handle->size - 1);
55 }
56
57 return ERR_OK;
58}
@ ERR_BUFFER_FULL
Definition err.h:48
uint8_t write_position
Definition cbuf.h:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cbuf_len()

ERR_te cbuf_len ( CBUF_HANDLE_ts const * cbuf_handle,
uint8_t * len_o )

Returns the number of bytes currently stored in the circular buffer.

The returned length ranges from 0 (empty) to size - 1 (full).

Parameters
[in]cbuf_handlePointer to the circular buffer to query.
[out]len_oPointer to a variable that will receive the byte count.
Returns
  • ERR_OK on success
See also
cbuf_len

Definition at line 61 of file cbuf.c.

61 {
62 *len_o = (cbuf_handle->write_position - cbuf_handle->read_position) & (cbuf_handle->size - 1);
63
64 return ERR_OK;
65}
Here is the caller graph for this function: