/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2021 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 "main.h"
#include "app_lorawan.h"
#include "gpio.h"
#include "usart.h"
#include "stdio.h"
#include "ev1527.h"
#include "tim.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
// #include "i2c.h"
// #include "app_tof.h"
/* USER CODE END Includes */
// #include "sts_aq_o3.h"
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
TIM_HandleTypeDef htim1;
//uint8_t rf_payload[3]={0xF8,0xCD,0x07}, rf_length=3;
uint8_t rf_payload[3]={0x1F,0xB3,0xE0}, rf_length=3;	// RC_PROJECTOR
uint8_t sos_rf_payload[3]={0x82,0x73,0xA0}, sos_rf_length=3;	// sos_button
enum rf_cmd_enum
{		BUTTON_NONE=0,
		BUTTON_ON,
		BUTTON_OFF,
		BUTTON_FIRST,
		BUTTON_NEXT,
		BUTTON_5S,
		BUTTON_10S,
		BUTTON_15S,
		BUTTON_30S,
		BUTTON_9,BUTTON_10,BUTTON_11,BUTTON_12,BUTTON_13,BUTTON_14,BUTTON_15,
};

uint8_t rf_cmd[16]={0x00, 0x8,0xC,0x4,0x6,0x1,0x9,0x2,0x3}; // cmd 1 = 1, cmd2=4, cmd3=3, cmd4=2
uint8_t sos_rf_cmd[16]={0x00, 0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; // cmd 1 = 1
void sts_rc_key(uint8_t key);
void  sts_rc_decoder(void);
uint8_t sts_rc_decodedx(void);
volatile uint8_t codexx=0, code_vt=0;
/* USER CODE END PTD */
uint8_t rc_cmd[9]={
		RC_NONE,
		RC_POWER_ON,
		RC_POWER_OFF,
		RC_PIC_FIRST,
		RC_PIC_NEXT,
		RC_SHOW_5S,
		RC_SHOW_10S,
		RC_SHOW_15S,
		RC_SHOW_30S
};
/* 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 PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int file, char *ptr, int len)
{
       (void) file;
       HAL_UART_Transmit (&huart2, (uint8_t*)ptr, len, 0xFFFF);
       return len;
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  // MX_I2C2_Init();
  MX_GPIO_Init();



   // MX_LoRaWAN_Init();
  /* USER CODE BEGIN 2 */
  MX_USART2_UART_Init();


  // MX_USART1_UART_Init();

  MX_TIM1_Init();

  printf("start \r\n");

  // EV1527_Init();

#if 0
  /*## Start the Input Capture in interrupt mode ##########################*/
  if (HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_2) != HAL_OK)
  {
	  printf("tim1 ch2 start IT error \r\n");
    /* Starting Error */
    Error_Handler();
  }
#endif

  /* ---------------------------------------------------------------------------
    TIM1 configuration: PWM Input mode

    In this example TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2),
      since APB2 prescaler is 1.
      TIM1CLK = PCLK2
      PCLK2 = HCLK
      => TIM1CLK = HCLK = SystemCoreClock

    External Signal Frequency = TIM1 counter clock / TIM1_CCR2 in Hz.

    External Signal DutyCycle = (TIM1_CCR1*100)/(TIM1_CCR2) in %.

    --------------------------------------------------------------------------- */
#if 1
    /*## Start the Input Capture in interrupt mode ##########################*/
    if (HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_2) != HAL_OK)
    {
      /* Starting Error */
      Error_Handler();
    }
#endif

#if 1
    /*## Start the Input Capture in interrupt mode ##########################*/
    if (HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_1) != HAL_OK)
    {
      /* Starting Error */
      Error_Handler();
    }

#endif

    while(1)
    {

    }


while(1)
{
	// printf("uwF=%ld Hz\r\n", uwFrequency);
	// STS_RF_Send_Multi_Times(payload, 3, 5);
}


#if 0
  uint8_t i;
  while (1)
  {
	  for(i=0; i<6; i++)
	  {
		  printf("\r\n Remote control Key down =%d \r\n", i);
		  sts_rc_key(i);
		  HAL_Delay(3000);

		  printf("\r\n Remote control decoded: %02x \r\n", codexx);
		  // sts_rc_decoder();

		  HAL_Delay(3000);
		  codexx=0;
	  }

	  HAL_Delay(2000);
  }
#endif

  /* USER CODE END 2 */

    /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  while (1)
  {
    /* USER CODE END WHILE */


    MX_LoRaWAN_Process();

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}


#if 0
void RF_Read_TIM_init(void)
{
	TIM_Base_InitTypeDef TIM_Base_Init_Struct;

	NVIC_InitTypeDef NVIC_Init_Struct;
	RF_Read_TIM_RCC;
	TIM_Base_Init_Struct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_Base_Init_Struct.TIM_CounterMode = TIM_CounterMode_Up;
	// every int trigger time = [(Tim_Period+1)*(TIM_Prescaler+1)/(SystemCoreClock)] (s)
	TIM_Base_Init_Struct.Tim_Prescalar = 48 -1;
	TIM_Base_Init_Struct.Tim_Period = 0xffff -1;
	TIM_Base_Init_Struct.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(RF_Read_TIM_TIMx, &TIM_Init_Struct);
	TIM_ITConfig(RF_Read_TIM_TIMx, TIM_IT_Update, ENABLE);
}
#endif

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	uint8_t single_button =0;
	switch(GPIO_Pin)
	{
	case BUT1_Pin:
#if 1
		printf("Button 1 pressed, sending cmd #1 \r\n");
		//STS_RF_Send_AddressBit_and_CmdBit(rf_payload, rf_length);
		// STS_RF_Send_Multi_Times(rf_payload, 3, 8);
		single_button = rf_cmd[BUTTON_ON];
		STS_RF_Send_Button_Multi_Times(rf_payload, single_button, 3, 8);
#endif

		// for SOS BUTTON
#if 0
		printf("SOS Button pressed, sending cmd #1 \r\n");
		//STS_RF_Send_AddressBit_and_CmdBit(rf_payload, rf_length);
		// STS_RF_Send_Multi_Times(rf_payload, 3, 8);
		single_button = sos_rf_cmd[BUTTON_ON];
		STS_RF_Send_Button_Multi_Times(sos_rf_payload, single_button, 3, 8);
#endif
		break;

	case BUT2_Pin:
		printf("Button 2 pressed, sending cmd #2 \r\n");
		single_button = rf_cmd[BUTTON_OFF];
		// STS_RF_Send_Multi_Times(rf_payload, 3, 5);
		STS_RF_Send_Button_Multi_Times(rf_payload, single_button, 3, 8);
		break;

	case BUT3_Pin:
		printf("Button 3 pressed, sending cmd #3 \r\n");
		single_button = rf_cmd[BUTTON_NEXT];
		// STS_RF_Send_Multi_Times(rf_payload, 3, 5);
		STS_RF_Send_Button_Multi_Times(rf_payload, single_button, 3, 8);
		break;


	case DATA_433_PIN:
		//printf("^");
		// HAL_TIM_IC_CaptureCallback(&htim1);
		//RF_Signal_Decode();

		break;

	default:
		break;
	}

#if 0
	codexx =0;
  switch (GPIO_Pin)
  {
    case  RC_D0_Pin:
    	codexx |= (HAL_GPIO_ReadPin(RC_D0_GPIO_Port, RC_D0_Pin)<<0);
    	printf("[0]=%02x ",codexx);
      break;
    case  RC_D1_Pin:
    	codexx |= (HAL_GPIO_ReadPin(RC_D1_GPIO_Port, RC_D1_Pin)<<1);
    	printf("[1]=%02x ",codexx);
      break;
    case  RC_D2_Pin:
    	codexx |= (HAL_GPIO_ReadPin(RC_D2_GPIO_Port, RC_D2_Pin)<<2);
    	printf("[2]=%02x ",codexx);
      break;
    case  RC_D3_Pin:
    	codexx |= (HAL_GPIO_ReadPin(RC_D3_GPIO_Port, RC_D3_Pin)<<3);
    	printf("[3]=%02x ",codexx);
      break;
#if 0
    case  RC_D4_Pin:
    	codexx |= (HAL_GPIO_ReadPin(RC_D4_GPIO_Port, RC_D4_Pin)<<4);
    	printf("[4]=%02x ",codexx);
      break;
    case  RC_D5_Pin:
    	codexx |= (HAL_GPIO_ReadPin(RC_D5_GPIO_Port, RC_D5_Pin)<<5);
    	printf("[5]=%02x ",codexx);
      break;
    case  RC_D6_Pin:
    	codexx |= (HAL_GPIO_ReadPin(RC_D6_GPIO_Port, RC_D6_Pin)<<6);
    	printf("[6]=%02x ",codexx);
      break;
    case  RC_D7_Pin:
    	codexx |= (HAL_GPIO_ReadPin(RC_D7_GPIO_Port, RC_D7_Pin)<<7);
    	printf("[7]=%02x ",codexx);
      break;
#endif
    case  RC_VT_Pin:
    	code_vt = (HAL_GPIO_ReadPin(RC_VT_GPIO_Port, RC_VT_Pin));
    	printf("[V]=%02x ",codexx);
      break;

    default:
      break;
  }
#endif

}


#if 0
void sts_rc_key(uint8_t key)
{
	HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_SET);
	HAL_Delay(100);

	switch (key) {
	case 0:
		HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_SET);
		break;
	case 1:
		HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_SET);
		break;
	case 2:
		HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_SET);

		break;
	case 3:
		HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_SET);

		break;
	case 4:
		HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_SET);

		break;
	case 5:
		HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_SET);

		break;
	case 6:
		HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_SET);

		break;
	case 7:
		HAL_GPIO_WritePin(RC_K0_GPIO_Port, RC_K0_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K1_GPIO_Port, RC_K1_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(RC_K2_GPIO_Port, RC_K2_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(RC_K3_GPIO_Port, RC_K3_Pin, GPIO_PIN_RESET);

		break;

	}

}

uint8_t sts_rc_decodedx(void)
{
	uint8_t codelow4=0, codehigh4=0;

	codelow4  = (HAL_GPIO_ReadPin(RC_D0_GPIO_Port, RC_D0_Pin)<<0)|(HAL_GPIO_ReadPin(RC_D1_GPIO_Port, RC_D1_Pin)<<1)|(HAL_GPIO_ReadPin(RC_D2_GPIO_Port, RC_D2_Pin)<<2)|(HAL_GPIO_ReadPin(RC_D3_GPIO_Port, RC_D3_Pin)<<3);
	codehigh4 = (HAL_GPIO_ReadPin(RC_D4_GPIO_Port, RC_D4_Pin)<<4)|(HAL_GPIO_ReadPin(RC_D5_GPIO_Port, RC_D5_Pin)<<5)|(HAL_GPIO_ReadPin(RC_D6_GPIO_Port, RC_D6_Pin)<<6)|(HAL_GPIO_ReadPin(RC_D7_GPIO_Port, RC_D7_Pin)<<7);
	// printf("%02x \r", (codehigh4|codelow4));
	return (codehigh4|codelow4);
}

void  sts_rc_decoder(void)
{
	uint8_t codex=0;

	codex = HAL_GPIO_ReadPin(RC_D0_GPIO_Port, RC_D0_Pin)|(HAL_GPIO_ReadPin(RC_D1_GPIO_Port, RC_D1_Pin)<<1)|(HAL_GPIO_ReadPin(RC_D2_GPIO_Port, RC_D2_Pin)<<2)|(HAL_GPIO_ReadPin(RC_D3_GPIO_Port, RC_D3_Pin)<<3);

	printf("decoded x= %02x \r\n", codex);

}
#endif
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure LSE Drive Capability
  */
  HAL_PWR_EnableBkUpAccess();
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK
                              |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
                              |RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  while (1)
  {
  }
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */