电赛练习----OLED多级菜单【mini开发板】

STM32-----OLED显示实验(8080并行通信,SPI串行通信)_8080接口和spi接口_夜路难行々的博客-CSDN博客

这篇文章以及详细讲解了128*64OLED的使用,最近因为电赛学习了OLED多级菜单,现在记录一下学习过程 

menu.c核心代码:

//2023/04/08                夜路难行
// stm32mini 开发板   key0--->next          wake_up --->enter 
 
void (*current_operation_index)();       //执行当前操作函数
uint8_t func_index = 0;
int key_state;                           //接收当前的按键状态
Menu_table table[9] =
{
        {0, 0, 1, (*fun0)},     //一级界面
        {1, 2, 5, (*fun1)},     //二级菜单  
        {2, 3, 6, (*fun2)},     //二级菜单  
        {3, 4, 7, (*fun3)},     //二级菜单  
        {4, 1, 0, (*fun4)},     //二级菜单 Back

        {5, 6, 4, (*fun5)},      //三级菜单 Back
        {6, 7, 0, (*fun6)},      //三级菜单  
        {7, 8, 0, (*fun7)},      //三级菜单  
        {8, 5, 0, (*fun8)},      //三级菜单  
};
void Menu_key_set(void)
{
    key_state = KEY_Scan(0);
	  //printf("%d\r\n",key_state);
    if (key_state == 1)
    {
	    OLED_Clear();
        func_index = table[func_index].next;
    }
    if(key_state == 3)
    {
		OLED_Clear();
		func_index = table[func_index].enter;
	}			
		printf("func_index:%d\r\n",func_index);
    current_operation_index = table[func_index].current_operation;
    (*current_operation_index)(); //执行当前操作函数
}

menu.h代码:

#ifndef __MENU_H
#define __MENU_H

#include "stm32f10x.h"
//私有定义
void Menu_key_set(void);
void fun0(void);
void fun1(void);
void fun2(void);
void fun3(void);
void fun4(void);
void fun5(void);
void fun6(void);
void fun7(void);
void fun8(void);
void fun9(void);
void fun10(void);
void fun11(void);
void fun12(void);
void fun13(void);
void fun14(void);
void fun15(void);
void fun16(void);
void fun17(void);
void fun18(void);
void fun19(void);
void fun20(void);
void fun21(void);
void fun22(void);
void fun23(void);
void fun24(void);
void fun25(void);

typedef struct
{
    uint8_t current;  //当前状态索引号
    uint8_t next;     //向下按 
    uint8_t enter;    //确定键按
    void (*current_operation)(void);//当前状态应该执行的操作
}Menu_table;

extern uint8_t Data_current[8];
#endif /* __MENU_H */

代码理解:

在.h文件里面定义结构体,里面存放基本需求。在.c文件定义结构体数组,每一个数组元素代表一种选择的情况。

eg.

        {0, 0, 1, (*fun0)},     //一级界面
        {1, 2, 5, (*fun1)},     //二级菜单  
        {2, 3, 6, (*fun2)},     //二级菜单

才开始进去的时候,uint8_t func_index = 0所以当前的索引就为0,而当按键按下的时候,即enter被触发,此时进入到【1】也就是二级菜单。以此类推,此时的索引为1,所以位于1界面,按下key0则将next给func_index,则下一个则指向2对应的界面,紧接着如果wake_up被按下,enter触发,则进入到6所对应的函数。如果没有按下key0【相当于未进行next】,此时按下wake_up则进行入到5页面。

//主页面
void fun0(void)
{
    //OLED_ShowString(57, 1, "", 16);
    //OLED_ShowString(57,4, ":  :", 16);
    //OLED_ShowString(25, 65, "num =", 16);
    //OLED_ShowNum(65, 66, 11, 4, 16);
    //OLED_ShowNum(73, 40, 23, 2, 16);
    //OLED_ShowNum(37, 40, 34, 2, 16);
	  //OLED_ShowNum(0, 40, 45, 2, 16);
	  OLED_DrawBMP(40,2,88,8,BMP2);
}
void fun1(void)
{
    meun1_func();
    OLED_ShowString(0, 0, "-> ", 16);
}

void fun2(void)
{
    meun1_func();
    OLED_ShowString(0, 2, "-> ", 16);
}

void fun3(void)
{
    meun1_func();
    OLED_ShowString(0, 4, "-> ", 16);
}

void fun4(void)
{
    meun1_func();
    OLED_ShowString(0, 6, "-> ", 16);
}
void fun5(void)
{
    meun2_func();
    OLED_ShowString(0, 0, "-> ", 16);
}

void fun6(void)
{
    meun2_func();
    OLED_ShowString(0, 2, "-> ", 16);
}

void fun7(void)
{
    meun2_func();
    OLED_ShowString(0, 4, "-> ", 16);
}

void fun8(void)
{
    meun2_func();
    OLED_ShowString(0, 6, "-> ", 16);
}

 实现的效果如图所示。

OLED多级菜单

猜你喜欢

转载自blog.csdn.net/weixin_63032791/article/details/130030506
今日推荐