-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_buffer.cpp
74 lines (53 loc) · 1.35 KB
/
card_buffer.cpp
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
#include "card_buffer.h"
#include <string.h>
namespace HID5455 {
CardBuffer::CardBuffer(void) {}
void CardBuffer::clear(void) {
memset(buffer, 0, data_width);
data_width = 0;
}
void CardBuffer::push(bool value) {
unsigned int position = data_width;
if(position > MAX_WIDTH || data_width >= MAX_WIDTH) return;
int slot = position / 8;
int offset = 7 - (position % 8);
// 1 read
if(value) {
buffer[slot] |= 1 << offset;
}
// 0 read
if(!value) {
buffer[slot] &= ~(1 << offset);
}
data_width++;
}
bool CardBuffer::get_bit_at(unsigned int position) {
int slot = position / 8;
int offset = 7 - (position % 8);
return buffer[slot] & (1 << offset);
}
unsigned int CardBuffer::get_bit_range(unsigned int begin, unsigned int end) {
if(begin > end) return 0;
unsigned int total = 0;
for(unsigned int i = begin; i < end; i++) {
total = (total << 1) + get_bit_at(i);
}
return total;
}
unsigned int CardBuffer::get_bits(const unsigned int bits[], unsigned int len) {
if(len <= 0) return 0;
unsigned int total = 0;
for(unsigned int i = 0; i < len; i++) {
total = (total << 1) + get_bit_at(bits[i]);
}
return total;
}
void CardBuffer::print(void) {
for(int i = 0; i < (data_width / 8) + 1; i++) {
Serial.println(buffer[i], BIN);
}
}
byte* CardBuffer::get_buffer(void) {
return buffer;
}
}