STM32实现多级菜单

发布于 2023-12-13  402 次阅读


先创建一个结构体,分别定义:页面,向上选择,向下选择,进入,要跳转的函数,然后再按下对应功能的按键时,在结构体中获得对应要进入的页面

typedef struct
{
	uint8_t current;//现在所在页面
	uint8_t up;//向上翻
	uint8_t down;//向下翻
	uint8_t enter;//进入功能
	void (*current_operation)();//执行函数
}key_meun;

key_meun table[15] =
{
/*******************主页**********************/
	{0,0,0,1,(*activity_main)},
	
/*******************一级菜单**********************/	
	{1,4,2,5,(*activity_meun_1)},
	{2,1,3,9,(*activity_meun_2)},
	{3,2,4,10,(*activity_meun_3)},
	{4,3,1,0,(*activity_meun_4)},//返回主页

/*******************二级页面**********************/
	{5,8,6,11,(*activity_wifiname)},
	{6,5,7,12,(*activity_wifipaw)},
	{7,6,8,13,(*activity_link_wifi)},
	{8,7,5,1,(*activity_wifiback)},
	{9,14,14,9,(*activity_fz)},
	{10,10,10,3,(*activity_about)},

/*******************三级页面**********************/
	{11,11,11,5,(*activity_set_wifiname)},
	{12,12,12,6,(*activity_set_wifipaw)},
	{13,5,5,5,(*activity_link_wifi_status)},
	{14,9,9,2,(*activity_fz_back)},
};
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin == GPIO_VCC_Pin)
	{
		HAL_NVIC_DisableIRQ(GPIO_VCC_EXTI_IRQn);

		for(uint8_t a  =0;a < 5;a++)
		{
			switch (a)
			{
				case 0:
					GPIOB ->BSRR = KEY_1_Pin; GPIOB ->BSRR = KEY_2_Pin << 16; GPIOB ->BSRR = KEY_3_Pin << 16; GPIOB ->BSRR = KEY_4_Pin << 16;GPIOB ->BSRR = KEY_5_Pin << 16;delay_us(50);
					if(GPIO_VCC_GPIO_Port->IDR & GPIO_VCC_Pin){
						if(activity!= 11 && activity!= 12 && activity!= 0){
							activity = table[activity].up;//设置向上翻页对应的活动
						}		
					}
					break;
				case 1:
					GPIOB ->BSRR = KEY_1_Pin << 16; GPIOB ->BSRR = KEY_2_Pin; GPIOB ->BSRR = KEY_3_Pin << 16; GPIOB ->BSRR = KEY_4_Pin << 16;GPIOB ->BSRR = KEY_5_Pin << 16;delay_us(50);
					if(GPIO_VCC_GPIO_Port->IDR & GPIO_VCC_Pin){
			
					}
					break;
				case 2:
					GPIOB ->BSRR = KEY_1_Pin << 16; GPIOB ->BSRR = KEY_2_Pin << 16; GPIOB ->BSRR = KEY_3_Pin; GPIOB ->BSRR = KEY_4_Pin << 16;GPIOB ->BSRR = KEY_5_Pin << 16;delay_us(50);
					if(GPIO_VCC_GPIO_Port->IDR & GPIO_VCC_Pin){
						activity = table[activity].enter;//要进入的活动
				}
					break;
				case 3:
					GPIOB ->BSRR = KEY_1_Pin << 16; GPIOB ->BSRR = KEY_2_Pin << 16; GPIOB ->BSRR = KEY_3_Pin << 16; GPIOB ->BSRR = KEY_4_Pin;GPIOB ->BSRR = KEY_5_Pin << 16;delay_us(50);
					if(GPIO_VCC_GPIO_Port->IDR & GPIO_VCC_Pin){
					
					}
					break;
				case 4:
					GPIOB ->BSRR = KEY_1_Pin << 16; GPIOB ->BSRR = KEY_2_Pin << 16; GPIOB ->BSRR = KEY_3_Pin << 16; GPIOB ->BSRR = KEY_4_Pin << 16;GPIOB ->BSRR = KEY_5_Pin;delay_us(50);
					if(GPIO_VCC_GPIO_Port->IDR & GPIO_VCC_Pin){
						activity = table[activity].down;
					}
					break;
				default: break;
			}
		}
		current_operation_index = table[activity].current_operation;
		(*current_operation_index)();//显示要进入的活动
		
		GPIOB ->BSRR = KEY_1_Pin; GPIOB ->BSRR = KEY_2_Pin; GPIOB ->BSRR = KEY_3_Pin; GPIOB ->BSRR = KEY_4_Pin ;GPIOB ->BSRR = KEY_5_Pin;
		HAL_NVIC_EnableIRQ(GPIO_VCC_EXTI_IRQn);//使能中断
		__HAL_GPIO_EXTI_CLEAR_IT(GPIO_VCC_Pin); //清除中断标志位
	}
}
void activity_meun_1(void)
{
	if(show.refresh_all_display || show.refresh_display)
	{
		if(show.refresh_all_display){
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(64,7,WHITE,BLUE,"菜单");
		show.refresh_all_display = 0;
		}
		showChar(5,7,WHITE,BLUE,8,'<');
		show.refresh_display = 0;
	}
	showZh_16(2,37,BLACK,WHITE,"WIFI设置");
	showZh_16(2,61,WHITE,BLACK,"负载设置");
	showZh_16(2,85,WHITE,BLACK,"关于");

}
void activity_meun_2(void)
{
	if(show.refresh_all_display || show.refresh_display)
	{
		if(show.refresh_all_display){
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(64,7,WHITE,BLUE,"菜单");
		show.refresh_all_display = 0;
		}
		showChar(5,7,WHITE,BLUE,8,'<');
		show.refresh_display = 0;
	}
	showZh_16(2,37,WHITE,BLACK,"WIFI设置");
	showZh_16(2,61,BLACK,WHITE,"负载设置");
	showZh_16(2,85,WHITE,BLACK,"关于");

}
void activity_meun_3(void)
{
	if(show.refresh_all_display || show.refresh_display)
	{
		if(show.refresh_all_display){
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(64,7,WHITE,BLUE,"菜单");
		show.refresh_all_display = 0;
		}
		showChar(5,7,WHITE,BLUE,8,'<');
		show.refresh_display = 0;
	}
	showZh_16(2,37,WHITE,BLACK,"WIFI设置");
	showZh_16(2,61,WHITE,BLACK,"负载设置");
	showZh_16(2,85,BLACK,WHITE,"关于");

}
void activity_meun_4(void)
{
	if(show.refresh_all_display || show.refresh_display)
	{
		if(show.refresh_all_display){
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(64,7,WHITE,BLUE,"菜单");
		show.refresh_all_display = 0;
		}
		showChar(5,7,BLUE,WHITE,8,'<');
		show.refresh_display = 0;
	}
	showZh_16(2,37,WHITE,BLACK,"WIFI设置");
	showZh_16(2,61,WHITE,BLACK,"负载设置");
	showZh_16(2,85,WHITE,BLACK,"关于");
}
void activity_joinwifi(void)
{
	if(show.refresh_all_display)
	{
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(45,7,WHITE,BLUE,"连接WIFI");
		show.refresh_all_display = 0;
	}
	showZh_16(2,37,WHITE,BLACK,"名称:");
	draw_Rectangle(46,37,100,17,WHITE);
	showZh_16(2,56,WHITE,BLACK,"密码:");
	draw_Rectangle(46,56,100,17,WHITE);
}
void activity_fz(void)
{
	if(show.refresh_all_display)
	{
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(45,7,WHITE,BLUE,"负载设置");
		showChar(5,7,WHITE,BLUE,8,'<');
		showZh_16(2,37,WHITE,BLACK,"负载开关:");
		if(show.fz){
			showChar(5,7,WHITE,BLUE,8,'<');
			showZh_16(80,37,BLACK,WHITE,"开");
		}else{
			showChar(5,7,WHITE,BLUE,8,'<');
			showZh_16(80,37,BLACK,WHITE,"关");
		}
		
		show.refresh_all_display = 0;
	}else if(show.refresh_display)
	{
		if(show.fz){
			showChar(5,7,WHITE,BLUE,8,'<');
			showZh_16(80,37,BLACK,WHITE,"开");
		}else{
			showChar(5,7,WHITE,BLUE,8,'<');
			showZh_16(80,37,BLACK,WHITE,"关");
		}
		show.refresh_display = 0;
	}
	
}

void activity_fz_back(void)
{
	if(show.refresh_all_display)
	{
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(45,7,WHITE,BLUE,"负载设置");
		show.refresh_all_display = 0;
		showChar(5,7,BLUE,WHITE,8,'<');
		showZh_16(2,37,WHITE,BLACK,"负载开关:");
		if(show.fz){
			showChar(5,7,BLUE,WHITE,8,'<');
			showZh_16(80,37,WHITE,BLACK,"开");
		}else{
			showChar(5,7,BLUE,WHITE,8,'<');
			showZh_16(80,37,WHITE,BLACK,"关");
		}
		
		show.refresh_all_display = 0;
	}else if(show.refresh_display)
	{
		
		if(show.fz){
			showChar(5,7,BLUE,WHITE,8,'<');
			showZh_16(80,37,WHITE,BLACK,"开");
		}else{
			showChar(5,7,BLUE,WHITE,8,'<');
			showZh_16(80,37,WHITE,BLACK,"关");
		}
		
		show.refresh_display = 0;
	}
}
void activity_about(void)
{
	if(show.refresh_all_display)
	{
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(62,7,WHITE,BLUE,"关于");
		show.refresh_all_display = 0;
	}
	showZh_16(2,37,WHITE,BLACK,"作者: rain");
	showZh_16(2,55,WHITE,BLACK,"软件版本: v1.0");
	showZh_16(2,73,WHITE,BLACK,"硬件版本: v1.0");
}
void activity_wifiname(void)
{
	if(show.refresh_display)
	{
		showZh_16(2,37,BLACK,WHITE,"名称");
		showZh_16(2,56,WHITE,BLACK,"密码");
		showZh_16(62,80,WHITE,BLACK,"连接");
		showChar(5,7,WHITE,BLUE,8,'<');
		
	}else if(show.refresh_all_display){
		showClean(0,0,160,128);
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(45,7,WHITE,BLUE,"连接WIFI");
		showChar(5,7,WHITE,BLUE,8,'<');
		showZh_16(2,37,BLACK,WHITE,"名称");
		draw_Rectangle(46,37,100,17,WHITE);
		showZh_16(2,56,WHITE,BLACK,"密码");
		draw_Rectangle(46,56,100,17,WHITE);
		draw_Rectangle(0,76,160,52,BLACK);
		showZh_16(62,80,WHITE,BLACK,"连接");
		
		if(key_wifiname_len >= 12)
		{
			for(uint8_t i = 0;i<12;i++){
				show_jianzhi_temp[i] = key_wifinam_jianzhi[key_wifiname_len-12+i];
			}
			showZh_16(47,38,BLACK,WHITE,show_jianzhi_temp);
		}
		else{showZh_16(47,38,BLACK,WHITE,key_wifinam_jianzhi);}
		if(key_wifipaw_len >= 12)
		{
			for(uint8_t i = 0;i<12;i++){
				show_jianzhi_temp[i] = key_wifipaw_jianzhi[key_wifipaw_len-12+i];
			}
			showZh_16(47,56,BLACK,WHITE,show_jianzhi_temp);
		}
		else{showZh_16(47,56,BLACK,WHITE,key_wifipaw_jianzhi);}	
		
	}
	show.refresh_display = 0;
	show.refresh_all_display = 0;

}
void activity_wifipaw(void)
{
	if(show.refresh_display)
	{
		showZh_16(2,37,WHITE,BLACK,"名称");
		showZh_16(2,56,BLACK,WHITE,"密码");
		showZh_16(62,80,WHITE,BLACK,"连接");
		showChar(5,7,WHITE,BLUE,8,'<');
	}
	else if(show.refresh_all_display){
		draw_Rectangle(0,0,160,30,BLUE);
		showZh_16(45,7,WHITE,BLUE,"连接WIFI");
		showChar(5,7,WHITE,BLUE,8,'<');
		showZh_16(2,37,WHITE,BLACK,"名称");
		draw_Rectangle(46,37,100,17,WHITE);
		showZh_16(2,56,BLACK,WHITE,"密码");
		draw_Rectangle(46,56,100,17,WHITE);
		draw_Rectangle(0,76,160,52,BLACK);
		showZh_16(62,80,WHITE,BLACK,"连接");
	}
	show.refresh_display = 0;
	show.refresh_all_display = 0;
}
void activity_wifiback(void)
{
	 if(show.refresh_all_display){
		showZh_16(45,7,WHITE,BLUE,"连接WIFI");
		showZh_16(2,56,WHITE,BLACK,"密码");
		draw_Rectangle(0,76,160,52,BLACK);
		show.refresh_all_display = 0;
	}
	showChar(5,7,BLUE,WHITE,8,'<');
	showZh_16(2,37,WHITE,BLACK,"名称");
	showZh_16(2,56,WHITE,BLACK,"密码");
	showZh_16(62,80,WHITE,BLACK,"连接");
	show.refresh_display = 0;
	
}



void activity_set_wifiname(void)
{
		if(key_wifiname_len >= 12)
		{
			for(uint8_t i = 0;i<12;i++){
				show_jianzhi_temp[i] = key_wifinam_jianzhi[key_wifiname_len-12+i];
			}
			showZh_16(47,38,BLACK,WHITE,show_jianzhi_temp);
		}
		else{showZh_16(47,38,BLACK,WHITE,key_wifinam_jianzhi);}
		
		if(show.refresh_all_display){
			if(key_keyboard == 0){
				showZh_16(4,76,BLACK,WHITE,"0 1 2 3 4 5 6 7 8 9");
				showZh_16(4,93,BLACK,WHITE,"/ # $ % & @ ( ) * +");
				showZh_16(4,110,BLACK,WHITE,"- = , . < > ? a D Y");
			}else if(key_keyboard == 1){
				showZh_16(4,76,BLACK,WHITE,"a b c d e f g h i j");
				showZh_16(4,93,BLACK,WHITE,"k l m n o p q r s t");
				showZh_16(4,110,BLACK,WHITE,"u v w x y z A 1 D Y");	
			}else if(key_keyboard == 2){
				showZh_16(4,76,BLACK,WHITE,"A B C D E F G H I J");
				showZh_16(4,93,BLACK,WHITE,"K L M N O P Q R S T");
				showZh_16(4,110,BLACK,WHITE,"U V W X Y Z a 1 D Y");	
			}
				show.refresh_all_display = 0;
		}else{
			static uint8_t l_key_xcont,l_key_ycont;
			draw_Rectangle(4+l_key_xcont*16,91+l_key_ycont*17,8,1,WHITE);//清除上次选择下标
			draw_Rectangle(4+key_data.key_xcont*16,91+key_data.key_ycont*17,8,1,BLACK);//绘制选择下标
			l_key_xcont = key_data.key_xcont;
			l_key_ycont = key_data.key_ycont;
			
			show.refresh_display = 0;	
		}
}


void activity_set_wifipaw(void)
{
	if(key_wifipaw_len >= 12)
	{
		for(uint8_t i = 0;i<12;i++){
			show_jianzhi_temp[i] = key_wifipaw_jianzhi[key_wifipaw_len-12+i];
		}
		showZh_16(47,56,BLACK,WHITE,show_jianzhi_temp);
	}
	else{
	showZh_16(47,56,BLACK,WHITE,key_wifipaw_jianzhi);}	
	
	if(show.refresh_all_display){
		if(key_keyboard == 0){
			showZh_16(4,76,BLACK,WHITE,"0 1 2 3 4 5 6 7 8 9");
			showZh_16(4,93,BLACK,WHITE,"/ # $ % & @ ( ) * +");
			showZh_16(4,110,BLACK,WHITE,"- = , . < > ? a D Y");
		}else if(key_keyboard == 1){
			showZh_16(4,76,BLACK,WHITE,"a b c d e f g h i j");
			showZh_16(4,93,BLACK,WHITE,"k l m n o p q r s t");
			showZh_16(4,110,BLACK,WHITE,"u v w x y z A 1 D Y");	
		}else if(key_keyboard == 2){
			showZh_16(4,76,BLACK,WHITE,"A B C D E F G H I J");
			showZh_16(4,93,BLACK,WHITE,"K L M N O P Q R S T");
			showZh_16(4,110,BLACK,WHITE,"U V W X Y Z a 1 D Y");	
		}
		show.refresh_all_display = 0;
	}
	static uint8_t l_key_xcont,l_key_ycont;
	draw_Rectangle(4+l_key_xcont*16,91+l_key_ycont*17,8,1,WHITE);//清除上次选择下标
	draw_Rectangle(4+key_data.key_xcont*16,91+key_data.key_ycont*17,8,1,BLACK);//绘制选择下标
	l_key_xcont = key_data.key_xcont;
	l_key_ycont = key_data.key_ycont;
	
	show.refresh_display = 0;	
}

void activity_link_wifi(void)
{
	show.refresh_display = 0;
	if(show.refresh_all_display)
	{
		show.refresh_all_display = 0;
	}
	showChar(5,7,WHITE,BLUE,8,'<');
	showZh_16(2,37,WHITE,BLACK,"名称");
	showZh_16(2,56,WHITE,BLACK,"密码");
	showZh_16(62,80,BLUE,WHITE,"连接");
	show.refresh_display = 0;
}
void activity_link_wifi_status(void)
{	
	showClean(0,0,160,128);
	showZh_16(2,56,WHITE,BLACK,"连接ing...");
	if(wifi_link(key_wifinam_jianzhi,key_wifipaw_jianzhi) == 1)
	{
		showZh_16(2,56,WHITE,BLACK,"连接ok            ");
		Flash_Write_wifi();
		delay_ms(1000);
	}else
	{
		showZh_16(2,56,WHITE,BLACK,"连接error        ");
	}
	show.refresh_display = 0;	
	show.refresh_all_display = 0;
}

  • alipay_img
  • wechat_img
想法不去做终究就只是想法
最后更新于 2023-12-13