《Arduino》开发 之 基于 u8g2 库 的 OLED 菜单界面

简介:

程序思想移植于:OLED多级菜单实现方法

硬件:

ESP8266:

ESP8266 NODEMCU  
gpio5 D1 SCL
gpio4 D2 SDA

程序示例:

/*
@作者:刘泽文
@板卡:ESP8266
@功能:基于U8g2的简单菜单演示
@时间:2020/5/3
*/
#include 
#include 
#include 

#define UP_KEY      D6 //gpio12
#define M_KEY       D5 //gpio14
#define DOWN_KEY    D7 //gpio13

//读取键值
#define keyup     digitalRead(UP_KEY)
#define keyenter  digitalRead(M_KEY)
#define keydown   digitalRead(DOWN_KEY)

uint8_t func_index=0;
void (*current_operation_index)();
void fun1();
void fun2();
void fun3();
void fun4();
void page_1_to_4();

typedef struct
{
	uint8_t current;
	uint8_t up;//向上翻索引号
	uint8_t down;//向下翻索引号
	uint8_t enter;//确认索引号
	void (*current_operation)();
}key_table;

key_table table[7]=
{
	{0,3,1,0,(*fun1)},
	{1,0,2,1,(*fun2)},
	{2,1,3,2,(*fun3)},
	{3,2,0,3,(*fun4)},
};

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup(void)
{
  u8g2.begin();
  u8g2.enableUTF8Print();
  ESP.wdtEnable(5000);//看门狗初始化
}
 
void loop(void)
{
  /*******************find index****************************/
	if((keyup==0)||(keydown==0)||(keyenter==0))
	{
		delay(10);//消抖
		if(keyup==0)
		{
			func_index=table[func_index].up;    //向上翻
			while(!keyup)ESP.wdtFeed();// 喂 狗    
		}
		if(keydown==0)
  	{
    	func_index=table[func_index].down;    //向下翻
    	while(!keydown)ESP.wdtFeed();// 喂 狗    
		}
    if(keyenter==0)                     
		{                
		  func_index=table[func_index].enter;    //确认
		  while(!keyenter)ESP.wdtFeed();// 喂 狗
		}
	}
	current_operation_index=table[func_index].current_operation;
	(*current_operation_index)();//执行当前操作函数
  ESP.wdtFeed();// 喂 狗     
}

void fun1(){
  u8g2.clearBuffer();
  page_1_to_4();
  //显示光标
  u8g2.setFont(u8g2_font_open_iconic_all_1x_t);
  u8g2.drawGlyph(5, 16*(1%4==0? 4:1%4)-4,118);
  u8g2.sendBuffer();
}
void fun2(){
  u8g2.clearBuffer();
  page_1_to_4();
  //显示光标
  u8g2.setFont(u8g2_font_open_iconic_all_1x_t);
  u8g2.drawGlyph(5, 16*(2%4==0? 4:2%4)-4,118);
  u8g2.sendBuffer();
}
void fun3(){
  u8g2.clearBuffer();
  page_1_to_4();
  //显示光标
  u8g2.setFont(u8g2_font_open_iconic_all_1x_t);
  u8g2.drawGlyph(5, 16*(3%4==0? 4:3%4)-4,118);
  u8g2.sendBuffer();
}
void fun4(){
  u8g2.clearBuffer();
  page_1_to_4();
  //显示光标
  u8g2.setFont(u8g2_font_open_iconic_all_1x_t);
  u8g2.drawGlyph(5, 16*(4%4==0? 4:4%4)-4,118);
  u8g2.sendBuffer();
}
void page_1_to_4(){
  u8g2.setFont(u8g2_font_wqy14_t_gb2312a);
  u8g2.setCursor(20, 16*1-2);
  u8g2.print(" 1.桌面设置");
  u8g2.setCursor(20, 16*2-2);
  u8g2.print(" 2.屏幕设置");
  u8g2.setCursor(20, 16*3-2);
  u8g2.print(" 3.网络信息");
  u8g2.setCursor(20, 16*4-2);
  u8g2.print(" 4.智能配网");
}

 

你可能感兴趣的:(Arduino,开发教程,ESP8266,学习笔记)