volatile uint32_t invcentory = 100;
volatile uint32_t retailCount = 0;
volatile uint32_t onlineCount = 0;
//句柄值为空
SemaphoreHandle_t mutex_food = NULL;
//线上售货量
void onlinetask(void *pt)
{
while(1)
{
//获取句柄,意味着拿到了钥匙
if(xSemaphoreTake(mutex_food,1000) == pdPASS)
{
uint32_t inv = invcentory;
delay(10);
if(invcentory > 0)
{
invcentory = inv - 1;
onlineCount++;
//释放句柄
xSemaphoreGive(mutex_food);
delay((100));
}
}
}
vTaskDelay(100);
}
void retailtask(void *pt)
{
while(1)
{
//获取句柄,意味着拿到了钥匙
if(xSemaphoreTake(mutex_food,1000) == pdPASS)
{
uint32_t inv1 = invcentory;
delay(10);
if(invcentory > 0)
{
invcentory = inv1 - 1;
retailCount++;
//释放句柄
xSemaphoreGive(mutex_food);
delay((100));
}
}
}
}
//后台监管
void TotalTask(void *pt)
{
while(1)
{
if(xSemaphoreTake(mutex_food,1000) == pdPASS)
{
printf("\n----------------\n");
printf("共销售:%d\n",onlineCount+retailCount);
printf("线上销售:%d\n",onlineCount);
printf("线下销售:%d\n",retailCount);
if(invcentory == 0)
{
printf("共销售:%d\n",onlineCount+retailCount);
}
xSemaphoreGive(mutex_food);
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S3!");
//创建句柄
mutex_food = xSemaphoreCreateMutex();
xTaskCreate(onlinetask,"线上",1024 * 4,NULL,1,NULL);
xTaskCreate(retailtask,"线下",1024 * 4,NULL,1,NULL);
xTaskCreate(TotalTask,"监视",1024 * 4,NULL,1,NULL);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}