/*
程序:Message Buffer
*/
#include <freertos/message_buffer.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
MessageBufferHandle_t xMessageBuffer = NULL;
void readGPS(void *pvParam)
{
size_t xBytesSent;//The number of bytes written to the message buffer.
String gpsInfo;
while (1) {
gpsInfo = randomGPS();//随机发送不同长度的信息
xBytesSent = xMessageBufferSend(xMessageBuffer,
(void *)&gpsInfo,
sizeof(gpsInfo),
portMAX_DELAY);
if (xBytesSent != sizeof(gpsInfo)) {
Serial.println("危险:xMessageBufferSend 发送数据不完整");
}
vTaskDelay(3000);
}
}
void showGPS(void *pvParam)
{
size_t xReceivedBytes;
String gpsInfo;
const size_t xMessageSizeMax = 100;
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" GPS INFO");//clear this line
while (1) {
xReceivedBytes = xMessageBufferReceive(xMessageBuffer,
(void *)&gpsInfo,
xMessageSizeMax,//This sets the maximum length of the message that can be received.
portMAX_DELAY);
if(xReceivedBytes > 0){
gpsDecoder(gpsInfo);//解码,并且显示到屏幕上
}
vTaskDelay(1000);
}
}
void monitorTask(void *pvParam)
{
size_t xAvailable,xUsed;
bool isFull;
while(1){
//Queries a stream buffer to see if it is full.
if(xMessageBufferIsFull(xMessageBuffer) == pdTRUE) Serial.println("xMessageBuffer is full");
//Queries a stream buffer to see how much free space it contains
xAvailable = xMessageBufferSpacesAvailable(xMessageBuffer);
char msg[40];
sprintf(msg,"xMessageBuffer可用空间为 %d 字节",xAvailable);
Serial.println(msg);
vTaskDelay(1000);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
const size_t xMessageBufferSizeBytes = 100;
xMessageBuffer = xMessageBufferCreate(xMessageBufferSizeBytes);
if(xMessageBuffer == NULL){
Serial.println("Unable to create message buffer");
}else{
xTaskCreate(readGPS,"Read GPX",1024*4,NULL,1,NULL);
xTaskCreate(showGPS,"Show GPX",1024*4,NULL,1,NULL);
xTaskCreate(monitorTask,"Monitor Message Buffer",1024*8,NULL,1,NULL);//对Stream Buffer进行监控
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}