【平头哥RVB2601创意应用开发】实践6-多级菜单显示测试
[复制链接]
多级菜单
多级菜单的实现,大体分为两种设计思路:
总体思路都是把菜单的各个界面联系起来,可以从上级菜单跳到下级菜单,也可从下级菜单返回上级菜单。
数组查表的方式比较简单,易于理解,本篇就来使用数组查表发在RVB2601上实现多级菜单的显示。
代码修改
数组查表
首先需要定义一个结构体:
typedef struct
{
uchar current;
uchar down;//向下翻索引号
uchar enter;//确认索引号
void (*current_operation)();
} key_table;
- current:当前页面的索引号
- down:按下“向下翻“按钮后要跳转到的页面索引号
- enter:按下“确认“按钮后要跳转到的页面索引号
- current_operation:当前页面的索引号要执行的显示函数
注意:因为RVB2601板子上只有2个用户按键,因此这里只定义了”向下翻“和”确认“按钮。如果还要更多的按钮,还可以定义一个”向上翻“的按钮,从而实现菜单中选项的上下切换。
然后定义一个表,用来定义各个页面间如何跳转
key_table table[30]=
{
//第1层
{0,1, 4,(*fun_a1)},
{1,2, 8,(*fun_b1)},
{2,3,12,(*fun_c1)},
{3,0,25,(*fun_d1)},
//第2层
{4,5,16,(*fun_a21)},
{5,6,17,(*fun_a22)},
{6,7,18,(*fun_a23)},
{7,4, 0,(*fun_a24)},
{ 8, 9,19,(*fun_b21)},
{ 9,10,20,(*fun_b22)},
{10,11,21,(*fun_b23)},
{11, 8, 1,(*fun_b24)},
{12,13,22,(*fun_c21)},
{13,14,23,(*fun_c22)},
{14,15,24,(*fun_c23)},
{15,12, 2,(*fun_c24)},
//第3层
{16,16,4,(*fun_a31)},
{17,17,5,(*fun_a32)},
{18,18,6,(*fun_a33)},
{19,19, 8,(*fun_b31)},
{20,20, 9,(*fun_b32)},
{21,21,10,(*fun_b33)},
{22,22,12,(*fun_c31)},
{23,23,13,(*fun_c32)},
{24,24,14,(*fun_c33)},
//第0层
{25,25,0,(*fun_0)},
};
具体的显示函数
/*********第1层***********/
void fun_a1()
{
u8g2_DrawStr(&u8g2,0,16,">");
u8g2_DrawStr(&u8g2,16,16,"[1]Weather");
u8g2_DrawStr(&u8g2,16,32,"[2]Music");
u8g2_DrawStr(&u8g2,16,48,"[3]Device Info");
u8g2_DrawStr(&u8g2,16,64,"<--");
}
void fun_b1()
{
u8g2_DrawStr(&u8g2,0,32,">");
u8g2_DrawStr(&u8g2,16,16,"[1]Weather");
u8g2_DrawStr(&u8g2,16,32,"[2]Music");
u8g2_DrawStr(&u8g2,16,48,"[3]Device Info");
u8g2_DrawStr(&u8g2,16,64,"<--");
}
void fun_c1()
{
u8g2_DrawStr(&u8g2,0,48,">");
u8g2_DrawStr(&u8g2,16,16,"[1]Weather");
u8g2_DrawStr(&u8g2,16,32,"[2]Music");
u8g2_DrawStr(&u8g2,16,48,"[3]Device Info");
u8g2_DrawStr(&u8g2,16,64,"<--");
}
void fun_d1()
{
u8g2_DrawStr(&u8g2,0,64,">");
u8g2_DrawStr(&u8g2,16,16,"[1]Weather");
u8g2_DrawStr(&u8g2,16,32,"[2]Music");
u8g2_DrawStr(&u8g2,16,48,"[3]Device Info");
u8g2_DrawStr(&u8g2,16,64,"<--");
}
/*********第2层***********/
void fun_a21()
{
u8g2_DrawStr(&u8g2,0,16,">");
u8g2_DrawStr(&u8g2,16,16,"* HangZhou");
u8g2_DrawStr(&u8g2,16,32,"* BeiJing");
u8g2_DrawStr(&u8g2,16,48,"* ShangHai");
u8g2_DrawStr(&u8g2,16,64,"<--");
}
//省略...
按键切换页面
void key_loop(void)
{
if(GPIO_PIN_LOW == csi_gpio_pin_read(&key1))
{
//持续按键置标志
if(g_key1)
{
g_key1 |= 0x40;
}
else
{
g_key1 = 1;
}
}
//持续按键后释放
else if(g_key1 & 0x40)
{
LOGD(TAG, "\n===key1=%d, %s, %d\n", g_key2, __FUNCTION__, __LINE__);
g_key1=0x80;
func_index = table[func_index].enter; //确认
}
else
{
g_key1 = 0;
}
if(GPIO_PIN_LOW == csi_gpio_pin_read(&key2))
{
if(g_key2)
{
g_key2 |= 0x40;
}
else
{
g_key2 = 1;
}
}
else if(g_key2 & 0x40)
{
LOGD(TAG, "\n===key2=%d, %s, %d\n", g_key2, __FUNCTION__, __LINE__);
g_key2=0x80;
func_index = table[func_index].down; //向下翻
}
else
{
g_key2 = 0;
}
if (func_index != last_index)
{
current_operation_index = table[func_index].current_operation;
u8g2_ClearBuffer(&u8g2);
(*current_operation_index)();//执行当前操作函数
u8g2_SendBuffer(&u8g2);
last_index = func_index;
}
}
演示
测试效果如下:
总结
本篇介绍了如何在RVB2601的屏幕上进行多级菜单的显示。
|