Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <bitset>
  4.  
  5. const int EmptyPktSize = 7; //Number of data bytes in a packet with no data field
  6. const int HEADERSIZE = 6; //Number of bytes taken up by the header alone
  7. const int FORWARD = 1;
  8. const int BACKWARD = 2;
  9. const int RIGHT = 3;
  10. const int LEFT = 4;
  11. const int UP = 5;
  12. const int DOWN = 6;
  13. const int OPEN = 7;
  14. const int CLOSE = 8;
  15.  
  16. enum CmdType { DRIVE, SLEEP, ARM, CLAW, ACK };
  17.  
  18. struct MotorBody {
  19. char Direction;
  20. // no 3 bytes of padding
  21. // unsigned int Duration : 8;
  22. char Duration;
  23. };
  24.  
  25. class PktDef {
  26. struct Header {
  27. unsigned int PktCount;
  28. unsigned Sleep : 1;
  29. unsigned Status : 1;
  30. unsigned Drive : 1;
  31. unsigned Claw : 1;
  32. unsigned Arm : 1;
  33. unsigned Ack : 1;
  34. unsigned Padding : 2;
  35. unsigned char Length;
  36. } Head;
  37. char * Data;
  38. unsigned char CRC;
  39.  
  40. char * RawBuffer;
  41. public:
  42. PktDef() {
  43. this->Head.PktCount = 0;
  44. this->Head.Sleep = 0;
  45. this->Head.Status = 0;
  46. this->Head.Drive = 0;
  47. this->Head.Claw = 0;
  48. this->Head.Arm = 0;
  49. this->Head.Ack = 0;
  50. this->Head.Padding = 0;
  51. this->Head.Length = (unsigned char)0;
  52. this->Data = nullptr;
  53. this->CRC = (unsigned char)0;
  54. this->RawBuffer = nullptr;
  55. }
  56.  
  57. PktDef(char * rawBuffer) {
  58. memcpy(&this->Head.PktCount, rawBuffer, 5);
  59. memcpy(&this->Head.Length, rawBuffer + 5, 1);
  60. int bodyLength = (int)this->Head.Length - EmptyPktSize;
  61. // memcpy(&this->Data, rawBuffer + HEADERSIZE, bodyLength);
  62. SetBodyData(rawBuffer + HEADERSIZE, bodyLength);
  63. memcpy(&this->CRC, rawBuffer + HEADERSIZE + bodyLength, sizeof(this->CRC));
  64. this->RawBuffer = nullptr;
  65. }
  66.  
  67. ~PktDef() {
  68. delete[] Data;
  69. // deletion of RawBuffer is the responsibility of the caller of GenPacket()
  70. }
  71.  
  72. void SetCmd(CmdType cmdType) {
  73. // A bit unnecessary to set all the flags each time, but you can never be too careful
  74. switch (cmdType) {
  75. case DRIVE:
  76. this->Head.Sleep = 0;
  77. this->Head.Drive = 1;
  78. this->Head.Claw = 0;
  79. this->Head.Arm = 0;
  80. break;
  81. case SLEEP:
  82. this->Head.Sleep = 1;
  83. this->Head.Drive = 0;
  84. this->Head.Claw = 0;
  85. this->Head.Arm = 0;
  86. break;
  87. case ARM:
  88. this->Head.Sleep = 0;
  89. this->Head.Drive = 0;
  90. this->Head.Claw = 0;
  91. this->Head.Arm = 1;
  92. break;
  93. case CLAW:
  94. this->Head.Sleep = 0;
  95. this->Head.Drive = 0;
  96. this->Head.Claw = 1;
  97. this->Head.Arm = 0;
  98. break;
  99. case ACK:
  100. this->Head.Ack = 1;
  101. break;
  102. }
  103. }
  104.  
  105. void SetCmdTest(CmdType cmdType) {
  106. this->Head.Sleep = 0;
  107. this->Head.Drive = 0;
  108. this->Head.Claw = 0;
  109. this->Head.Arm = 0;
  110. this->Head.Ack = 0;
  111.  
  112. switch (cmdType) {
  113. case DRIVE:
  114. this->Head.Drive = 1;
  115. break;
  116. case SLEEP:
  117. this->Head.Sleep = 1;
  118. break;
  119. case ARM:
  120. this->Head.Arm = 1;
  121. break;
  122. case CLAW:
  123. this->Head.Claw = 1;
  124. break;
  125. case ACK:
  126. this->Head.Ack = 1;
  127. break;
  128. }
  129. }
  130.  
  131. void SetPktCount(int count) {
  132. this->Head.PktCount = count;
  133. }
  134.  
  135. CmdType GetCmd() {
  136. CmdType output = SLEEP;
  137. if (this->Head.Sleep == 1) { output = SLEEP; }
  138. else if (this->Head.Drive == 1) { output = DRIVE; }
  139. else if (this->Head.Claw == 1) { output = CLAW; }
  140. else if (this->Head.Arm == 1) { output = ARM; }
  141. else if (this->Head.Ack == 1) { output = ACK; }
  142. return output;
  143. }
  144.  
  145. void SetBodyData(char * bodyData, int length) {
  146. this->Data = nullptr;
  147.  
  148. if (length == 0)
  149. return;
  150.  
  151. this->Head.Length = (unsigned char)(length + EmptyPktSize);
  152. this->Data = new char[length];
  153. memcpy(this->Data, bodyData, length);
  154. }
  155.  
  156. bool GetAck() { return (this->Head.Ack == 1); }
  157.  
  158. int GetLength() { return (int)this->Head.Length; }
  159.  
  160. char * GetBodyData() { return this->Data; }
  161.  
  162. int GetPktCount() { return this->Head.PktCount; }
  163.  
  164. bool CheckCRC(char * data, int size) {
  165. unsigned int ones = 0;
  166. for (int i = 0; i < size - 1; i++)
  167. if (data[i] != 0)
  168. for (int j = 0; j < 8; j++)
  169. ones += (data[i] >> j) & 1;
  170.  
  171. return ones == data[size - 1];
  172. }
  173.  
  174. void CalcCRC() {
  175. char* data = (char*)this;
  176. unsigned int ones = 0;
  177. // printPacketBits();
  178. for (int i = 0; i < (unsigned int)this->Head.Length - sizeof(CRC); i++)
  179. if (data[i] != 0)
  180. for (int j = 0; j < 8; j++)
  181. ones += (data[i] >> j) & 1;
  182.  
  183. this->CRC = (unsigned char)ones;
  184. }
  185.  
  186. char * GenPacket() {
  187. this->RawBuffer = new char[this->Head.Length];
  188. memcpy(this->RawBuffer, &this->Head, HEADERSIZE);
  189. memcpy(this->RawBuffer + 5, &this->Head.Length, 1);
  190.  
  191. if ((int)this->Head.Length > EmptyPktSize)
  192. memcpy(this->RawBuffer + HEADERSIZE, &this->Data, (int)this->Head.Length - EmptyPktSize);
  193.  
  194. memcpy(this->RawBuffer + HEADERSIZE + ((int)this->Head.Length - EmptyPktSize), &this->CRC, sizeof(CRC));
  195. return RawBuffer;
  196. }
  197.  
  198. void printHeaderContents() {
  199. // Helper function for testing
  200. std::cout << "Header Contents" << std::endl;
  201. std::cout << "----------------------" << std::endl;
  202. std::cout << "PktCount : " << (int)this->Head.PktCount << std::endl;
  203. std::cout << "Sleep : " << (int)this->Head.Sleep << std::endl;
  204. std::cout << "Status : " << (int)this->Head.Status << std::endl;
  205. std::cout << "Drive : " << (int)this->Head.Drive << std::endl;
  206. std::cout << "Claw : " << (int)this->Head.Claw << std::endl;
  207. std::cout << "Arm : " << (int)this->Head.Arm << std::endl;
  208. std::cout << "Ack : " << (int)this->Head.Ack << std::endl;
  209. std::cout << "Padding : " << (int)this->Head.Padding << std::endl;
  210. std::cout << "Length : " << (int)this->Head.Length << std::endl;
  211. }
  212.  
  213. void printPacketBits() {
  214. // Helper function for testing
  215. std::cout << "Packet Bits" << std::endl;
  216. std::cout << "----------------------" << std::endl;
  217. std::cout << "PktCount : " << std::bitset<8>(this->Head.PktCount) << std::endl;
  218. std::cout << "Sleep : " << std::bitset<8>(this->Head.Sleep) << std::endl;
  219. std::cout << "Status : " << std::bitset<8>(this->Head.Status) << std::endl;
  220. std::cout << "Drive : " << std::bitset<8>(this->Head.Drive) << std::endl;
  221. std::cout << "Claw : " << std::bitset<8>(this->Head.Claw) << std::endl;
  222. std::cout << "Arm : " << std::bitset<8>(this->Head.Arm) << std::endl;
  223. std::cout << "Ack : " << std::bitset<8>(this->Head.Ack) << std::endl;
  224. std::cout << "Padding : " << std::bitset<8>(this->Head.Padding) << std::endl;
  225. std::cout << "Length : " << std::bitset<8>(this->Head.Length) << std::endl;
  226. std::cout << "Data : ";
  227. for (int i = 0; i < (unsigned int)Head.Length - EmptyPktSize; i++)
  228. std::cout << std::bitset<8>(this->Data[i]) << ' ';
  229. std::cout << std::endl;
  230. }
  231. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement