A simple library for building and parsing data frames for serial interfaces (like UART / RS232)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TinyFrame/demo/simple/test.c

66 lines
1.3 KiB

7 years ago
#include <stdio.h>
#include <string.h>
#include "../../TinyFrame.h"
#include "../utils.h"
7 years ago
/**
* This function should be defined in the application code.
* It implements the lowest layer - sending bytes to UART (or other)
*/
void TF_WriteImpl(const uint8_t *buff, size_t len)
{
printf("--------------------\n");
printf("\033[32mTF_WriteImpl - sending frame:\033[0m\n");
dumpFrame(buff, len);
// Send it back as if we received it
TF_Accept(buff, len);
}
7 years ago
/** An example listener function */
7 years ago
TF_Result myListener(TF_Msg *msg)
7 years ago
{
dumpFrameInfo(msg);
7 years ago
return TF_STAY;
7 years ago
}
7 years ago
TF_Result testIdListener(TF_Msg *msg)
7 years ago
{
printf("OK - ID Listener triggered for msg!\n");
dumpFrameInfo(msg);
7 years ago
return TF_CLOSE;
}
7 years ago
void main(void)
{
7 years ago
TF_Msg msg;
const char *longstr = "Lorem ipsum dolor sit amet.";
// Set up the TinyFrame library
TF_Init(TF_MASTER); // 1 = master, 0 = slave
TF_AddGenericListener(myListener);
7 years ago
printf("------ Simulate sending a message --------\n");
7 years ago
7 years ago
TF_ClearMsg(&msg);
msg.type = 0x22;
msg.data = (pu8)"Hello TinyFrame";
msg.len = 16;
TF_Send(&msg);
7 years ago
msg.type = 0x33;
msg.data = (pu8)longstr;
msg.len = (TF_LEN) (strlen(longstr)+1); // add the null byte
TF_Send(&msg);
7 years ago
msg.type = 0x44;
msg.data = (pu8)"Hello2";
msg.len = 7;
TF_Send(&msg);
7 years ago
msg.len = 0;
msg.type = 0x77;
TF_Query(&msg, testIdListener, 0);
7 years ago
}