/**
  ******************************************************************************
  * File Name          : RTC.c
  * Description        : This file provides code for the configuration
  *                      of the RTC instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "rtc.h"

/* USER CODE BEGIN 0 */
RTC_DateTypeDef currentDate;
RTC_TimeTypeDef currentTime;
RTC_DateTypeDef setDate;
RTC_TimeTypeDef setTime;
struct TIMER timer[TIMER_COUNT];
struct BLINK_TIMER blinker[BLINKER_COUNT];
struct BREATHE_TIMER breathe;
struct FLICKER_TIMER flicker[FLICKER_COUNT]; 
/* USER CODE END 0 */

RTC_HandleTypeDef hrtc;

/* RTC init function */
void MX_RTC_Init(void)
{
  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};

  /** Initialize RTC Only 
  */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */
    
  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date 
  */
  sTime.Hours = 0;
  sTime.Minutes = 0;
  sTime.Seconds = 0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 1;
  sDate.Year = 0;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{

  if(rtcHandle->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspInit 0 */

  /* USER CODE END RTC_MspInit 0 */
    /* RTC clock enable */
    __HAL_RCC_RTC_ENABLE();
  /* USER CODE BEGIN RTC_MspInit 1 */

  /* USER CODE END RTC_MspInit 1 */
  }
}

void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
{

  if(rtcHandle->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspDeInit 0 */

  /* USER CODE END RTC_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_RTC_DISABLE();
  /* USER CODE BEGIN RTC_MspDeInit 1 */

  /* USER CODE END RTC_MspDeInit 1 */
  }
} 

/* USER CODE BEGIN 1 */
void timerEnable(uint8_t tmrIdx, uint32_t timeoutSpec)
{
	timer[tmrIdx].timeoutSpec = timeoutSpec;
	timer[tmrIdx].isEnable = ON;
}

void timerDisable(uint8_t tmrIdx)
{
	timer[tmrIdx].isEnable = OFF;
}

void timerRefresh(uint8_t tmrIdx)
{
    timer[tmrIdx].startTime = HAL_GetTick();
    timer[tmrIdx].isAlarm = OFF;
}

void blinkerTimeSet(uint8_t idx, uint16_t on, uint16_t off, uint16_t rest, uint8_t count)
{
    blinker[idx].blinkOnSpec     = on;
    blinker[idx].blinkOffSpec    = off;
    blinker[idx].blinkRestSpec   = rest;
    blinker[idx].blinkCountSpec  = count;
    blinker[idx].blinkisFinish   = OFF ;
}

void disblinkerTime (uint8_t idx)
{
    blinker[idx].isOn            = 0 ;           // <- this function only set 1 times ,  No re-entry allowed in loop
    blinker[idx].blinkOnSpec     = 0 ;
    blinker[idx].blinkOffSpec    = 0 ;
    blinker[idx].blinkRestSpec   = 0 ;
    blinker[idx].blinkCountSpec  = 0 ;
    blinker[idx].blinkCount      = 0 ;
}

void flickerTimeSet(uint8_t idx, uint16_t on, uint16_t off)
{
    flicker[idx].OnSpec     = on;
    flicker[idx].OffSpec    = off;    
}

void breatheTimeSet(uint16_t up, uint16_t down, uint16_t R_color, uint16_t G_color, uint16_t B_color, uint16_t W_color)
{
    breathe.timeUp = up;
    breathe.timeDown = down;    
    breathe.RGB[0] = R_color; 
    breathe.RGB[1] = G_color; 
    breathe.RGB[2] = B_color; 
    breathe.RGB[3] = W_color; 
}

void LedOnRGBSet(uint16_t R_color, uint16_t G_color, uint16_t B_color, uint16_t W_color)
{
    Charger.led_pwm_duty[0] = R_color; 
    Charger.led_pwm_duty[1] = G_color; 
    Charger.led_pwm_duty[2] = B_color; 
    Charger.led_pwm_duty[3] = W_color; 
}

uint32_t timerGetTimePass(uint8_t tmrIdx)
{
    return ((HAL_GetTick()-timer[tmrIdx].startTime)/1000);
}

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
