参考了别人的索引法,修改了一下,用作记录。
缺点:每个界面都要自己写,界面之间的关联需要自己写,要写很多函数。优点:通俗易懂。
硬件:stm32f103核心板、3个按键、一个lcd显示屏。软件:裸机。
首先定义索引结构。
struct menu_struct
{
int index; //索引值
int key1; //按下时要切换的索引值
int key2; //按下时要切换的索引值
int key3; //按下时要切换的索引值
int (* point)(); //函数指针,输入值为空,返回类型为int
};
typedef struct menu_struct MenuStruct;
只用了3个按键,3个键依次是,返回键(返回上一级菜单),切换键(同级菜单选项切换),确定键(进入子菜单或执行函数)。
定义索引逻辑。这个取决于自己选择。
/*定义了索引数组,索引数组要配合主程序按键值进行选择*/
MenuStruct menu[11]=
{
{0,0,1,4,show1},
{1,1,2,5,show2},
{2,2,3,2,show3},
{3,3,0,3,show4},
{4,0,0,4,show5},
{5,1,6,8,show6},
{6,1,7,9,show7},
{7,1,5,10,show8},
{8,1,6,8,action1},
{9,1,7,9,action2},
{10,1,5,10,action3},
};
这是各个索引的函数指针对应的函数,返回值为1代表是一个显示值刷新函数,返回值为0代表是一个动作函数()。
int show1()
{
draw_Rectangle(0,0,127,159,WHITE);
show_str(10,10,16,">monitor",BLACK);
show_str(10,42,16," param set",BLACK);
show_str(10,74,16," other",BLACK);
show_str(10,106,16," other",BLACK);
return 1;
}
int show2()
{
draw_Rectangle(0,0,127,159,WHITE);
show_str(10,10,16," monitor",BLACK);
show_str(10,42,16,">param set",BLACK);
show_str(10,74,16," other",BLACK);
show_str(10,106,16," other",BLACK);
return 1;
}
int show3()
{
draw_Rectangle(0,0,127,159,WHITE);
show_str(10,10,16," monitor",BLACK);
show_str(10,42,16," param set",BLACK);
show_str(10,74,16,">other",BLACK);
show_str(10,106,16," other",BLACK);
return 1;
}
int show4()
{
draw_Rectangle(0,0,127,159,WHITE);
show_str(10,10,16," monitor",BLACK);
show_str(10,42,16," param set",BLACK);
show_str(10,74,16," other",BLACK);
show_str(10,106,16,">other",BLACK);
return 1;
}
int show5()
{
draw_Rectangle(0,0,127,159,WHITE);
sprintf(adc_str,"adc=%5.3f",adc);
show_str(10,10,16,adc_str,BLACK);
sprintf(adc_str,"level=%3.1f",adc_level);
show_str(10,26,16,adc_str,BLACK);
memset(adc_str,0,sizeof(adc_str));
//
if(adc>adc_level)
draw_round(63,79,20,RED);
else
draw_round(63,79,20,GREEN);
return 1;
}
int show6()
{
draw_Rectangle(0,0,127,159,WHITE);
sprintf(adc_str," level=%3.1f",adc_level);
show_str(10,10,16,adc_str,BLACK);
sprintf(adc_str," level_set=%3.1f",adc_level_set);
show_str(10,26,16,adc_str,BLACK);
show_str(10,42,16,">+0.1",BLACK);
show_str(10,58,16," -0.1",BLACK);
show_str(10,74,16," set",BLACK);
return 1;
}
int show7()
{
draw_Rectangle(0,0,127,159,WHITE);
sprintf(adc_str," level=%3.1f",adc_level);
show_str(10,10,16,adc_str,BLACK);
sprintf(adc_str," level_set=%3.1f",adc_level_set);
show_str(10,26,16,adc_str,BLACK);
show_str(10,42,16," +0.1",BLACK);
show_str(10,58,16,">-0.1",BLACK);
show_str(10,74,16," set",BLACK);
return 1;
}
int show8()
{
draw_Rectangle(0,0,127,159,WHITE);
sprintf(adc_str," level=%3.1f",adc_level);
show_str(10,10,16,adc_str,BLACK);
sprintf(adc_str," level_set=%3.1f",adc_level_set);
show_str(10,26,16,adc_str,BLACK);
show_str(10,42,16," +0.1",BLACK);
show_str(10,58,16," -0.1",BLACK);
show_str(10,74,16,">set",BLACK);
return 1;
}
int action1()
{
adc_level_set=adc_level_set+0.1;
return 0;
}
int action2()
{
adc_level_set=adc_level_set-0.1;
return 0;
}
int action3()
{
adc_level=adc_level_set;
return 0;
}
下面是主函数。
u8 INDEX=0; //索引变量
u8 fresh_flag; //刷新标志
int (*show)()=NULL; //显示刷新函数指针
u8 key_value; //读取的按键值
看下主函数就明白了。
int main(void)
{
/*一些初始化函数*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
delay_init();
usart_init();
spi_init();
LCD_Init();
adc_init();
key_init();
show=menu[INDEX].point; //INDEX为0,把函数指针show1赋值给show
while(1)
{
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC )==0);//等待转换完成
adc = ADC_GetConversionValue(ADC1)*3.3/4096;//adc值获取
key_value=KEY_Scan(0); //获取按键值
switch(key_value) //根据按键值改变索引
{
case NONE:
break;
case KEY1:
INDEX=menu[INDEX].key1;
if(menu[INDEX].point()==1) //如过索引值对应的函数返回值为1,是需要更改
显示的函数,则将其
{
fresh_flag=1;
show=menu[INDEX].point;
}
break;
case KEY2:
INDEX=menu[INDEX].key2;
if(menu[INDEX].point()==1) //如过索引值对应的函数返回值为1,是需要更
改显示的函数,则将其
{
fresh_flag=1;
show=menu[INDEX].point;
}
break;
case KEY3:
INDEX=menu[INDEX].key3;
if(menu[INDEX].point()==1) //如过索引值对应的函数返回值为1,是需要更改
显示的函数,则将其
{
fresh_flag=1;
show=menu[INDEX].point;
}
printf("3");
break;
default :
break;
}
if(fresh_flag==1) //判断是否为显示函数已经刷新过一次了
fresh_flag=0;
else
show(); //执行显示刷新函数
LCD_Fill(); //刷屏函数
}
return 0;
}
这个程序的本质就是根据按键值在索引数组里找需要执行的函数,是果函数是显示函数,则更新主函数里显示函数指针的值,如果不是显示函数则不更新显示函数指针的值,只执行对应函数内容。
下面演示一下。这个有两级界面,一个子界面是显示adc采集值,adc值大于设置值就红灯,小于绿灯,另一个子界面设置adc设置值。
不知道为什么我上传视频没反应。把链接贴这了。
版权声明:本文为CSDN博主「一只悲伤的土狗」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_40562287/article/details/120253304
暂无评论