stm32_serDes_protobuf_project/app/app.c

76 lines
1.7 KiB
C
Raw Permalink Normal View History

2024-06-10 18:46:02 +03:00
#include "app.h"
#include <pb_encode.h>
#include <pb_decode.h>
#include "simple.pb.h"
2024-06-11 19:59:25 +03:00
#include "quaternion.pb.h"
2024-06-10 18:46:02 +03:00
#define SER_BUF_SIZE 128
2024-06-11 20:47:55 +03:00
#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};
2024-06-11 19:59:25 +03:00
uint8_t deserialize(uint8_t* buffer, size_t len) {
bool status = 0;
pb_istream_t stream = pb_istream_from_buffer(buffer, len);
2024-06-11 20:47:55 +03:00
status = pb_decode(&stream, Quaternion_fields, &quaternion);
2024-06-11 19:59:25 +03:00
if (!status)
{
printf("Decoding failed: %s\r\n", PB_GET_ERROR(&stream));
return 1;
}
}
2024-06-10 18:46:02 +03:00
uint8_t serialize(uint8_t* buffer, size_t* len) {
bool status = 0;
2024-06-11 20:47:55 +03:00
pb_ostream_t stream = pb_ostream_from_buffer(buffer, &len);
2024-06-11 01:26:20 +03:00
2024-06-11 20:47:55 +03:00
status = pb_encode(&stream, Quaternion_fields, &quaternion);
2024-06-10 18:46:02 +03:00
*len = stream.bytes_written;
if (!status)
{
2024-06-11 20:47:55 +03:00
printf("Encoding failed: %s\r\n", PB_GET_ERROR(&stream));
2024-06-10 18:46:02 +03:00
return 1;
}
return 0;
}
2024-06-11 19:59:25 +03:00
void commandHandler() {
}
2024-06-10 18:46:02 +03:00
void sendData(sd_callback_t send_data_callback) {
size_t ser_len = 0;
2024-06-11 20:47:55 +03:00
serialize(ser_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
);
}
2024-06-10 18:46:02 +03:00
}