STM32中实现OLED多级菜单(⽀持修改参数)STM32中实现OLED多级菜单
⽬录
⼀、完整⼯程源码下载
⼆、硬件连接
1、OLED12864
OLED_SCK—>PA0 //时钟管脚
OLED_MOSI—>PC13 //数据管脚(主机输出从机输⼊)
OLED_RES—>PB9 //复位管脚(低电平有效)
OLED_DC—>PB8 //数据/命令(低电平有效)选择管脚
OLED_CS—>PB7 //⽚选管脚(低电平有效)
2、按键
KEY_UP(上⼀页)—>PB12
KEY_DOWN(下⼀页)—>PB14
KEY_LEFT(确认) —>PB13
KEY_RIGHT(返回)—>PB15
3、蜂鸣器
BEEP–>PB4
三、效果展⽰
1、图⽚效果
主界⾯
菜单选项界⾯
设置界⾯
电池信息界⾯
2、视频效果
stm32+OLED实现多级菜单
四、核⼼代码
1、gui.h⽂件
#ifndef _GUI_H_
#define _GUI_H_
#include"stm32f10x.h"
#define ON 1
#define OFF 0
typedef struct
{
u8 Cur_Index;//当前索引项
u8 previous;//上⼀页
u8 next;//下⼀页
u8 enter;//确认
u8 back;//返回
void(*current_operation)(u8,u8);// 当前索引执⾏的函数(界⾯) }Main_Menu;
/
/各界⾯的索引值
enum
{
_Main_UI=0,
_Wifi_Option,
_Bluetooth_Option,
_Setting_Option,
_Info_Option,
_Wifi_Child,
_Bluetooth_Child,
_Setting_Child,
_Info_Child,
_OLED_Lock,
};
//按键索引值
enum
{
KEY_PREVIOUS=2,
KEY_ENTER,
KEY_NEXT,
KEY_BACK
};
void Main_UI(u8 page_index,u8 key_val);
void Main_Menu_Func(u8 page_index,u8 key_val);
void Wifi_Child(u8 page_index,u8 key_val);
void Bluetooth_Child(u8 page_index,u8 key_val);
void Setting_Child(u8 page_index,u8 key_val);
void Info_Child(u8 page_index,u8 key_val);
void OLED_Lock(u8 page_index,u8 key_val);
void GUI_Refresh(void);
#endif
2、gui.c⽂件
#include"gui.h"
#include"oled.h"
#include"oledfont.h"
#include"key.h"
#include"led.h"
#include<stdio.h>
#include"rtc.h"
#include<string.h>
#include"beep.h"
#include"adc.h"
#include"oled.h"
#include"oled.h"
#include"esp8266.h"
static u8 func_index=_Main_UI;//当前页⾯索引值
write的返回值static u8 last_index=_Main_UI;//上⼀个界⾯索引值
static void(*current_operation_func)(u8,u8);//定义⼀个函数指针
static u8 BEEP_swi=ON;
//索引表
Main_Menu table[20]=
{
//Cur_Index,previous,next,enter,back,(*current_operation)(u8,u8)
//主界⾯
{_Main_UI,_Main_UI,_Main_UI,_Wifi_Option,_OLED_Lock,Main_UI},
/
/主菜单
{_Wifi_Option,_Info_Option,_Bluetooth_Option,_Wifi_Child,_Main_UI,Main_Menu_Func},//Wifi
{_Bluetooth_Option,_Wifi_Option,_Setting_Option,_Bluetooth_Child,_Main_UI,Main_Menu_Func},//蓝⽛
{_Setting_Option,_Bluetooth_Option,_Info_Option,_Setting_Child,_Main_UI,Main_Menu_Func},//设置
{_Info_Option,_Setting_Option,_Wifi_Option,_Info_Child,_Main_UI,Main_Menu_Func},//关于信息
//⼦菜单
{_Wifi_Child,_Wifi_Child,_Wifi_Child,_Wifi_Child,_Wifi_Option,Wifi_Child},//Wifi⼦菜单
{_Bluetooth_Child,_Bluetooth_Child,_Bluetooth_Child,_Bluetooth_Child,_Bluetooth_Option,Bluetooth_Child},//蓝⽛⼦菜单{_Setting_Child,_Setting_Child,_Setting_Child,_Setting_Child,_Setting_Option,Setting_Child},//设置⼦菜单
{_Info_Child,_Info_Child,_Info_Child,_Info_Child,_Info_Option,Info_Child},//关于信息⼦菜单
{_OLED_Lock,_Main_UI,_Main_UI,_Main_UI,_Main_UI,OLED_Lock},//OLED熄屏
};
/*
函数功能:刷新界⾯
参数:⽆
返回值:⽆
*/
void GUI_Refresh(void)
{
u8 key_val=Get_KEY_Value();
if(key_val!=0)//只有按键按下才刷屏
{
last_index=func_index;//更新上⼀界⾯索引值
switch(key_val)
{
case KEY_PREVIOUS: func_index=table[func_index].previous;//更新索引值
break;
case KEY_ENTER: func_index=table[func_index].enter;//更新索引值
break;
case KEY_NEXT:func_index=table[func_index].next;//更新索引值
break;
case KEY_BACK:func_index=table[func_index].back;//更新索引值
break;
default:break;
}
if(BEEP_swi==ON)
{
BEEP=1;
Delay_Ms(50);
BEEP=0;
}
OLED_Clear(0);//清屏
}
current_operation_func=table[func_index].current_operation;
(*current_operation_func)(func_index,key_val);//执⾏当前索引对应的函数
}
/*
函数功能:显⽰主界⾯
参数:u8 page_index,u8 key_val
返回值:⽆
*/
void Main_UI(u8 page_index,u8 key_val)
{
char buff[20];
OLED_Display_Image(0,0,11,7,(char*)Signal_11x7,NO_ALGIN);//显⽰信号塔
OLED_Display_Image(14,0,9,9,(char*)Bluetooth_9x9,NO_ALGIN);//显⽰蓝⽛
snprintf(buff,sizeof(buff),"%02d-%02d-%02d",ar%100,h,Current_Time.day); OLED_Display_String(0,1,8,8,buff,NORMAL,CEN_ALIGN);//显⽰⽇期
snprintf(buff,sizeof(buff),"%02d:%02d",Current_Time.hour,Current_Time.min);
OLED_Display_String(0,2,16,32,buff,NORMAL,CEN_ALIGN);//显⽰时间
OLED_Display_Image(112,0,16,8,(char*)Bat_16x8,NO_ALGIN);//显⽰电量
OLED_Display_Chinese(40,6,16,16,(char*)(Week_Font_16x16+7*32),NO_ALGIN);//星
OLED_Display_Chinese(56,6,16,16,(char*)(Week_Font_16x16+8*32),NO_ALGIN);//期
OLED_Display_Chinese(72,6,16,16,(char*)(Week_Font_16x16+(Week_Get()-1)*32),NO_ALGIN);//
⼀~七OLED_Display_Image(0,6,16,16,(char*)Menu_16x16,NO_ALGIN);//显⽰菜单
OLED_Display_Image(112,6,16,16,(char*)Lock_16x16,NO_ALGIN);//显⽰锁屏
}
/*
函数功能:主菜单显⽰函数
参数:u8 page_index,u8 key_val
返回值:⽆
*/
void Main_Menu_Func(u8 page_index,u8 key_val)
{
char buff[20];
OLED_Display_Image(0,0,11,7,(char*)Signal_11x7,NO_ALGIN);//显⽰信号塔
OLED_Display_Image(14,0,9,9,(char*)Bluetooth_9x9,NO_ALGIN);//显⽰蓝⽛
snprintf(buff,sizeof(buff),"%02d:%02d:%02d",Current_Time.hour,Current_Time.min,Current_Time.sec); OLED_Display_String(0,0,8,8,buff,NORMAL,CEN_ALIGN);//显⽰时间
OLED_Display_Image(112,0,16,8,(char*)Bat_16x8,NO_ALGIN);//显⽰电量
OLED_Display_Image(15,4,16,16,(char*)Left_16x16,NO_ALGIN);//显⽰左箭头
OLED_Display_Image(97,4,16,16,(char*)Right_16x16,NO_ALGIN);//显⽰右箭头
OLED_Display_Image(0,6,16,16,(char*)Enter_16x16,NO_ALGIN);//显⽰确定
OLED_Display_Image(112,6,16,16,(char*)Back_16x16,NO_ALGIN);//显⽰返回箭头
switch(page_index)
{
case _Wifi_Option:OLED_Display_Image(0,2,48,48,(char*)Wifi_48x48,CEN_ALIGN);//显⽰Wifi图标break;
case _Bluetooth_Option:OLED_Display_Image(0,2,48,48,(char*)Bluetooth_48x48,CEN_ALIGN);//显⽰蓝⽛图标break;
case _Setting_Option:OLED_Display_Image(0,2,48,48,(char*)Setting_48x48,CEN_ALIGN);//显⽰设置图标break;
case _Info_Option:OLED_Display_Image(0,2,48,48,(char*)Info_48x48,CEN_ALIGN);//显⽰关于信息图标break;
default:break;
}
}
/*
函数功能:Wifi选项⼦菜单
参数:u8 page_index,u8 key_val
返回值:⽆
*/
void Wifi_Child(u8 page_index,u8 key_val)
{
static u8 cur_pos=1;
static u8 wifi_status=ON;
static u8 wifi_mode=SoftAP_Station;
static u8 esp_tcpserver=OFF;
static u8 esp_unvarnishedmode=OFF;
if(last_index!=_Wifi_Option)//判断是否是第⼀次进⼊此界⾯
{
switch(key_val)
{

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。