/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : app_freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2026 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 "oled.h"
#include "led.h"
#include "readpin.h"
#include "variables.h"
#include <stdint.h>
#include <stdio.h> 
#include "dma.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

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

/* USER CODE END PM */

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

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t displayHandle;
const osThreadAttr_t display_attributes = {
  .name = "display",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};

osThreadId_t pinreadHandle;
const osThreadAttr_t pinread_attributes = {
  .name = "pinread",
  .priority = (osPriority_t) osPriorityHigh,
  .stack_size = 128 * 4
};

osThreadId_t ledHandle;
const osThreadAttr_t led_attributes = {
  .name = "led",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};

osThreadId_t ADCHandle;
const osThreadAttr_t ADC_attributes = {
  .name = "ADC",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};

osThreadId_t UART_ReceiveHandle;
const osThreadAttr_t UART_Receive_attributes = {
  .name = "UART_Receive",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};

osThreadId_t UART_TransHandle;
const osThreadAttr_t UART_Trans_attributes = {
  .name = "UART_Trans",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};

osThreadId_t UART_O2_receiveHandle;
const osThreadAttr_t UART_O2_receive_attributes = {
  .name = "UART_O2_receive",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};



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

/* USER CODE END FunctionPrototypes */


void pinread_function(void *argument);
void display_function(void *argument);
void led_function(void *argument);
void ADC_function(void *argument);
void UART_Receive_function(void *argument);
void UART_Trans_function(void *argument);
void UART_O2_receive_function(void *argument);

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

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* 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) */
  /* creation of defaultTask */
  
	pinreadHandle = osThreadNew(pinread_function, NULL, &pinread_attributes);
	displayHandle = osThreadNew(display_function, NULL, &display_attributes);
	ledHandle = osThreadNew(led_function, NULL, &led_attributes);
	ADCHandle = osThreadNew(ADC_function, NULL, &ADC_attributes);
	UART_ReceiveHandle = osThreadNew(UART_Receive_function, NULL, &UART_Receive_attributes);
	UART_TransHandle = osThreadNew(UART_Trans_function, NULL, &UART_Trans_attributes);
	UART_O2_receiveHandle = osThreadNew(UART_O2_receive_function, NULL, &UART_O2_receive_attributes);

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

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */


/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void pinread_function(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    pin_value=ReadPin();
		osDelay(50);
  }
  /* USER CODE END StartDefaultTask */
}


void display_function(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
		char result[30]="";
		float voltage,O2;
		plusstring("pin: ", 0,0);
		//plusstring("     ", 20,0);
		sprintf(result, "%d",pin_value);
		plusstring(result, 25,0);
		
		plusstring("ADC: ", 60,0);
		//plusstring("        ", 80,0);		
		sprintf(result, "%d",adc_value);
		plusstring(result, 80,0);
		
		
		plusstring("uart: ", 0,15);	
		//plusstring("        ", 5,15);		
		sprintf(result, "%d",receive);
		plusstring(result, 25,15);		
		
		
		voltage=((float)adc_value/4095)*3.3;
		plusstring("U: ", 65,15);
		//plusstring("        ", 85,15);		
		sprintf(result, "%.2f",voltage);
		plusstring(result, 85,15);	
		
		O2=(float)(((float)O2_H*256+(float)O2_L)/10);
		plusstring("O2: ", 0,30);	
		//plusstring("        ", 20,30);		
		sprintf(result, "%f",O2);
		plusstring(result, 20,30);

    osDelay(200);
  }
  /* USER CODE END StartDefaultTask */
}

void led_function(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
		led(pin_value, 1);
    osDelay(100);
  }
  /* USER CODE END StartDefaultTask */
}

void ADC_function(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
		HAL_ADC_Start(&hadc1);
		adc_value=HAL_ADC_GetValue(&hadc1);
    osDelay(50);
  }
  /* USER CODE END StartDefaultTask */
}

void UART_Receive_function(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
		__HAL_UART_CLEAR_OREFLAG(&huart1);
		HAL_UART_Receive_DMA(&huart1, &receive, 1);
    osDelay(50);
  }
  /* USER CODE END StartDefaultTask */
}

void UART_Trans_function(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {

		trans[2]=pin_value;
		trans[3]=((adc_value>>8)&0xFF);
		trans[4]=(adc_value&0xFF);
		HAL_UART_Transmit_DMA(&huart1, &O2_sensor_receive[0], 9);
    osDelay(300);
  }
	
	
  /* USER CODE END StartDefaultTask */
}


/* USER CODE END Application */
void UART_O2_receive_function(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
		__HAL_UART_CLEAR_OREFLAG(&huart3);
		HAL_UART_Receive_IT(&huart3,&O2_sensor_receive[0],9);
    osDelay(100);
  }
  /* USER CODE END StartDefaultTask */
}
