/* The example of ESP-IDF
*
* This sample code is in the public domain.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/stream_buffer.h"
#include "esp_log.h"
StreamBufferHandle_t xStreamBuffer = NULL;
void ReceivingTask(void *pvParameters)
{
ESP_LOGI(pcTaskGetName(NULL), "Start");
char cRxBuffer[
20 ];
memset(
cRxBuffer, 0x00, sizeof( cRxBuffer ) );
while(1) {
size_t readBytes = xStreamBufferReceive(xStreamBuffer,
cRxBuffer,
sizeof(cRxBuffer),
pdMS_TO_TICKS(1000) );
ESP_LOGD(pcTaskGetName(NULL), "readBytes=%d",
readBytes);
if (readBytes != 0) {
ESP_LOGI(pcTaskGetName(NULL), "readBytes=%d
cRxBuffer=[%.*s]", readBytes, readBytes, cRxBuffer);
}
}
}
void SendingTask(void *pvParameters)
{
ESP_LOGI(pcTaskGetName(NULL),"Start");
static size_t
xNextByteToSend = 0;
static size_t
xTotalByteToSend = 0;
const
BaseType_t xBytesToSend = 2;
static const
char * pcStringToSend = "_____Hello FreeRTOS_____";
ESP_LOGI(pcTaskGetName(NULL),"length=%d",
strlen(pcStringToSend));
for(int i=0;
i<strlen(pcStringToSend); i++) {
xStreamBufferSend( xStreamBuffer,
( void * ) ( pcStringToSend + xNextByteToSend ),
xBytesToSend,
portMAX_DELAY );
xTotalByteToSend += xBytesToSend;
ESP_LOGI(pcTaskGetName(NULL),"xTotalByteToSend=%d",
xTotalByteToSend);
xNextByteToSend += xBytesToSend;
if( xNextByteToSend >= strlen( pcStringToSend ) )
{
xNextByteToSend = 0;
}
vTaskDelay(10);
}
vTaskDelete(NULL);
}
void app_main()
{
// Get current
priority
UBaseType_t
priority = uxTaskPriorityGet(NULL);
ESP_LOGI(pcTaskGetName(NULL), "priority=%d",
priority);
// Create
Stream Buffer
xStreamBuffer =
xStreamBufferCreate(100, 10);
// Check
everything was created
configASSERT(
xStreamBuffer );
/* Create task
*/
xTaskCreate(&SendingTask, "SEND", 1024*2, NULL,
priority, NULL);
xTaskCreate(&ReceivingTask, "RECV", 1024*2, NULL,
priority, NULL);
}
|