|
|
@@ -0,0 +1,196 @@
|
|
|
+/**
|
|
|
+ ******************************************************************************
|
|
|
+ * @file App_preheat.c
|
|
|
+ * @brief 预加热控制任务 (App 应用层)
|
|
|
+ *
|
|
|
+ * @note 屏端 (UI) 与上位机命令经队列集 (UI 队列 / 上位机队列) 汇聚,
|
|
|
+ * Task_Preheat 统一按命令驱动状态机:
|
|
|
+ * IDLE = 未预热
|
|
|
+ * RUNNING = 预热中 (控制段为骨架占位, 后续接入
|
|
|
+ * 设定加热块/风扇 → 读反馈温度 → 达标进入下一阶段)
|
|
|
+ ******************************************************************************
|
|
|
+ */
|
|
|
+#include <stdio.h>
|
|
|
+#include "App_preheat.h"
|
|
|
+#include "FreeRTOS.h"
|
|
|
+#include "task.h"
|
|
|
+#include "queue.h"
|
|
|
+
|
|
|
+/* 队列长度 */
|
|
|
+#define APP_PREHEAT_UI_QUEUE_LEN 4 /* 屏端命令队列 */
|
|
|
+#define APP_PREHEAT_UPPER_QUEUE_LEN 8 /* 上位机命令队列 (预留) */
|
|
|
+
|
|
|
+/* 队列集超时: 作为状态"控制周期步进"间隔 (ms) */
|
|
|
+#define APP_PREHEAT_PERIOD_MS 200
|
|
|
+
|
|
|
+/* 任务参数 */
|
|
|
+#define APP_PREHEAT_TASK_PRIORITY 2
|
|
|
+#define APP_PREHEAT_TASK_STACK 512
|
|
|
+
|
|
|
+/* 状态机 */
|
|
|
+#define PREHEAT_STATE_IDLE 0
|
|
|
+#define PREHEAT_STATE_RUNNING 1
|
|
|
+
|
|
|
+/* 队列集成员 */
|
|
|
+static QueueHandle_t s_ui_q = NULL;
|
|
|
+static QueueHandle_t s_upper_q = NULL;
|
|
|
+static QueueSetHandle_t s_qset = NULL;
|
|
|
+
|
|
|
+TaskHandle_t xPreheatTaskHandle = NULL;
|
|
|
+
|
|
|
+/* 当前状态 */
|
|
|
+static uint8_t s_state = PREHEAT_STATE_IDLE;
|
|
|
+static int16_t s_target = 0;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief START 命令处理
|
|
|
+ * @param cmd : 来源命令
|
|
|
+ * @return 无
|
|
|
+ */
|
|
|
+static void preheat_handle_start(const app_preheat_cmd_t *cmd)
|
|
|
+{
|
|
|
+ s_target = cmd->target;
|
|
|
+ printf("[Preheat] start target=%d src=%d\r\n", (int)s_target, (int)cmd->src);
|
|
|
+ s_state = PREHEAT_STATE_RUNNING;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief STOP 命令处理
|
|
|
+ * @param cmd : 来源命令
|
|
|
+ * @return 无
|
|
|
+ */
|
|
|
+static void preheat_handle_stop(const app_preheat_cmd_t *cmd)
|
|
|
+{
|
|
|
+ printf("[Preheat] stop src=%d\r\n", (int)cmd->src);
|
|
|
+ s_state = PREHEAT_STATE_IDLE;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief 控制周期步进 (骨架占位)
|
|
|
+ * @note 后续接入: 设定加热块/风扇 → 等待反馈温度 → 达标进入下一阶段
|
|
|
+ * @param 无
|
|
|
+ * @return 无
|
|
|
+ */
|
|
|
+static void preheat_ctrl_step(void)
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+static void App_preheat_init(void)
|
|
|
+{
|
|
|
+ if (s_qset != NULL)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ s_ui_q = xQueueCreate(APP_PREHEAT_UI_QUEUE_LEN, sizeof(app_preheat_cmd_t));
|
|
|
+ s_upper_q = xQueueCreate(APP_PREHEAT_UPPER_QUEUE_LEN, sizeof(app_preheat_cmd_t));
|
|
|
+
|
|
|
+ s_qset = xQueueCreateSet((APP_PREHEAT_UI_QUEUE_LEN + APP_PREHEAT_UPPER_QUEUE_LEN)
|
|
|
+ * sizeof(app_preheat_cmd_t));
|
|
|
+ if (s_qset == NULL)
|
|
|
+ {
|
|
|
+ printf("[ERR] Preheat queue-set create failed\r\n");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (s_ui_q != NULL)
|
|
|
+ {
|
|
|
+ xQueueAddToSet(s_ui_q, s_qset);
|
|
|
+ }
|
|
|
+ if (s_upper_q != NULL)
|
|
|
+ {
|
|
|
+ xQueueAddToSet(s_upper_q, s_qset);
|
|
|
+ }
|
|
|
+ printf("[OK] Preheat queue-set init\r\n");
|
|
|
+}
|
|
|
+
|
|
|
+BaseType_t App_preheat_send_cmd(app_preheat_cmd_t *cmd)
|
|
|
+{
|
|
|
+ BaseType_t ret;
|
|
|
+
|
|
|
+ if (cmd == NULL)
|
|
|
+ {
|
|
|
+ return pdFAIL;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (cmd->src == APP_PREHEAT_SRC_UPPER)
|
|
|
+ {
|
|
|
+ ret = xQueueSend(s_upper_q, cmd, 0);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ret = xQueueSend(s_ui_q, cmd, 0);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief 预加热任务体: 队列集汇聚多来源命令并周期步进状态机
|
|
|
+ * @note 阻塞 xQueueSelectFromSet() 等待 UI/上位机命令; 200ms 超时视为
|
|
|
+ * 一次控制周期步进 (仅 RUNNING 状态执行); 命令处理逻辑与来源无关。
|
|
|
+ * @param argument : 任务入参 (未使用)
|
|
|
+ * @return 无 (任务不可返回)
|
|
|
+ */
|
|
|
+void Task_Preheat(void *argument)
|
|
|
+{
|
|
|
+ (void)argument;
|
|
|
+ QueueSetMemberHandle_t qm;
|
|
|
+ app_preheat_cmd_t cmd;
|
|
|
+
|
|
|
+ printf("Preheat task running...\r\n");
|
|
|
+
|
|
|
+ while (1)
|
|
|
+ {
|
|
|
+ /* 阻塞等待任一来源命令; 超时则执行控制周期步进 */
|
|
|
+ qm = xQueueSelectFromSet(s_qset, pdMS_TO_TICKS(APP_PREHEAT_PERIOD_MS));
|
|
|
+
|
|
|
+ if (qm == s_ui_q)
|
|
|
+ {
|
|
|
+ xQueueReceive(s_ui_q, &cmd, 0);
|
|
|
+ if (cmd.type == APP_PREHEAT_CMD_START)
|
|
|
+ {
|
|
|
+ preheat_handle_start(&cmd);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ preheat_handle_stop(&cmd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (qm == s_upper_q)
|
|
|
+ {
|
|
|
+ xQueueReceive(s_upper_q, &cmd, 0);
|
|
|
+ if (cmd.type == APP_PREHEAT_CMD_START)
|
|
|
+ {
|
|
|
+ preheat_handle_start(&cmd);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ preheat_handle_stop(&cmd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (s_state == PREHEAT_STATE_RUNNING)
|
|
|
+ {
|
|
|
+ preheat_ctrl_step();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief 创建预加热任务 (内部先初始化队列集, 再创建任务)
|
|
|
+ * @note 在 vTaskStartScheduler() 之前调用一次; 句柄存于 xPreheatTaskHandle。
|
|
|
+ * @param 无
|
|
|
+ * @return 无
|
|
|
+ */
|
|
|
+void App_preheat_task_create(void)
|
|
|
+{
|
|
|
+ BaseType_t ret;
|
|
|
+
|
|
|
+ App_preheat_init();
|
|
|
+
|
|
|
+ ret = xTaskCreate(Task_Preheat, "preheatTask", APP_PREHEAT_TASK_STACK,
|
|
|
+ NULL, APP_PREHEAT_TASK_PRIORITY, &xPreheatTaskHandle);
|
|
|
+ if (ret != pdPASS)
|
|
|
+ {
|
|
|
+ printf("[ERR] Preheat task create failed\r\n");
|
|
|
+ }
|
|
|
+}
|