/* USER CODE BEGIN Header */ /** ****************************************************************************** * File Name : freertos.c * Description : Code for freertos applications ****************************************************************************** * @attention * * Copyright (c) 2026 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "FreeRTOS.h" #include "cmsis_os.h" #include "main.h" #include "task.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "device_ctrl.h" #include "dx_wf_24.h" #include "elog.h" #include "mp3_driver.h" #include "spi_st7735s.h" #include "stdio.h" #include "mp3_play_index.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define TAG "Main" /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN Variables */ /* USER CODE END Variables */ /* Definitions for defaultTask */ osThreadId_t defaultTaskHandle; const osThreadAttr_t defaultTask_attributes = { .name = "defaultTask", .stack_size = 256 * 4, .priority = (osPriority_t)osPriorityHigh1, }; /* Definitions for wifi_mqtt */ osThreadId_t wifi_mqttHandle; const osThreadAttr_t wifi_mqtt_attributes = { .name = "wifi_mqtt", .stack_size = 3000 * 4, .priority = (osPriority_t)osPriorityHigh, }; /* Definitions for LCD_SHOW_Task */ osThreadId_t LCD_SHOW_TaskHandle; const osThreadAttr_t LCD_SHOW_Task_attributes = { .name = "LCD_SHOW_Task", .stack_size = 1024 * 4, .priority = (osPriority_t)osPriorityHigh, }; /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ /* USER CODE END FunctionPrototypes */ void StartDefaultTask(void *argument); extern void wifi_task_mqtt(void *argument); void LCD_Task(void *argument); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ /** * @brief FreeRTOS initialization * @param None * @retval None */ void MX_FREERTOS_Init(void) { /* USER CODE BEGIN Init */ ST7735_Init(); // 初始化ST7735显示屏 /* USER CODE END Init */ /* USER CODE BEGIN RTOS_MUTEX */ /* add mutexes, ... */ /* USER CODE END RTOS_MUTEX */ /* USER CODE BEGIN RTOS_SEMAPHORES */ /* add semaphores, ... */ /* USER CODE END RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */ /* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ /* creation of defaultTask */ defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes); /* creation of wifi_mqtt */ wifi_mqttHandle = osThreadNew(wifi_task_mqtt, NULL, &wifi_mqtt_attributes); /* creation of LCD_SHOW_Task */ LCD_SHOW_TaskHandle = osThreadNew(LCD_Task, NULL, &LCD_SHOW_Task_attributes); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ /* USER CODE END RTOS_THREADS */ /* USER CODE BEGIN RTOS_EVENTS */ /* add events, ... */ /* USER CODE END RTOS_EVENTS */ } /* USER CODE BEGIN Header_StartDefaultTask */ /** * @brief Function implementing the defaultTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void *argument) { /* USER CODE BEGIN StartDefaultTask */ // 1. 打开运行灯 Device_Control(DEVICE_LED_RUN, 1); // 2. 初始化日志和屏幕(在 RTOS 任务中) easylogger_init(); // 初始化MP3模块 HAL_StatusTypeDef ret = MP3_Init(); if (ret != HAL_OK) { elog_e("MP3", "MP3模块初始化失败"); return; } elog_i("MP3", "模块初始化完成"); MP3_Play(SYS_POWER_ON); /* Infinite loop */ for (;;) { Device_Control(DEVICE_LED_RUN, 1); // 打开运行灯 osDelay(1000); Device_Control(DEVICE_LED_RUN, 0); // 关闭运行灯 osDelay(1000); } /* USER CODE END StartDefaultTask */ } /* USER CODE BEGIN Header_LCD_Task */ /** * @brief Function implementing the LCD_SHOW_Task thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_LCD_Task */ void LCD_Task(void *argument) { /* USER CODE BEGIN LCD_Task */ char time_str[16]; SNTP_Time_t now; uint16_t text_color = ST7735_BLACK; uint16_t bg_color = ST7735_WHITE; // 显示时间的区域大小,假设字体16x24 const uint16_t x = 0; const uint16_t y = 0; const uint16_t w = 6 * 16; // 6个字符,每个字符16px宽 const uint16_t h = 24; // 高度与字体匹配 /* Infinite loop */ for (;;) { if (WIFI_Is_Time_Valid()) { WIFI_Get_SNTP_Time(); now = WIFI_Get_Current_Time(); if (now.valid) { snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d", now.hour, now.minute, now.second); } else { snprintf(time_str, sizeof(time_str), "--:--:--"); } // 先清空显示区域 ST7735_FillRectangleFast(x, y, w, h, bg_color); // 写入时间 ST7735_WriteString(x, y, time_str, &Font_16x26, text_color, bg_color); } osDelay(1000); } /* USER CODE END LCD_Task */ } /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ /* USER CODE END Application */