/*
 * mymain.c
 *
 *  Created on: 2022年4月15日
 *      Author: wuwen
 */
#include "mymain.h"




#define morse_play_t1 50
#define morse_play_t2 (morse_play_t1*3)
#define morse_play_hz 1000


char config_buzzer_for_button_pass=1;
char config_buzzer_for_button_longpass=1;
char config_buzzer_for_sele_change=0;

int config_oled_luminance=100;

char sys_lan=0;



button  B1,TACKA,TACKB;//创建一个按钮
encoder E1;//创建一个编码器
int mode=0;
uint32_t run_tick=0,jump_tick=0;

uint16_t fps=0,fps_=0;


int encode_c=0;

int test_code=0;





word turnon=
{
	"ON",
	"开"
};

word turnoff=
{
	"OF",
	"关"
};

const char * GET_WORD(word*word,char lan)
{
	switch(lan)
	{
		case 0:
			return word->en;
			break;
		case 1:
			return word->ch;
			break;
	}
	return "X.X";
}


int SHOW_MENU(menu *m,int encoder,char button,char lan,char dismode)
{
	int return_code=-1;
	const char *list;
	int sele_dis_len=0;
	char temp[10];
	//get list
	switch(lan)
	{
	case 0:
		list=m->list_en;
		break;
	case 1:
		list=m->list_ch;
		break;
	default :
		return -1;

	}

	m->sele+=encoder;

	if(m->sele<0){m->sele=0;}
	if(m->sele>m->list_const){m->sele=m->list_const;}

	if(m->sele<m->list_dis_top){m->list_dis_top--;}
	if((m->sele>m->list_dis_top+2)&&(m->list_dis_top+3<m->list_const)){m->list_dis_top++;}

	
	
	OLED_Str_list(0,0,16,list,m->list_dis_top,4,1);
	
	if(m->r_sw_list!=NULL)
	{
		OLED_Str_list(95,0,16,m->r_sw_list,m->list_dis_top,4,1);
	}
	
	if(dismode==1)
	{
		sprintf(temp,"%d/%d",m->sele,m->list_const);
		OLED_Str(96,56,8,temp,0,1);
	}
	
	

	OLED_square(0,(m->sele-m->list_dis_top)*16,128,((m->sele-m->list_dis_top)*16)+16,2);
	
	
	
	if(button==1)
	{
		return_code=m->sele;
	}

	return return_code;

}


void di()
{
	BUZZER_PLAY_NOTES(morse_play_hz,morse_play_t1,50);
	BUZZER_PLAY_NOTES(0,morse_play_t1,0);
	mo_di();
}

void da()
{
	BUZZER_PLAY_NOTES(morse_play_hz,morse_play_t2,50);
	BUZZER_PLAY_NOTES(0,morse_play_t1,0);
	mo_da();
}
//papers
void main_gui()
{
	char str[32];
	GEI_BUTTON_CODE(&TACKA,tack_a());
	GEI_BUTTON_CODE(&TACKB,tack_b());
	if(encode_c>0||TACKB.code!=0)
	{
		di();
	}
	if(encode_c<0||TACKA.code!=0)
	{
		da();
	}			
	OLED_Str(0,0,8,moser_buff,0,1);


	fps_++;
	sprintf(str,"FPS:%d",fps);
	OLED_Str(0,56,8,str,0,1);

	if(B1.code==255)
	{
		
		if(config_buzzer_for_button_longpass==1)
		{
			BUZZER_PLAY_NOTES(1000,100,50);
			BUZZER_PLAY_NOTES(0,0,0);
		}					
		
		mode=2;
		
	}

}

int main_menu()
{
	if(HAL_GetTick()>jump_tick){mode=1;}
	static menu menu_main=
	{
				0,0,
			"Main GUI\nInput mode\nBuzzer\nDisplay\nLanguage\nAbout",
		"主页\n输入模式\n蜂鸣器\n显示\n语言\n关于",5,
		NULL
	};
	return SHOW_MENU(&menu_main,encode_c,B1.code,sys_lan,1);

}

void buzzer_settings()
{
	static menu buzzer_setting=
	{
				0,0,
			"Back\nClick\nPress\nSele change",
		"返回\n点击\n长按\n选择切换",3,NULL
	};
	
	char str[32];
	
		buzzer_setting.r_sw_list=&str;
		sprintf(str,"../");
		if(config_buzzer_for_button_pass)
		{
			sprintf(str,"%s\n%s",str,GET_WORD(&turnon,sys_lan));
		}else
		{
			sprintf(str,"%s\n%s",str,GET_WORD(&turnoff,sys_lan));
		}
		if(config_buzzer_for_button_longpass)
		{
			sprintf(str,"%s\n%s",str,GET_WORD(&turnon,sys_lan));
		}else
		{
			sprintf(str,"%s\n%s",str,GET_WORD(&turnoff,sys_lan));
		}
		if(config_buzzer_for_sele_change)
		{
			sprintf(str,"%s\n%s",str,GET_WORD(&turnon,sys_lan));
		}else
		{
			sprintf(str,"%s\n%s",str,GET_WORD(&turnoff,sys_lan));
		}				
		switch(SHOW_MENU(&buzzer_setting,encode_c,B1.code,sys_lan,0))
		{
			case 0:
				//jump to main GUI
				mode=2;
				break;
			case 1:
				if(config_buzzer_for_button_pass){config_buzzer_for_button_pass=0;}else{config_buzzer_for_button_pass=1;}
				break;
			case 2:
				if(config_buzzer_for_button_longpass){config_buzzer_for_button_longpass=0;}else{config_buzzer_for_button_longpass=1;}
				break;
			case 3:
				if(config_buzzer_for_sele_change){config_buzzer_for_sele_change=0;}else{config_buzzer_for_sele_change=1;}
				break;

		}
		if(HAL_GetTick()>jump_tick){mode=2;}
}


void display_settings()
{
	static menu display_setting=
	{
				0,0,
			"Back\nluminance\nSpin",
		"返回\n亮度\n旋转",2,NULL
	};
	static char config_oled_luminance_sele=0;
	char str[32];
	display_setting.r_sw_list=&str;
	
	
	if(config_oled_luminance_sele)
	{
		if(B1.code!=0)
		{
			config_oled_luminance_sele=0;
		}
		if(encode_c!=0)
		{
			config_oled_luminance+=encode_c;
			if(config_oled_luminance>100){config_oled_luminance=100;}
			if(config_oled_luminance<0){config_oled_luminance=0;}
			OLED_Setting_luminance(config_oled_luminance);
			
		}
		sprintf(str,"../\n%3d%%",config_oled_luminance);
		SHOW_MENU(&display_setting,0,0,sys_lan,0);
		OLED_square(0,16,95,32,2);
		
	}else
	{
		sprintf(str,"../\n%3d%%",config_oled_luminance);
		switch(SHOW_MENU(&display_setting,encode_c,B1.code,sys_lan,0))
		{
			case 0:
				mode=2;
				break;
			case 1:
				config_oled_luminance_sele=1;
				break;
		}	
	}

	
	if(HAL_GetTick()>jump_tick){config_oled_luminance_sele=0;mode=2;}
}

void set_language()
{
	static menu display_setting=
	{
				0,0,
		"Back\nEnglish\n中文",
		"返回\nEnglish\n中文",2,NULL
	};
	char str[32];
	display_setting.r_sw_list=&str;
	
	
	if(sys_lan==0)
	{
		sprintf(str,"../\n<-");
	}else
	{
		sprintf(str,"../\n\n<-");
	}
	
			switch(SHOW_MENU(&display_setting,encode_c,B1.code,sys_lan,0))
		{
			case 0:
				mode=2;
				break;
			case 1:
				sys_lan=0;
				break;
			case 2:
				sys_lan=1;
				break;
		}	
	
	
	
	
	if(HAL_GetTick()>jump_tick){mode=2;}
}

void mymain()
{
	int temp;
	PWR_EN(1);
	

	
	
	
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);//启动n通道的pwm
	HAL_TIM_Base_Start_IT(&htim3);
	
	EPPROM_SLOWWRITE_INIT();
	OLED_Init();//屏幕初始化
	OLED_Setting_luminance(config_oled_luminance);
	
	BUZZER_PLAY_INIT();
	MUTE(0);
	
	BUZZER_PLAY_NOTES(1000,1000,50);
	BUZZER_PLAY_NOTES(0,1000,50);
	
	B1.config_longtimes=1;//启用B1按钮的长按检测
	
	while(1)
	{
		GEI_BUTTON_CODE(&B1,en_c());//循环更新按钮
		encode_c=GET_ENCODE(&E1);   //更新编码器
		
		//蜂鸣器控制
		if(B1.code>0)
		{
			if(B1.code<255)//1-254
			{
				if(config_buzzer_for_button_pass==1)
				{
					BUZZER_PLAY_NOTES(1000,100,50);
					BUZZER_PLAY_NOTES(0,0,0);
				}
			}else
			{

			}
			jump_tick=HAL_GetTick()+10000;
		}
			
		switch(mode)
		{
			case 0:
				//启动跳转模式
				//用于读取系统配置后跳转到其他程序
				//OLED_Str_list(0,0,16,"123中88文9\n456错\n你好",0,0,1);
				mode=1;
				break;
			
			case 1:
				//主界面
				main_gui();			
				break;
			
			case 2:
				//菜单界面
				temp=main_menu();
				switch(temp)
				{
					case 0:
						mode=1;
						break;
					case 2:
						mode=3;
						break;
					case 3:
						mode=4;
						break;
					case 4:
						mode=5;
						break;
				}
				break;
				
			case 3:
				//buzzer setting
				buzzer_settings();
				break;
			
			case 4:
				display_settings();
				break;
			
			case 5:
				set_language();
				break;
		
		}

		
		mo_server();
		BUZZER_PLAY_SERVER();
		OLED_Cache_to_hardware();//刷新屏幕
		EEPROM_SLOWWRITE_SERVER();
		
		if(HAL_GetTick()>run_tick)
		{
			run_tick+=1000;
			RUNLED_TICK();
			fps=fps_;
			fps_=0;
		}
	
	}
}

//外部中断回调
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{

	switch (GPIO_Pin)//中断线监测
	{
  	case en_a_Pin: //编码器A脚 
			switch(HAL_GPIO_ReadPin(en_a_GPIO_Port,en_a_Pin))//监测编码器A脚
			{
				case 1://通过监测B脚的状态识别正反转					
					if(HAL_GPIO_ReadPin(en_b_GPIO_Port,en_b_Pin))
					{
						E1.code+=1;
						E1.move_flag=1;//发生旋转的标记
						jump_tick=HAL_GetTick()+10000;
					}					
					break;
				case 0:
					if(HAL_GPIO_ReadPin(en_b_GPIO_Port,en_b_Pin))
					{
						E1.code-=1;
						E1.move_flag=1;//发生旋转的标记
						jump_tick=HAL_GetTick()+10000;
					}				
					break;
			}
		break;


			

  	default:
  	break;

  	//__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  }
}



void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//1ms
{
    if (htim == (&htim3))
		{
		
		}
}