Framework for reading and writing variables in real time on any MBED platform.

DistantIO

This is the C implementation of the DistantIO slave framework.

Library is working but slight API breaks may occur in the future. C++ version is also in development.

To get the master-side implementation, see https://github.com/Overdrivr/DistantIO

Revision:
3:135f55b5334e
Parent:
2:f2c816b681e3
Child:
4:d675ad9c57ff
--- 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);
 }