ESP8266驱动oled显示多级菜单(spi)

OLED显示

一. 前言

多级菜单显示
在这里插入图片描述

注意环境

  • ESP8266 WIFI模块
  • OLED屏 (0.96英寸 spi)
  • Arduino IDE 编译环境

二. 代码

整个工程代码

oled显示多级菜单

1. 定义一个结构体

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

这里定义了三个按键,大家可以根据自己的需求来定义

2. 定义一个结构体数组

key_table  code_table[10]=
{ 	
	{0,0,1,0,(*fun1)},  //主界面 
	{1,0,2,1,(*fun2)},  //  
	{2,1,3,7,(*fun3)},  //QQ   
	{3,2,4,3,(*fun4)},  //
	{4,3,5,4,(*fun5)},
	{5,4,6,5,(*fun6)},
	{6,5,1,6,(*fun7)},
	{7,6,7,7,(*fun8)},

};

3. 主程序

#include "oledfont.h"
#include "fun.h"

//按键管脚
#define keyup 12
#define keydown 13
#define keyenter 14

#define uchar unsigned char

uchar func_index = 0; 
void (*current_operation_index)();


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

key_table  code_table[10]=
{ 	
	{0,0,1,0,(*fun1)},  //主界面 
	{1,0,2,1,(*fun2)},  //  
	{2,1,3,7,(*fun3)},  //QQ   
	{3,2,4,3,(*fun4)},  //
	{4,3,5,4,(*fun5)},
	{5,4,6,5,(*fun6)},
	{6,5,1,6,(*fun7)},
	{7,6,7,7,(*fun8)},

};

//键值
int up_val, down_val, enter_val;

void setup()
{
	OLED_Init();
	OLED_ColorTurn(0);//0正常显示 1反色显示
	OLED_DisplayTurn(0);//0正常显示 1翻转180度显示
	
	//按键管脚定义
	pinMode(keyup,INPUT_PULLUP); //上拉输入模式
	pinMode(keydown,INPUT_PULLUP);
	pinMode(keyenter,INPUT_PULLUP);
}

void loop()
{
	up_val = digitalRead(keyup);
	down_val = digitalRead(keydown);
	enter_val = digitalRead(keyenter);

	if((up_val==LOW)||(down_val==LOW)||	(enter_val==LOW)) 		
	{ 			
		delay(10);//消抖 			
		if(up_val==LOW) 		
		{ 
      		while(!up_val)//松手检 
      		{
        		up_val = digitalRead(keyup);
        		current_operation_index=code_table[func_index].current_operation;     
        		(*current_operation_index)();//执行当前操作函数
      		}
      		OLED_Clear();
      		func_index=code_table[func_index].up; //向上     
		} 
    			
		if(down_val==LOW) 		 	
		{ 	
			while(!down_val)//松手检 
      		{
        		down_val = digitalRead(keydown);
		        current_operation_index=code_table[func_index].current_operation;     
		        (*current_operation_index)();//执行当前操作函数
     		 }
	      OLED_Clear();
	      func_index=code_table[func_index].down; //向下翻  			
		}
    		 
		if(enter_val==LOW) 			
		{ 
		      while(!enter_val)//松手检 
		      {
		        enter_val = digitalRead(keyenter);
		        current_operation_index=code_table[func_index].current_operation;     
		        (*current_operation_index)();//执行当前操作函数
     		  }
		      OLED_Clear();
		      func_index=code_table[func_index].enter; //确认      			
		} 				
	}
	current_operation_index=code_table[func_index].current_operation; 		
	(*current_operation_index)();//执行当前操作函数
}

4. key_table code table[n] 的理解

1.首先可以先这样定义该数组

key_table code table[n]=
{
	{0,x,y,z,(*fun1)},
	{1,x,y,z,(*fun2)},
	{2,x,y,z,(*fun3)},
	{3,x,y,z,(*fun4)},
	{4,x,y,z,(*fun5)},
	{5,x,y,z,(*fun6)},
	{6,x,y,z,(*fun7)},};

其中,x,y,z是未知数,先不确定,他们对应的是三个键按下要要执行的索引值,
如果四个键,就有四个未知数,在确x,y,z之前,必须要了解自己函数执行什么样的命令。

2.假如开始时,执行数组里面的第一个即table[0],而想在此按上键执行函数fun6
那么table[0]里面需要这样设置 {0,5,y,z,(*fun1)}
同样,如果希望按下键执行fun7则需要设置为 {0,x,6 ,z,(*fun1)}
如果希望按确认键执行fun3则需要设置为 {0,x,y, 2 , (*fun1)}
如果上面三种情况都想要就设置为 即操作不同的按键执行不同的操作。{0,5,6,2,(*fun1)}

  • 27
    点赞
  • 190
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值