浏览代码

create PLAIN dataframe format (for NRF packets)

Juraj Ďuďák 2 年之前
父节点
当前提交
340d7aaea7
共有 3 个文件被更改,包括 44 次插入19 次删除
  1. 15 5
      readme.md
  2. 23 13
      src/dataframe.cpp
  3. 6 1
      src/dataframe.h

+ 15 - 5
readme.md

@@ -8,13 +8,23 @@ Create formatted frame from basic types:
  - uint32_t, int32_t => 4Bytes
  - float => 4Bytes
 
- Data are stared in Little Endian format.
+Support modes:
+ - Plain (TYPE_PLAIN): construct simple dataframe, 
+ - Basic (TYPE_HEADER_CRC): construct dataframe with header byte as irst byte and CRC checksum as last byte.
+ 
+ Data are stored in Little Endian format.
 
-Structure of data frame:
-  - starting byte (0xCC)
-  - length of frame: count of bytes in payload. The first 2 bytes is not included to overal length. Maximum length: 254 (0xFE)
+Structure of data frame (TYPE_HEADER_CRC):
+  - data[0] - starting byte (0xCC)
+  - data[1] - length of frame: count of bytes in payload. The first 2 bytes is not included to overal length. Maximum length: 254 (0xFE)
   - payload
   - CRC8
+  
+  
+Structure of data frame (TYPE_PLAIN):
+  - data[0] - length of frame: count of bytes in payload. The first bytee is not included to overal length. Maximum length: 254 (0xFE)
+  - payload
+ 
  
  The library contans universal function:
  - `uint8_t crc8(uint8_t crc, uint8_t Size, uint8_t *Buffer);`
@@ -29,7 +39,7 @@ Structure of data frame:
     void main(){
     uint8_t data[128];
 
-    DataFrame packet(data_packet, sizeof(data_packet));
+    DataFrame packet(data_packet, sizeof(data_packet), TYPE_HEADER_CRC);
 
     packet.AddUint8(0xAB);
     packet.AddUint16(65874);

+ 23 - 13
src/dataframe.cpp

@@ -104,21 +104,26 @@ uint8_t crc8(uint8_t crc, uint8_t Size, uint8_t *Buffer)
 
 
 
-DataFrame::DataFrame(uint8_t *frame, uint8_t size)
+DataFrame::DataFrame(uint8_t *frame, uint8_t size, DataframeType_t type)
 {
 	_frame = frame;
 	_capacity = size;
-	_frame[0] = HEADER_CHAR;
-	_frame[1] = 0;	//length of packet
-	_length = 2;
-
+	_type = type;
+	this->Init();
 }
 
 void DataFrame::Init(void)
 {
-	_frame[0] = HEADER_CHAR;
-	_frame[1] = 0;	//length of packet
-	_length = 2;
+	if(_type == TYPE_HEADER_CRC){
+		_frame[0] = HEADER_CHAR;
+		_frame[1] = 0;	//length of packet
+		_length = 2;
+	}
+
+	if(_type == TYPE_PLAIN){
+		_frame[1] = 0;	//length of packet
+		_length = 1;
+	}
 }
 
 bool DataFrame::AddUint8(uint8_t d)
@@ -193,11 +198,16 @@ bool DataFrame::AddFloat(float f)
 
 uint8_t DataFrame::Commit(void)
 {
-	// compute _length
-	_length++;
-	_frame[1] = _length - 2;
-	_frame[_length - 1] = crc8(0, _length-1, _frame);
-	// length of payload: from byte 2 to CRC byte (the last)
+	if(_type == TYPE_HEADER_CRC){
+		// compute _length
+		_length++;
+		_frame[1] = _length - 2;
+		_frame[_length - 1] = crc8(0, _length-1, _frame);
+		// length of payload: from byte 2 to CRC byte (the last)
+	}
+	if(_type == TYPE_PLAIN){
+		_frame[0] = _length;
+	}
 	return _length;
 
 }

+ 6 - 1
src/dataframe.h

@@ -13,6 +13,10 @@
 
 #define HEADER_CHAR 0xCC
 
+typedef enum{
+	TYPE_PLAIN,
+	TYPE_HEADER_CRC,
+}DataframeType_t;
 /**
  * @brief Class for representing data frame.
  */
@@ -22,6 +26,7 @@ private:
 	uint8_t *_frame;
 	uint8_t _length;
 	uint8_t _capacity;
+	DataframeType_t _type;
 
 public:
 	/**
@@ -29,7 +34,7 @@ public:
 	 * @param frame pointer to existing byte array
 	 * @param size size of buffer `frame`
 	 */
-	DataFrame(uint8_t *frame, uint8_t size);
+	DataFrame(uint8_t *frame, uint8_t size, DataframeType_t type);
 	/**
 	 * @brief Initialize packet
 	 * - set the header of packet