Subversion Repositories HomeAutomation

Rev

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

  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 {
  11. namespace message {
  12.  
  13. BitBuffer::BitBuffer(byte_list bytes) {
  14.     this->setFromBytes(bytes);
  15. }
  16.  
  17. BitBuffer::BitBuffer() {
  18.     this->myPointer = 0;
  19. }
  20.  
  21. BitBuffer::~BitBuffer() {
  22. }
  23.  
  24.  
  25. byte_list BitBuffer::getAsBytes()
  26. {
  27.     byte_list bytes;
  28.  
  29.     // TODO implement
  30.  
  31.     return bytes;
  32. }
  33.  
  34. void BitBuffer::setFromBytes(byte_list bytes)
  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.  
  48.     this->myPointer = 0;
  49. }
  50.  
  51. unsigned int BitBuffer::getSize()
  52. {
  53.     return this->myBits.size();
  54. }
  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;
  64. }
  65.  
  66. void BitBuffer::clear()
  67. {
  68.     this->myBits.clear();
  69.     this->myPointer = 0;
  70. }
  71.  
  72.  
  73. void BitBuffer::read(unsigned int length, unsigned long & value)
  74. {
  75.     value = 0;
  76.  
  77.     for (unsigned int n = 0; n < length; n++)
  78.     {
  79.         value = (value << n) | this->readBit();
  80.     }
  81. }
  82.  
  83. void BitBuffer::read(unsigned int length, long & value)
  84. {
  85.     // TODO Implement
  86. }
  87.  
  88. void BitBuffer::read(unsigned int length, bool & value)
  89. {
  90.     value = this->readBit();
  91. }
  92.  
  93.  
  94.  
  95. void BitBuffer::write(unsigned int length, unsigned long value)
  96. {
  97.     for (unsigned int n = length-1; n >= 0; n--)
  98.     {
  99.         this->writeBit((value >> n) & 1);
  100.     }
  101. }
  102.  
  103. void BitBuffer::write(unsigned int length, long value)
  104. {
  105.     // TODO Implement
  106. }
  107.  
  108. void BitBuffer::write(unsigned int length, bool value)
  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);
  129. }
  130.  
  131. }
  132. }
  133.