This article explains the use of 128*64OLED in detail. Recently, I learned the OLED multi-level menu because of the electric competition. Now record the learning process
menu.c core code:
//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 code:
#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 */
Code understanding:
Define the structure in the .h file, which stores the basic requirements. The structure array is defined in the .c file, and each array element represents a selection situation.
eg.
{0, 0, 1, (*fun0)}, //一级界面
{1, 2, 5, (*fun1)}, //二级菜单
{2, 3, 6, (*fun2)}, //二级菜单
At the beginning of entering, uint8_t func_index = 0, so the current index is 0, and when the button is pressed, enter is triggered, and then enter [1], which is the second-level menu. By analogy, the index at this time is 1, so it is located in the 1 interface, press key0, next will be given to func_index, and the next one will point to the interface corresponding to 2, and then if wake_up is pressed and enter is triggered, it will enter 6 the corresponding function. If key0 is not pressed [equivalent to not performing next], press wake_up at this time to enter page 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);
}
The achieved effect is shown in the figure.
OLED multi-level menu