#include "task\user_task.h"
#include "device\can.h"


/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */

CAN_t can;
CAN_RawRx_t can_rx;

/*懒得写config了直接写这里好了*/
CAN_Params_t can_param = {
    .sick = BSP_CAN_1,
    .c_board = BSP_CAN_2,
};

/* Exported functions ------------------------------------------------------- */
void Task_Can(void *argument) {
    (void)argument; // 消除未使用参数的警告
    const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_CAN; // 100Hz
    uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */

    // 初始化 CAN
    CAN_Init(&can, &can_param);
    while (1) {
        tick += delay_tick; /* 计算下一个唤醒时刻 */
        while (osMessageQueueGet(can.msgq_raw, &can_rx, 0, 0) == osOK) {
            CAN_StoreMsg(&can, &can_rx);
          }
        // 发送数据
        osMessageQueueReset(task_runtime.msgq.can.feedback.sick);
        osMessageQueuePut(task_runtime.msgq.can.feedback.sick, &can, 0, 0);
    
        osMessageQueueReset(task_runtime.msgq.pc);
        osMessageQueuePut(task_runtime.msgq.pc, &can, 0, 0);
        
        CAN_Sick_Send(&can);
        
        osDelayUntil(tick); /* 运行结束，等待下一次唤醒 */
    }
}