////////////////////////////////////////////////////////////////////////////////

/// 终端交互模块文件
///
///
/// 包含底层驱动初始化及任务创建 
/// @file    device_term.c 
/// @author  gkl 
/// @date    2017-05-27创建 
/// @version v0.1 

////////////////////////////////////////////////////////////////////////////////

// 包含的头文件
#include "device_term.h"
#include "comm_resource.h"
#include "device_time.h"
#include "comm_types.h"
#include "app_cfg.h"
#include "wdog.h"
#include "adc.h"
#include "w25q128.h"
#include "timer.h"
#include "check.h"
#include "lwip/inet.h"
#include "rotate_bldc.h"
#include "rotate_step.h"
/// ping测试 
///
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         gkl与2017-06-01创建
ip_addr_t ping_dest_target;
void func_ping_test()
{
     char cmmd_buff[20] = {0};
     char temp = 0;
     int ret = 0;
     term_printf("note: ctrl+c断开ping连接\n\r");
	 term_printf("请输入目标ip: ");
     while(1){
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0) {
		    if (ret > 5) {
				cmmd_buff[ret-1] = '\0';
				ping_dest_target.addr = ipaddr_addr(cmmd_buff);
				term_printf("\n");   
				// 恢复ping任务
				OSTaskResume(TASK_PING_THREAD_PRIO); 
			} else {
				temp = cmmd_buff[0];
				if (temp == 0x03){
				    // 将ping任务挂起
					OSTaskSuspend(TASK_PING_THREAD_PRIO);
					term_printf("\n--> 1ip设置\n--> 2ping测试\n--> r返回\n--> h帮助\r\n");
					break;
				 } else {
					term_printf("\n--> ");   
		  		 }
		 	}
        }
	}
}

/// 以太网设置 
///
/// 包括ping测试，ip地址设置等功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         gkl与2017-06-01创建
void func_ethernet_setting()
{ 
     char cmmd_buff[20] = {0};
     char temp = 0;
     int ret = 0;
   //  pdebug(DEBUG_LEVEL_INFO,"\n--> 1ip设置\n--> 2ping测试\n--> r返回\n--> h 帮助\r\n"); 
     term_printf("\n     --> 1ip设置\n     --> 2ping测试\n     --> r返回\n     --> h帮助\r\n");
     while(1){
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
		 if (ret > 0) {
		     temp = cmmd_buff[0];
			 if (temp == '2'){
			    func_ping_test();
			 }else if(temp == 'r'){ 
                term_printf("--> 1设备初始化\n--> 2网络设置\n--> 3传感器调试\n--> q系统复位\n--> h帮助\r\n");
				break;
			 }else if(temp == 'h'){ 
             //  pdebug(DEBUG_LEVEL_INFO,"\n--> 1ip设置\n--> 2ping测试\n--> r返回\n--> h帮助\r\n"); 
			  term_printf("\n     --> 1ip设置\n     --> 2ping测试\n     --> r返回\n     --> h帮助\r\n");
             }else
            //   pdebug(DEBUG_LEVEL_INFO,"\n--> "); 
               term_printf("\n--> ");
		  }
	 }
}


/// 传感器调试 
///
/// 包括O2,CO,H2S,CH4,环流,温湿度测试等功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void func_sensor_test()
{
       char cmmd_buff[2] = {0};
       char temp = 0;
       int ret = 0;
   //    u_int8_t err = 0; 
       term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
     //  pdebug(DEBUG_LEVEL_INFO,"\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3硫化氢传感器\n     --> 4甲烷传感器\n     --> 4接地电流传感器\n     --> r返回\n     --> h帮助\n\r"); 
       while(1){
       BSP_OS_TimeDlyMs(100);
       ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
       if (ret > 0){
       //      OSTmrStart(tmr_delete_term_task, &err);
             temp = cmmd_buff[0];
             if (temp == '1'){
               #if CFG_MODULE_O2
                 fun_o2_test();
               #endif
              }else if(temp == '2'){
               #if CFG_MODULE_CO
                 fun_co_test();
               #endif                
              }else if(temp == '3'){
               #if CFG_MODULE_CH4
                 fun_ch4_test();
               #endif                   
              }else if(temp == '4'){
               #if CFG_MODULE_H2S
                 fun_h2s_test();
               #endif                 
              }else if(temp == '5'){
               #if CFG_MODULE_WATER
                 fun_water_test();
               #endif                 
              }else if(temp == '6'){
               #if CFG_MODULE_CURRENT
                 fun_current_test();
               #endif 
                 
              }else if(temp == '7'){
               #if CFG_MODULE_TEMPHUMI
                 fun_temphumi_test();
               #endif 
              }else if(temp == '8'){
                 fun_flash_test();
              }else if(temp == 'r'){
                 term_printf("--> 1设备初始化\n--> 2网络设置\n--> 3传感器调试\n--> q系统复位\n--> h帮助\r\n");
                 break;
              }else if (temp == 'h'){
                   term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
              //   pdebug(DEBUG_LEVEL_INFO,"\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3硫化氢传感器\n     --> 4甲烷传感器\n     --> 4接地电流传感器\n     --> r返回\n     --> h帮助\n\r"); 
              }else
               term_printf("\n--> ");  
        }    
     }
}

#if CFG_MODULE_O2
/// 氧气传感器调试 
///
/// 包括O2功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void fun_o2_test()
{

    int ret    = 0;   
    char cmmd_buff[2] = {0};
  //  u_int8_t err;
    char temp = 0;
    SENSOR_POWER_ON;   //打开氧气传感器电源
    term_printf("\n正在打开传感器电源...\n\r");    
    BSP_OS_TimeDlyMs(5000);
    term_printf("\n已打开传感器电源\n\r");
    term_printf("\n     --> 1读取氧气传感器\n     --> r返回\n     --> h帮助\n\r");
    
     while(1){
         BSP_OS_TimeDlyMs(100);
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0){
            //   OSTmrStart(tmr_delete_term_task, &err);
               temp = cmmd_buff[0];
               if (temp == '1'){
                term_printf("\n正在读取...\n\r"); 
                BSP_OS_SemWait(&rs485_mutex, 0u);    
                for(u_int8_t i = 0; i < 5; i++){ 
                   BSP_OS_TimeDlyMs(10);
                   send_collect_o2_cmd(O2_ADDR);//发送数据采集命令                          
                   BSP_OS_TimeDlyMs(150);
                   ret = rs485_uart_read_o2_pack(rs485_read_buff,sizeof(rs485_read_buff));
                   if (ret > 0){
                       process_rs485_uart_o2_data_pack((o2_data_pack*)rs485_read_buff);
                       term_printf("\n氧气值：%.2f%",o2_data.o2_value); 
                       BSP_OS_SemPost(&rs485_mutex);  
                       SENSOR_POWER_OFF;
                       break;
                   }
                   if(i == 4) 
                   {
                       term_printf("\n\r采集数据失败\n\r");
                       BSP_OS_SemPost(&rs485_mutex);  
                       SENSOR_POWER_OFF;
                   }
                }  
               }else if(temp == 'r'){ 
                  term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
                  break;
               }else if(temp == 'h'){ 
                  term_printf("\n     --> 1读取氧气传感器\n     --> r返回\n     --> h帮助\n\r");
               }else
                  term_printf("\n--> ");           
          }
     }     
}
#endif

#if CFG_MODULE_CO
/// 一氧化碳传感器调试 
///
/// 包括co功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void fun_co_test()
{
    int ret    = 0;   
    char cmmd_buff[2] = {0};
  //  u_int8_t err;
    char temp = 0;
    SENSOR_POWER_ON;   //打开氧气传感器电源
    term_printf("\n正在打开传感器电源...\n\r");    
    BSP_OS_TimeDlyMs(5000);
    term_printf("\n已打开传感器电源\n\r");
    term_printf("\n     --> 1读取一氧化碳传感器\n     --> r返回\n     --> h帮助\n\r");
    
     while(1){
         BSP_OS_TimeDlyMs(100);
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0){
            //   OSTmrStart(tmr_delete_term_task, &err);
               temp = cmmd_buff[0];
               if (temp == '1'){
                term_printf("\n正在读取...\n\r"); 
                BSP_OS_SemWait(&rs485_mutex, 0u);    
                for(u_int8_t i = 0; i < 5; i++){ 
                   BSP_OS_TimeDlyMs(10);
                   send_collect_co_cmd(CO_ADDR);//发送数据采集命令                          
                   BSP_OS_TimeDlyMs(150);
                   ret = rs485_uart_read_co_pack(rs485_read_buff,sizeof(rs485_read_buff));
                   if (ret > 0){
                       process_rs485_uart_co_data_pack((co_data_pack*)rs485_read_buff);
                       term_printf("\n一氧化碳值：%.2fppm",co_data.co_value); 
                       BSP_OS_SemPost(&rs485_mutex); 
                       SENSOR_POWER_OFF;
                       break;
                   }
                   if(i == 4) 
                   {
                       term_printf("\n\r采集数据失败\n\r");
                       BSP_OS_SemPost(&rs485_mutex);  
                       SENSOR_POWER_OFF;
                   }
                }                 
               }else if(temp == 'r'){ 
                  term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
                  break;
               }else if(temp == 'h'){ 
                  term_printf("\n     --> 1读取一氧化碳传感器\n     --> r返回\n     --> h帮助\n\r");
               }else
                  term_printf("\n--> ");           
          }
     }     
}
#endif


#if CFG_MODULE_CH4
/// 甲烷传感器调试 
///
/// 包括ch4功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void fun_ch4_test()
{
    int ret    = 0;   
    char cmmd_buff[2] = {0};
  //  u_int8_t err;
    char temp = 0;
    SENSOR_POWER_ON;   //打开氧气传感器电源
    term_printf("\n正在打开传感器电源...\n\r");      
    BSP_OS_TimeDlyMs(5000);
    term_printf("\n已打开传感器电源\n\r");
    term_printf("\n     --> 1读取甲烷传感器\n     --> r返回\n     --> h帮助\n\r");
    
     while(1){
         BSP_OS_TimeDlyMs(100);
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0){
            //   OSTmrStart(tmr_delete_term_task, &err);
               temp = cmmd_buff[0];
               if (temp == '1'){
                term_printf("\n正在读取...\n\r"); 
                BSP_OS_SemWait(&rs485_mutex, 0u);    
                for(u_int8_t i = 0; i < 5; i++){ 
                   BSP_OS_TimeDlyMs(10);
                   send_collect_ch4_cmd(CH4_ADDR);//发送数据采集命令                          
                   BSP_OS_TimeDlyMs(150);
                   ret = rs485_uart_read_ch4_pack(rs485_read_buff,sizeof(rs485_read_buff));
                   if (ret > 0){
                       process_rs485_uart_ch4_data_pack((ch4_data_pack*)rs485_read_buff);
                       term_printf("\n甲烷值：%.2fppm",ch4_data.ch4_value); 
                       BSP_OS_SemPost(&rs485_mutex);  
                       SENSOR_POWER_OFF;
                       break;
                   }
                   if(i == 4) 
                   {
                       term_printf("\n\r采集数据失败\n\r");
                       BSP_OS_SemPost(&rs485_mutex);  
                       SENSOR_POWER_OFF;
                   }
                }                  
               }else if(temp == 'r'){ 
                  term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
                  break;
               }else if(temp == 'h'){ 
                  term_printf("\n     --> 1读取甲烷传感器\n     --> r返回\n     --> h帮助\n\r");
               }else
                  term_printf("\n--> ");           
          }
     }     
}
#endif


#if CFG_MODULE_H2S
/// 硫化氢传感器调试 
///
/// 包括h2s功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void fun_h2s_test()
{
    int ret    = 0;   
    char cmmd_buff[2] = {0};
  //  u_int8_t err;
    char temp = 0;
    SENSOR_POWER_ON;   //打开氧气传感器电源
    term_printf("\n正在打开传感器电源...\n\r");    
    BSP_OS_TimeDlyMs(5000);
    term_printf("\n已打开传感器电源\n\r");
    term_printf("\n     --> 1读取硫化氢传感器\n     --> r返回\n     --> h帮助\n\r");
    
     while(1){
         BSP_OS_TimeDlyMs(100);
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0){
            //   OSTmrStart(tmr_delete_term_task, &err);
               temp = cmmd_buff[0];
               if (temp == '1'){
                term_printf("\n正在读取...\n\r"); 
                BSP_OS_SemWait(&rs485_mutex, 0u);    
                for(u_int8_t i = 0; i < 5; i++){ 
                   BSP_OS_TimeDlyMs(10);
                   send_collect_h2s_cmd(H2S_ADDR);//发送数据采集命令                          
                   BSP_OS_TimeDlyMs(150);
                   ret = rs485_uart_read_h2s_pack(rs485_read_buff,sizeof(rs485_read_buff));
                   if (ret > 0){
                       process_rs485_uart_h2s_data_pack((h2s_data_pack*)rs485_read_buff);
                       term_printf("\n硫化氢值：%.2fppm",h2s_data.h2s_value);  
                       BSP_OS_SemPost(&rs485_mutex);  
                       SENSOR_POWER_OFF;
                       break;
                   }
                   if(i == 4) 
                   {
                       term_printf("\n\r采集数据失败\n\r");
                       BSP_OS_SemPost(&rs485_mutex); 
                       SENSOR_POWER_OFF;
                   }
                }                 
               }else if(temp == 'r'){ 
                  term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
                  break;
               }else if(temp == 'h'){ 
                  term_printf("\n     --> 1读取硫化氢传感器\n     --> r返回\n     --> h帮助\n\r");
               }else
                  term_printf("\n--> ");           
          }
     }     
}
#endif

#if CFG_MODULE_WATER
/// 水位传感器调试 
///
/// 包括水位功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void fun_water_test()
{
    int ret    = 0;   
    char cmmd_buff[2] = {0};
  //  u_int8_t err;
    char temp = 0;
    SENSOR_POWER_ON;   //打开氧气传感器电源
    term_printf("\n正在打开传感器电源...\n\r");    
    BSP_OS_TimeDlyMs(500);
    term_printf("\n已打开传感器电源\n\r");
    term_printf("\n     --> 1读取水位传感器\n     --> r返回\n     --> h帮助\n\r");
    
     while(1){
         BSP_OS_TimeDlyMs(100);
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0){
            //   OSTmrStart(tmr_delete_term_task, &err);
               temp = cmmd_buff[0];
               if (temp == '1'){
                term_printf("\n正在读取...\n\r"); 
                BSP_OS_SemWait(&rs485_mutex, 0u);    
                for(u_int8_t i = 0; i < 5; i++){ 
                   BSP_OS_TimeDlyMs(10);
                   send_collect_water_cmd(WATER_ADDR);//发送数据采集命令                          
                   BSP_OS_TimeDlyMs(100);
                   ret = rs485_uart_read_water_pack(rs485_read_buff,sizeof(rs485_read_buff));
                   if (ret > 0){
                       process_rs485_uart_water_data_pack((water_data_pack*)rs485_read_buff);
                       term_printf("\n水位值：%.2fm",water_data.water_value);  
                       BSP_OS_SemPost(&rs485_mutex); 
                       SENSOR_POWER_OFF;
                       break;
                   }
                   if(i == 4) 
                   {
                       term_printf("\n\r采集数据失败\n\r");
                       BSP_OS_SemPost(&rs485_mutex); 
                       SENSOR_POWER_OFF;
                   }
                }                
               }else if(temp == 'r'){ 
                  term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
                  break;
               }else if(temp == 'h'){ 
                  term_printf("\n     --> 1读取水位传感器\n     --> r返回\n     --> h帮助\n\r");
               }else
                  term_printf("\n--> ");           
          }
     }     
}
#endif

#if CFG_MODULE_CURRENT
/// 水位传感器调试 
///
/// 包括水位功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void fun_current_test()
{
    int ret    = 0;   
    char cmmd_buff[2] = {0};
    u_int8_t err;
    char temp = 0;
    term_printf("\n     --> 1读取接地环流传感器\n     --> r返回\n     --> h帮助\n\r");
    
    while(1){
         BSP_OS_TimeDlyMs(100);
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0){
               temp = cmmd_buff[0];
               if (temp == '1'){
                   term_printf("\n正在读取...\n\r"); 
                   DrvPIT_EnableINT(PIT0);  
                   OSSemPend(sem_sample_finish,0,&err);
                   if((ADC_Data.A_results >= 0) && (ADC_Data.A_results <= 500))
                   {
                       term_printf("\nA相电流值值：%.2fA",ADC_Data.A_results); 
                   }
                   else
                   {
                       term_printf("\n\rA相数据采集失败\n\r");
                   }
                   if((ADC_Data.B_results >= 0) && (ADC_Data.B_results <= 500))
                   {
                       term_printf("\nB相电流值值：%.2fA",ADC_Data.B_results); 
                   }
                   else
                   {
                       term_printf("\n\rB相数据采集失败\n\r");
                   }                   
                   if((ADC_Data.C_results >= 0) && (ADC_Data.C_results <= 500))
                   {
                       term_printf("\nC相电流值值：%.2fA",ADC_Data.C_results); 
                   }
                   else
                   {
                       term_printf("\n\rC相数据采集失败\n\r");
                   }                   
               }else if(temp == 'r'){ 
                  term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
                  break;
               }else if(temp == 'h'){ 
                  term_printf("\n     --> 1读取水位传感器\n     --> r返回\n     --> h帮助\n\r");
               }else
                  term_printf("\n--> ");           
          }
     }     
}
#endif


#if CFG_MODULE_TEMPHUMI
/// 温湿度传感器调试 
///
/// 包括温湿度功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void fun_temphumi_test()
{
    int ret    = 0;   
    char cmmd_buff[2] = {0};
    char temp = 0;
    SENSOR_POWER_ON;   //打开温湿度传感器电源
    term_printf("\n正在打开传感器电源...\n\r");    
    BSP_OS_TimeDlyMs(2000);
    term_printf("\n已打开传感器电源\n\r");
    term_printf("\n     --> 1读取温湿度传感器\n     --> r返回\n     --> h帮助\n\r");
    while(1){
         BSP_OS_TimeDlyMs(100);
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0){
               temp = cmmd_buff[0];
               if (temp == '1'){
                   term_printf("\n正在读取...\n\r"); 
                   for(char i = 0;i<10;i++)
                   {
                       TempHumi_Data.Tempresults[i] = drv_adc_mid(ADC0_BASE_PTR,16);  //ADC0_SE16
                       TempHumi_Data.Humiresults[i] = drv_adc_mid(ADC1_BASE_PTR,18);  //ADC1_SE18
                   }
                     //0.0032258 = 3.3 / 1024
                     //1.6667 = 5 / 3 将0~5v电压分压变成0~3v输入adc管脚
                     //得到的电压根据0~5v对应-40~125℃转换 -40 + 测得电压 /(5 / 165) = -40 + 测得电压 / 0.030303
                     //  TempHumi_Data.Tempresults_f[i] = (TempHumi_Data.Tempresults[i] *  0.0032258 * 1.6667) / 0.030303 - 40;      
                   TempHumi_Data.Temp_Data = Middle2(TempHumi_Data.Tempresults) *  0.177422 - 40;
                       //0.0032258 = 3.3 / 1024
                       //1.6667 = 5 / 3 将0~5v电压分压变成0~3v输入adc管脚
                       //得到的电压根据0~5v对应0~100%转换 
                       //  TempHumi_Data.Tempresults_f[i] = (TempHumi_Data.Tempresults[i] *  0.0032258 * 1.6667) / 0.05;     
                   TempHumi_Data.Humi_Data = Middle2(TempHumi_Data.Humiresults) *  0.107528;
                   if((TempHumi_Data.Temp_Data >= -40) && (TempHumi_Data.Temp_Data <= 125))
                   {
                       term_printf("\n温度值：%.2f°",TempHumi_Data.Temp_Data); 
                   }
                   else
                   {
                      term_printf("\n\r温度数据采集失败\n\r");
                   }
                   if((TempHumi_Data.Humi_Data >= 0) && (TempHumi_Data.Humi_Data <= 100))
                   {
                       term_printf("\n湿度值：%.2f%",TempHumi_Data.Humi_Data); 
                   }
                   else
                   {
                      term_printf("\n\r湿度数据采集失败\n\r");
                   } 
                   SENSOR_POWER_OFF; 
               }else if(temp == 'r'){ 
                  term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
                  break;
               }else if(temp == 'h'){ 
                  term_printf("\n     --> 1读取温湿度传感器\n     --> r返回\n     --> h帮助\n\r");
               }else
                  term_printf("\n--> ");           
          }
     }     
}
#endif

/// flash调试 
///
/// 包括温湿度功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建
void fun_flash_test()
{
    int ret    = 0;   
    char cmmd_buff[2] = {0};
    char temp = 0;
    u_int8_t w_buff[2]={0x55,0xaa};
    u_int8_t r_buff[2]={0x00,0x00};
    term_printf("\n     --> 1开始测试flash\n     --> r返回\n     --> h帮助\n\r");
     while(1){
         BSP_OS_TimeDlyMs(100);
         ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
         if (ret > 0){
               temp = cmmd_buff[0];
               if (temp == '1'){
                   // 配置IO管脚，初始化flsh芯片
                    Flash_GPIO_Init();
                #ifdef FLASH_W25Q256
                    Enter4ByteAddrMode();      //new add    进入4Byte模式
                #endif
                    
                #ifdef FLASH_W25Q128    
                      Exit4ByteAddrMode();     //new add    退出4Byte模式
                #endif
                Flash_Erase_Sector(0,0);
                Flash_Write_MorePage(w_buff,0,2);
                term_printf("\n写入2个字节：0x%x,0x%x",w_buff[0],w_buff[1]); 
                Flash_Read(r_buff,0,2);
                term_printf("\n读出2个字节：0x%x,0x%x",r_buff[0],r_buff[1]); 
                if((w_buff[0] == r_buff[0]) && (w_buff[1] == r_buff[1]))
                {
                    term_printf("\n\rFLASH正常！\n\r");
                }
                else
                {
                    term_printf("\n\rFLASH异常！\n\r");
                }
               }else if(temp == 'r'){ 
                  term_printf("\n     --> 1氧气传感器\n     --> 2一氧化碳传感器\n     --> 3甲烷传感器\n     --> 4硫化氢传感器\n     --> 5水位传感器\n     --> 6接地电流传感器\n     --> 7温湿度传感器\n     --> 8FLASH测试\n     --> r返回\n     --> h帮助\n\r");
                  break;
               }else if(temp == 'h'){ 
                  term_printf("\n     --> 1开始测试flash\n     --> r返回\n     --> h帮助\n\r");
               }else
                  term_printf("\n--> ");           
          }
     }     
}

/// 初始化相关信息
///
/// 包括下载配置文件功能 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         jjj与2017-08-01创建 
void func_device_init()
{                                                                
       char cmmd_buff[2] = {0};
       char temp = 0;
       int ret = 0;
   //    u_int8_t err = 0; 
       
       term_printf("\n     --> 1下载配置文件\n     --> r返回\n     --> h帮助\n\r");
       while(1){
       BSP_OS_TimeDlyMs(500);
       ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
       if (ret > 0){
             temp = cmmd_buff[0];
             if (temp == '1'){
                 term_printf("\n  --> 下载配置文件");
                // func_cmd_download_cfgfile(HY_PKG_TYPE_CFG_FILE);
              }else if (temp == '2'){
                  
             }else if (temp == '3'){

             }else if(temp == '4'){

             }else if(temp == '5'){

             }else if(temp == '6'){

             }else if(temp == 'r'){
                  term_printf("--> 1设备初始化\n--> 2网络设置\n--> 3传感器调试\n--> q系统复位\n--> h帮助\r\n");
                  break;
             }else if(temp == 'h'){
                  term_printf("\n     --> 1下载配置文件\n     --> r返回\n     --> h帮助\n\r");
             }else
                term_printf("\n--> "); 
          }           
   }
}





/// 终端交互任务 
///
/// 终端交互 
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         gkl与2017-05-26创建
static  void task_term (void        *p_arg)
{
    char cmmd_buff[10] = {0};
    char temp = 0;
    int ret = 0;  
    
	p_arg = p_arg;
	
//    term_printf("\n\r\n\r...欢迎进入mk60 host命令行模式...\r\n");
//    term_printf("\n请输入命令（帮助请输入：help）\r\n");
//    term_printf("--> 1设备初始化\n--> 2网络设置\n--> 3传感器调试\n--> q系统复位\n--> h帮助\r\n");
    
	while(1) {
		ret = term_uart_readln((u_int8_t*)cmmd_buff,sizeof(cmmd_buff),0);
		if (ret > 0){
		   temp = cmmd_buff[0];
		   if (temp == '1'){
			   //term_printf("\n--> 设备初始化\r\n");
			   func_device_init();                
		   }else if (temp == '2'){                 
			   //term_printf("\n--> 网络设置\r\n");
			   //func_ethernet_setting(); 
		   }else if (temp =='3'){
			   //term_printf("\n--> 传感器调试\r\n");
			   //func_sensor_test(); 
		   }else if (temp == 'q'){
			   //term_printf("\n--> 系统重启中...\r\n");
                           ptz_hori_stop(0);
                           ptz_vert_stop(0);
			   //k60_rest();
		   }else if(temp == 'h'){
				//term_printf("--> 1设备初始化\n--> 2加密芯片调试\n--> 3传感器调试\n--> q系统复位\n--> h帮助\r\n"); 
		   }else
			 term_printf("\n--> "); 
		}
    }              
}

/// 创建终端交互任务 
///
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         gkl与2017-05-26创建
static OS_STK task_term_stk[TASK_TERM_STK_SIZE];
static void task_create_term (void)
{ 
   CPU_INT08U  task_err;   
   CPU_INT08U  name_err;
   
   task_err = OSTaskCreateExt((void (*)(void *)) task_term,           
		   (void           *) 0,
		   (OS_STK         *)&task_term_stk[TASK_TERM_STK_SIZE - 1],
		   (INT8U           ) TASK_TERM_PRIO,                   
		   (INT16U          ) TASK_TERM_PRIO,
		   (OS_STK         *)&task_term_stk[0],
		   (INT32U          ) TASK_TERM_STK_SIZE,
		   (void           *) 0,
		   (INT16U          )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));     
    #if (OS_TASK_NAME_EN > 0)
    OSTaskNameSet(TASK_TERM_PRIO, "task_term", &name_err);
    #endif
       
    if ((task_err == OS_ERR_NONE) && (name_err == OS_ERR_NONE)) {
        pdebug(DEBUG_LEVEL_INFO,"create task_term success...\n\r");
	} else { 
        pdebug(DEBUG_LEVEL_FATAL,"create task_term failed...\n\r");
	}
}

/// 终端串口初始化
///
/// 初始化终端串口驱动
/// @param  none   
/// @param  none
/// @return none 
/// @note   修改日志
///         gkl与2017-05-26创建
void init_term_uart()
{    
	// 初始化终端串口	
	g_term_uart_handle = uart_dev_init(TERM_INDEX, TERMINAL_BAUD, term_in_buff, sizeof(term_in_buff));    
}

/// 终端交互模块初始化 
///
/// 创建任务
/// @param  none   
/// @param  none
/// @return none   
/// @note   修改日志 
///         gkl与2017-05-26创建
void init_term_module()
{    
    pdebug(DEBUG_LEVEL_INFO,"init term module\n\r"); 		
	
	// 创建串口终端交互任务
	task_create_term();
	
	pdebug(DEBUG_LEVEL_INFO,"init term module finished\n\r"); 
}

