Uinput和树莓派

4
我曾尝试在树莓派论坛上提出这个问题,但是没有得到任何回应。因此,我想向StackOverflow社区请教,这个社区在过去一直很有帮助。
我正在为树莓派编写用户空间驱动程序(可能稍后会移植到其他平台),该驱动程序利用bcm2835库(GPIO)和uinput(Linux用户输入虚拟设备)。我需要读取GPIO引脚并将它们的值转换为虚拟键盘上的模拟按键。GPIO部分已经完成,并且翻译部分也已经完成。不幸的是,虚拟键盘部分还没有解决。Uinput不愿意合作。
现在,完全相同的代码在Debian桌面机上运行得非常完美。测试中所有情况都需要加载evdevuinput模块。在桌面机上,输入可以被触发,但是在树莓派上,我可以验证GPIO子系统已经注册了输入,但是uinput事件没有触发。有人能给我一些提示吗?
非常感谢您的帮助。如果需要任何信息、日志或其他内容,请告诉我,我会尽快发布它们。

这是一个非常有趣的问题,但它太具体了,我认为你在 #Linux IRC 频道上提问会更好。 - jpaugh
@jpaugh,是的,那样会更好。 - PyroAVR
1个回答

0

这是一个完整的解决方案,适用于我。我有一个定制的键盘,这些是我定义的键。这里 是我使用的原始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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接