-
Notifications
You must be signed in to change notification settings - Fork 3
/
fifo.h
73 lines (64 loc) · 2.1 KB
/
fifo.h
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
//==============================================================================
// _____ __ __ __ ____ ____ ___ _ __
// | ____| \/ | / /_| ___| / ___( _ )/ |/ /_
// | _| | |\/| |_____| '_ \___ \| | / _ \| | '_ \
// | |___| | | |_____| (_) |__) | |__| (_) | | (_) |
// |_____|_|__|_|___ __\___/____/ \____\___/|_|\___/
// | ____/ ___|| _ \___ /___ \
// | _| \___ \| |_) ||_ \ __) |
// | |___ ___) | __/___) / __/
// |_____|____/|_| |____/_____|
//
//------------------------------------------------------------------------------
// Copyright (C),2019 Andrew John Jacobs
// All rights reserved.
//
// This work is made available under the terms of the Creative Commons
// Attribution-NonCommercial-ShareAlike 4.0 International license. Open the
// following URL to see the details.
//
// http://creativecommons.org/licenses/by-nc-sa/4.0/
//------------------------------------------------------------------------------
// Notes:
//
//==============================================================================
#ifndef FIFO_H
#define FIFO_H
#include <stdint.h>
//==============================================================================
template <uint16_t size> class Fifo
{
private:
volatile uint16_t head;
volatile uint16_t tail;
volatile uint8_t data [size];
public:
// Construct an empty Fifo instance
Fifo (void)
: head(0), tail(0)
{ }
// Is the Fifo completely full?
bool isFull (void) const
{
return ((tail + 1) % size == head);
}
// Is the Fifo completely empty?
bool isEmpty (void) const
{
return (head == tail);
}
// Enqueue a value. The Fifo MUST NOT be full.
void enqueue (uint8_t value)
{
data [tail] = value;
tail = (tail + 1) % size;
}
// Dequeue a value. The Fifo MUST NOT be empty
uint8_t dequeue (void)
{
uint8_t value = data [head];
head = (head + 1) % size;
return (value);
}
};
#endif