sdk程序:按键扫描(移植原子的程序) - minichao9901/TangNano-20k-Zynq-7020 GitHub Wiki

按键扫描

//GPIO1,小梅哥扩展板
#define PS_KEY1 (54+4)
#define PS_KEY2 (54+5)
#define PS_KEY3 (54+6)
#define PS_KEY4 (54+7)
#define PS_KEY5 (54+8)

int main_ps_emio(void)
{
    PS_GPIO_Init(); //初始化PS端MIO和EMIO

    PS_GPIO_SetMode(PS_KEY1, INPUT, 1);
    PS_GPIO_SetMode(PS_KEY2, INPUT, 1);
    PS_GPIO_SetMode(PS_KEY3, INPUT, 1);
    PS_GPIO_SetMode(PS_KEY4, INPUT, 1);
    PS_GPIO_SetMode(PS_KEY5, INPUT, 1);
}

#define KEY1_PRES 	1  	//KEY1按下后返回值
#define KEY2_PRES	2	//KEY2按下后返回值
#define KEY3_PRES	3	//KEY3按下后返回值
#define KEY4_PRES   4	//KEY4按下后返回值
#define KEY5_PRES   5	//KEY5按下后返回值

#define KEY1 PS_GPIO_GetPort(PS_KEY1)
#define KEY2 PS_GPIO_GetPort(PS_KEY2)
#define KEY3 PS_GPIO_GetPort(PS_KEY3)
#define KEY4 PS_GPIO_GetPort(PS_KEY4)
#define KEY5 PS_GPIO_GetPort(PS_KEY5)

u8 KEY_Scan(u8 mode)
{
	static u8 key_up = 1; // 按键松开标志
	if (mode == 1)
		key_up = 1; // 支持连按
	if (key_up && (KEY1 == 0 || KEY2 == 0 || KEY3 == 0 || KEY4 == 0 || KEY5 == 0))
	{
		usleep(50 * 1000);
		key_up = 0;
		if (KEY1 == 0)
			return KEY1_PRES;
		else if (KEY2 == 0)
			return KEY2_PRES;
		else if (KEY3 == 0)
			return KEY3_PRES;
		else if (KEY4 == 0)
			return KEY4_PRES;
		else if (KEY5 == 0)
			return KEY5_PRES;
	}
	else if (KEY1 == 1 && KEY2 == 1 && KEY3 == 1 && KEY4 == 1 && KEY5 == 1)
		key_up = 1;
	return 0; // 无按键按下
}

lvgl用按键模拟touch

/*------------------
 * Touchpad
 * -----------------*/
/* Will be called by the library to read the touchpad */
static bool key_touch_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static lv_coord_t last_x = 0;
    static lv_coord_t last_y = 0;

    /*Save the pressed coordinates and the state*/
    extern u8 KEY_Scan(u8 mode);
    u8 key = KEY_Scan(0);
    if (key != 0)
    {
        data->state = LV_INDEV_STATE_PR;
        switch (key)
        {
        case 1:
            last_x = 60, last_y = 60; break;
        case 2:
            last_x = 180, last_y = 60; break;
        case 3:
            last_x = 60, last_y = 180; break;
        case 4:
            last_x = 180, last_y = 180; break;
        case 5:
            last_x = 120, last_y = 120; break;
        }
    }
    else
        data->state = LV_INDEV_STATE_REL;

    /*Set the last pressed coordinates*/
    xil_printf("touch state=%d, x=%d, y=%d, key=%d\r\n", data->state, last_x, last_y, key);
    data->point.x = last_x;
    data->point.y = last_y;

    /*Return `false` because we are not buffering and no more data to read*/
    return false;
}