#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "./SYSTEM/sys/sys.h"
#include "./hmi_app_example.h"
#include "./hmi_cmd_example.h"
#include "./hmi/hmi_cmd.h"
#include "./hmi/hmi_rx_parse.h"
#include "./hmi/hmi_hex_parse.h"

#include "./BSP/LED/led.h"
#include "./BSP/USART2/usart2.h"
#include "./BSP/RTC/rtc.h"


extern UART_HandleTypeDef uart2_handler;       /*UART句柄*/


static uint8_t current_screen_id;                                                 /*  当前画面ID     */
static int8_t curves_type = -1;                                                       /*  曲线标志位     */               
static uint8_t curves_num;                                                        /*  曲线采样点计数 */
static int16_t pointer_num;                                                       /*  指针旋转点计数 */
static int32_t test_value;                                                        /*  文本测试值     */
static char tx_buf[50];                                                         /*  发送指令缓冲区 */                                                                                                                  
static uint8_t sin_buf[256] = {1,1,1,1,1,2,3,4,6,8,10,13,15,
              19,22,25,29,33,38,42,47,52,57,62,68,73,79,85,91,97,103,109,115,121,127,134,
              140,146,152,158,164,170,176,182,187,193,198,203,208,213,217,222,226,230,233,
              236,240,242,245,247,249,251,252,253,254,254,254,254,254,253,252,251,249,247,
              245,242,240,236,233,230,226,222,217,213,208,203,198,193,187,182,176,170,164,
              158,152,146,140,134,128,121,115,109,103,97,91,85,79,73,68,62,57,52,47,42,38,
              33,29,25,22,19,15,13,10,8,6,4,3,2,1,1,1,1,1,2,3,4,6,8,10,13,15,
              19,22,25,29,33,38,42,47,52,57,62,68,73,79,85,91,97,103,109,115,121,127,134,
              140,146,152,158,164,170,176,182,187,193,198,203,208,213,217,222,226,230,233,
              236,240,242,245,247,249,251,252,253,254,254,254,254,254,253,252,251,249,247,
              245,242,240,236,233,230,226,222,217,213,208,203,198,193,187,182,176,170,164,
              158,152,146,140,134,128,121,115,109,103,97,91,85,79,73,68,62,57,52,47,42,38,
              33,29,25,22,19,15,13,10,8,6,4,3,2};

static uint8_t sawtooth[180] = {0,9,18,27,36,45,54,63,72,81,90,99,108,117,126,135,144,153,162,
              171,180,189,198,207,216,225,234,243,252,0,9,18,27,36,45,54,63,72,81,90,99,108,
              117,126,135,144,153,162,171,180,189,198,207,216,225,234,243,252,0,9,18,27,36,
              45,54,63,72,81,90,99,108,117,126,135,144,153,162,171,180,189,198,207,216,225,
              234,243,252,0,9,18,27,36,45,54,63,72,81,90,99,108,117,126,135,144,153,162,171,
              180,189,198,207,216,225,234,243,252,0,9,18,27,36,45,54,63,72,81,90,99,108,117,
              126,135,144,153,162,171,180,189,198,207,216,225,234,243,252,0,9,18,27,36,45,54,
              63,72,81,90,99,108,117,126,135,144,153,162,171,180,189,198,207,216,225,234,243,
              252};
  
//static void notify_screen(char data)
//{
//   char str[128] = {0};
//   sprintf(str,"%d",data);
//   api_page(str);//使用API_PAGE会把str当成字符串 page(str)
//   current_screen_id = data; 
//   HAL_Delay(10); //切换页面后需要加延时，否则不生效

//   /* 切换页面后 ，可以在此根据iD赋值常量属性*/

//}  
  
//void hmi_hex_cmd_parse(struct hex_cmd_fmt *fram)
//{
//   char page = fram->func;
//   char control = fram->data[0];
//   char data = fram->data[1];
//   bool res = false; 
//   cmd_res_t *back = hmi_cmd_back_res();
//  
//   current_screen_id = page;
//  if(current_screen_id == 0) //main
//  {
//    notify_screen(data);
//  }
//  if(current_screen_id == 1) //文本
//  {
//      if(control == 3)//切换按钮
//        notify_screen(data); 
//      if(control == 4)//发送数值文本字符按钮            
//      {    
//        res = API_GET(tn2.val);
//          if (!res)
//          {
//              printf("huoqu err!");
//          }
//        back = hmi_cmd_back_res();
//        int val = back->val.value;
//        if(val >= 3660)
//        {
//          val = 3660;
//          sprintf(tx_buf,"tn3.val = %d;",val);
//          hmi_cmd_send(tx_buf);   
//        }
//        sprintf(tx_buf,"tn3.val = %d;",val);//发送整数1024  对应硬件屏显示10.24
//        hmi_cmd_send(tx_buf);   
//      }
//      if(control == 0x18)//获取字符串测试
//      {
//        hmi_cmd_wait_parse(RET_GET);
//        back = hmi_cmd_back_res();
//        sprintf(tx_buf,"t8.txt.str =\"%s\";",back->val.str);
//        hmi_cmd_send(tx_buf);          
//      }
//  }  
//  if(current_screen_id == 2) //曲线
//  {
//    switch(control)
//    {
//        case 1: curves_type = 0; curves_num = 0; API_CCLEAR(c0,1); break;//sin
//        case 2: curves_type = 1; curves_num = 0; API_CCLEAR(c0,1); break;//sawtooth
//        case 3: notify_screen(data);               break;
//        case 5: API_CCLEAR(c0,1);                  break;
//        case 6: curves_type = -1;                  break;       
//    }
//  }  
//  if(current_screen_id == 3) //指针
//  {
//      if(control == 5)//切换按钮
//        notify_screen(data);   
//  }
//  if(current_screen_id == 4) //滑块
//  {
//      
//      if(control == 1)
//      {
//        res = API_GET(sl0.val);
//        if (!res)
//        {
//            printf("huoqu err!");
//        }
//        back = hmi_cmd_back_res();
//        TIM3->CCR2 = back->val.value;
//      }
//      if(control == 2)
//        notify_screen(data);       
//      if(control == 3)
//      {
//        res = API_GET(csl0.val);
//        if (!res)
//        {
//            printf("huoqu err!");
//        }
//        back = hmi_cmd_back_res();
//        TIM3->CCR2 = back->val.value;
//      }     
//      
//   } 
//  if(current_screen_id == 5) //二维码
//  {
//      if(control == 2)//切换按钮
//         notify_screen(data);   
//     API_CMD_SEND(qr0.str="www.baidu.com";);
//  }  
//  if(current_screen_id == 6) //RTC&选择文本
//  {
//      int year,month,day,hour,min,sec = 0;
//      if(control == 1)//切换按钮
//         notify_screen(data);   
//      if(control == 0x12)//日期按钮
//      {  
//        API_GET(sys.year);
//        back = hmi_cmd_back_res();
//        year = back->val.value;
//        
//        API_GET(sys.month);
//        back = hmi_cmd_back_res();
//        month = back->val.value;        
//        
//        API_GET(sys.day);
//        back = hmi_cmd_back_res();
//        day = back->val.value;      
//        
//        RTC_Set(year,month,day,calendar.hour,calendar.min,calendar.sec);    
//      }
//      if(control == 0x07)//时间按钮
//      {
//          API_GET(sys.hour);
//          back = hmi_cmd_back_res();
//          hour = back->val.value;

//          API_GET(sys.min);
//          back = hmi_cmd_back_res();
//          min = back->val.value;        

//          API_GET(sys.sec);
//          back = hmi_cmd_back_res();
//          sec = back->val.value;      
//          RTC_Set(calendar.w_year,calendar.w_month,calendar.w_date,hour,min,sec);    
//      }
//      if(control == 0x03)//获取时间按钮
//      {
//          API_RTCS(calendar.w_year,calendar.w_month,calendar.w_date,calendar.hour,calendar.min,calendar.sec);
//      }      
//  }
//  if(current_screen_id == 7) //下拉列表
//  { 
//        res = API_GET(l0.val);
//        if (!res)
//        {
//            printf("huoqu err!");
//        }
//        back = hmi_cmd_back_res();
//    if(control == 0x01)
//    {
//       if(back->val.value == 0)
//       {
//          TIM3->CCR2 = 100;
//          API_CMD_SEND(img1.pic=19);
//       }
//       else if(back->val.value == 1)
//       {
//          TIM3->CCR2 = 10;
//          API_CMD_SEND(img1.pic=18);
//       } 
//       else if(back->val.value == 2)
//       {
//          TIM3->CCR2 = 0;
//          API_CMD_SEND(img1.pic=20);
//       } 
//    }
//    if(control == 0x04)
//    {  
//       notify_screen(data);   
//    }
//  }
//  if(current_screen_id == 8) //进度条
//  { 
//    if(control == 0x02)
//    {
//       notify_screen(data);   
//    }
//  }     
//}


 bool real_time_curve(char *name,uint8_t ch,uint16_t points,uint8_t *data)
{
    bool res = api_caddt(name, ch, points);
    if (!res) return false;

    cmd_res_t *back_res = hmi_cmd_back_res();
    if (!strstr(back_res->val.str, "start"))
    {
        return false;
    }

    usart2_send_data(&data[points],points);

    res = hmi_cmd_wait_parse(RET_CADDT);
    if (!res) return false;

    back_res = hmi_cmd_back_res();
    if (!strstr(back_res->val.str, "end"))
    {
        return false;
    }

    return true;
}

void  update_ui(void)
{  
    bool res = false; 
    cmd_res_t *back = hmi_cmd_back_res();  
    /*文本页面*/
    if(current_screen_id == 1)
    {
        API_CMD_SEND(t0.txt.str = "机房10";);
        sprintf(tx_buf,"tn0.val =%d;",test_value%1000);
        hmi_cmd_send(tx_buf);
        sprintf(tx_buf,"tn1.val =%d;",test_value%1000);
        hmi_cmd_send(tx_buf);
        test_value++;
        if(test_value >= 1000)                                                       
        {                                                                            
          test_value = 0;                                                            
        }  
        if(test_value>0&&test_value<500)                                             //大于0小于500文本显示红色
        { 
          API_CMD_SEND(tn0.bg.color =0xF800;);                                       //设置文本背景色
        }                                                                            
        else if(test_value>=500)                                                     //大于500文本显蓝色
        {    
          API_CMD_SEND(tn0.bg.color =0x001F;);           
        }     
    }
    /*曲线页面*/
    if(current_screen_id == 2)
    {
      if(curves_type==0) 
      {
        api_cadd("c0",1,sin_buf[curves_num]);
        curves_num+=1;
//        res = real_time_curve("c0",1,2,&sin_buf[curves_num]);
//          if(!res) printf("err\r\n");
//          API_CCLEAR(c0,1);
//        curves_num+=2;
        if(curves_num >= 255)                                                                                                                                           
            curves_num =0;                                                                         
      }
      else if(curves_type==1)
      { 
//        api_cadd("c0",1,sawtooth[curves_num]);       
//        curves_num+=1;          
        real_time_curve("c0",1,2,&sawtooth[curves_num]);
        curves_num+=2;
        if(curves_num >= 180)                                                                                                                                          
            curves_num =0;                                                                  
      }
    }
    /*指针控件*/
    if(current_screen_id == 3)
    {
        static char flag;
        if(flag)
        {
          pointer_num +=6;
          if(pointer_num >= 275)
            flag = 0;
        }
        else
        {
           pointer_num -=6;
          if(pointer_num <= 0)
            flag = 1;          
        }
        sprintf(tx_buf,"g0.val =%d;",pointer_num);
        hmi_cmd_send(tx_buf);
    }
    if(current_screen_id ==8)//进度条
    {
        res = API_GET(bar0.val);
        if (!res)
        {
            printf("huoqu err!");
        }
        back = hmi_cmd_back_res();
        TIM3->CCR2 = back->val.value;
        API_CMD_SEND(bar0.val+=2);
    }   
}



void tim3_led_init(uint16_t arr,uint16_t psc)
{		 					 
	//此部分需手动修改IO口设置
	RCC->APB1ENR|=1<<1; 	//TIM3时钟使能    
	RCC->APB2ENR|=1<<3;    	//使能PORTB时钟	
	GPIOB->CRL&=0XFF0FFFFF;	//PB5输出
	GPIOB->CRL|=0X00B00000;	//复用功能输出 	  	 
	   
	RCC->APB2ENR|=1<<0;     //开启辅助时钟	   
	AFIO->MAPR&=0XFFFFF3FF; //清除MAPR的[11:10]
	AFIO->MAPR|=1<<11;      //部分重映像,TIM3_CH2->PB5

	TIM3->ARR=arr;			//设定计数器自动重装值 
	TIM3->PSC=psc;			//预分频器不分频
	
	TIM3->CCMR1|=7<<12;  	//CH2 PWM2模式		 
	TIM3->CCMR1|=1<<11; 	//CH2预装载使能	   
	TIM3->CCER|=1<<4;   	//OC2 输出使能	   
	TIM3->CR1=0x0080;   	//ARPE使能 
	TIM3->CR1|=0x01;    	//使能定时器3 											  
} 

volatile uint8_t ui_flag;
void hmi_test(void)
{
    uint8_t t =0;
    timer6_init(20-1,7200-1);/* 10->1ms  100->10ms */
    tim3_led_init(100-1,7200-1);
    // hmi_cbs_register(func_parse_handle);
    while(RTC_Init())
	{ 
	}	
    while(1)
    {
        hmi_rx_parse_task();  
        if(ui_flag)
        {
            ui_flag = 0;
            update_ui();
            HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_5);
#if 1
            if( t!=calendar.sec)
            {
                t=calendar.sec;
                printf("%d 年 %d 月 %d 日\r\n",calendar.w_year,calendar.w_month,calendar.w_date);
                printf("%d : %d : %d \r\n",calendar.hour,calendar.min,calendar.sec);
                switch(calendar.week)
                {
                    case 0:
                        printf("Sunday   ");
                        break;
                    case 1:
                        printf("Monday   ");
                        break;
                    case 2:
                        printf("Tuesday  ");
                        break;
                    case 3:
                        printf("Wednesday");
                        break;
                    case 4:
                        printf("Thursday ");
                        break;
                    case 5:
                        printf("Friday   ");
                        break;
                    case 6:
                        printf("Saturday ");
                        break;  
                }
            }
#endif
        }

    }

}

//void TIM6_IRQHandler(void)
//{ 		    		  			       
//    if(__HAL_TIM_GET_IT_SOURCE(&TIM6_Handler,TIM_IT_UPDATE)==SET)//溢出中断
//    {
//        ui_flag = 1;
//    }
//    __HAL_TIM_CLEAR_IT(&TIM6_Handler, TIM_IT_UPDATE);//清除中断标志位
//}

