/*
    radio Task
    
*/

/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "dma.h"
#include "usart.h"
#include "module/mr16.h"
#include "bsp/flash.h"
#include "bsp/uart.h"
#include "device/sx1281_driver/radio.h"
#include "device/sx1281_driver/sx1281.h"
#include "module/config.h"
#include "module/mr16.h"
#include "device/ws2812.h"
/* USER INCLUDE END */

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
MR16_t mr16;
uint8_t txdata[1];
/* USER STRUCT END */

/* Private function --------------------------------------------------------- */


/* Exported functions ------------------------------------------------------- */
void Task_radio(void *argument) {
  (void)argument; /* 未使用argument，消除警告 */

  
  /* 计算任务运行到指定频率需要等待的tick数 */
  const uint32_t delay_tick = osKernelGetTickFreq() / RADIO_FREQ;

  osDelay(RADIO_INIT_DELAY); /* 延时一段时间再开启任务 */

  uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
  /* USER CODE INIT BEGIN */
	
__HAL_UART_ENABLE_IT(&huart2,UART_IT_IDLE);
	MR16_Init(&mr16,&Config_Get()->mr16);
	
	// 初始化WS2812状态指示LED (1个LED)
	WS2812_Init(1);
	WS2812_SetBrightness(25);  // 25%亮度,减少功耗对射频干扰
	WS2812_SetCommTimeout(1000);  // 1秒通信超时
	// 初始状态: 根据角色设置
	if (Config_Get()->mr16.radioParams.RadioRole == RadioRoleTX) {
		WS2812_SetStatus(WS2812_STATUS_TX_OK);
	} else {
		WS2812_SetStatus(WS2812_STATUS_RX_OK);
	}
	WS2812_NotifyCommSuccess();  // 初始化通信时间戳
	
	// DBUS精确定时发送: 14ms = 14 ticks (FreeRTOS默认1ms一个tick)
	uint32_t dbus_tick = osKernelGetTickCount();
	const uint8_t DBUS_INTERVAL_TICKS = 14;  // 14ms间隔
  /* USER CODE INIT END */

  while (1) {
    tick += delay_tick; /* 计算下一个唤醒时刻 */

    /* USER CODE BEGIN */
	  
	  MR16_Main(&mr16);
	  //必须加点延迟要不然不能正常跑
	  osDelay(1);
	  
	  // DBUS精确14ms定时发送(使用FreeRTOS tick计数,避免浮点累积误差)
	  uint32_t current_tick = osKernelGetTickCount();
	  if (current_tick - dbus_tick >= DBUS_INTERVAL_TICKS) {
      /* 数据格式: Header(2) + 用户数据 + CRC(2) */
      uint16_t dbusLen;
      if (mr16.param->format == MR16_FORMAT_VARIABLE) {
          /* VARIABLE模式: 使用实际接收到的UART数据长度 */
          extern uint8_t uart2_datalength;
          dbusLen = MR16_HEADER_LEN + uart2_datalength + MR16_CRC_LEN;
      } else {
          /* FIXED模式: 使用固定长度 */
          dbusLen = MR16_HEADER_LEN + mr16.param->fixedLength + MR16_CRC_LEN;
      }
      // DBUS使用中断模式发送(false),DMA留给WS2812使用
      BSP_UART_Transmit(BSP_UART_DBUS, mr16.txbuffer, dbusLen, false);
      
      // 更新下次发送时刻(累加方式,避免漂移)
      dbus_tick += DBUS_INTERVAL_TICKS;
      mr16.timer.lastDbusSend = mr16.timer.now;  // 同步更新浮点时间戳
    }
	  




	mr16.txbuffer[1]=1;

    /* USER CODE END */
    // osDelayUntil(tick); /* 运行结束，等待下一次唤醒 */
  }
  
}