Subversion Repositories HomeAutomation

Rev

Rev 1326 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1326 Rev 1328
Line 7... Line 7...
7
 
7
 
8
#include "BitBuffer.h"
8
#include "BitBuffer.h"
9
 
9
 
10
namespace atom {
10
namespace atom {
11
namespace message {
11
namespace message {
12
 
-
 
13
BitBuffer::BitBuffer(unsigned char bytes[], unsigned int length)
-
 
14
{
-
 
15
    for (unsigned int n = 0; n < length; n++)
-
 
16
    {
-
 
17
        this->myBuffer.push_back(bytes[n]);
-
 
18
    }
-
 
19
}
-
 
20
 
12
 
21
BitBuffer::BitBuffer(byte_list bytes) {
13
BitBuffer::BitBuffer(byte_list bytes) {
22
    this->setFromBytes(bytes);
14
    this->setFromBytes(bytes);
23
}
15
}
24
 
-
 
25
 
16
 
26
BitBuffer::BitBuffer() {
17
BitBuffer::BitBuffer() {
-
 
18
    this->myPointer = 0;
27
}
19
}
28
 
20
 
29
BitBuffer::~BitBuffer() {
21
BitBuffer::~BitBuffer() {
30
}
22
}
31
 
23
 
32
 
24
 
33
byte_list BitBuffer::getAsBytes()
25
byte_list BitBuffer::getAsBytes()
34
{
26
{
-
 
27
    byte_list bytes;
-
 
28
 
-
 
29
    // TODO implement
-
 
30
 
35
    return this->myBuffer;
31
    return bytes;
36
}
32
}
37
 
33
 
38
void BitBuffer::setFromBytes(byte_list bytes)
34
void BitBuffer::setFromBytes(byte_list bytes)
39
{
35
{
-
 
36
    for (unsigned int n = 0; n < bytes.size(); n++)
-
 
37
    {
-
 
38
        this->myBits.push_back((bytes[n] >> 7 & 1) > 0);
-
 
39
        this->myBits.push_back((bytes[n] >> 6 & 1) > 0);
-
 
40
        this->myBits.push_back((bytes[n] >> 5 & 1) > 0);
-
 
41
        this->myBits.push_back((bytes[n] >> 4 & 1) > 0);
-
 
42
        this->myBits.push_back((bytes[n] >> 3 & 1) > 0);
-
 
43
        this->myBits.push_back((bytes[n] >> 2 & 1) > 0);
-
 
44
        this->myBits.push_back((bytes[n] >> 1 & 1) > 0);
-
 
45
        this->myBits.push_back((bytes[n] >> 0 & 1) > 0);
-
 
46
    }
-
 
47
 
40
    this->myBuffer = bytes;
48
    this->myPointer = 0;
41
}
49
}
42
 
50
 
43
void BitBuffer::incrementPointer(unsigned int steps)
51
unsigned int BitBuffer::getSize()
44
{
52
{
-
 
53
    return this->myBits.size();
-
 
54
}
45
 
55
 
-
 
56
unsigned int BitBuffer::getPointer()
-
 
57
{
-
 
58
    return this->myPointer;
-
 
59
}
-
 
60
 
-
 
61
void BitBuffer::incrementPointer(unsigned int steps)
-
 
62
{
-
 
63
    this->myPointer += steps;
46
}
64
}
47
 
65
 
48
void BitBuffer::clear()
66
void BitBuffer::clear()
49
{
67
{
50
    this->myBuffer.clear();
68
    this->myBits.clear();
51
    // TÒDO resent pointers
69
    this->myPointer = 0;
52
}
70
}
53
 
71
 
54
 
72
 
55
void BitBuffer::read(unsigned int length, unsigned long & value)
73
void BitBuffer::read(unsigned int length, unsigned long & value)
56
{
74
{
-
 
75
    value = 0;
-
 
76
 
-
 
77
    for (unsigned int n = 0; n < length; n++)
-
 
78
    {
-
 
79
        value = (value << n) | this->readBit();
-
 
80
    }
57
}
81
}
58
 
82
 
59
void BitBuffer::read(unsigned int length, long & value)
83
void BitBuffer::read(unsigned int length, long & value)
60
{
84
{
-
 
85
    // TODO Implement
61
}
86
}
62
 
87
 
63
void BitBuffer::read(unsigned int length, bool & value)
88
void BitBuffer::read(unsigned int length, bool & value)
64
{
89
{
-
 
90
    value = this->readBit();
65
}
91
}
66
 
92
 
67
 
93
 
68
 
94
 
69
void BitBuffer::write(unsigned int length, unsigned long value)
95
void BitBuffer::write(unsigned int length, unsigned long value)
70
{
-
 
71
}
-
 
72
 
-
 
73
void BitBuffer::write(unsigned int length, long  value)
-
 
74
{
96
{
-
 
97
    for (unsigned int n = length-1; n >= 0; n--)
-
 
98
    {
-
 
99
        this->writeBit((value >> n) & 1);
-
 
100
    }
75
}
101
}
-
 
102
 
-
 
103
void BitBuffer::write(unsigned int length, long value)
-
 
104
{
-
 
105
    // TODO Implement
-
 
106
}
76
 
107
 
77
void BitBuffer::write(unsigned int length, bool value)
108
void BitBuffer::write(unsigned int length, bool value)
78
{
109
{
-
 
110
    this->writeBit(value);
-
 
111
}
-
 
112
 
-
 
113
 
-
 
114
bool BitBuffer::readBit()
-
 
115
{
-
 
116
    this->myPointer++;
-
 
117
 
-
 
118
    if (this->myPointer > this->myBits.size())
-
 
119
    {
-
 
120
        return false;
-
 
121
    }
-
 
122
 
-
 
123
    return this->myBits[this->myPointer-1];
-
 
124
}
-
 
125
 
-
 
126
void BitBuffer::writeBit(bool value)
-
 
127
{
-
 
128
    this->myBits.push_back(value);
79
}
129
}
80
 
-
 
81
 
130
 
82
}
131
}
83
}
132
}