0.96OLED界面菜单的绘制
最近空闲时间较多,整理下之前做过的项目。
2019/12
void KEY(void){
if((HAL_GPIO_ReadPin(KEY_SELECT_GPIO_Port,KEY_SELECT_Pin))||(HAL_GPIO_ReadPin(KEY_RETURN_GPIO_Port,KEY_RETURN_Pin))||(HAL_GPIO_ReadPin(KEY_ENTER_GPIO_Port,KEY_ENTER_Pin)))
{
HAL_Delay(20);//消抖
if(HAL_GPIO_ReadPin(KEY_SELECT_GPIO_Port,KEY_SELECT_Pin))
{
func_index=code_table[func_index].key_select; //向上翻
while(HAL_GPIO_ReadPin(KEY_SELECT_GPIO_Port,KEY_SELECT_Pin));//松手检测
}
if(HAL_GPIO_ReadPin(KEY_RETURN_GPIO_Port,KEY_RETURN_Pin))
{
func_index=code_table[func_index].key_return; //向下翻
while(HAL_GPIO_ReadPin(KEY_RETURN_GPIO_Port,KEY_RETURN_Pin));//松手检
}
if(HAL_GPIO_ReadPin(KEY_ENTER_GPIO_Port,KEY_ENTER_Pin))
{
func_index=code_table[func_index].key_enter; //确认
while(HAL_GPIO_ReadPin(KEY_ENTER_GPIO_Port,KEY_ENTER_Pin));
}
current_operation_index=code_table[func_index].current_operation;
(*current_operation_index)();//执行当前操作函数
}
}
界面核心逻辑主要就是以上函数,通过检测不同 的按键执行对应的操作函数,类似于其他高级开发中的事件函数,回调函数等。
最基本的界面通过PS绘图图像取模后显示,例如:
具体哪个操作对应事件,有结构体来实现。
typedef struct {
uint8_t current;
uint8_t key_select;
uint8_t key_enter;
uint8_t key_return;
void (*current_operation)();
}key_table;
key_table code_table[]={
{0,1,0,15,(*fun1)},//界面1
{1,2,5,0,(*fun2)},//界面二
{2,3,8,0,(*fun3)},
{3,4,11,0,(*fun4)},
{4,1,8,0,(*fun5)},
{5,6,14,1,(*fun6)},//界面2.1
{6,7,16,1,(*fun7)},
{7,5,17,1,(*fun8)},
{8,9,18,2,(*fun9)},//界面2.2
{9,10,19,2,(*fun10)},
{10,8,21,2,(*fun11)},
{11,12,20,3,(*fun12)},//界面2.3
{12,13,22,3,(*fun13)},
{13,11,23,3,(*fun14)},
{14,5,5,5,(*fun15)},
{15,0,0,0,(*fun16)},
{16,6,6,6,(*fun17)},
{17,7,7,7,(*fun18)},
{18,8,8,8,(*fun19)},
{19,9,9,9,(*fun20)},
{20,11,11,11,(*fun21)},
{21,10,10,10,(*fun22)},
{22,12,12,12,(*fun23)},
{23,13,13,13,(*fun24)}
};
效果视频
墨水屏显示终端
:
OLED_MEMU.C
#include "main.h"
#include "gpio.h"
#include "oled_menu.h"
#include "oled.h"
#include "usart_printf.h"
#include "esp8266.h"
#include "DS18B20.h"
#include "DS18B20.h"
#include <stdlib.h>
void (*current_operation_index)();
uint8_t func_index;
uint8_t refresh_flag=0;
_Bool timebase_flag=0;//0为24进制
_Bool point_flag=0;
extern TIME time;
extern const unsigned char BMP1_1[];
extern const unsigned char BMP2_1[];
extern const unsigned char BMP2_2[];
extern const unsigned char BMP2_3[];
extern const unsigned char BMP2_1_1[];
extern const unsigned char BMP2_1_2[];
extern const unsigned char BMP2_1_3[];
extern const unsigned char BMP2_2_1[];
extern const unsigned char BMP2_2_2[];
extern const unsigned char BMP2_2_3[];
extern const unsigned char BMP2_3_1[];
extern const unsigned char BMP2_3_2[];
extern const unsigned char BMP2_3_3[];
extern uint8_t ImageBuffer[15000];
extern float Temperature[12];
extern uint8_t Tem_index;
void fun30(void);
key_table code_table[]={
{0,1,0,15,(*fun1)},//界面1
{1,2,5,0,(*fun2)},//界面二
{2,3,8,0,(*fun3)},
{3,4,11,0,(*fun4)},
{4,1,8,0,(*fun5)},
{5,6,14,1,(*fun6)},//界面2.1
{6,7,16,1,(*fun7)},
{7,5,17,1,(*fun8)},
{8,9,18,2,(*fun9)},//界面2.2
{9,10,19,2,(*fun10)},
{10,8,21,2,(*fun11)},
{11,12,20,3,(*fun12)},//界面2.3
{12,13,22,3,(*fun13)},
{13,11,23,3,(*fun14)},
{14,5,5,5,(*fun15)},
{15,0,0,0,(*fun16)},
{16,6,6,6,(*fun17)},
{17,7,7,7,(*fun18)},
{18,8,8,8,(*fun19)},
{19,9,9,9,(*fun20)},
{20,11,11,11,(*fun21)},
{21,10,10,10,(*fun22)},
{22,12,12,12,(*fun23)},
{23,13,13,13,(*fun24)}
};
void fun1(void){
OLED_Clear();
// OLED_ShowPicture(0,0,128,8,BMP1_1);
OLED_DrawPoint(6,3);//wifi图标
OLED_DrawPoint(7,2);
OLED_DrawLine(8,1,16,1);
OLED_DrawPoint(16,2);
OLED_DrawPoint(17,3);
OLED_DrawPoint(8,5);
OLED_DrawLine(9,4,15,4);
OLED_DrawPoint(15,5);
OLED_DrawLine(10,7,14,7);
OLED_DrawSquare(11,9,12,10);
OLED_DrawPoint(12,10);
OLED_DrawLine(88,1,88,10);//上下箭头
OLED_DrawLine(85,4,92,4);
OLED_DrawLine(86,3,91,3);
OLED_DrawLine(87,2,90,2);
OLED_DrawLine(96,1,96,9);
OLED_DrawLine(93,5,100,5);
OLED_DrawLine(94,6,99,6);
OLED_DrawLine(95,7,98,7);
OLED_DrawSquare(106,1,122,9);
OLED_DrawSquare(104,3,106,7);
OLED_DrawLine(108,3,108,8);
OLED_DrawLine(110,3,110,8);
OLED_DrawLine(111,3,111,8);
OLED_DrawLine(112,3,112,8);
OLED_DrawLine(114,3,114,8);
OLED_DrawLine(115,3,115,8);
OLED_DrawLine(116,3,116,8);
OLED_DrawLine(118,3,118,8);
OLED_DrawLine(119,3,119,8);
OLED_DrawLine(120,3,120,8);
OLED_DrawPoint(122,9);
OLED_ShowChinese(6,47,37,16);
OLED_ShowChinese(23,47,38,16);
OLED_ShowChinese(93,47,39,16);
OLED_ShowChinese(110,47,40,16);
OLED_ShowNum(30,20,time.hour,2,24);
if(point_flag==0){
OLED_ShowChar(55,20,':',24);
point_flag=!point_flag;
}
else{
point_flag=!point_flag;
}
OLED_ShowNum(69,20,time.minute,2,24);
OLED_Refresh();
}
void fun2(void){
OLED_ShowPicture(0,0,128,8,BMP2_1);
}
void fun3(void){
OLED_ShowPicture(0,0,128,8,BMP2_2);
}
void fun4(void){
OLED_ShowPicture(0,0,128,8,BMP2_3);
}
void fun5(void){
OLED_ShowPicture(0,0,128,8,BMP2_2);
}
void fun6(void){
OLED_ShowPicture(0,0,128,8,BMP2_1_1);
}
void fun7(void){
OLED_ShowPicture(0,0,128,8,BMP2_1_2);
}
void fun8(void){
OLED_ShowPicture(0,0,128,8,BMP2_1_3);
}
void fun9(void){
OLED_ShowPicture(0,0,128,8,BMP2_2_1);
}
void fun10(void){
OLED_ShowPicture(0,0,128,8,BMP2_2_2);
}
void fun11(void){
OLED_ShowPicture(0,0,128,8,BMP2_2_3);
}
void fun12(void){
OLED_ShowPicture(0,0,128,8,BMP2_3_1);
}
void fun13(void){
OLED_ShowPicture(0,0,128,8,BMP2_3_2);
}
void fun14(void){
OLED_ShowPicture(0,0,128,8,BMP2_3_3);
}
void fun15(void){
OLED_Clear();//开始无线配网
OLED_ShowChinese(0,0,6,16);
OLED_ShowChinese(16,0,7,16);
OLED_ShowChinese(32,0,8,16);
OLED_ShowChinese(48,0,9,16);
OLED_ShowChinese(64,0,10,16);
OLED_ShowChinese(80,0,11,16);
OLED_ShowChinese(96,0,12,16);
OLED_ShowString(112,0,(uint8_t*)"...",16);
OLED_Refresh();
if(ESP_AirLink()){
OLED_Clear();//无线配网成功
OLED_ShowChinese(0,0,10,16);
OLED_ShowChinese(16,0,11,16);
OLED_ShowChinese(32,0,13,16);
OLED_ShowChinese(48,0,14,16);
OLED_ShowChar(64,0,'!',16);
OLED_Refresh();
}
else{
OLED_Clear();//配网失败
OLED_ShowChinese(0,0,10,16);
OLED_ShowChinese(16,0,11,16);
OLED_ShowChinese(32,0,31,16);
OLED_ShowChinese(48,0,32,16);
OLED_ShowChar(64,0,'!',16);
OLED_Refresh();
}
}
void fun16(void){
if(refresh_flag==0){
refresh_flag=1;
HAL_UART_Transmit(&huart1,(uint8_t*)"开始连接服务器",14,200);
ESP_StartConnect((uint8_t*)"TCP",(uint8_t*)"47.95.13.66",777);
ESP_SendData((uint8_t*)"1",1);
ESP_SetTransMode(1);
HAL_UART_Receive_DMA(&huart2,ImageBuffer,15000);
}
}
void fun17(void){
OLED_Clear();//网络已断开!
OLED_ShowChinese(0,0,15,16);
OLED_ShowChinese(16,0,16,16);
OLED_ShowChinese(32,0,17,16);
OLED_ShowChinese(48,0,18,16);
OLED_ShowChinese(64,0,19,16);
OLED_ShowChar(80,0,'!',16);
OLED_Refresh();
};
void fun18(void){
uint8_t state = ESP_CheckStatus();
switch(state){
case CONNETC_AP_OK | 2:
OLED_Printf((uint8_t*)"CONNECT AP OK!");
break;
case CONNECT_TCP_OK| 3:
OLED_Printf((uint8_t*)"CONNECT TCP OK!");
break;
case DISCONNECT| 4:
OLED_Printf((uint8_t*)"DISCONNECT!");
break;
case DISCONNECT_AP| 5:
OLED_Printf((uint8_t*)"DISCONNECT AP!");
break;
default:
OLED_Printf((uint8_t*)"DISCONNECT!");
break;
}
};
void fun19(void){
OLED_Clear();
OLED_ShowNum(5,20,time.hour,2,24);
OLED_ShowChar(30,20,':',24);
OLED_ShowNum(44,20,time.minute,2,24);
OLED_ShowChar(70,20,':',24);
OLED_ShowNum(84,20,time.sec,2,24);
OLED_Refresh();
};
void fun20(void){
ESP_Time();
OLED_Clear();//更新时间成功
OLED_ShowChinese(0,0,33,16);
OLED_ShowChinese(16,0,34,16);
OLED_ShowChinese(32,0,20,16);
OLED_ShowChinese(48,0,21,16);
OLED_ShowChinese(64,0,13,16);
OLED_ShowChinese(80,0,14,16);
OLED_ShowChar(96,0,'!',16);
OLED_Refresh();
};
void fun21(void){
OLED_Clear();
float tem;
char temp[10];
tem = DS18B20_GetTemp_SkipRom();
sprintf(temp,"%2.1f",tem);
OLED_ShowChinese(3,5,0,16);
OLED_ShowChinese(20,5,1,16);
OLED_ShowChinese(38,5,2,16);
OLED_ShowChinese(56,5,3,16);
OLED_ShowString(35,25,(uint8_t*)temp,16);
OLED_ShowChinese(78,25,5,16);
OLED_Refresh();
};
void fun22(void){
timebase_flag=!timebase_flag;
OLED_Clear();
if(timebase_flag==0){
//时间切换为24进制
OLED_ShowChinese(0,0,22,16);
OLED_ShowChinese(16,0,23,16);
OLED_ShowChinese(32,0,24,16);
OLED_ShowNum(48,0,24,2,16);
OLED_ShowChinese(64,0,35,16);
OLED_ShowChinese(80,0,36,16);
}
else{
//时间切换为12进制
OLED_ShowChinese(0,0,22,16);
OLED_ShowChinese(16,0,23,16);
OLED_ShowChinese(32,0,24,16);
OLED_ShowNum(48,0,12,2,16);
OLED_ShowChinese(64,0,35,16);
OLED_ShowChinese(80,0,36,16);
}
OLED_Refresh();
};
void fun23(void){
float tem;
char temp[6];
char send[10]="2,";
if(func_index == 22){
OLED_Clear();
OLED_ShowChinese(3,5,6,16);
OLED_ShowChinese(20,5,7,16);
OLED_ShowChinese(38,5,27,16);
OLED_ShowChinese(56,5,28,16);
OLED_ShowString(74,5,(uint8_t*)"...",16);
OLED_Refresh();
}
tem = DS18B20_GetTemp_SkipRom();
sprintf(temp,"%2.1f",tem);
strcat(send,temp);
HAL_UART_Transmit(&huart1,(uint8_t*)"开始连接服务器",14,200);
ESP_StartConnect((uint8_t*)"TCP",(uint8_t*)"47.95.13.66",777);
ESP_SendData((uint8_t*)send,6);
ESP_Close();
if(func_index == 22){
OLED_Clear();
OLED_ShowChinese(3,5,27,16);
OLED_ShowChinese(20,5,28,16);
OLED_ShowChinese(38,5,4,16);
OLED_ShowString(35,25,(uint8_t*)temp,16);
OLED_ShowChinese(78,25,5,16);
OLED_Refresh();
}
};
void fun24(void){
OLED_Clear();
float min;
uint8_t y[11];
char temp[10];
for(uint8_t i=0;i<=10;i++){
min=Temperature[i];
if(Temperature[i+1]<min && Temperature[i+1]!=0){
min=Temperature[i+1];
}
}
sprintf(temp,"%2.1f",min);
for(uint8_t i=0;i<=11;i++){
y[i]=(Temperature[i]-min)*10;
}
for(uint8_t i=0;i<=10;i++){
OLED_DrawLine(7+10*i,58-y[i],7+10*(i+1),58-y[i+1]);
}
OLED_DrawLine(6,0,6,64);//Y侧坐标
OLED_DrawLine(5,1,8,1);
OLED_DrawLine(4,2,9,2);
OLED_DrawLine(3,3,5,3);
OLED_DrawLine(8,3,10,3);
for(int i=0;i<10;i++){
OLED_DrawPoint(7,(8)+i*5);
}
OLED_DrawLine(0,58,128,58); //X侧坐标
OLED_DrawLine(126,57,126,60);
OLED_DrawLine(125,56,125,61);
OLED_DrawLine(124,55,124,57);
OLED_DrawLine(124,60,124,62);
for(int i=0;i<11;i++){
OLED_DrawPoint(17+i*10,57);
}
OLED_ShowString(50,3,(uint8_t*)"Min",16);
OLED_ShowString(75,3,(uint8_t*)temp,16);
OLED_ShowChinese(109,3,5,16);
OLED_Refresh();
};
void fun25(void){};
void fun26(void){};
void fun27(void){};
void fun_empty(void){
};
void KEY(void){
if((HAL_GPIO_ReadPin(KEY_SELECT_GPIO_Port,KEY_SELECT_Pin))||(HAL_GPIO_ReadPin(KEY_RETURN_GPIO_Port,KEY_RETURN_Pin))||(HAL_GPIO_ReadPin(KEY_ENTER_GPIO_Port,KEY_ENTER_Pin)))
{
HAL_Delay(20);//消抖
if(HAL_GPIO_ReadPin(KEY_SELECT_GPIO_Port,KEY_SELECT_Pin))
{
func_index=code_table[func_index].key_select; //向上翻
while(HAL_GPIO_ReadPin(KEY_SELECT_GPIO_Port,KEY_SELECT_Pin));//松手检测
}
if(HAL_GPIO_ReadPin(KEY_RETURN_GPIO_Port,KEY_RETURN_Pin))
{
func_index=code_table[func_index].key_return; //向下翻
while(HAL_GPIO_ReadPin(KEY_RETURN_GPIO_Port,KEY_RETURN_Pin));//松手检
}
if(HAL_GPIO_ReadPin(KEY_ENTER_GPIO_Port,KEY_ENTER_Pin))
{
func_index=code_table[func_index].key_enter; //确认
while(HAL_GPIO_ReadPin(KEY_ENTER_GPIO_Port,KEY_ENTER_Pin));
}
current_operation_index=code_table[func_index].current_operation;
(*current_operation_index)();//执行当前操作函数
}
}
OLED_MENU.H
#ifndef __OLED_MENU_H
#define __OLED_MENU_H
#include "main.h"
#include "gpio.h"
typedef struct {
uint8_t current;
uint8_t key_select;
uint8_t key_enter;
uint8_t key_return;
void (*current_operation)();
}key_table;
void KEY(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);
void fun26(void);
void fun27(void);
void fun_empty(void);
#endif