Remi Beges / DistantIO

Files at this revision

API Documentation at this revision

Comitter:
Overdrivr
Date:
Mon Oct 12 13:29:40 2015 +0000
Parent:
2:f2c816b681e3
Child:
4:d675ad9c57ff
Commit message:
New protocol update. DistantIO Protocol now supports 2 extra identifier fields for sending time of recorded value and index for array support. Also, it will enable semi-manual data send for special debugging cases. Longer var/group names too.

Changed in this revision

distantio.cpp Show annotated file Show diff for this revision Revisions of this file
distantio.h Show annotated file Show diff for this revision Revisions of this file
--- a/distantio.cpp	Thu Oct 08 13:14:32 2015 +0000
+++ b/distantio.cpp	Mon Oct 12 13:29:40 2015 +0000
@@ -18,7 +18,7 @@
 static log Log;
 uint32_t tmp;
 
-void send_variable(uint16_t index);
+void send_variable(uint16_t index,float extra_identifier_1,uint16_t extra_identifier_2=0);
 uint16_t get_size(dio_type type);
 void send_descriptor(uint16_t index);
 void send_group_descriptor(uint16_t index);
@@ -37,13 +37,13 @@
 		Log.variables[i].ptr = 0;
 		Log.variables[i].writeable = 0;
 		Log.variables[i].id = i;
-		strncpy(Log.variables[i].name,default_name,8);
+		strncpy(Log.variables[i].name,default_name,NAMESIZE);
 		Log.variables[i].send = 0;
 		Log.variables[i].groupID = 0;
 	}
 	tmp=0;
 	Log.current_group_id = 0;
-	strncpy(Log.groups[0].name,"default",8);
+	strncpy(Log.groups[0].name,"default",NAMESIZE);
 }
 
 /**
@@ -60,7 +60,7 @@
 	Log.variables[Log.amount].writeable = writeable;
 	Log.variables[Log.amount].type = type;
 	Log.variables[Log.amount].groupID = Log.current_group_id;
-	strncpy(Log.variables[Log.amount].name,name,8);
+	strncpy(Log.variables[Log.amount].name,name,NAMESIZE);
 	Log.variables[Log.amount].refresh_rate = refresh_rate;
 	Log.variables[Log.amount].last_refreshed = 0;
 	Log.amount++;
@@ -76,7 +76,7 @@
 void start_group(char* groupname)
 {
 	Log.current_group_id++;
-	strncpy(Log.groups[Log.current_group_id].name,groupname,8);
+	strncpy(Log.groups[Log.current_group_id].name,groupname,NAMESIZE);
 }
 
 /**
@@ -88,7 +88,7 @@
 	if(index >= Log.amount)
 		return;
 
-	static uint8_t buffer[PAYLOAD_SIZE];
+	static uint8_t buffer[FRAMESIZE];
 	uint8_t type;
 
 	// Respond returned-descriptor
@@ -111,7 +111,8 @@
 
 	//Write name
 	uint16_t i = 4;
-	for(uint16_t k = 0 ; k < 8 ; k++)
+	// TODO : Replace with strncpy
+	for(uint16_t k = 0 ; k < NAMESIZE ; k++)
 	{
 		if(k < strlen(Log.variables[index].name))
 		{
@@ -138,7 +139,7 @@
 	if(index > Log.current_group_id)
 		return;
 
-	static uint8_t buffer[PAYLOAD_SIZE];
+	static uint8_t buffer[FRAMESIZE];
 
 	// Respond returned-descriptor
 	buffer[0] = 0x00;
@@ -154,7 +155,7 @@
 
 	//Write name
 	uint16_t i = 4;
-	for(uint16_t k = 0 ; k < 8 ; k++)
+	for(uint16_t k = 0 ; k < NAMESIZE ; k++)
 	{
 		if(k < strlen(Log.groups[index].name))
 		{
@@ -179,13 +180,12 @@
 void distantio_decode(uint8_t* data,uint16_t datasize)
 {
 	// First check data size
-	// 1 byte cmd + 2 bytes id + 1 byte type + FRAME_SIZE + 2 byte CRC
-	if(datasize != PAYLOAD_SIZE)
+	if(datasize != FRAMESIZE)
 		return;
 
 	// Second, check CRC
-	uint16_t crc_value = crc16(data,PAYLOAD_SIZE-2);
-	uint16_t crc_rx = ((uint16_t)data[PAYLOAD_SIZE-2] << 8) | data[PAYLOAD_SIZE-1];
+	uint16_t crc_value = crc16(data,FRAMESIZE-2);
+	uint16_t crc_rx = ((uint16_t)data[FRAMESIZE-2] << 8) | data[FRAMESIZE-1];
 
 	if(crc_value != crc_rx)
 		return;
@@ -224,7 +224,7 @@
 			if(Log.variables[ID].type != type)
 				return;
 
-			uint16_t start_address = 4 + DATA_SIZE - 1;
+			uint16_t start_address = DATASTART + DATASIZE - 1;
 
 			// Copy contents directly into variable
 			for(uint16_t i = 0 ; i < Log.variables[ID].size ; i++)
@@ -263,17 +263,17 @@
 		if(current_time < Log.variables[i].last_refreshed + Log.variables[i].refresh_rate)
 			continue;
 			
-		send_variable(i);
+		send_variable(i,current_time);
 		Log.variables[i].last_refreshed = current_time;
 	}
 }
 
-void send_variable(uint16_t index)
+void send_variable(uint16_t index,float extra_identifier_1,uint16_t extra_identifier_2)
 {
 	if(index >= Log.amount)
 		return;
 
-	static uint8_t buffer[PAYLOAD_SIZE];
+	static uint8_t buffer[FRAMESIZE];
 
 	// Response code 0x01
 	buffer[0] = 0x01;
@@ -287,13 +287,25 @@
 	// Write variable type
 	buffer[3] = Log.variables[index].type;
 	//TODO writeable
-
-	uint16_t i = 4;
+	
+	// Extra identifier 1
+	temp_ptr = (uint8_t*)(&extra_identifier_1);
+	buffer[4] = *(temp_ptr + 3);
+	buffer[5] = *(temp_ptr + 2);
+	buffer[6] = *(temp_ptr + 1);
+	buffer[7] = *(temp_ptr    );
+	
+	// Extra identifier 2
+	temp_ptr = (uint8_t*)(&extra_identifier_2);
+	buffer[8] = *(temp_ptr + 1);
+	buffer[9] = *(temp_ptr    );
+	
+	uint16_t i = 10;
 
 	// Write data
-	for(uint16_t k = 0 ; k < DATA_SIZE ; k++)
+	for(uint16_t k = 0 ; k < DATASIZE ; k++)
 	{
-		uint16_t off = DATA_SIZE - 1 - k;
+		uint16_t off = DATASIZE - 1 - k;
 
 		// Fill buffer with data
 		if(off < Log.variables[index].size)
@@ -321,24 +333,18 @@
 
 void send_alive()
 {
-	static uint8_t buffer[PAYLOAD_SIZE] = {0x03,0x00,0x10,0x00,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x00,0x00};
-	
-	uint16_t index = 1;
-	uint16_t group = 0;
-	uint16_t ID = ((group & 0x003F) << 10) + (index & 0x3FF);
-	uint8_t * temp_ptr = (uint8_t*)(&ID);
-	buffer[1] = *(temp_ptr + 1);
-	buffer[2] = *(temp_ptr    );
-
+	static uint8_t buffer[FRAMESIZE];
+	buffer[0] = 0x03;
+	 
 	// Compute crc
-	uint16_t crc_value = crc16(buffer,PAYLOAD_SIZE - 2);
+	uint16_t crc_value = crc16(buffer,FRAMESIZE - 2);
 
 	// Write crc into buffer's last byte
-	buffer[PAYLOAD_SIZE - 1] = crc_value & 0xFF;
-	buffer[PAYLOAD_SIZE - 2] = (crc_value >> 8) & 0xFF;
+	buffer[FRAMESIZE - 1] = crc_value & 0xFF;
+	buffer[FRAMESIZE - 2] = (crc_value >> 8) & 0xFF;
 
 	// Send frame to encoding
-	encode(buffer,PAYLOAD_SIZE);
+	encode(buffer,FRAMESIZE);
 }
 
 
--- a/distantio.h	Thu Oct 08 13:14:32 2015 +0000
+++ b/distantio.h	Mon Oct 12 13:29:40 2015 +0000
@@ -10,10 +10,12 @@
 
 #include <stdint.h>
 
-#define PAYLOAD_SIZE 14
-#define DATA_SIZE 8
+#define FRAMESIZE 20
+#define DATASIZE 8
+#define DATASTART 10
 #define VARIABLES_AMOUNT 256
 #define GROUPS_AMOUNT 128
+#define NAMESIZE 14
 
 typedef enum dio_type dio_type;
 enum dio_type
@@ -35,7 +37,7 @@
 	uint8_t writeable;
 	uint16_t id;
 	dio_type type;
-	char name[8];
+	char name[NAMESIZE];
 	uint8_t send;
 	uint8_t groupID;
 	float refresh_rate;
@@ -45,7 +47,7 @@
 typedef struct group group;
 struct group
 {
-	char name[8];
+	char name[NAMESIZE];
 	uint8_t groupID;
 };