• Linux操作USB手柄


    Linux控制原理

      Linux C控制JoyStick的比较简单,首先在JoyStick在Linux 安装好驱动后会在/dev/input生成js0.对其设备控制,就是读取相应的结构来判断用户输入哪一些指令.

    当用户操作手柄时,驱动发送js_event的结构给应用程序以通知用户作了哪一些操作。js_event有如下定义

    struct js_event {
    unsigned int time; /* event timestamp in milliseconds */
    short value; /* value */
    unsigned char type; /* event type */
    unsigned char number; /* axis/button number */
    };

    手柄按钮分布

    当按下1-4号键会发送type = 1 (Button),number  = 0- 3的值

    按下 select 返回 type = 1,number = 8,

    按下 start 返回 type = 1,number  = 9

    当按下左侧四个键.有两种情况,当按ANALOG键时(即红灯亮起)。它返回如下四个值

       js_event 的type 为2.(AXIS).

         上键/下键:number  = 6,两者的区别在于value,上键按下value = -32767,松开等于 = 0.

                            下健按下value = 32767,松开等于 0

    ,

         左/右键:number  = 5两者的区别在于value,左键按下value = -32767,松开等于 = 0.

                            右健按下value = 32767,松开等于 0

    当ANALOG键关闭时(即红灯灭时).

        上键/下键:number  =1,    左/右键:number  =0.

    下面的转动控制杆相对比较复杂。

         左侧旋转杆,向上拨 type = 2,number = 1,value = -32767

                  向下拨  type = 2,number = 1,value = 32767

                   向左  type = 2,number = 2,value = -32767

                   向右  type = 2,number = 2,value = 32767

          如果是偏于某个方向,即于返回最接近的键的值type/number,但是value有介于0到最大值之间。

      当选旋转时,相当于向系统传送一系列的value渐变的jse_event。

          右侧转动控制杆是

                向上type = 2,number = 2 ,value =-32767

                向下拨  type = 2,number = 2,value = 32767

                向左 type = 2,number = 3,value = -32767

                向右  type = 2,number = 3,value = 32767

    完整的控制代码

    这是从网上下载的演示代码,这是调整过后正确误的

    JoyStick.h

    /*
        (C) Copyright 2007,2008, Stephen M. Cameron.
        This file is part of wordwarvi.
        wordwarvi is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.
        wordwarvi is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
        GNU General Public License for more details.
        You should have received a copy of the GNU General Public License
        along with wordwarvi; if not, write to the Free Software
        Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    */
    #include <stdio.h>
    #ifndef __JOYSTICK_H__
    #define __JOYSTICK_H__
    #define JOYSTICK_DEVNAME "/dev/input/js0"
    #define JS_EVENT_BUTTON 0x01 /* button pressed/released */
    #define JS_EVENT_AXIS 0x02 /* joystick moved */
    #define JS_EVENT_INIT 0x80 /* initial state of device */
    struct js_event {
    unsigned int time; /* event timestamp in milliseconds */
    short value; /* value */
    unsigned char type; /* event type */
    unsigned char number; /* axis/button number */
    };
    struct wwvi_js_event {
    int button[11];
    int stick1_x;
    int stick1_y;
    int stick2_x;
    int stick2_y;
    };
    extern int open_joystick(char *joystick_device);
    extern int read_joystick_event(struct js_event *jse);
    extern void set_joystick_y_axis(int axis);
    extern void set_joystick_x_axis(int axis);
    extern void close_joystick();
    extern int get_joystick_status(struct wwvi_js_event *wjse);
    #endif

    JoyStick.c

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include "joystick.h"
    static int joystick_fd = -1;
    int open_joystick(char *joystick_device)
    {
        joystick_fd = open(joystick_device, O_RDONLY | O_NONBLOCK); /* read write for force feedback? */
    if (joystick_fd < 0)
    return joystick_fd;
    /* maybe ioctls to interrogate features here? */
    return joystick_fd;
    }
    int read_joystick_event(struct js_event *jse)
    {
    int bytes;
        bytes = read(joystick_fd, jse, sizeof(*jse));
    if (bytes == -1)
    return 0;
    if (bytes == sizeof(*jse))
    return 1;
    printf("Unexpected bytes from joystick:%d ", bytes);
    return -1;
    }
    void close_joystick()
    {
    close(joystick_fd);
    }
    int get_joystick_status(struct wwvi_js_event *wjse)
    {
    int rc;
    struct js_event jse;
    if (joystick_fd < 0)
    return -1;
    // memset(wjse, 0, sizeof(*wjse));
    while ((rc = read_joystick_event(&jse) == 1)) {
            jse.type &= ~JS_EVENT_INIT; /* ignore synthetic events */
    if (jse.type == JS_EVENT_AXIS) {
    switch (jse.number) {
    case 0: wjse->stick1_x = jse.value;
    break;
    case 1: wjse->stick1_y = jse.value;
    break;
    case 2: wjse->stick2_x = jse.value;
    break;
    case 3: wjse->stick2_y = jse.value;
    break;
    default:
    break;
    }
    } else if (jse.type == JS_EVENT_BUTTON) {
    if (jse.number < 10) {
    switch (jse.value) {
    case 0:
    case 1: wjse->button[jse.number] = jse.value;
    break;
    default:
    break;
    }
    }
    }
    }
    // printf("%d ", wjse->stick1_y);
    return 0;
    }
    #if 1
    /* a little test program */
    int main(int argc, char *argv[])
    {
    int fd, rc;
    int done = 0;
    struct js_event jse;
        fd = open_joystick("/dev/js0");
    if (fd < 0) {
    printf("open failed. ");
    exit(1);
    }
    while (!done) {
            rc = read_joystick_event(&jse);
    usleep(1000);
    if (rc == 1) {
    printf("Event: time %8u, value %8hd, type: %3u, axis/button: %u ",
                    jse.time, jse.value, jse.type, jse.number);
    }
    }
    }
    #endif

    转载链接:http://blog.chinaunix.net/uid-20587912-id-405148.html

  • 相关阅读:
    JqueryQrcode生成二维码不支持中文的解决办法
    [转载]浅析海量用户的分布式系统设计
    [转载]大型网站应用中 MySQL 的架构演变史
    CSS3变形记(上):千变万化的Div
    JavaScript进阶之路——认识和使用Promise,重构你的Js代码
    Visual Studio Code预览版Ver 0.3.0试用体验
    Apache Spark 2.3.0 正式发布
    Apache Spark 2.2.0 新特性详细介绍
    Apache Spark 2.2.0 正式发布
    Spark 论文篇-论文中英语单词集
  • 原文地址:https://www.cnblogs.com/klcf0220/p/11652302.html
Copyright © 2020-2023  润新知