Full cycle of serdes

This commit is contained in:
Vasily Markov 2024-06-11 20:47:55 +03:00
parent b302c1d306
commit 2c885f73cf
3 changed files with 35 additions and 28 deletions

View File

@ -5,17 +5,21 @@
#include "quaternion.pb.h" #include "quaternion.pb.h"
#define SER_BUF_SIZE 128 #define SER_BUF_SIZE 128
uint8_t serdes_buffer[SER_BUF_SIZE]; #define DESER_BUF_SIZE 128
typedef void (*sd_callback_t)(const uint8_t*, uint16_t);
uint8_t ser_buffer[SER_BUF_SIZE];
uint8_t des_buffer[SER_BUF_SIZE];
static Quaternion quaternion = {0,0,0,0};
uint8_t deserialize(uint8_t* buffer, size_t len) { uint8_t deserialize(uint8_t* buffer, size_t len) {
bool status = 0; bool status = 0;
SimpleMessage message = SimpleMessage_init_zero;
Quaternion quat = Quaternion_init_zero;
pb_istream_t stream = pb_istream_from_buffer(buffer, len); pb_istream_t stream = pb_istream_from_buffer(buffer, len);
status = pb_decode(&stream, Quaternion_fields, &quat); status = pb_decode(&stream, Quaternion_fields, &quaternion);
if (!status) if (!status)
{ {
@ -23,25 +27,20 @@ uint8_t deserialize(uint8_t* buffer, size_t len) {
return 1; return 1;
} }
printf("Quaternion: w: %.2f, x: %.2f, y: %.2f, z: %.2f\r\n", quat.w, quat.x, quat.y, quat.z);
} }
uint8_t serialize(uint8_t* buffer, size_t* len) { uint8_t serialize(uint8_t* buffer, size_t* len) {
size_t message_length = 0;
bool status = 0; bool status = 0;
static uint32_t cnt = 0;
SimpleMessage message = SimpleMessage_init_zero;
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); pb_ostream_t stream = pb_ostream_from_buffer(buffer, &len);
message.number = cnt++; status = pb_encode(&stream, Quaternion_fields, &quaternion);
status = pb_encode(&stream, SimpleMessage_fields, &message);
*len = stream.bytes_written; *len = stream.bytes_written;
if (!status) if (!status)
{ {
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); printf("Encoding failed: %s\r\n", PB_GET_ERROR(&stream));
return 1; return 1;
} }
@ -54,6 +53,24 @@ void commandHandler() {
void sendData(sd_callback_t send_data_callback) { void sendData(sd_callback_t send_data_callback) {
size_t ser_len = 0; size_t ser_len = 0;
serialize(serdes_buffer, &ser_len); serialize(ser_buffer, &ser_len);
send_data_callback(serdes_buffer, ser_len); send_data_callback(ser_buffer, ser_len);
}
void app() {
lwip_status_t* lwip_status = getLwipStatus();
if (lwip_status->udp_packet_rdy == PACKET_RDY) {
lwip_status->udp_packet_rdy = PACKET_NOT_RDY;
udp_receive_buffer_t* udp_buffer = getUdpReceiveBuffer();
deserialize(udp_buffer->buf, udp_buffer->len);
sendData(udp_send_data);
printf("Quaternion: w: %.2f, x: %.2f, y: %.2f, z: %.2f\r\n",
quaternion.w,
quaternion.x,
quaternion.y,
quaternion.z
);
}
} }

View File

@ -2,8 +2,6 @@
#define __APP_H #define __APP_H
#include "bsp.h" #include "bsp.h"
typedef void (*sd_callback_t)(const uint8_t*, uint16_t); void app();
void sendData(sd_callback_t);
uint8_t deserialize(uint8_t* buffer, size_t len);
#endif #endif

View File

@ -5,18 +5,10 @@ int main(void)
{ {
boardInit(); boardInit();
udpServer_init(); udpServer_init();
uint32_t cnt = 0;
char numStr[10];
while (1) while (1)
{ {
lwipProcess(); lwipProcess();
GPIO_ToggleBits(GPIOB, GPIO_Pin_14); app();
if (getLwipStatus()->udp_packet_rdy == PACKET_RDY) {
getLwipStatus()->udp_packet_rdy = PACKET_NOT_RDY;
udp_receive_buffer_t* udp_buffer = getUdpReceiveBuffer();
deserialize(udp_buffer->buf, udp_buffer->len);
}
} }
} }