/*
程序: Direct Task Notification
要点:
一个任务可以有多个notification
每个notification包含4个字节的value 和 1个字节的stats
stats用来记录当前的notification有没有被处理 pending or not pending
我们不能对stats进行直接的读写操作,是系统自动的
我们只能对notification value 进行操作
本程序演示了对notification的读,写 和 等待。
公众号:孤独的二进制
*/
static TaskHandle_t xFuncExec = NULL;
static TaskHandle_t xTaskGive = NULL;
bool gFuncBits[29] = {0};
void taskWait(void * pvParam) {
bool *pFuncStat;
pFuncStat = (bool *)pvParam;
// for (size_t i = 0; i < 29; i++)
// {
// printf("num%d = %d\n", i, *(pFuncStat + i));
// }
uint32_t ulNotificationValue; //用来存放本任务的4个字节的notification value
BaseType_t xResult;
while (1) {
//vTaskDelay(1000);
xResult = xTaskNotifyWait(0x00, //在运行前这个命令之前,先清除这几位
0x00, //运行后,重置所有的bits 0x00 or ULONG_MAX or 0xFFFFFFFF
&ulNotificationValue, //重置前的notification value
portMAX_DELAY ); //一直等待
if (xResult == pdTRUE) {
// Serial.println(ulNotificationValue,BIN); //将自己的notification value以二进制方式打出来
switch (ulNotificationValue)
{
case BIT0:
Serial.print(*pFuncStat);
if (*(pFuncStat))
{
Serial.println("Func 1 on");
}
else
{
Serial.println("Func 1 off");
}
break;
case BIT1:
if (*(pFuncStat + 1) )
{
Serial.println("Func 2 on");
}
else
{
Serial.println("Func 2 off");
}
break;
case BIT2:
if (*(pFuncStat + 2))
{
Serial.println("Func 3 on");
}
else
{
Serial.println("Func 3 off");
}
break;
case BIT3:
if (*(pFuncStat + 3))
{
Serial.println("Func 4 on");
}
else
{
Serial.println("Func 4 off");
}
break;
case BIT4:
if (*(pFuncStat + 4))
{
Serial.println("Func 5 on");
}
else
{
Serial.println("Func 5 off");
}
break;
}
} else {
Serial.println("Timeout");
}
}
}
void taskGive(void *pvParam) {
// BaseType_t xResult;
while (1) {
uint8_t incom = Serial.read();
switch (incom)
{
case 48://"0"
gFuncBits[0] = !gFuncBits[0];
xTaskNotify( xFuncExec, BIT0, eSetValueWithOverwrite );
break;
case 49:// "1"
gFuncBits[1] = !gFuncBits[1];
xTaskNotify( xFuncExec, BIT1, eSetValueWithOverwrite );
break;
case 50://"2"
gFuncBits[2] = !gFuncBits[2];
xTaskNotify( xFuncExec, BIT2, eSetValueWithOverwrite );
break;
case 51://"3"
Serial.println(gFuncBits[3]);
gFuncBits[3] = !gFuncBits[3];
xTaskNotify( xFuncExec, BIT3, eSetValueWithOverwrite );
break;
case 52://"4"
Serial.println(gFuncBits[4]);
gFuncBits[4] = !gFuncBits[4];
xTaskNotify( xFuncExec, BIT4, eSetValueWithOverwrite );
break;
}
// Serial.println(xResult == pdPASS ? "成功\n":"失败\n");
}
}
void setup() {
Serial.begin(115200);
xTaskCreate(taskGive, "", 1024 * 4, NULL, 1, &xTaskGive);
xTaskCreate(taskWait, "", 1024 * 4, (void *)gFuncBits, 1, &xFuncExec);
vTaskDelete(NULL);
}
void loop() { }