我尝试使用按键历程,修改为硬件SPI 方式驱动,屏幕正常显示,可是按键无响应,按键引脚已经自定义,请问可能是什么原因呢?我是用的是4.0.2完整版,ST的L496开发板
struct menu_entry_type
{
const uint8_t *font;
uint16_t icon;
const char *name;
};
struct menu_state
{
int16_t menu_start; /* in pixel */
int16_t frame_position; /* in pixel */
uint8_t position; /* position, array index */
};
/*
Icon configuration
Width and height must match the icon font size
GAP: Space between the icons
BGAP: Gap between the display border and the cursor.
*/
#define ICON_WIDTH 32
#define ICON_HEIGHT 32
#define ICON_GAP 4
#define ICON_BGAP 16
#define ICON_Y 32+ICON_GAP
static struct menu_entry_type menu_entry_list[] =
{
{ u8g2_font_open_iconic_play_4x_t, 77, "音乐"},
{ u8g2_font_open_iconic_mime_4x_t, 64, "收音机"},
{ u8g2_font_open_iconic_embedded_4x_t, 70, "心率"},
{ u8g2_font_open_iconic_embedded_4x_t, 74, "蓝牙"},
{ u8g2_font_open_iconic_embedded_4x_t, 66, "设置"},
{ NULL, 0, NULL }
};
static void draw(struct menu_state *state)
{
int16_t x;
uint8_t i;
x = state->menu_start;
i = 0;
while( menu_entry_list*.icon > 0 )
{
if ( x >= -ICON_WIDTH && x < u8g2.getDisplayWidth() )
{
u8g2.setFont(menu_entry_list*.font);
u8g2.drawGlyph(x, ICON_Y, menu_entry_list*.icon );
}
i++;
x += ICON_WIDTH + ICON_GAP;
}
u8g2.drawFrame(state->frame_position-1, ICON_Y-ICON_HEIGHT-1, ICON_WIDTH+2, ICON_WIDTH+2);
u8g2.drawFrame(state->frame_position-2, ICON_Y-ICON_HEIGHT-2, ICON_WIDTH+4, ICON_WIDTH+4);
u8g2.drawFrame(state->frame_position-3, ICON_Y-ICON_HEIGHT-3, ICON_WIDTH+6, ICON_WIDTH+6);
}
static void to_right(struct menu_state *state)
{
if ( menu_entry_list[state->position+1].font != NULL )
{
if ( (int16_t)state->frame_position+ 2*(int16_t)ICON_WIDTH + (int16_t)ICON_BGAP < (int16_t)u8g2.getDisplayWidth() )
{
state->position++;
state->frame_position += ICON_WIDTH + (int16_t)ICON_GAP;
}
else
{
state->position++;
state->frame_position = (int16_t)u8g2.getDisplayWidth() - (int16_t)ICON_WIDTH - (int16_t)ICON_BGAP;
state->menu_start = state->frame_position - state->position*((int16_t)ICON_WIDTH + (int16_t)ICON_GAP);
}
}
}
static void to_left(struct menu_state *state)
{
if ( state->position > 0 )
{
if ( (int16_t)state->frame_position >= (int16_t)ICON_BGAP+(int16_t)ICON_WIDTH+ (int16_t)ICON_GAP )
{
state->position--;
state->frame_position -= ICON_WIDTH + (int16_t)ICON_GAP;
}
else
{
state->position--;
state->frame_position = ICON_BGAP;
state->menu_start = state->frame_position - state->position*((int16_t)ICON_WIDTH + (int16_t)ICON_GAP);
}
}
}
static uint8_t towards_int16(int16_t *current, int16_t dest)
{
if ( *current < dest )
{
(*current)++;
return 1;
}
else if ( *current > dest )
{
(*current)--;
return 1;
}
return 0;
}
static uint8_t towards(struct menu_state *current, struct menu_state *destination)
{
uint8_t r = 0;
r |= towards_int16( &(current->frame_position), destination->frame_position);
r |= towards_int16( &(current->frame_position), destination->frame_position);
r |= towards_int16( &(current->menu_start), destination->menu_start);
r |= towards_int16( &(current->menu_start), destination->menu_start);
return r;
}
static struct menu_state current_state = { ICON_BGAP, ICON_BGAP, 0 };
static struct menu_state destination_state = { ICON_BGAP, ICON_BGAP, 0 };
static void menu_entry(void *parameter)
{
u8g2.begin(/*Select=*/ U8G2_PIN_SELECT, /*Right/Next=*/ U8G2_PIN_RIGHT, /*Left/Prev=*/ U8G2_PIN_LEFT, /*Up=*/ U8G2_PIN_UP, /*Down=*/ U8G2_PIN_DOWN, /*Home/Cancel=*/ U8G2_PIN_HOME);
u8g2.enableUTF8Print();
u8g2.setFont(u8g2_font_6x12_tr);
u8g2.setContrast(128);
while(1)
{
int8_t event;
do
{
u8g2.clearBuffer();
draw(¤t_state);
// u8g2.setFont(u8g2_font_helvB10_tr);
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
u8g2.setCursor((u8g2.getDisplayWidth()-u8g2.getStrWidth(menu_entry_list[destination_state.position].name))/2,u8g2.getDisplayHeight()-5);
// u8g2.setFont(u8g2_font_wqy16_t_gb2312);
u8g2.print(menu_entry_list[destination_state.position].name);
u8g2.sendBuffer();
rt_thread_mdelay(10);
event = u8g2.getMenuEvent();
if ( event == U8X8_MSG_GPIO_MENU_NEXT )
to_right(&destination_state);
if ( event == U8X8_MSG_GPIO_MENU_PREV )
to_left(&destination_state);
if ( event == U8X8_MSG_GPIO_MENU_SELECT )
{
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
// u8g2.setFont(u8g2_font_helvB10_tr);
u8g2.userInterfaceMessage("Selection:", menu_entry_list[destination_state.position].name, "", " Ok ");
}
} while ( towards(¤t_state, &destination_state) );
rt_thread_mdelay(10);
}
}
int rt_hw_menu_port(void)
{
menu = rt_thread_create("menu",
menu_entry, RT_NULL,
2048,
2, 10);
if (menu != RT_NULL)
rt_thread_startup(menu);
return 0;
}
INIT_APP_EXPORT(rt_hw_menu_port);
#define __MENU_H__
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <U8g2lib.h>
#include <stdio.h>
//#define U8G2_PIN_UP 26 // PB10
//#define U8G2_PIN_DOWN 27 // PB11
//#define U8G2_PIN_LEFT 28 // PB12
//#define U8G2_PIN_RIGHT 29 // PB13
//#define U8G2_PIN_SELECT 30 // PB14
//#define U8G2_PIN_HOME 31 // PB15
#define U8G2_PIN_UP GET_PIN(B,12)
#define U8G2_PIN_DOWN GET_PIN(B,13)
#define U8G2_PIN_LEFT GET_PIN(B,10)
#define U8G2_PIN_RIGHT GET_PIN(B,11)
#define U8G2_PIN_SELECT GET_PIN(B,14)
#define U8G2_PIN_HOME GET_PIN(B,15)
#define OLED_SPI_PIN_RES 8 // PA8
#define OLED_SPI_PIN_DC 9 // PA9
#define OLED_SPI_PIN_CS 10 // PA10
#define THREAD_PRIORITY 4
#define THREAD_STACK_SIZE 512
#define THREAD_TIMESLICE 10
static rt_thread_t menu = RT_NULL;
//static rt_thread_t collision_check = RT_NULL;
static U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0,
/* cs=*/ OLED_SPI_PIN_CS,
/* dc=*/ OLED_SPI_PIN_DC,
/* reset=*/ OLED_SPI_PIN_RES);
#endif
上面代码定义了6个引脚,但是下面有很多按键是悬空的,
可以试试把悬空的脚上拉 …
您好,全部加了上拉电阻,还是没响应,不知道是不是我程序哪里出了问题,检查了好几遍,想不通:’(
上面代码定义了6个引脚,但是下面有很多按键是悬空的,
可以试试把悬空的脚上拉 …
大佬,问题解决了,已经可以使用了,非常感谢帮助:lol