summaryrefslogtreecommitdiff
path: root/trunk/darwin/firmware/Sanguino/Sanguino3G/SanguinoMaster/CircularBuffer.h
blob: 5e7459e8e9e07a43d8a07fed4bc22ea1cc48a48b (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef _CIRCULAR_BUFFER_H_
#define _CIRCULAR_BUFFER_H_
/**
 *  Sanguino 3rd Generation Firmware (S3G)
 *
 *  Specification for this protocol is located at: 
 *    http://docs.google.com/Doc?id=dd5prwmp_14ggw37mfp
 *  
 *  License: GPLv2
 *  Authors: Marius Kintel, Adam Mayer, and Zach Hoeken
 */

#include <stddef.h>
#include <stdint.h>
#include "Timer1.h"

/**
 * This is an implementation of a simple in-memory circular
 * buffer.
 *
 * Please note that this implementation is NOT thread/interrupt
 * safe.  Remember that 16-bit data access is not atomic on
 * 8-bit platforms!  Turn off interrupts before accessing the
 * buffer.
 */
class CircularBuffer {
private:
  uint8_t* buffer;        // ptr to the in-memory buffer
  uint16_t capacity;      // size of the buffer
  uint16_t head;          // index of first element of data
  uint16_t tail;          // index of last element of data
  uint16_t currentSize;   // number of elements of valid data in buffer
public:
  /**
   * Create a circular buffer of the given size with a provided
   * chunk of memory.
   * This implementation does not claim ownership of the buffer!
   */
  CircularBuffer(uint16_t capacity, uint8_t* pBuf) {
    this->capacity = capacity;
    buffer = pBuf;
    clear();
  }

  ~CircularBuffer() {
  }

  /**
   * Reset the circular buffer to an empty state.
   */
  void clear() {
    head = tail = 0;
    currentSize = 0;
  }

  /**
   * Return a byte of data in the circular buffer specified by its index.
   */
  uint8_t operator[](uint16_t i) {
    uint16_t idx;
    idx = (i + tail) % capacity;
    return buffer[idx];
  }

  /**
   * Return the remaining capacity of the circular buffer.
   */
  uint16_t remainingCapacity()
  {
    uint16_t remaining;
    remaining = (capacity - currentSize);
    return remaining;
  }

  /**
   * Return the current number of valid bytes in the buffer.
   */
  uint16_t size() {
    uint16_t csize;
    csize = currentSize;
    return csize;
  }

private:
  /**
   * Internal: append a byte of data to the buffer's contents.
   */
  void appendInternal(uint8_t datum)
  {
    if ((currentSize + 1) <= capacity)
    {
      buffer[head] = datum;
      head = (head + 1) % capacity;
      currentSize++;
    }
  }
  
public:
  /**
   * Append a single byte of data to the buffer.
   */
  void append(uint8_t datum) {
    appendInternal(datum);
  }

  /**
   * Append a two-byte value to the buffer.
   * The buffer stores values in little-endian format.
   */
  void append_16(uint16_t datum) {
    appendInternal(datum & 0xff);
    appendInternal((datum >> 8) & 0xff);
  }

  /**
   * Append a four-byte value to the buffer.
   * The buffer stores values in little-endian format.
   */
  void append_32(uint32_t datum) {
    appendInternal(datum & 0xff);
    appendInternal((datum >> 8) & 0xff);
    appendInternal((datum >> 16) & 0xff);
    appendInternal((datum >> 24) & 0xff);
  }

private:
  /**
   * Internal: Remove the first byte of the buffer's data and return it.
   */
  inline uint8_t removeInternal() {
    if (currentSize == 0)
      return 0;
    else
    {
      uint8_t c = buffer[tail];
      tail = (tail + 1) % capacity;
      currentSize--;
      return c;
    }
  }

public:    
  /**
   * Remove and return a byte from the start of the circular buffer.
   */
  uint8_t remove_8() {
    return removeInternal();
  }

  /*
   * Remove and return a two-byte value  from the start of the circular 
   * buffer.  The buffer stores values in little-endian format.
   */
  uint16_t remove_16() {
    uint8_t v[2];
    v[0] = removeInternal();
    v[1] = removeInternal();
    return v[0] | ((uint16_t)v[1] << 8);
  }

  /*
   * Remove and return a four-byte value  from the start of the circular 
   * buffer.  The buffer stores values in little-endian format.
   */
  uint32_t remove_32() {
    uint8_t v[4];
    v[0] = removeInternal();
    v[1] = removeInternal();
    v[2] = removeInternal();
    v[3] = removeInternal();
    return v[0] |
      ((uint32_t)v[1] << 8) |
      ((uint32_t)v[2] << 16) |
      ((uint32_t)v[3] << 24);
  }

  void removeCount( uint16_t count ) {
    if (currentSize < count)
      count = currentSize;
    tail = (tail + count) % capacity;
    currentSize -= count;
  }

  class Cursor {
  private:
    CircularBuffer& buffer;
    uint16_t idx;
  public:
    Cursor(CircularBuffer& src) : buffer(src), idx(0) {}
    uint8_t read_8() {
      return buffer[idx++];
    }
    uint16_t read_16() {
      return 
	((uint16_t)read_8() << 0) |
	((uint16_t)read_8() << 8);
    }
    uint32_t read_32() {
      return 
	((uint32_t)read_8() << 0) |
	((uint32_t)read_8() << 8) |
	((uint32_t)read_8() << 16) |
	((uint32_t)read_8() << 24);
    }
    void commit() {
      buffer.removeCount(idx);
    }
    void rollback() {
      idx = 0;
    }
  };

  Cursor newCursor() {
    return Cursor(*this);
  }

};

#endif // _CIRCULAR_BUFFER_H_