Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
/* Copyright (C) 2010-2011 Circuits At Home, LTD. All rights reserved.
2
 
3
This software may be distributed and modified under the terms of the GNU
4
General Public License version 2 (GPL2) as published by the Free Software
5
Foundation and appearing in the file GPL2.TXT included in the packaging of
6
this file. Please note that GPL2 Section 2[b] requires that all works based
7
on this software must also be made publicly available under the terms of
8
the GPL2 ("Copyleft").
9
 
10
Contact information
11
-------------------
12
 
13
Circuits At Home, LTD
14
Web      :  http://www.circuitsathome.com
15
e-mail   :  support@circuitsathome.com
16
*/
17
#if !defined(__SIMPLEFIFO_H__)
18
#define __SIMPLEFIFO_H__
19
 
20
template <class TYPE, const uint8_t SIZE>
21
class SimpleFIFO
22
{
23
    TYPE      theBuffer[SIZE];
24
    uint8_t   tail, head;
25
 
26
private:
27
    void inc(uint8_t &val)
28
    {
29
        val ++;
30
 
31
        if (val >= SIZE)
32
            val = 0;
33
    };
34
public:
35
    SimpleFIFO() :
36
        tail(0),
37
        head(0)
38
    {
39
    };
40
    uint8_t Size()
41
    {
42
        if (tail == head)
43
            return 0;
44
 
45
        if (tail > head)
46
            return (tail - head);
47
        else
48
            return (SIZE - head + tail);
49
    };
50
    void Empty()
51
    {
52
        tail = head = 0;
53
    };
54
    void Push(TYPE val)
55
    {
56
        if (Size() >= SIZE-1)
57
            return;
58
 
59
        theBuffer[tail] = val;
60
        inc(tail);
61
//        Serial.print(">");
62
//        Serial.print(head,DEC);
63
//        Serial.print(":");
64
//        Serial.print(tail,DEC);
65
//        Serial.print(":");
66
//        Serial.println(Size(),DEC);
67
    };
68
    TYPE Pop()
69
    {
70
        if (head == tail)
71
            return (TYPE)0;
72
 
73
        TYPE ret = theBuffer[head];
74
        inc(head);
75
//        Serial.print("<");
76
//        Serial.print(head,DEC);
77
//        Serial.print(":");
78
//        Serial.print(tail,DEC);
79
//        Serial.print(":");
80
//        Serial.println(Size(),DEC);
81
        return ret;
82
    };
83
};
84
 
85
 
86
#endif // __SIMPLEFIFO_H__