Les Message Buffers
Les message buffers sont construits sur la base des stream buffers et permettent de communiquer avec des messages de tailles variables.
On crée un message buffer avec la fonction xMessageBufferCreate()
ou xMessageBufferCreateWithCallback()
:
MessageBufferHandle_t xMessageBufferCreate(size_t xBufferSizeBytes);
MessageBufferHandle_t xMessageBufferCreateWithCallback(
size_t xBufferSizeBytes,
StreamBufferCallbackFunction_t pxSendCompletedCallback,
StreamBufferCallbackFunction_t pxReceiveCompletedCallback);
On utilise la fonction xMessageBufferSend()
pour envoyer des messages :
size_t xMessageBufferSend(MessageBufferHandle_t xMessageBuffer,
const void* pvTxData,
size_t xDataLengthBytes,
TickType_t xTicksToWait);
Exemple:
void vAFunction(MessageBufferHandle_t xMessageBuffer) {
size_t xBytesSent;
uint8_t ucArrayToSend[] = {0, 1, 2, 3};
char* pcStringToSend = "String to send";
const TickType_t x100ms = pdMS_TO_TICKS(100);
// Send an array to the message buffer, blocking for a maximum of 100ms to
// wait for enough space to be available in the message buffer.
xBytesSent = xMessageBufferSend(
xMessageBuffer, (void*)ucArrayToSend, sizeof(ucArrayToSend), x100ms);
if (xBytesSent != sizeof(ucArrayToSend)) {
// The call to xMessageBufferSend() times out before there was enough
// space in the buffer for the data to be written.
}
// Send the string to the message buffer. Return immediately if there is
// not enough space in the buffer.
xBytesSent = xMessageBufferSend(
xMessageBuffer, (void*)pcStringToSend, strlen(pcStringToSend), 0);
if (xBytesSent != strlen(pcStringToSend)) {
// The string could not be added to the message buffer because there was
// not enough free space in the buffer.
}
}
On reçoit des messages avec la fonction xMessageBufferReceive()
:
size_t xMessageBufferReceive(MessageBufferHandle_t xMessageBuffer,
void* pvRxData,
size_t xBufferLengthBytes,
TickType_t xTicksToWait);
Exemple :
void vAFunction(MessageBuffer_t xMessageBuffer) {
uint8_t ucRxData[20];
size_t xReceivedBytes;
const TickType_t xBlockTime = pdMS_TO_TICKS(20);
// Receive the next message from the message buffer. Wait in the Blocked
// state (so not using any CPU processing time) for a maximum of 100ms for
// a message to become available.
xReceivedBytes = xMessageBufferReceive(
xMessageBuffer, (void*)ucRxData, sizeof(ucRxData), xBlockTime);
if (xReceivedBytes > 0) {
// A ucRxData contains a message that is xReceivedBytes long. Process
// the message here....
}
}