gpt4 book ai didi

linux - Uinput 和树莓派

转载 作者:行者123 更新时间:2023-12-03 09:48:03 25 4
gpt4 key购买 nike

我试图在 Raspberry Pi 论坛上问这个问题,但我根本没有收到任何回复。我想我可能会询问过去很有帮助的 StackOverflow 社区的想法。

我正在为使用 bcm2835 库 (GPIO) 和 uinput(Linux 用户输入虚拟设备)的 Raspberry Pi 编写用户空间驱动程序(具体来说,以后可能会移植到其他平台)。我需要读取 GPIO 引脚并将它们的值转换为虚拟键盘上的模拟按键。 GPIO部分已经完成,翻译部分也完成了。不幸的是,虚拟键盘部分还没有解决。 Uinput 拒绝合作。

现在,完全相同的代码可以在 Debian 桌面机器上完美运行。 evdevuinput模块是必需的,这两个模块都已在所有测试用例中加载。在桌面上,可以触发输入,但是,在树莓派上,我可以验证 GPIO 子系统已经注册了输入,但是 uinput 事件没有触发。有没有人知道我可能会做什么?

非常感谢,如果您需要任何信息、日志或其他信息,请告诉我,我会尽快发布。

最佳答案

这是一个适合我的完整解决方案。我有一个定制的键盘,这些是我定义的键。 Here是我使用的原始pdf的链接。
当然你可以定义你想要的任何键,只需将它添加到数组中。
注意:此代码仅适用于提升的权限。

int allowed_keys[allowed_KEYS_size][2] = {0};
void main()
{
init_keys();

int fd = open_uinput();

int key_evt = getKeyEVT(49); // ASCII code for 1

// simulate key press and key release
emit(fd, EV_KEY, key_evt, 1);
emit(fd, EV_SYN, SYN_REPORT, 0);
emit(fd, EV_KEY, key_evt, 0);
emit(fd, EV_SYN, SYN_REPORT, 0);
}

long int emit(int fd, int type, int code, int val)
{
struct input_event ie;

ie.type = type;
ie.code = code;
ie.value = val;
/* timestamp values below are ignored */
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;

long int y = write(fd, &ie, sizeof(ie));
return y;
}
int open_uinput()
{
int fdui = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (fdui < 0)
{
printf("uinput fd creation failed!\n");
exit(EXIT_FAILURE);
}

ioctl(fdui, UI_SET_EVBIT, EV_KEY);
ioctl(fdui, UI_SET_EVBIT, EV_SYN); //added by behzad

for (int i = 0; i < allowed_KEYS_size; i++)
ioctl(fdui, UI_SET_KEYBIT, allowed_keys[i][1]);

struct uinput_setup usetup;
memset(&usetup, 0, sizeof(usetup));
usetup.id.bustype = BUS_USB;
usetup.id.vendor = 0x1234; /* sample vendor */
usetup.id.product = 0x5678; /* sample product */
strcpy(usetup.name, "My Keypad. Ver 1.1");

ioctl(fdui, UI_DEV_SETUP, &usetup);
ioctl(fdui, UI_DEV_CREATE);

sleep(2);
return fdui;
}

int getKeyEVT(int k)
{
for (int i = 0; i < allowed_KEYS_size; i++)
{
if (allowed_keys[i][0] == k)
return allowed_keys[i][1];
}
return -1;
}
void init_keys()
{
// Reference:
// https://www.alt-codes.net/arrow_alt_codes.php
// /usr/include/linux/input-event-codes.h

allowed_keys[0][0] = 48; //ASCII ---> 0
allowed_keys[0][1] = KEY_0; //LINUX

allowed_keys[1][0] = 49; //ASCII
allowed_keys[1][1] = KEY_1; //LINUX

allowed_keys[2][0] = 50; //ASCII
allowed_keys[2][1] = KEY_2; //LINUX

allowed_keys[3][0] = 51; //ASCII
allowed_keys[3][1] = KEY_3; //LINUX
}

关于linux - Uinput 和树莓派,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29336740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com