Files

79 lines
1.9 KiB
C++
Raw Permalink Normal View History

2026-04-26 16:24:42 +08:00
#include "work_task.h"
#include "monitor_task.h"
#include "led_driver.h"
#include <cstdio>
2026-04-26 16:24:42 +08:00
#define WORK_TASK_PRIORITY (RT_THREAD_PRIORITY_MAX - 3)
#define WORK_TASK_STACK_SIZE 1024
extern Led led1;
rt_thread_t work_thread = RT_NULL;
rt_mailbox_t work_mb = RT_NULL;
2026-04-26 16:24:42 +08:00
static const char *work_descriptions[] = {
"Calibrating sensors...",
"Processing data...",
"Analyzing results...",
"Generating report...",
"Task complete!",
};
static void work_work(void *parameter)
2026-04-26 16:24:42 +08:00
{
(void)parameter;
while (1)
{
rt_ubase_t msg;
if (rt_mb_recv(work_mb, &msg, RT_WAITING_FOREVER) == RT_EOK)
{
printf("\r\n[WORK] Received job #%lu, starting...\r\n", (unsigned long)msg);
for (int step = 0; step < 5; step++)
{
led1.toggle();
g_work_progress = step * 25;
printf("[WORK] Step %d/5: %s (%d%%)\r\n",
step + 1, work_descriptions[step], g_work_progress);
rt_thread_delay(RT_TICK_PER_SECOND / 2);
}
g_work_progress = -1;
led1.off();
printf("[WORK] Job #%lu finished!\r\n\r\n", (unsigned long)msg);
}
}
2026-04-26 16:24:42 +08:00
}
RetCode work_task_init(void)
2026-04-26 16:24:42 +08:00
{
work_mb = rt_mb_create("work_mb", 8, RT_IPC_FLAG_FIFO);
if (work_mb == RT_NULL)
return RET_ERROR;
work_thread = rt_thread_create("worker",
work_work,
RT_NULL,
WORK_TASK_STACK_SIZE,
WORK_TASK_PRIORITY,
10);
if (work_thread == RT_NULL)
return RET_ERROR;
rt_thread_startup(work_thread);
2026-04-26 16:24:42 +08:00
return RET_OK;
}
void work_task_send(uint32_t msg)
2026-04-26 16:24:42 +08:00
{
if (work_mb != RT_NULL)
{
rt_mb_send(work_mb, (rt_ubase_t)msg);
2026-04-26 16:24:42 +08:00
}
}