Subversion Repositories HomeAutomation

Rev

Rev 1326 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1309 runge 1
/*
2
 * BitBuffer.cpp
3
 *
4
 *  Created on: Jul 17, 2009
5
 *      Author: Mattias Runge
6
 */
7
 
8
#include "BitBuffer.h"
9
 
10
namespace atom {
1319 runge 11
namespace message {
1309 runge 12
 
1322 runge 13
BitBuffer::BitBuffer(byte_list bytes) {
1309 runge 14
    this->setFromBytes(bytes);
15
}
16
 
17
BitBuffer::BitBuffer() {
1328 runge 18
    this->myPointer = 0;
1309 runge 19
}
20
 
21
BitBuffer::~BitBuffer() {
22
}
23
 
24
 
1322 runge 25
byte_list BitBuffer::getAsBytes()
1309 runge 26
{
1328 runge 27
    byte_list bytes;
28
 
29
    // TODO implement
30
 
31
    return bytes;
1309 runge 32
}
33
 
1322 runge 34
void BitBuffer::setFromBytes(byte_list bytes)
1309 runge 35
{
1328 runge 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
 
48
    this->myPointer = 0;
1309 runge 49
}
50
 
1328 runge 51
unsigned int BitBuffer::getSize()
1323 runge 52
{
1328 runge 53
    return this->myBits.size();
54
}
1309 runge 55
 
1328 runge 56
unsigned int BitBuffer::getPointer()
57
{
58
    return this->myPointer;
1323 runge 59
}
60
 
1328 runge 61
void BitBuffer::incrementPointer(unsigned int steps)
62
{
63
    this->myPointer += steps;
64
}
65
 
1323 runge 66
void BitBuffer::clear()
67
{
1328 runge 68
    this->myBits.clear();
69
    this->myPointer = 0;
1323 runge 70
}
71
 
1309 runge 72
 
1326 runge 73
void BitBuffer::read(unsigned int length, unsigned long & value)
1309 runge 74
{
1328 runge 75
    value = 0;
76
 
77
    for (unsigned int n = 0; n < length; n++)
78
    {
79
        value = (value << n) | this->readBit();
80
    }
1309 runge 81
}
82
 
1326 runge 83
void BitBuffer::read(unsigned int length, long & value)
1309 runge 84
{
1328 runge 85
    // TODO Implement
1309 runge 86
}
87
 
1326 runge 88
void BitBuffer::read(unsigned int length, bool & value)
1309 runge 89
{
1328 runge 90
    value = this->readBit();
1309 runge 91
}
92
 
93
 
1326 runge 94
 
95
void BitBuffer::write(unsigned int length, unsigned long value)
1309 runge 96
{
1328 runge 97
    for (unsigned int n = length-1; n >= 0; n--)
98
    {
99
        this->writeBit((value >> n) & 1);
100
    }
1309 runge 101
}
102
 
1328 runge 103
void BitBuffer::write(unsigned int length, long value)
1309 runge 104
{
1328 runge 105
    // TODO Implement
1309 runge 106
}
107
 
1326 runge 108
void BitBuffer::write(unsigned int length, bool value)
1309 runge 109
{
1328 runge 110
    this->writeBit(value);
1309 runge 111
}
112
 
113
 
1328 runge 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];
1309 runge 124
}
1328 runge 125
 
126
void BitBuffer::writeBit(bool value)
127
{
128
    this->myBits.push_back(value);
1309 runge 129
}
1328 runge 130
 
131
}
132
}