【问题标题】:Getting live info from /dev/input从 /dev/input 获取实时信息
【发布时间】:2013-04-08 14:50:16
【问题描述】:

我不确定这是否是这个问题的正确位置。我试图从我系统上的操纵杆/dev/input/js0 获取轴位置值。如果我运行jstest /dev/input/js0,它会给我所有按钮和轴位置的实时反馈。

我正在尝试将此信息输入我的 C 程序以控制伺服系统。有这样做的功能吗?我在编程中没有太多使用输入设备,所以这对我来说是全新的。

【问题讨论】:

    标签: device c udev block-device


    【解决方案1】:

    您可以运行此python code 来读取事件。
    您也可以使用高级模块python-evdev

    #!/usr/bin/env python
    
    import struct
    
    infile_path = "/dev/input/js0"
    EVENT_SIZE = struct.calcsize("llHHI")
    file = open(infile_path, "rb")
    event = file.read(EVENT_SIZE)
    while event:
        print(struct.unpack("llHHI", event))
        (tv_sec, tv_usec, type, code, value) = struct.unpack("llHHI", event)
        event = file.read(EVENT_SIZE)
    

    示例输出:

    (73324490, 8454144, 55242, 1118, 25231360)
    (73324490, 42008576, 55242, 1118, 58785792)
    (73324490, 75563008, 55242, 1118, 92340224)
    (73324490, 109117440, 55242, 1118, 125894656)
    (73324490, 142671872, 55242, 1118, 159449088)
    (73324490, 176226304, 55242, 1118, 193003520)
    (73324490, 209780736, 55242, 1118, 226557952)
    (73324490, 243335168, 55242, 1118, 8519680)
    (73324490, 25296896, 55242, 1118, 42074112)
    (73324490, 58884097, 55242, 1118, 75661313)
    (73324490, 92405760, 55242, 1118, 109215745)
    (73324490, 125992961, 55242, 1118, 142737408)
    (73324490, 159514624, 55242, 1118, 176324609)
    (73327790, 84041474, 58542, 1118, 84049919)
    (73328030, 84044852, 58782, 1118, 84017152)
    (73331790, 33749013, 62542, 1118, 33742256)
    (73331790, 33736851, 62562, 1118, 33731108)
    (73331830, 33723339, 62602, 1118, 33718273)
    (73332090, 33723339, 62862, 1118, 33685504)
    

    【讨论】:

      【解决方案2】:

      以kev的帖子为基础

      来自 linux/joystick.h 文件:

      struct js_event {
          __u32 time;     /* event timestamp in milliseconds */
          __s16 value;    /* value */
          __u8 type;      /* event type */
          __u8 number;    /* axis/button number */
      };
      

      所以python format 应该是:"LhBB"

      infile_path = "/dev/input/js0"
      EVENT_SIZE = struct.calcsize("LhBB")
      file = open(infile_path, "rb")
      event = file.read(EVENT_SIZE)
      while event:
          print(struct.unpack("LhBB", event))
          (tv_msec,  value, type, number) = struct.unpack("LhBB", event)
          event = file.read(EVENT_SIZE)
      

      XBOX One S 控制器的示例输出:

      // type=button, number=button number
      //msec, value, type, number
      (2114530, 1, 1, 0) // A pressed
      (2114670, 0, 1, 0) // A released
      (2116490, 1, 1, 1) // B pressed
      (2116620, 0, 1, 1) // B released
      (2117370, 1, 1, 2) // X pressed
      (2117520, 0, 1, 2) // X released
      (2118220, 1, 1, 3) // Y pressed
      (2118360, 0, 1, 3) // Y released
      

      【讨论】:

        【解决方案3】:

        此页面:http://scaryreasoner.wordpress.com/2008/02/22/programming-joysticks-with-linux/ 有一篇关于如何从 /dev/input/js0 读取信息的精彩文章

        您从文件中读取的事件格式记录在此处:https://www.kernel.org/doc/Documentation/input/input.txt。这是一个包含时间戳、事件类型和标识符以及值的简单结构:

        struct input_event {
            struct timeval time;
            unsigned short type;
            unsigned short code;
            unsigned int value;
        };
        

        【讨论】:

          猜你喜欢
          • 2023-03-20
          • 1970-01-01
          • 2014-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-05
          • 1970-01-01
          相关资源
          最近更新 更多