forked from mehmetgok/fifo_test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.cpp
More file actions
executable file
·56 lines (47 loc) · 849 Bytes
/
Copy pathfifo.cpp
File metadata and controls
executable file
·56 lines (47 loc) · 849 Bytes
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
/*
* fifo.cpp
*
* Created on: May 21, 2011
* Author: Sagar
*/
#include "fifo.h"
fifo::fifo()
{
this->head = 0;
this->tail = 0;
}
uint32_t fifo::available()
{
return (FIFO_SIZE + this->head - this->tail) % FIFO_SIZE;
}
uint32_t fifo::free()
{
return (FIFO_SIZE - 1 - available());
}
uint8_t fifo::put(FIFO_TYPE data)
{
uint32_t next;
// check if FIFO has room
next = (this->head + 1) % FIFO_SIZE;
if (next == this->tail)
{
// full
return 1;
}
this->buffer[this->head] = data;
this->head = next;
return 0;
}
uint8_t fifo::get(FIFO_TYPE* data)
{
uint32_t next;
// check if FIFO has data
if (this->head == this->tail)
{
return 1; // FIFO empty
}
next = (this->tail + 1) % FIFO_SIZE;
*data = this->buffer[this->tail];
this->tail = next;
return 0;
}