-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.c
More file actions
executable file
·70 lines (49 loc) · 1.26 KB
/
fifo.c
File metadata and controls
executable file
·70 lines (49 loc) · 1.26 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
#include "fifo.h"
void initializeFIFO( FIFO *p_fifo ){
p_fifo -> head_read = 0;
p_fifo -> head_write = 0;
}
ErrAddFIFO addFIFO( FIFO *p_fifo, fifo_type data )
{
unsigned char next = (p_fifo -> head_write+ 1) & (FIFO_SIZE - 1);
if( next == p_fifo->head_read ){
return FIFO_OVERFLOWED;
}
p_fifo->data[p_fifo->head_write] = data;
p_fifo->head_write = next;
return ADD_SUCCESS;
}
ErrGetFIFO getFIFO( FIFO *p_fifo, fifo_type *buf )
{
if( p_fifo->head_read == p_fifo->head_write ){
return FIFO_EMPTY;
}
*buf = p_fifo->data[p_fifo->head_read];
p_fifo->head_read = (p_fifo->head_read + 1) & (FIFO_SIZE - 1 );
return GET_SUCCESS;
}
ErrGetAllFIFO getAllFIFO( FIFO *p_fifo, fifo_type *buf, unsigned short buf_size, unsigned short *num_of_data )
{
unsigned short i = 0;
while( getFIFO(p_fifo, buf+i) == GET_SUCCESS ){
i++;
if( i >= buf_size ){
*num_of_data = i;
return BUFFER_SIZE_IS_NOT_ENOUGH;
}
}
*num_of_data = i;
return SUCCESS_GET_ALL_FIFO;
}
void clearFIFO( FIFO *p_fifo )
{
p_fifo->head_read = p_fifo->head_write;
}
unsigned short getFillFIFO(FIFO *p_fifo)
{
return (FIFO_SIZE + p_fifo->head_write - p_fifo->head_read) % FIFO_SIZE;
}
bool isEmptyFIFO(FIFO *p_fifo)
{
return (p_fifo->head_write == p_fifo->head_read) ? true : false;
}