#include "TaskProcessCom.h"
#include "IIC_Hardware.h"
#include "IIC_Software.h"
#include "IIC2_Software.h"
#include "ch32x035_usbpd.h"
#include "tmos.h"
#include "sgm452.h"
//#include "sgm832.h"



#define KEY_ADC_DET                           0x0001
#define CHECK_TOUCH_INT_EVT                   0x0002
#define USB_HID_DEBUG_EVT                     0x0004
#define RESET_TOUCH_CHIP_EVT                  0x0008
#define CHECK_USB_CONNECT_EVT                 0x0010
#define HDMI_RX_DET                           0x0020
#define LED_DET                               0x0040
#define TEMPTURE_DET                          0x0080
#define POWER_DET                             0x0100

static UINT16 Task_ProcessEvent( UINT8 task_id, UINT16 events );
static UINT8 TaskID = INVALID_TASK_ID;

CHIP_TYPE ChipType;

UINT8 ShareBuf[10];

void (*TouchProc)(void);


void TIM4_Init( uint16_t arr, uint16_t psc )
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = { 0 };
    NVIC_InitTypeDef NVIC_InitStructure = { 0 };

    /* Enable Timer4 Clock */
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );

    /* Initialize Timer4 */
    TIM_TimeBaseStructure.TIM_Period = arr;
    TIM_TimeBaseStructure.TIM_Prescaler = psc;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit( TIM4, &TIM_TimeBaseStructure );

    TIM_ITConfig( TIM4, TIM_IT_Update, ENABLE );

    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init( &NVIC_InitStructure );

    TIM_Cmd( TIM4, ENABLE );
}

/* Interrupt Function Declaration */
void TIM4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM4_IRQHandler( void )
{
    if( TIM_GetITStatus( TIM4, TIM_IT_Update ) != RESET )
    {
        TIM_ClearITPendingBit( TIM4, TIM_IT_Update );
    }
}


//TIM3_Init(1, SystemCoreClock / 10000 - 1 );
void TIM3_Init( uint16_t arr, uint16_t psc )
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = { 0 };
    NVIC_InitTypeDef NVIC_InitStructure = { 0 };

    /* Enable Timer3 Clock */
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );

    /* Initialize Timer3 */
    TIM_TimeBaseStructure.TIM_Period = arr;
    TIM_TimeBaseStructure.TIM_Prescaler = psc;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );

    TIM_ITConfig( TIM3, TIM_IT_Update, ENABLE );

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init( &NVIC_InitStructure );

    /* Enable Timer3 */
    TIM_Cmd( TIM3, ENABLE );
}

/* Interrupt Function Declaration */
void TIM3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM3_IRQHandler( void )
{
    if( TIM_GetITStatus( TIM3, TIM_IT_Update ) != RESET )
    {
        //timems++;
        ms933x_media_service();

        //printf("timems= %d\n",timems);
        TIM_ClearITPendingBit( TIM3, TIM_IT_Update );
    }
}


void ProcessEventInit()
{
  //注册事件处理函数
  TaskID = TMOS_ProcessEventRegister(Task_ProcessEvent);
  //
  InitChipStartTask();

  //TIM3_Init( 100-1, 9600-1 ); //10ms

  TIM3_Init( 500-1, 9600-1 ); //50ms

//  TIM3_Init( 40, SystemCoreClock / 1018 - 1 );
}

static UINT16 Task_ProcessEvent( UINT8 task_id, UINT16 events )
{
    if(events & HDMI_RX_DET)
    {
      //USART1_ReceData();
      //printf("[EVENT]: HDMI_RX_DET\n");
      //ms933x_media_service();
      events ^= HDMI_RX_DET;
      tmos_start_task(task_id, HDMI_RX_DET, PERIO_10ms ); //sea
      //fill_picture(0xFF);
      goto OUT_FUMC;
    }

    //TEMPTURE_DET task
    if(events & TEMPTURE_DET)
    {
      //printf("[EVENT]: TEMPTURE_DET\n");
      sgm452_Proc();
      events ^= TEMPTURE_DET;
      tmos_start_task(task_id, TEMPTURE_DET, PERIO_500ms );
      goto OUT_FUMC;
    }

    //POWER_DET task
    if(events & POWER_DET)
    {
      //printf("[EVENT]: POWER_DET\n");
      //sgm832_Power_detect_Proc_Bl();
      //sgm832_Power_detect_Proc_Sys();
      events ^= POWER_DET;
      tmos_start_task(task_id, POWER_DET, PERIO_800ms );
      goto OUT_FUMC;
    }

    if(events & RESET_TOUCH_CHIP_EVT)
    {
      printf("[EVENT]: RESET_TOUCH_CHIP_EVT\n");
      //InitChipStartTask();
      events ^= RESET_TOUCH_CHIP_EVT;
      //tmos_set_event(task_id, RESET_TOUCH_CHIP_EVT);
      goto OUT_FUMC;
    }

    return 0;

OUT_FUMC:

    return  events;
}


void InitChipStartTask()
{
//ms933x chip setting
  //libs auto write iic address 0xB2, no need set it
  ms933x_init();
  tmos_start_task(TaskID, HDMI_RX_DET, PERIO_10ms );
  printf("[task]: ms933x start.\n");

//sgm452 chip setting
  sgm452_GPIO_I2cChannel_Init();
  tmos_start_task(TaskID, TEMPTURE_DET, PERIO_500ms );
  printf("[task]: SGM452 start.\n");

//sgm832 chip setting
  //sgm832_GPIO_I2cChannel_Init();
  //tmos_start_task(TaskID, POWER_DET, PERIO_800ms );
  //printf("[task]: SGM832 start.\n");

  Delay_Ms(80);
}
