-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcircular-buffer.c
More file actions
93 lines (78 loc) · 3.24 KB
/
Copy pathcircular-buffer.c
File metadata and controls
93 lines (78 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**\cond
******************************************************************************
* ______ _ ___ ___ _
* | ___ \(_) | \/ | | |
* | |_/ / _ _ __ __ _ _ __ _ _ | . . | __ _ | | __ ___ _ __
* | ___ \| || '_ \ / _` || '__|| | | | | |\/| | / _` || |/ // _ \| '__|
* | |_/ /| || | | || (_| || | | |_| | | | | || (_| || <| __/| |
* \____/ |_||_| |_| \__,_||_| \__, | \_| |_/ \__,_||_|\_\\___||_|
* __/ |
* |___/
*
* Copyright (C) 2020 Binary Maker - All Rights Reserved
*
* This program and the accompanying materials are made available
* under the terms described in the LICENSE file which accompanies
* this distribution.
* Written by Binary Maker <https://github.com/binarymaker>
******************************************************************************
\endcond*/
/* Includes ------------------------------------------------------------------*/
#include "circular-buffer.h"
#include "circular-buffer-cfg.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void
CIRCULAR_BUFFER_Init(circularBuffer_st * self, void * buffer_ptr,
uint32_t data_size_u8,uint32_t size_u8)
{
self->buffer_ptr = buffer_ptr;
self->data_size_u8 = data_size_u8;
self->size_u8 = size_u8 ;
self->head_u8 = 0u;
self->tail_u8 = 0u;
}
void
CIRCULAR_BUFFER_Write(circularBuffer_st * self, void * data_ptr)
{
uint8_t * data_u8ptr = (uint8_t *) data_ptr;
uint8_t * buffer_u8ptr = (uint8_t *) self->buffer_ptr;
if (CIRCULAR_BUFFER_Available(self) == (self->size_u8 - 1u))
{
self->tail_u8 = (self->tail_u8 + 1u) % self->size_u8;
}
uint8_t start_u8 = self->head_u8 * self->data_size_u8;
for(uint8_t i_u8 = 0; i_u8 < self->data_size_u8; i_u8++)
{
uint8_t mem_loc_u8 = start_u8 + i_u8;
buffer_u8ptr[mem_loc_u8] = data_u8ptr[i_u8];
}
self->head_u8 = (self->head_u8 + 1u) % self->size_u8;
}
void
CIRCULAR_BUFFER_Read(circularBuffer_st * self, void * data_ptr)
{
uint8_t * data_u8ptr = (uint8_t *) data_ptr;
uint8_t * buffer_u8ptr = (uint8_t *) self->buffer_ptr;
while(CIRCULAR_BUFFER_Available(self) == 0u)
{
// TODO timeout
}
uint8_t start_u8 = self->tail_u8 * self->data_size_u8;
for(uint8_t i_u8 = 0; i_u8 < self->data_size_u8; i_u8++)
{
data_u8ptr[i_u8] = buffer_u8ptr[start_u8 + i_u8];
}
self->tail_u8 = (self->tail_u8 + 1u) % self->size_u8;
}
uint8_t
CIRCULAR_BUFFER_Available(circularBuffer_st * self)
{
uint8_t len_u8;
len_u8 = (self->size_u8 + self->head_u8 - self->tail_u8) % self->size_u8;
return (len_u8);
}