Project

General

Profile

Statistics
| Branch: | Revision:

robobuggy / arduino / InterruptRCRecieverReference / PinChangeInt / Examples / ByteBuffer / ByteBuffer.h @ c5d6b0e8

History | View | Annotate | Download (2.78 KB)

1
/*
2
  ByteBuffer.h - A circular buffer implementation for Arduino
3
  Created by Sigurdur Orn, July 19, 2010.  siggi@mit.edu
4
  Updated by GreyGnome (aka Mike Schwager) Thu Feb 23 17:25:14 CST 2012
5
          added the putString() method and the fillError variable.
6
        added the checkError() and resetError() methods.  The checkError() method resets the fillError variable
7
        to false as a side effect.
8
        added the ByteBuffer(unsigned int buf_size) constructor.
9
        added the init() method, and had the constructor call it automagically.
10
        protected certain sections of the code with cli()/sei() calls, for safe use by interrupts.
11
        Also made the capacity, position, length, and fillError variables volatile, for safe use by interrupts.
12
 */
13
 
14
#ifndef ByteBuffer_h
15
#define ByteBuffer_h
16

    
17
#if defined(ARDUINO) && ARDUINO >= 100
18
  #include <Arduino.h>
19
#else
20
  #include <WProgram.h>
21
#endif
22
//#include <util/atomic.h>
23

    
24
#define DEFAULTBUFSIZE 32
25
class ByteBuffer
26
{
27
public:
28
        ByteBuffer() {
29
                init();
30
        };
31
        ByteBuffer(unsigned int buf_size) {
32
                init(buf_size);
33
        };
34

    
35
        // This method initializes the datastore of the buffer to a certain size.
36
        void init(unsigned int buf_size);
37

    
38
        // This method initializes the datastore of the buffer to the default size.
39
        void init();
40

    
41
        // This method resets the buffer into an original state (with no data)        
42
        void clear();
43

    
44
        // This method resets the fillError variable to false.
45
        void resetError();
46

    
47
        // This method tells you if your buffer overflowed at some time since the last
48
        // check.  The error state will be reset to false.
49
        boolean checkError();
50

    
51
        // This releases resources for this buffer, after this has been called the buffer should NOT be used
52
        void deAllocate();
53

    
54
        // Returns how much space is used in the buffer
55
        int getSize();
56
        
57
        // Returns the maximum capacity of the buffer
58
        int getCapacity();
59

    
60
        // This method returns the byte that is located at index in the buffer but doesn't modify the buffer like the get methods (doesn't remove the retured byte from the buffer)
61
        byte peek(unsigned int index);
62

    
63
        //
64
        // Put methods, either a regular put in back or put in front
65
        // 
66
        uint8_t putInFront(byte in);
67
        uint8_t put(byte in);
68
        uint8_t putString(char *in);
69

    
70
        void putIntInFront(int in);
71
        void putInt(int in);
72

    
73
        void putLongInFront(long in);
74
        void putLong(long in);
75

    
76
        void putFloatInFront(float in);
77
        void putFloat(float in);
78

    
79
        //
80
        // Get methods, either a regular get from front or from back
81
        // 
82
        byte get();
83
        byte getFromBack();
84

    
85
        int getInt();
86
        int getIntFromBack();
87

    
88
        long getLong();        
89
        long getLongFromBack();        
90

    
91
        float getFloat();        
92
        float getFloatFromBack();        
93

    
94
private:
95
        byte* data;
96

    
97
        volatile unsigned int capacity;
98
        volatile unsigned int position;
99
        volatile unsigned int length;
100
        volatile boolean fillError;
101
};
102

    
103
#endif
104