/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @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 "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "adc.h"
#include "usart.h"
#include "control.h"
#include "icd.h"
#include "parse.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
bool TEST_I = false;
//bool TEST_I = true;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
extern adc_struct adc_str;//структура ацп
extern ctrl_struct Control; //FLASH    (rx)
extern icd_str ICD; //
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId defaultTaskHandle;
uint32_t defaultTaskBuffer[ 300 ];
osStaticThreadDef_t defaultTaskControlBlock;
osThreadId OprosTaskHandle;
uint32_t OprosTaskBuffer[ 300 ];
osStaticThreadDef_t OprosTaskControlBlock;
osThreadId ControlTaskHandle;
uint32_t ControlTaskBuffer[ 300 ];
osStaticThreadDef_t ControlTaskControlBlock;
osThreadId LowSpeedTaskHandle;
uint32_t LowSpeedTaskBuffer[ 300 ];
osStaticThreadDef_t LowSpeedTaskControlBlock;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);
void StartOprosTask(void const * argument);
void StartControlTask(void const * argument);
void StartLowSpeedTask(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
	init_icd(&ICD);
	//плавный разряд включен
	HAL_GPIO_WritePin(HV_DIS_GPIO_Port, HV_DIS_Pin, GPIO_PIN_RESET);
  /* 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) */
  /* definition and creation of defaultTask */
  osThreadStaticDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 300, defaultTaskBuffer, &defaultTaskControlBlock);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* definition and creation of OprosTask */
  osThreadStaticDef(OprosTask, StartOprosTask, osPriorityIdle, 0, 300, OprosTaskBuffer, &OprosTaskControlBlock);
  OprosTaskHandle = osThreadCreate(osThread(OprosTask), NULL);

  /* definition and creation of ControlTask */
  osThreadStaticDef(ControlTask, StartControlTask, osPriorityIdle, 0, 300, ControlTaskBuffer, &ControlTaskControlBlock);
  ControlTaskHandle = osThreadCreate(osThread(ControlTask), NULL);

  /* definition and creation of LowSpeedTask */
  osThreadStaticDef(LowSpeedTask, StartLowSpeedTask, osPriorityIdle, 0, 300, LowSpeedTaskBuffer, &LowSpeedTaskControlBlock);
  LowSpeedTaskHandle = osThreadCreate(osThread(LowSpeedTask), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* 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 const * argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(100);
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_StartOprosTask */
/**
* @brief Function implementing the OprosTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartOprosTask */
void StartOprosTask(void const * argument)
{
  /* USER CODE BEGIN StartOprosTask */
	/* Infinite loop */
	for (;;)
	{
		if(adc_str.drdy_trigger)
		{
			adc_str.drdy_trigger = false; //сбросили флаг
			if (ICD.active_ch == 1)
				get_data(&ICD, adc_str.ecg_1_raw);
			if (ICD.active_ch == 2)
				get_data(&ICD, adc_str.ecg_2_raw);
			if (ICD.active_ch == 3)
				get_data(&ICD, adc_str.ecg_3_raw);
			//обычный алгоритм поиска
			search_alg(&ICD);
			ble_HEX_new(&Control, &ICD, &adc_str, true);
		}
		osDelay(1);
	}
  /* USER CODE END StartOprosTask */
}

/* USER CODE BEGIN Header_StartControlTask */
/**
* @brief Function implementing the ControlTask thread.
* @param argument: Not used
* @retval None
*/
//	тут терапия располагается
/* USER CODE END Header_StartControlTask */
void StartControlTask(void const * argument)
{
  /* USER CODE BEGIN StartControlTask */
  /* Infinite loop */
	for (;;)
	{
		terapy_algorithm(&ICD);
		osDelay(100);
	}
  /* USER CODE END StartControlTask */
}

/* USER CODE BEGIN Header_StartLowSpeedTask */
/**
* @brief Function implementing the LowSpeedTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartLowSpeedTask */
void StartLowSpeedTask(void const * argument)
{
  /* USER CODE BEGIN StartLowSpeedTask */
	static uint8_t buff[16];

	HAL_StatusTypeDef status;
  /* Infinite loop */
	for (;;)
	{
		status = HAL_UART_Receive_DMA(&huart1, (uint8_t*)buff, 7);
		osDelay(10);
		if(status == HAL_OK)
		{
			parse_command(buff, &ICD);
		}
	}
  /* USER CODE END StartLowSpeedTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart->Instance == USART1)
  {
    // USART1 завершил прием данных
	 // 1;

//	  тут должет быть парсинг сообщений и стирание старых сообщений

  }
}
/* USER CODE END Application */

