generated from Template/H563ZI-HAL-CMake-Template
	
		
			
				
	
	
		
			158 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			158 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /* USER CODE BEGIN Header */
 | ||
| /**
 | ||
|   ******************************************************************************
 | ||
|   * @file    app_threadx.c
 | ||
|   * @author  MCD Application Team
 | ||
|   * @brief   ThreadX applicative file
 | ||
|   ******************************************************************************
 | ||
|     * @attention
 | ||
|   *
 | ||
|   * Copyright (c) 2025 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 "app_threadx.h"
 | ||
| 
 | ||
| /* Private includes ----------------------------------------------------------*/
 | ||
| /* USER CODE BEGIN Includes */
 | ||
| 
 | ||
| /* USER CODE END Includes */
 | ||
| 
 | ||
| /* Private typedef -----------------------------------------------------------*/
 | ||
| /* USER CODE BEGIN PTD */
 | ||
| 
 | ||
| /* USER CODE END PTD */
 | ||
| 
 | ||
| /* Private define ------------------------------------------------------------*/
 | ||
| /* USER CODE BEGIN PD */
 | ||
| #define BLE_RX_THREAD_STACK_SIZE 2048
 | ||
| #define BLE_RX_THREAD_PRIORITY    10
 | ||
| 
 | ||
| #define BLE_TX_THREAD_STACK_SIZE 2048
 | ||
| #define BLE_TX_THREAD_PRIORITY    10
 | ||
| /* USER CODE END PD */
 | ||
| 
 | ||
| /* Private macro -------------------------------------------------------------*/
 | ||
| /* USER CODE BEGIN PM */
 | ||
| 
 | ||
| /* USER CODE END PM */
 | ||
| 
 | ||
| /* Private variables ---------------------------------------------------------*/
 | ||
| /* USER CODE BEGIN PV */
 | ||
| /* ȫ<>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
 | ||
| TX_QUEUE ble_tx_queue;
 | ||
| TX_EVENT_FLAGS_GROUP system_events;
 | ||
| MotorCommand current_motor_cmd = {0,0};
 | ||
| _GPSData gps_data;
 | ||
| 
 | ||
| TX_THREAD ble_rx_thread;
 | ||
| UCHAR ble_rx_stack[BLE_RX_THREAD_STACK_SIZE];
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| #define BLE_TX_QUEUE_DEPTH 4
 | ||
| UCHAR ble_tx_queue_buffer[BLE_TX_QUEUE_DEPTH * sizeof(BLE_Message)];
 | ||
| /* USER CODE END PV */
 | ||
| 
 | ||
| /* Private function prototypes -----------------------------------------------*/
 | ||
| /* USER CODE BEGIN PFP */
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| /* USER CODE END 1 */
 | ||
| /* USER CODE END 1 */
 | ||
| 
 | ||
| /* USER CODE END PFP */
 | ||
| 
 | ||
| /**
 | ||
|   * @brief  Application ThreadX Initialization.
 | ||
|   * @param memory_ptr: memory pointer
 | ||
|   * @retval int
 | ||
|   */
 | ||
| UINT App_ThreadX_Init(VOID *memory_ptr)
 | ||
| {
 | ||
|     HCBle_SendData("进入 App_ThreadX_Init\r\n");
 | ||
| 
 | ||
| 
 | ||
|     return TX_SUCCESS;
 | ||
| }
 | ||
| 
 | ||
| 
 | ||
|   /**
 | ||
|   * @brief  Function that implements the kernel's initialization.
 | ||
|   * @param  None
 | ||
|   * @retval None
 | ||
|   */
 | ||
| void MX_ThreadX_Init(void)
 | ||
| {
 | ||
|   /* USER CODE BEGIN Before_Kernel_Start */
 | ||
| 
 | ||
|   /* USER CODE END Before_Kernel_Start */
 | ||
| 
 | ||
|   tx_kernel_enter();
 | ||
| 
 | ||
|   /* USER CODE BEGIN Kernel_Start_Error */
 | ||
| 
 | ||
|   /* USER CODE END Kernel_Start_Error */
 | ||
| }
 | ||
| 
 | ||
| /* USER CODE BEGIN 1 */
 | ||
| #ifdef TEST  //Ŀǰ<C4BF><C7B0><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| /* USER CODE BEGIN 1 */
 | ||
| /* USER CODE BEGIN 1 */
 | ||
| void main_control_thread_entry(ULONG thread_input)
 | ||
| {
 | ||
|     ULONG events;
 | ||
|     MotorCommand motor_cmd = {0, 0};
 | ||
|     
 | ||
|     while(1)
 | ||
|     {
 | ||
|         // 等待事件发生
 | ||
|         tx_event_flags_get(&system_events, 
 | ||
|                           EVENT_OBSTACLE_DETECTED | EVENT_BLE_COMMAND_RECEIVED,
 | ||
|                           TX_OR_CLEAR, &events, TX_WAIT_FOREVER);
 | ||
|         
 | ||
|         // 处理障碍物事件
 | ||
|         if(events & EVENT_OBSTACLE_DETECTED)
 | ||
|         {
 | ||
|             // 触发蜂鸣器和振动提示
 | ||
|             Buzzer_Open();
 | ||
|             Shake_Motor_Open();
 | ||
|             tx_thread_sleep(100); // 振动100ms
 | ||
|             Buzzer_Close();
 | ||
|             Shake_Motor_Close();
 | ||
|             
 | ||
|             // 发送报警到手机
 | ||
|             BLE_Message msg;
 | ||
|             msg.msg_type = 2;
 | ||
|             snprintf(msg.data, sizeof(msg.data), "#{\"alert\":\"Obstacle detected!\"}\n");
 | ||
|             tx_queue_send(&ble_tx_queue, &msg, TX_WAIT_FOREVER);
 | ||
|         }
 | ||
|         
 | ||
|         // 处理蓝牙控制指令
 | ||
|         if(events & EVENT_BLE_COMMAND_RECEIVED)
 | ||
|         {
 | ||
|             // 从队列获取电机命令
 | ||
| //            tx_queue_receive(&ble_rx_queue, &motor_cmd, TX_WAIT_FOREVER);
 | ||
|             
 | ||
|             // 实际控制电机 (伪代码)
 | ||
|             // set_motor_speed(motor_cmd.leftSpeed, motor_cmd.rightSpeed);
 | ||
|         }
 | ||
|     }
 | ||
| }
 | ||
| #endif
 | ||
| /* USER CODE END 1 */
 |