dataframe.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * @file dataframe.h
  3. * @brief DataFrame interface
  4. *
  5. * @date: 31. 12. 2022
  6. * @author: juraj
  7. */
  8. #ifndef __DATAFRAME_H__
  9. #define __DATAFRAME_H__
  10. #include "stdint.h"
  11. #define HEADER_CHAR 0xCC
  12. /**
  13. * @brief Class for representing data frame.
  14. */
  15. class DataFrame{
  16. private:
  17. uint8_t *_frame;
  18. uint8_t _length;
  19. uint8_t _capacity;
  20. public:
  21. /**
  22. * Constructor.
  23. * @param frame pointer to existing byte array
  24. * @param size size of buffer `frame`
  25. */
  26. DataFrame(uint8_t *frame, uint8_t size);
  27. /**
  28. * @brief Initialize packet
  29. * - set the header of packet
  30. * - reserver 1st byte to length of frame
  31. */
  32. void Init(void);
  33. /**
  34. * @brief Add one byte to data frame.
  35. * @return true, if it is success
  36. */
  37. bool AddUint8(uint8_t d);
  38. /**
  39. * @brief add 2 Bytes length variable to dataframe
  40. * @param d variable added to data frame
  41. * @return true, if it is success
  42. */
  43. bool AddUint16(uint16_t d);
  44. /**
  45. * @brief add 4 Bytes long variable to dataframe
  46. * @param d variable added to data frame
  47. * @return true, if it is success
  48. */
  49. bool AddUint32(uint32_t);
  50. /**
  51. * @brief add one byte signed variable to data frame
  52. * @param d variable added to data frame
  53. * @return true, if it is success
  54. */
  55. bool AddInt8(int8_t d);
  56. /**
  57. * @brief add 2 bytes length signed variable to data frame
  58. * @param d variable added to data frame
  59. * @return true, if it is success
  60. */
  61. bool AddInt16(int16_t);
  62. /**
  63. * @brief add 4 bytes length signed variable to data frame
  64. * @param d variable added to data frame
  65. * @return true, if it is success
  66. */
  67. bool AddInt32(int32_t);
  68. /**
  69. * @brief add float value to data frame.
  70. * Float value is encoded to 4 bytes according IEEE 754
  71. * @param d variable added to data frame
  72. * @return true, if it is success
  73. */
  74. bool AddFloat(float);
  75. /**
  76. * @brief Commit the creation of the packet.
  77. * - add to second position (frame[1]) length of packet
  78. * - compute CRC of packet
  79. * @return real length of packet (including CRC byte)
  80. */
  81. uint8_t Commit(void);
  82. /**
  83. * @brief Return pointer to data frame.
  84. * @return resulting packet
  85. */
  86. uint8_t* GetFrame(void);
  87. };
  88. #ifdef __cplusplus
  89. extern "C" {
  90. #endif
  91. uint8_t crc8(uint8_t crc, uint8_t Size, uint8_t *Buffer);
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. #endif /* DATAFRAME_H_ */