-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitBuffer.h
137 lines (117 loc) · 4.78 KB
/
BitBuffer.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
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
/*
* DEBUGGING
* for debugging purpose define BB_DEBUG_LEVEL with one of the below values, all debug information will be sent to Serial
* 0 - no debug information
* 1 - high level information (size of created array, ...); this also makes runTest() and printContent2Serial() method available to you
* 2 - more detailed processing information (index data is inserted, internal states after getValue, ...)
* 3 - bit level information for error search
*/
#define BB_DEBUG_LEVEL 0
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class BitBuffer
{
public:
// ##### static constANTS #####
// static constant for ranges defining the maximum number of distinct values to be stored
static const byte RANGE2;
static const byte RANGE4;
static const byte RANGE8;
static const byte RANGE16;
static const byte RANGE32;
static const byte RANGE64;
static const byte RANGE128;
static const byte RANGE256;
static const byte RANGE512;
static const byte RANGE1024;
static const byte RANGE2048;
static const byte RANGE4096;
static const byte RANGE8192;
static const byte RANGE16384;
static const byte RANGE32768;
// static constants for overflow state
static const byte OVERFLOW_MAX;
static const byte OVERFLOW_MIN;
static const byte OVERFLOW_SKIP;
// ##### static constRUCTOR #####
BitBuffer(byte p_range, unsigned int p_size);
// ##### METHODS #####
/*
* Resets buffer instance and frees memory
*/
void flush();
/*
* Overflow handling
* Defines the behavior in case the defined values is beyond the defined range
* OVERFLOW_MAX - the maximum value for this range will be written
* OVERFLOW_MIN - zero will be written
* OVERFLOW_SKIP - value will not be stored
*/
byte getOverflowState();
void setOverflowState(byte p_overflow);
// returns capacity of values that can be stored in buffer for defined range
unsigned int getSize();
// returns the number of values currently stored in buffer
unsigned int getValueCount();
/*
* central methods for buffer to fill and retrieve values.
* buffer will act like a FIFO, replacing old values once capacity of buffer was reached. calling
* pop will remove retrieved value from buffer and free up on index.
* internal handling will ensure that values are mapped to defined range, if value does not
* correspond to defined range it will be mapped or skipped according to define overflow state.
*
* returns: whether action could be performed successfully / first value in buffer
*/
boolean push(unsigned int p_value);
unsigned int pop();
/*
* Returns the specified index in the buffer without deleting it.
* The index does not correspond with the internal bit-Index nor the array-Index but represents the
* index in a FIFO. If for example one more value was added than the capacity of the buffer and the first
* index is requested this would be the second one added to the buffer, with the first value being already
* overwritten.
*
* p_index: index in FIFO starting with 1
*/
unsigned int getValue(unsigned p_index);
#if BB_DEBUG_LEVEL > 0
void runTest();
/*
* method for tracing behavior of buffer. This will print out all previously added values in a
* string to Serial.
*/
void printContent2Serial();
#endif
private:
// ###### VARIABLES #####
byte s_range; //value range
byte s_overflow; //overflow behaviour
byte* s_data; //dataset array
unsigned long s_bitIndex; //location index for next write on bitlevel
unsigned int s_popCount; //number of values currently retrieved from buffer
unsigned int s_size; //capacity of values that can be stored in buffer for defined range
unsigned int s_bitSize; //number of bits per value for defined range
boolean s_full; //keep state whether first overrun of FIFO happened already
// ##### METHODS #####
// returns the maximum value that can be stored in buffer for defined range.
unsigned int getMaxRangeValue();
unsigned int getMaxRangeValue(byte p_range);
/*
* returns size of values in bits for defined range
* value range defines the number of bits required, as range defines the maximum value we have to calclate the exponent
* example range = 0x07 that corresponds to 2^X=8; the exponent X is what we are looking for
* math.h does not define a logarithm to the base of two, http://stackoverflow.com/questions/758001/log2-not-found-in-my-math-h
*/
unsigned int getBitSize();
// returns the size of the byte array for internal calculation
unsigned int getArraySize();
/*
* Returns the value for the defined bitIndex
*/
unsigned int getValueInternal(unsigned long p_bitIndex);
//performs a transformation of two byte into an integer
unsigned int getIntegerValue(byte highByte, byte lowByte);
};