#include "bsp.h"
#include "menu.h"
#include "string.h"
#include "typedef.h"
#include <stdio.h>
#define VLINE2_START 74
#define VLINE1_START 33
static u8 code modelist[5][5]={"STOP","CHG","REPR","DCHG","TEST"};//0停止，1充电，2修复，3放电，4测试
static u8 cur_setting_index=0,cur_status_index=0,cur_mode_index=0;
static u8 code statuslist[10]={26,27,19,0,20,0,24,25,28,20};
void show_main_menu_unit(void)
{
	bsp_lcd_draw_hline(1,0,127);
	bsp_lcd_draw_hline(3,0,127);
	bsp_lcd_draw_hline(5,0,127);
	bsp_lcd_draw_hline(7,0,127);
	bsp_lcd_display_str(0,115,"AH",FALSE);
	bsp_lcd_display_str(0,66,"V",FALSE);
	bsp_lcd_display_str(2,25,"A",FALSE);
	bsp_lcd_display_str(2,66,"W",FALSE);
	bsp_lcd_display_str(2,115,"WH",FALSE);
	bsp_lcd_display_str(4,12,"Pcs",FALSE);
	bsp_lcd_display_str(4,98,":",FALSE);
	bsp_lcd_display_str(6,46,"V",FALSE);
	bsp_lcd_display_str(6,80,"m",FALSE);
	bsp_lcd_diplay_unit(6,86,0,FALSE);
	bsp_lcd_diplay_unit(6,116,1,FALSE);
	bsp_lcd_draw_vline(VLINE1_START,0,5);
	bsp_lcd_draw_vline(VLINE2_START,0,5);
}
//void show_adj_temp_menu(void)
void show_main_menu(void)
{
	bsp_lcd_clear(0);
	cur_setting_index = 0;
	show_main_menu_unit();
	
	//bsp_lcd_display_str(0,4,"STOP",FALSE);
	//bsp_lcd_display_str(0,36,"500.0",FALSE);
	//bsp_lcd_display_str(0,77,"123.45",FALSE);
	bsp_lcd_display_str(2,1,"48.1",FALSE);
	bsp_lcd_display_str(2,36,"1.23",FALSE);
	bsp_lcd_display_str(2,83,"12.23",FALSE);
	//bsp_lcd_display_str(4,3,"6",FALSE);
	bsp_lcd_display_str(4,36,"12.34",FALSE);
	bsp_lcd_display_str(4,86,"00:00",FALSE);
	//bsp_lcd_display_str(6,1,"1>",FALSE);
	//bsp_lcd_display_str(6,15,"12.23",FALSE);
	//bsp_lcd_display_str(6,56,"12.3",FALSE);
	//bsp_lcd_display_str(6,102,"30",FALSE);
//	bsp_lcd_display_16x16(0,0,0,UNDERLINE,FALSE);//电
//	bsp_lcd_display_16x16(0,12,1,UNDERLINE,FALSE);//压
//	//bsp_lcd_display_str(0,23,"12.3V",TRUE);
//	bsp_lcd_display_16x16(0,61,3,UNDERLINE,FALSE);//容
//	bsp_lcd_display_16x16(0,73,4,UNDERLINE,FALSE);//量
//	//bsp_lcd_display_str(0,85,"4.56AH",FALSE);
//	bsp_lcd_display_16x16(2,0,0,UNDERLINE,FALSE);//电
//	bsp_lcd_display_16x16(2,12,2,UNDERLINE,FALSE);//流
//	//bsp_lcd_display_str(2,23,"50.0A",FALSE);
//	bsp_lcd_display_16x16(2,61,0,UNDERLINE,FALSE);//电
//	bsp_lcd_display_16x16(2,73,4,UNDERLINE,FALSE);//量
//	//bsp_lcd_display_str(2,85,"7.89WH",FALSE);
//	bsp_lcd_display_16x16(4,0,5,UNDERLINE,FALSE);//模
//	bsp_lcd_display_16x16(4,12,6,UNDERLINE,FALSE);//式
//	//bsp_lcd_display_16x16(4,36,13,UNDERLINE,TRUE);//一
//	bsp_lcd_display_16x16(4,61,7,UNDERLINE,FALSE);//状
//	bsp_lcd_display_16x16(4,73,8,UNDERLINE,FALSE);//态
//	//bsp_lcd_display_16x16(4,92,19,NONUNDERLINE,FALSE);//充
//	//bsp_lcd_display_16x16(4,104,0,NONUNDERLINE,FALSE);//电
//	bsp_lcd_display_16x16(6,0,11,UNDERLINE,FALSE);//强
//	bsp_lcd_display_16x16(6,12,12,UNDERLINE,FALSE);//度
//	//bsp_lcd_display_str(6,24," 60%",FALSE);
//	bsp_lcd_display_16x16(6,61,9,UNDERLINE,FALSE);//时
//	bsp_lcd_display_16x16(6,73,10,UNDERLINE,FALSE);//间
//	//bsp_lcd_display_str(6,89,"12:14",FALSE);
//	bsp_lcd_draw_vline(59,0,7);	
//	bsp_lcd_display_ascii(6,103,':',FALSE);
}
static u8 get_str_len(char* buf)
{
	u8 result = 0;
	while(*(buf+result)){
		result++;
	}
	return result;
}
void menu_init(void)
{
}
void show_battery_count(u8 count,bit reserve)
{
	char buf[2];
	sprintf(buf,"%bd",count);
	buf[1]=0;
	bsp_lcd_display_str(4,3,buf,reserve);
}
void show_action_menu(u8 menu_index,u8 submenu_index,u8 actionresult)
{
	char showbuf[22]={0};
	switch(menu_index)
	{
		case PARAM_SETTING_MENU:
			break;
		case OFFSET_MENU:
			if(submenu_index == 0)
			{
				bsp_lcd_clear_line(2,7);//擦除下面3行
				bsp_lcd_display_str_without_underline(2,0,"Zero point correction",FALSE);
				if(actionresult == 0){
					bsp_lcd_display_str_without_underline(4,0,"Waiting...",FALSE);
				}else{
					bsp_lcd_display_str_without_underline(4,0,"Done",FALSE);
					bsp_lcd_display_str_without_underline(6,0,"Press ENTER to back.",FALSE);
				}
			}else if(submenu_index == 1){//总电压
				bsp_lcd_clear_line(2,7);//擦除下面3行
				bsp_lcd_display_str_without_underline(2,0,"Voltage correction",FALSE);
				bsp_lcd_display_str_without_underline(4,0,"Raw data:        V",FALSE);
				bsp_lcd_display_str_without_underline(6,0,"Adj data:        V",FALSE);				
			}else if(submenu_index == 2){//总电流
				bsp_lcd_clear_line(2,7);//擦除下面3行
				bsp_lcd_display_str_without_underline(2,0,"Current correction",FALSE);
				bsp_lcd_display_str_without_underline(4,0,"Raw data:        A",FALSE);
				bsp_lcd_display_str_without_underline(6,0,"Adj data:        A",FALSE);				
			}else{
				bsp_lcd_clear_line(2,7);//擦除下面3行
				if((submenu_index % 2) == 1){
					sprintf(showbuf,"CH%d VOL correction",((int)submenu_index-1)/2);
					bsp_lcd_display_str_without_underline(2,0,showbuf,FALSE);
					bsp_lcd_display_str_without_underline(4,0,"Raw data:        V",FALSE);
					bsp_lcd_display_str_without_underline(6,0,"Adj data:        V",FALSE);
				}else{
					sprintf(showbuf,"CH%d TEMP correction",((int)submenu_index-1)/2);
					bsp_lcd_display_str_without_underline(2,0,showbuf,FALSE);
					bsp_lcd_display_str_without_underline(4,0,"Raw data:        ",FALSE);
					bsp_lcd_diplay_unit_without_underline(4,102,1,FALSE);
					bsp_lcd_display_str_without_underline(6,0,"Adj data:        ",FALSE);	
					bsp_lcd_diplay_unit_without_underline(6,102,1,FALSE);						
				}
			}
			break;
	}
}
void show_param_setting_menu(void)
{
	bsp_lcd_clear(0);
	bsp_lcd_draw_hline(1,0,127);
	bsp_lcd_display_str(0,37,"PARAM SET",FALSE);
}
void show_offset_menu(u8 sub_menu_index)
{
	bsp_lcd_clear(0);
	bsp_lcd_draw_hline(1,0,127);
	bsp_lcd_display_str(0,25,"OFFSET ADJUST",FALSE);
	if(sub_menu_index < 3)
	{
		bsp_lcd_display_str_without_underline(2,1,"ZERO ADJ",FALSE);
		if(sub_menu_index == 0)
			bsp_lcd_display_str_without_underline(2,70,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(2,70,"GO",FALSE);
		bsp_lcd_display_str_without_underline(4,1,"VOL ADJ",FALSE);
		if(sub_menu_index == 1)
			bsp_lcd_display_str_without_underline(4,70,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(4,70,"GO",FALSE);
		bsp_lcd_display_str_without_underline(6,1,"CUR ADJ",FALSE);
		if(sub_menu_index == 2)
			bsp_lcd_display_str_without_underline(6,70,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(6,70,"GO",FALSE);
	}
	else if(sub_menu_index < 9)
	{
		bsp_lcd_display_str_without_underline(2,1,"CH1:  VOL",FALSE);
		if(sub_menu_index == 3)
			bsp_lcd_display_str_without_underline(2,58,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(2,58,"GO",FALSE);
		bsp_lcd_display_str_without_underline(2,78,"TEMP",FALSE);
		if(sub_menu_index == 4)
			bsp_lcd_display_str_without_underline(2,108,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(2,108,"GO",FALSE);
		bsp_lcd_display_str_without_underline(4,1,"CH2:  VOL",FALSE);
		if(sub_menu_index == 5)
			bsp_lcd_display_str_without_underline(4,58,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(4,58,"GO",FALSE);
		bsp_lcd_display_str_without_underline(4,78,"TEMP",FALSE);
		if(sub_menu_index == 6)
			bsp_lcd_display_str_without_underline(4,108,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(4,108,"GO",FALSE);	
		bsp_lcd_display_str_without_underline(6,1,"CH3:  VOL",FALSE);
		if(sub_menu_index == 7)
			bsp_lcd_display_str_without_underline(6,58,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(6,58,"GO",FALSE);
		bsp_lcd_display_str_without_underline(6,78,"TEMP",FALSE);
		if(sub_menu_index == 8)
			bsp_lcd_display_str_without_underline(6,108,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(6,108,"GO",FALSE);			
	}
	else
	{
		bsp_lcd_display_str_without_underline(2,1,"CH4:  VOL",FALSE);
		if(sub_menu_index == 9)
			bsp_lcd_display_str_without_underline(2,58,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(2,58,"GO",FALSE);
		bsp_lcd_display_str_without_underline(2,78,"TEMP",FALSE);
		if(sub_menu_index == 10)
			bsp_lcd_display_str_without_underline(2,108,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(2,108,"GO",FALSE);
		bsp_lcd_display_str_without_underline(4,1,"CH5:  VOL",FALSE);
		if(sub_menu_index == 11)
			bsp_lcd_display_str_without_underline(4,58,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(4,58,"GO",FALSE);
		bsp_lcd_display_str_without_underline(4,78,"TEMP",FALSE);
		if(sub_menu_index == 12)
			bsp_lcd_display_str_without_underline(4,108,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(4,108,"GO",FALSE);	
		bsp_lcd_display_str_without_underline(6,1,"CH6:  VOL",FALSE);
		if(sub_menu_index == 13)
			bsp_lcd_display_str_without_underline(6,58,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(6,58,"GO",FALSE);
		bsp_lcd_display_str_without_underline(6,78,"TEMP",FALSE);
		if(sub_menu_index == 14)
			bsp_lcd_display_str_without_underline(6,108,"GO",TRUE);
		else
			bsp_lcd_display_str_without_underline(6,108,"GO",FALSE);			
	}
}
u8 show_next_setting_menu(void)
{
	
	cur_setting_index++;
	if(cur_setting_index>OFFSET_MENU)
		cur_setting_index = MAIN_MENU;
	switch(cur_setting_index)
	{
		case MAIN_MENU:
//			bsp_lcd_display_16x16(0,0,21,UNDERLINE,FALSE);//0行0列，“上”，带下划线，不反色
//			bsp_lcd_display_16x16(0,12,23,UNDERLINE,FALSE);//0行12列，“限”，带下划线，不反色
			show_main_menu();
		break;
		case PARAM_SETTING_MENU:
			
			show_param_setting_menu();

		break;
		case OFFSET_MENU:
			show_offset_menu(0);
////			bsp_lcd_display_16x16(0,0,29,UNDERLINE,FALSE);//0行0列，“校”，带下划线，不反色
////			bsp_lcd_display_16x16(0,12,30,UNDERLINE,FALSE);//0行12列，“准”，带下划线，不反色
			break;
		default:	
		break;
	}
	return cur_setting_index;
}
void show_channel_value(u8 ch_index,float vol_value,float ir_value,int temp_value,bit reverse)
{
	char buf[10]={0};
	sprintf(buf,"%d>",(int)(ch_index+1));
	bsp_lcd_display_str(6,1,buf,reverse);
	if(vol_value < 9.995f)
		sprintf(buf," %0.2f",vol_value);
	else if(vol_value < 99.995f)
		sprintf(buf,"%0.2f",vol_value);
	else
		sprintf(buf,"%0.1f",vol_value);
	bsp_lcd_display_str(6,15,buf,reverse);
	if(ir_value < 9.995f){
		sprintf(buf,"%0.2f",ir_value);
		bsp_lcd_display_str(6,80,"m",FALSE);
	}else if(ir_value < 99.95f){
		sprintf(buf,"%0.1f",ir_value);
		bsp_lcd_display_str(6,80,"m",FALSE);
	}else if(ir_value <999.5f){
		sprintf(buf,"%d",((int)(ir_value+0.5f)));
		bsp_lcd_display_str(6,80,"m",FALSE);
	}else{
		bsp_lcd_display_str(6,80," ",FALSE);
		sprintf(buf,"0.2f",ir_value/1000);
		
	}
	bsp_lcd_display_str(6,56,buf,FALSE);
	if(temp_value < -50){
		sprintf(buf,"---");
	}else if(temp_value <=-10){
		sprintf(buf,"%d",temp_value);
	}else if(temp_value <0){
		sprintf(buf," %d",temp_value);
	}else if(temp_value<10){
		sprintf(buf,"  %d",temp_value);
	}else if( temp_value <100){
		sprintf(buf," %d",temp_value);
	}else{
		sprintf(buf,"%d",temp_value);
	}
	buf[3]=0;
	buf[4]=0;
	bsp_lcd_display_str(6,96,buf,FALSE);	
}
//void show_vol_setting(float svalue)
//{
//	char showbuf[7];

//	if(svalue < 10)
//		sprintf(showbuf," %0.2f",svalue);
//	else
//		sprintf(showbuf,"%0.2f",svalue);
//	
//	showbuf[5]='\0';
//	bsp_lcd_display_str(0,23,showbuf,TRUE);
//}
void show_vol_value_with_yx(u8 row,u8 startcol,u8 endcol,float value,bit reverse,bit underline)
{
	char showbuf[10];
	u8 strlen = 0;
	u8 newstartcol=0;
	bsp_lcd_clear_span(row,startcol,endcol,FALSE);
	bsp_lcd_clear_span(row+1,startcol,endcol,underline);
	if(value<99.95f && value > -99.95f)
		sprintf(showbuf,"%0.2f",value);
	else
		sprintf(showbuf,"%0.1f",value);
	strlen=get_str_len(showbuf);
	newstartcol = (endcol - startcol - strlen*6) /2 + startcol +1;
	if(underline)
		bsp_lcd_display_str(row,newstartcol,showbuf,reverse);
	else
		bsp_lcd_display_str_without_underline(row,newstartcol,showbuf,reverse);
}
void show_vol_value(float value,bit reverse)
{
	show_vol_value_with_yx(0,VLINE1_START+1,66,value,reverse,1);
}
void show_cur_value(float value,bit reverse)
{
	char showbuf[6];
	if(value>=9.995f && value<99.95f)
		sprintf(showbuf,"%0.1f",value);
	else if(value < 9.995f)
		sprintf(showbuf,"%0.2f",value);
	else
		sprintf(showbuf," %d",(int)(value+0.5f));
	bsp_lcd_display_str(2,1,showbuf,reverse);
	
}
void show_mode_value(u8 mode_index,bit reverse)
{
	cur_mode_index = mode_index;
	bsp_lcd_display_str(0,4,"    ",FALSE);
	bsp_lcd_display_str(0,4,modelist[cur_mode_index],reverse);
}
void show_cap_value(float value,bit reverse)
{
	char showbuf[8];
	u8 strlen=0;
	u8 endcol=0,startcol;
	if(value>=999.995f && value < 10000.0f){
		sprintf(showbuf,"%0.1f",(value+0.05f));
		bsp_lcd_display_str(0,109," ",reverse);
		endcol = 115;
	}else if(value < 999.995f){
		sprintf(showbuf,"%0.2f",value);
		bsp_lcd_display_str(0,109," ",reverse);
		endcol=115;
	}else{
		sprintf(showbuf,"%0.3f",(value/1000.0f));
		bsp_lcd_display_str(0,109,"K",reverse);
		endcol=109;
	}
	strlen = get_str_len(showbuf);
	startcol = (endcol - VLINE2_START - strlen*6) /2 + VLINE2_START + 1;
	bsp_lcd_display_str(0,startcol,showbuf,reverse);	
}
void show_power_value(float value,bit reverse)
{
	char showbuf[8];
	if(value<9.995f){
		sprintf(showbuf," %0.2f",value);
	}else if(value < 99.995f){
		sprintf(showbuf,"%0.2f",value);
	}else if(value < 999.5f){
		sprintf(showbuf,"%0.1f",value);
	}else{
		sprintf(showbuf,"%0.1fK",value/1000);
	}
	bsp_lcd_display_str(2,36,showbuf,reverse);
}
void show_qe_value(float value,bit reverse)
{
	char showbuf[8];
	if(value>=10.0f && value<100.0f)
		sprintf(showbuf,"%0.1fWH",(value+0.05f));
	else if(value < 10.0f)
		sprintf(showbuf,"%0.2fWH",value);
	else if(value >=100.0f && value < 1000.0f)
		sprintf(showbuf," %dWH",(int)(value+0.5f));
	else if(value >= 1000.0f && value < 10000.0f)
		sprintf(showbuf,"%dWH",(int)(value+0.5f));
	else
		sprintf(showbuf,"%0.1fKWH",(value/1000.0f));
	bsp_lcd_display_str(2,85,showbuf,reverse);
}
void show_status_value(u8 status_index,bit reverse)
{
	cur_status_index = status_index;
	bsp_lcd_display_16x16(4,92,statuslist[status_index*2],NONUNDERLINE,reverse);//充
	bsp_lcd_display_16x16(4,104,statuslist[status_index*2+1],NONUNDERLINE,reverse);//电	
}

void show_time_value(u16 minutes,bit reverse)
{
	static u8 curhour=0xff;
	static u8 curmin = 0xff;
	static bit lastreverse=0;
	char showbuf[3];
	u8 hour,min;
	hour=(u8)(minutes / 60);
	min=(u8)(minutes % 60);
	if(curhour != hour || lastreverse != reverse)
	{
		curhour = hour;
		sprintf(showbuf,"%02bd",curhour);
		bsp_lcd_display_str(4,86,showbuf,reverse);
	}
	if(curmin != min || lastreverse != reverse)
	{
		curmin = min;
		sprintf(showbuf,"%02bd",curmin);
		bsp_lcd_display_str(4,104,showbuf,reverse);
	}
	lastreverse = reverse;
}
