|
|
@ -1,5 +1,6 @@
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef linux
|
|
|
|
#ifdef linux
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
@ -11,29 +12,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Takes in float values, casts them to uint16_t and creates a Serial_Data struct */
|
|
|
|
/* Takes in float values, casts them to uint16_t and creates a Serial_Data struct */
|
|
|
|
Serial_Data Serial_create_data(float pad_x, float pad_y, float ball_x, float ball_y) {
|
|
|
|
Serial_Data Serial_create_data(float pad_x, float pad_y, float ball_x, float ball_y, bool should_quit) {
|
|
|
|
Serial_Data data;
|
|
|
|
Serial_Data data;
|
|
|
|
data.pad_x = (uint16_t)pad_x;
|
|
|
|
data.pad_x = (uint16_t)pad_x;
|
|
|
|
data.pad_y = (uint16_t)pad_y;
|
|
|
|
data.pad_y = (uint16_t)pad_y;
|
|
|
|
data.ball_x = (uint16_t)ball_x;
|
|
|
|
data.ball_x = (uint16_t)ball_x;
|
|
|
|
data.ball_y = (uint16_t)ball_y;
|
|
|
|
data.ball_y = (uint16_t)ball_y;
|
|
|
|
|
|
|
|
data.should_quit = should_quit;
|
|
|
|
return data;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Serializes a 'Data' struct into a byte array, converted to network-byte order */
|
|
|
|
/* Serializes a 'Data' struct into a byte array, converted to network-byte order */
|
|
|
|
uint8_t* Serial_serialize(Serial_Data data) {
|
|
|
|
uint8_t* Serial_serialize(Serial_Data data) {
|
|
|
|
/* Create a pointer that can fit the entire struct */
|
|
|
|
/* Create a pointer that can fit the entire struct */
|
|
|
|
uint8_t* serialized = malloc(4 * sizeof(uint16_t) + 1);
|
|
|
|
uint8_t* serialized = malloc(sizeof(Serial_Data) + 1);
|
|
|
|
uint8_t* pad_x_ptr;
|
|
|
|
uint8_t* pad_x_ptr;
|
|
|
|
uint8_t* pad_y_ptr;
|
|
|
|
uint8_t* pad_y_ptr;
|
|
|
|
uint8_t* ball_x_ptr;
|
|
|
|
uint8_t* ball_x_ptr;
|
|
|
|
uint8_t* ball_y_ptr;
|
|
|
|
uint8_t* ball_y_ptr;
|
|
|
|
memset(serialized, 0, 4 * sizeof(uint16_t) + 1); // Zero out the memory
|
|
|
|
uint8_t* should_quit_ptr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
memset(serialized, 0, sizeof(Serial_Data) + 1); // Zero out the memory
|
|
|
|
pad_x_ptr = serialized;
|
|
|
|
pad_x_ptr = serialized;
|
|
|
|
pad_y_ptr = pad_x_ptr + sizeof(uint16_t);
|
|
|
|
pad_y_ptr = pad_x_ptr + sizeof(uint16_t);
|
|
|
|
ball_x_ptr = pad_y_ptr + sizeof(uint16_t);
|
|
|
|
ball_x_ptr = pad_y_ptr + sizeof(uint16_t);
|
|
|
|
ball_y_ptr = ball_x_ptr + sizeof(uint16_t);
|
|
|
|
ball_y_ptr = ball_x_ptr + sizeof(uint16_t);
|
|
|
|
|
|
|
|
should_quit_ptr = ball_y_ptr + sizeof(uint16_t);
|
|
|
|
|
|
|
|
|
|
|
|
*((uint16_t *)pad_x_ptr) = data.pad_x;
|
|
|
|
*((uint16_t *)pad_x_ptr) = data.pad_x;
|
|
|
|
*((uint16_t *)pad_x_ptr) = htons(*((uint16_t *)pad_x_ptr));
|
|
|
|
*((uint16_t *)pad_x_ptr) = htons(*((uint16_t *)pad_x_ptr));
|
|
|
@ -46,7 +50,9 @@ uint8_t* Serial_serialize(Serial_Data data) {
|
|
|
|
|
|
|
|
|
|
|
|
*((uint16_t *)ball_y_ptr) = data.ball_y;
|
|
|
|
*((uint16_t *)ball_y_ptr) = data.ball_y;
|
|
|
|
*((uint16_t *)ball_y_ptr) = htons(*((uint16_t *)ball_y_ptr));
|
|
|
|
*((uint16_t *)ball_y_ptr) = htons(*((uint16_t *)ball_y_ptr));
|
|
|
|
*(ball_y_ptr + sizeof(uint16_t)) = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
*((bool *)should_quit_ptr) = data.should_quit;
|
|
|
|
|
|
|
|
*(should_quit_ptr + sizeof(bool)) = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
return serialized;
|
|
|
|
return serialized;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -59,6 +65,7 @@ Serial_Data Serial_deserialize(uint8_t* serialized) {
|
|
|
|
uint8_t* pad_y_ptr = serialized + sizeof(uint16_t);
|
|
|
|
uint8_t* pad_y_ptr = serialized + sizeof(uint16_t);
|
|
|
|
uint8_t* ball_x_ptr = pad_y_ptr + sizeof(uint16_t);
|
|
|
|
uint8_t* ball_x_ptr = pad_y_ptr + sizeof(uint16_t);
|
|
|
|
uint8_t* ball_y_ptr = ball_x_ptr + sizeof(uint16_t);
|
|
|
|
uint8_t* ball_y_ptr = ball_x_ptr + sizeof(uint16_t);
|
|
|
|
|
|
|
|
uint8_t* should_quit_ptr = ball_y_ptr + sizeof(uint16_t);
|
|
|
|
|
|
|
|
|
|
|
|
/* Dereference (and cast) the pointers, and store them into the struct */
|
|
|
|
/* Dereference (and cast) the pointers, and store them into the struct */
|
|
|
|
deserialized.pad_x = *((uint16_t *)pad_x_ptr);
|
|
|
|
deserialized.pad_x = *((uint16_t *)pad_x_ptr);
|
|
|
@ -73,6 +80,8 @@ Serial_Data Serial_deserialize(uint8_t* serialized) {
|
|
|
|
deserialized.ball_y = *((uint16_t *)ball_y_ptr);
|
|
|
|
deserialized.ball_y = *((uint16_t *)ball_y_ptr);
|
|
|
|
deserialized.ball_y = ntohs(deserialized.ball_y);
|
|
|
|
deserialized.ball_y = ntohs(deserialized.ball_y);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deserialized.should_quit = *((bool *)should_quit_ptr);
|
|
|
|
|
|
|
|
|
|
|
|
return deserialized;
|
|
|
|
return deserialized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|