callbacks sending data

This commit is contained in:
Vasily Markov 2024-06-10 18:46:02 +03:00
parent e94442e12b
commit a9f146ef4d
7 changed files with 71 additions and 41 deletions

View File

@ -5,7 +5,8 @@ find_package(Nanopb REQUIRED)
nanopb_generate_cpp(TARGET proto simple.proto) nanopb_generate_cpp(TARGET proto simple.proto)
add_executable(${elf_file} add_executable(${elf_file}
app.c
main.c main.c
syscalls.c syscalls.c
) )

42
app/app.c Normal file
View File

@ -0,0 +1,42 @@
#include "app.h"
#include <pb_encode.h>
#include <pb_decode.h>
#include "simple.pb.h"
#define SER_BUF_SIZE 128
uint8_t ser_buffer[SER_BUF_SIZE];
uint8_t serialize(uint8_t* buffer, size_t* len) {
size_t message_length = 0;
bool status = 0;
static uint32_t cnt = 0;
SimpleMessage message = SimpleMessage_init_zero;
/* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
/* Fill in the lucky number */
message.number = cnt++;
/* Now we are ready to encode the message! */
status = pb_encode(&stream, SimpleMessage_fields, &message);
*len = stream.bytes_written;
/* Then just check for any errors.. */
if (!status)
{
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
return 1;
}
return 0;
}
void sendData(sd_callback_t send_data_callback) {
size_t ser_len = 0;
serialize(ser_buffer, &ser_len);
send_data_callback(ser_buffer, ser_len);
}

8
app/app.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef __APP_H
#define __APP_H
#include "bsp.h"
typedef void (*sd_callback_t)(const uint8_t*, uint16_t);
void sendData(sd_callback_t);
#endif

View File

@ -1,37 +1,7 @@
#include "../bsp/bsp.h" #include "../bsp/bsp.h"
#include <pb_encode.h> #include "app.h"
#include <pb_decode.h>
#include "simple.pb.h"
void serialzie() {
/* This is the buffer where we will store our message. */
uint8_t buffer[128];
size_t message_length;
bool status;
/* Encode our message */
{
SimpleMessage message = SimpleMessage_init_zero;
/* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
/* Fill in the lucky number */
message.number = 42;
/* Now we are ready to encode the message! */
status = pb_encode(&stream, SimpleMessage_fields, &message);
message_length = stream.bytes_written;
udp_send_data(buffer, message_length);
/* Then just check for any errors.. */
if (!status)
{
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
return 1;
}
}
return 0;
}
int main(void) int main(void)
{ {
@ -43,12 +13,7 @@ int main(void)
while (1) while (1)
{ {
lwipProcess(); lwipProcess();
const char *data = "Hello, world!"; sendData(udp_send_data);
sprintf(numStr, "%d", cnt++);
char* strCat = strcat(data, numStr);
// udp_send_data(numStr, strlen(numStr));
serialzie();
// printf("Test\r\n");
GPIO_ToggleBits(GPIOB, GPIO_Pin_14); GPIO_ToggleBits(GPIOB, GPIO_Pin_14);
delay(100); delay(100);
} }

View File

@ -3,12 +3,27 @@
#define kHz_1 1000 #define kHz_1 1000
static USART_TypeDef* dbg_uart = NULL;
static lwip_status_t lwip_status = {.link_status = LINK_DOWN}; static lwip_status_t lwip_status = {.link_status = LINK_DOWN};
lwip_status_t* getLwipStatus() { lwip_status_t* getLwipStatus() {
return &lwip_status; return &lwip_status;
} }
void register_dbg_uart(USART_TypeDef* uart) {
dbg_uart = uart;
}
void send_uart(const uint8_t* data, uint8_t len) {
uint8_t i = 0;
while(i < len) {
while (USART_GetFlagStatus(dbg_uart, USART_FLAG_TXE) == RESET);
USART_SendData(dbg_uart, *data++);
i++;
}
}
void sysInit() { void sysInit() {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
@ -134,6 +149,7 @@ void boardInit() {
sysInit(); sysInit();
gpioInit(); gpioInit();
uartInit(); uartInit();
register_dbg_uart(USART2);
ethInit(); ethInit();
tim_init(); tim_init();
lwipInit(); lwipInit();

View File

@ -22,5 +22,5 @@ typedef struct {
void boardInit(); void boardInit();
lwip_status_t* getLwipStatus(); lwip_status_t* getLwipStatus();
void send_uart(const uint8_t*, uint8_t);
#endif #endif

View File

@ -83,8 +83,6 @@ void udp_send_data(const char *data, u16_t len)
} }
} }
void lwipInit(void) void lwipInit(void)
{ {
IP_ADDRESS[0] = 192; IP_ADDRESS[0] = 192;