/*
 * user.c
 *
 *  Created on: Apr 21, 2025
 *      Author: andre
 */

#include "main.h"
#include "user.h"
#include "uart.h"
#include "adc.h"
#include "settings.h"

extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim3;
extern SettingsStruct Settings;
extern RC_LinkStatistics LinkStatistics;

uint8_t RC_LinkUpAtLeastOnce = 0;

void USER_Init(void) {
	unsigned int ChannelValue;
	unsigned int ch;

	SettingsInit();

	for(ch=0; ch<5; ch++) {
		ChannelValue = Settings.start[ch];
		if (ChannelValue > 0) {
			USER_SetPWM(ch, ChannelValue);
		} else {
			USER_SetPWM(ch, 1000);
		}
	}

	TIM3->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_1 & 0x1FU)); // PWM1
	TIM3->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_2 & 0x1FU)); // PWM2
	TIM1->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_1 & 0x1FU)); // PWM3
	TIM1->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_4 & 0x1FU)); // PWM4
	TIM1->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_2 & 0x1FU)); // PWM5

	__HAL_TIM_MOE_ENABLE(&htim1);
	__HAL_TIM_ENABLE(&htim1);

	__HAL_TIM_MOE_ENABLE(&htim3);
	__HAL_TIM_ENABLE(&htim3);

	Uart_StartReceive();
	ADC_Init();
}

void USER_SetPWM(uint8_t chanel, uint16_t value) {
	switch ( chanel )
	{
	    case 0:
	    	TIM3->CCR1 = value;
	        break;
	    case 1:
	    	TIM3->CCR2 = value;
	        break;
	    case 2:
	    	TIM1->CCR1 = value;
	        break;
	    case 3:
	    	TIM1->CCR4 = value;
	        break;
	    case 4:
	    	TIM1->CCR2 = value;
	        break;
	    default:
	    	break;

	}
}

void USER_Main_Loop(void) {
	unsigned int ChannelValue;
	unsigned int tmp;
	unsigned int ch;

	if ((LinkStatistics.up_link_quality > 0) & (Uart_GetCounter() < 10)) { // ELRS connection checking
		RC_LinkUpAtLeastOnce = 1;

		for(ch=0; ch<5; ch++) {
			ChannelValue = Uart_GetChannel(ch);
			if (ChannelValue > 0) {
				USER_SetPWM(ch, ChannelValue);
			}
		}

		if (HAL_GPIO_ReadPin(SET_DEFAULT_GPIO_Port, SET_DEFAULT_Pin) == GPIO_PIN_RESET) { // Set Default Values
			for(ch=0; ch<5; ch++) {
				Settings.start[ch] = Uart_GetChannel(ch);
			}
			SettingsSave();

			while (HAL_GPIO_ReadPin(SET_DEFAULT_GPIO_Port, SET_DEFAULT_Pin) == GPIO_PIN_RESET) {
				HAL_Delay(10);
			}
			HAL_Delay(100);
		}

		if (HAL_GPIO_ReadPin(SET_FAILSAFE_GPIO_Port, SET_FAILSAFE_Pin) == GPIO_PIN_RESET) { // Set Fail safe Values
			for(ch=0; ch<5; ch++) {
				Settings.fail[ch] = Uart_GetChannel(ch);
			}
			SettingsSave();

			while (HAL_GPIO_ReadPin(SET_FAILSAFE_GPIO_Port, SET_FAILSAFE_Pin) == GPIO_PIN_RESET) {
				HAL_Delay(10);
			}
			HAL_Delay(100);
		}


	} else {
		if (RC_LinkUpAtLeastOnce == 1) {
			// Check if the values ​​are set
			tmp = 0;
			for(ch=0; ch<5; ch++) {
				tmp = tmp + Settings.fail[ch];
			}

			if (tmp > 1000) {
				// Set Fail safe values
				for(ch=0; ch<5; ch++) {
					ChannelValue = Settings.fail[ch];
					if (ChannelValue > 0) {
						USER_SetPWM(ch, ChannelValue);
					}
				}
			}
		}
	}

	ADC_Start_Convertion();
	Make_Frame();
	Uart_StartSendFrame();
	Uart_IncCounter();

	HAL_Delay(1);
}

