forked from jburr/dsPicMidg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
circBuffer.h
104 lines (80 loc) · 2.48 KB
/
circBuffer.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
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
// ==============================================================
// circBuffer.h
// This is the header file for the circular buffer. This library
// implements a circular buffer to be used in the serial readers
// for the GPS, IPC and telemetry decoders of the UCSC AutoPilot.
//
// Comment the DEBUG define prior to compile for dsPIC
//
// Code by: Mariano I. Lizarraga
// First Revision: Aug 16 2008 @ 00:36
// Second Revision: Dec 2 2008 @ 12:11
// ==============================================================
#ifndef _CIRCBUFFER_H_
#define _CIRCBUFFER_H_
#include "apDefinitions.h"
#ifdef __cplusplus
extern "C"{
#endif
# if __IN_DSPIC__
typedef struct CircBuffer{
unsigned char buffer[BSIZE];
int head;
int tail;
unsigned int size;
unsigned char overflowCount;
}CircBuffer;
#else
typedef struct CircBuffer{
unsigned char* buffer;
int head;
int tail;
unsigned int size;
unsigned char overflowCount;
}CircBuffer;
#endif
// Exported Types
// ==============
typedef struct CircBuffer* CBRef;
// Constructors - Destructors
// ==========================
// this Function returns a pointer to a new Circular Buffer of size pm_size
#if __IN_DSPIC__
void newCircBuffer (CBRef cB);
#else
CBRef newCircBuffer (int pm_size);
#endif
// this function frees the Circular Buffer CB Ref
void freeCircBuffer (CBRef* cB);
// Accesor Methods
// ===============
// returns the amount of unread bytes in the circular buffer
unsigned int getLength (CBRef cB);
// returns the actual index of the head
int readHead (CBRef cB);
// returns the actual index of the tail
int readTail (CBRef cB);
// returns the byte (actual value) that the head points to. this
// does not mark the byte as read, so succesive calls to peak will
// always return the same value
unsigned char peak(CBRef cB);
// Manipulation Procedures
// ======================
// returns the front of the circular buffer and marks the byte as read
unsigned char readFront (CBRef cB);
// writes one byte at the end of the circular buffer, returns 1 if overflow occured
unsigned char writeBack (CBRef cB, unsigned char data);
// empties the circular buffer. It does not change the size. use with caution!!
void makeEmpty (CBRef cB);
// returns the amount of times the CB has overflown;
unsigned char getOverflow(CBRef cB);
#if DEBUG
// Other Functions
// ===============
// prints the circular buffer, used for debug
void printCircBuf(CBRef cB);
#endif /* DEBUG */
#ifdef __cplusplus
}
#endif
#endif /* _CIRCBUFFER_H_ */