#pragma once

#ifdef __cplusplus
extern "C" {
#endif
/* Includes ----------------------------------------------------------------- */
#include <cmsis_os2.h>
#include "FreeRTOS.h"
#include "task.h"

/* USER INCLUDE BEGIN */

/* USER INCLUDE END */
/* Exported constants ------------------------------------------------------- */
/* 任务运行频率 */
#define ATTI_ESTI_FREQ (1000.0)
#define BLINK_FREQ (100.0)
#define HEIGHT_CAL_FREQ (500.0)
#define QUAD_CTRL_FREQ (1000.0)
#define CMD_FREQ (500.0)

/* 任务初始化延时ms */
#define TASK_INIT_DELAY (100u)
#define ATTI_ESTI_INIT_DELAY (0)
#define BLINK_INIT_DELAY (0)
#define HEIGHT_CAL_INIT_DELAY (500)
#define QUAD_CTRL_INIT_DELAY (3000)
#define CMD_INIT_DELAY (0)

/* Exported defines --------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */

/* 任务运行时结构体 */
typedef struct {
    /* 各任务，也可以叫做线程 */
    struct {
        osThreadId_t atti_esti;
        osThreadId_t blink;
        osThreadId_t height_cal;
        osThreadId_t quad_ctrl;
        osThreadId_t cmd;
    } thread;

    /* USER MESSAGE BEGIN */

    struct {
        osMessageQueueId_t user_msg; /* 用户自定义任务消息队列 */
        struct 
        {
            osMessageQueueId_t accl;  
			osMessageQueueId_t quaternion;
        }heightestimation;
		
		struct{
			osMessageQueueId_t eulr;
            osMessageQueueId_t accl;
            osMessageQueueId_t gyro;
			osMessageQueueId_t alt;
			osMessageQueueId_t vel;
			osMessageQueueId_t cmd;
		}quad;
    } msgq;

    /* USER MESSAGE END */

    /* 机器人状态 */
    struct {
        float battery; /* 电池电量百分比 */
        float vbat; /* 电池电压 */
        float cpu_temp; /* CPU温度 */
    } status;

    /* USER CONFIG BEGIN */

    /* USER CONFIG END */

    /* 各任务的stack使用 */
    struct {
        UBaseType_t atti_esti;
        UBaseType_t blink;
        UBaseType_t height_cal;
        UBaseType_t quad_ctrl;
        UBaseType_t cmd;
    } stack_water_mark;

    /* 各任务运行频率 */
    struct {
        float atti_esti;
        float blink;
        float height_cal;
        float quad_ctrl;
        float cmd;
    } freq;

    /* 任务最近运行时间 */
    struct {
        float atti_esti;
        float blink;
        float height_cal;
        float quad_ctrl;
        float cmd;
    } last_up_time;

} Task_Runtime_t;

/* 任务运行时结构体 */
extern Task_Runtime_t task_runtime;

/* 初始化任务句柄 */
extern const osThreadAttr_t attr_init;
extern const osThreadAttr_t attr_atti_esti;
extern const osThreadAttr_t attr_blink;
extern const osThreadAttr_t attr_height_cal;
extern const osThreadAttr_t attr_quad_ctrl;
extern const osThreadAttr_t attr_cmd;

/* 任务函数声明 */
void Task_Init(void *argument);
void Task_atti_esti(void *argument);
void Task_blink(void *argument);
void Task_height_cal(void *argument);
void Task_quad_ctrl(void *argument);
void Task_cmd(void *argument);

#ifdef __cplusplus
}
#endif