-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstore.h
311 lines (270 loc) · 9.73 KB
/
bitstore.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Library for storing and retrieving multibple bits in one byte
* Copyright (C) 2014-2017 S.Butzek
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BITSTORE_H
#define BITSTORE_H
#include "Arduino.h"
template<uint16_t bufSize> // bufSize in Byte
class BitStore
{
public:
/** Default constructor */
BitStore(uint8_t bitlength);
//~BitStore();
bool addValue(uint8_t value);
uint8_t getValue(const uint16_t pos);
bool moveLeft(const uint16_t begin);
bool changeValue(const uint16_t pos, byte value);
const uint16_t getSize();
//unsigned char *datastore; // Reserve 40 Bytes for our store. Should be edited to aa dynamic way
unsigned char datastore[bufSize];
void reset();
bool getByte(const uint16_t idx, uint8_t *retvalue);
uint16_t bytecount; // Number of stored bytes
uint16_t valcount; // Number of total values stored
//uint8_t debug;
uint16_t bcnt; // fuer debugzwecke nach public verschoben
uint8_t operator[](const uint16_t pos) {
getValue(pos);
return _rval;
}
BitStore &operator+=(const uint8_t value) {
addValue(value);
return *this;
}
#ifndef UNITTEST
protected:
private:
#endif
uint8_t valuelen; // Number of bits for every value
uint8_t bmask;
//uint8_t bcnt;
const uint16_t buffsize;
uint8_t _rval;
};
/*
* Library for storing and retrieving multibple bits in one byte
* Copyright (C) 2014 S.Butzek
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
template<uint16_t bufSize>
BitStore<bufSize>::BitStore(uint8_t bitlength) :buffsize(bufSize)
{
valuelen = bitlength; // How many bits shoudl be reserved for one value added ?
bmask = 0;
//buffsize = bufsize;
//datastore= (unsigned char*) calloc(bufsize,sizeof(char)); // Speicher allokieren und 0 zuweisen
//bytecount = bufSize;
reset();
for (uint8_t x = 7; x>(7 - valuelen); x--)
{
bmask = bmask | (1 << x);
}
}
/** @brief (one liner)
*
* (documentation goes here)
*/
/*template<uint8_t bufSize>
BitStore<bufSize>::~BitStore()
{
//free(datastore);
}
*/
template<uint16_t bufSize>
bool BitStore<bufSize>::addValue(uint8_t value)
{
if (bcnt == 7 && valcount > 0)
{
if (bytecount >= buffsize - 1) {
//Serial.println("OOB");
return false; // Out of Buffer
}
bytecount++;
datastore[bytecount] = 0;
}
//Serial.print("Adding value:"); Serial.print(value, DEC);
//Serial.print(" ("); Serial.print(value, BIN); Serial.print(") ");
//store[bytecount]=datastore[bytecount] | (value<<bcnt)
if (bcnt + 1 - valuelen >0 )
value <<= (bcnt + 1 - valuelen);
//debug = value;
datastore[bytecount] = datastore[bytecount] | value; // (valcount*valuelen%8)
/*
Serial.print(" at byte: "); Serial.print(bytecount, DEC);
Serial.print(" at bitpos: "); Serial.print(bcnt, DEC);
Serial.print(" datastore is (bin)"); Serial.print(datastore[bytecount], BIN);
Serial.print(" (dec)"); Serial.print(datastore[bytecount], DEC);
Serial.print(" : ");
Serial.print(" (valuelen)"); Serial.print(valuelen, DEC);
*/
valcount++;
if (int8_t(bcnt) - valuelen >= 0) // Soalnge nicht 8 Bit gepeichert wurden, erhoehen wir den counter zum verschieben
{
bcnt = bcnt - valuelen; //+valuelen
}
else {
bcnt = 7;
}
return true;
//Serial.println("");
}
template<uint16_t bufSize>
bool BitStore<bufSize>::changeValue(const uint16_t pos, uint8_t value)
{
uint16_t bytepos = pos*valuelen / 8;
/*
Serial.print("Changing value:"); Serial.print(value, DEC);
Serial.print(" ("); Serial.print(value, BIN); Serial.print(")@valpos:");
Serial.print(pos, DEC);
Serial.print(" @byte: "); Serial.print(bytepos, DEC);
Serial.print(" datastore is (bin)"); Serial.print(datastore[bytepos], BIN);
Serial.print(" (dec)"); Serial.print(datastore[bytepos], DEC);
*/
if ((bytepos) >= buffsize) return false; // Out of Buffer
// Serial.print("Pos:"); Serial.print(pos, DEC);
// 01 11 11 00 10
value <<= (8 - (pos*valuelen % 8) - valuelen); // shift bits in value to new positon
//Serial.print(" new_value(bin)"); Serial.print(value, BIN);
datastore[bytepos] = datastore[bytepos] & ~(bmask >> (pos*valuelen % 8)); // Clear bits to be changed
datastore[bytepos] = datastore[bytepos] | value; // Apply new bits
/*
Serial.print(" -> ");
Serial.print(" datastore to (bin)"); Serial.print(datastore[bytepos], BIN);
Serial.print(" (valuelen)"); Serial.println(valuelen, DEC);
*/
return true;
}
/** @brief (return the last position with a value)
*
* (returns the variable valcount. This represents the number of values )
*/
template<uint16_t bufSize>
const uint16_t BitStore<bufSize>::getSize()
{
return valcount;
}
template<uint16_t bufSize>
bool BitStore<bufSize>::moveLeft(const uint16_t begin)
{
if (begin == 0 || begin > valcount) return false;
uint16_t startbyte = begin / 2;
/*
Serial.print("moveleft startbyte:"); Serial.print(startbyte, DEC); Serial.print("@valpos"); Serial.print(begin, DEC);
Serial.print(" bytecount:"); Serial.print(bytecount, DEC);
Serial.print(" vlen:"); Serial.print(valuelen, DEC);
*/
if (begin == valcount)
{
reset();
}
else if (begin % 2 != 0) { // begin ungerade
//uint8_t shift_left = (begin % 2) * 4;
//uint8_t shift_right = 8 - shift_left;
//Serial.print(" sleft "); Serial.print(shift_left, DEC); Serial.print(" sright"); Serial.print(shift_right, DEC);
//valcount = valcount - (shift_left / 4);
uint16_t i = startbyte;
uint16_t z = 0;
for (; i < bytecount; ++i, ++z)
{
/*
Serial.println("");
Serial.print("@["); Serial.print(i, DEC); Serial.print("]");
Serial.print("->["); Serial.print(z, DEC); Serial.print("] ");
Serial.print("z="); Serial.print(datastore[z], BIN);
Serial.print(" ileft="); Serial.print(datastore[i] << shift_left, BIN);
Serial.print(" iright="); Serial.print(datastore[i + 1] >> shift_right, BIN);
*/
datastore[z] = (datastore[i] << 4) | (datastore[i + 1] >> 4);;
}
if (valcount % 2 == 0) //Wenn valcount auf eine gerade Zahl faellt, brauchen wir noch eine Zuweisung
datastore[z] = datastore[i] << 4;
valcount = valcount - begin;
bytecount = (valcount - 1) / 2;
if (valcount % 2 == 0)
bcnt = 7;
else
bcnt = 3;
}
else {
bytecount = bytecount - startbyte;
//Serial.print(" memmove "); Serial.print(bytecount, DEC); Serial.print(" bytes");
memmove(datastore, datastore + startbyte, sizeof(datastore[0]) * (bytecount + 1));
//bcnt = 7;
valcount = valcount - begin;
}
/*
Serial.print(" bcnt: "); Serial.print(bcnt, DEC);
Serial.print(" valcount: "); Serial.print(valcount, DEC);
Serial.print(" bytecount: "); Serial.print(bytecount, DEC);
Serial.println(" ");
*/
return true;
}
template<uint16_t bufSize>
uint8_t BitStore<bufSize>::getValue(const uint16_t pos)
{
uint16_t bytepos = pos*valuelen / 8;
if ((bytepos) >= buffsize ) return -1; // Out of Buffer
//Serial.print("getValue Pos:"); Serial.print(pos, DEC);
uint8_t mask; // Local modified bitmask
//ret= (datastore[pos*valuelen/8]>>(pos*valuelen%8))&bmask;
mask = bmask >> (pos*valuelen % 8); //Mask the position we want to retrieve
//Serial.print(" Bitmask:"); Serial.print(mask, BIN);
_rval = datastore[bytepos] & mask; // Combine the mask with our store to extract the bit
byte scnt = 8 - (valuelen*(pos) % 8) - valuelen;// ((pos + 1)*valuelen % 8); // pos*valuelen 0*1/1*1/2*1/../7*1 7/6/5/0 8-1 8-2 8-3 8-8
//byte scnt = ((pos+1)*valuelen % 8); // 0*4/1*4
//Serial.print(" datastore[bytepos]: ("); Serial.print(_rval, DEC); Serial.print(") "); Serial.println(_rval, BIN);
//Serial.print(" shift:"); Serial.print(scnt, DEC);
//_rval = _rval >> abs(8 - (pos + 1)*valuelen) % 8;//(pos + 1)*valuelen % 8;
_rval = _rval >> scnt;
//Serial.print(" return: ("); Serial.print(_rval, DEC); Serial.print(") "); Serial.println(_rval, BIN);
return _rval;
}
template<uint16_t bufSize>
bool BitStore<bufSize>::getByte(const uint16_t idx,uint8_t *retvalue)
{
if (idx >= buffsize ) return false; // Out of buffer range
*retvalue =datastore[idx];
return true;
}
template<uint16_t bufSize>
void BitStore<bufSize>::reset()
{
/*for (uint8_t i = 0; i < bytecount; i++)
{
datastore[i] = 0;
}*/
datastore[0] = 0;
bytecount = 0;
valcount = 0;
bcnt = 7;
//Serial.print("_bsres:"); Serial.print(valuelen); Serial.print("_");
}
#endif // BITSTORE_H