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\n", 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\n", 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.\n");
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\n",
                jse.time, jse.value, jse.type, jse.number);
}
}
}
#endif

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

Linux操作USB手柄的更多相关文章

  1. Kali Linux Live USB初始化+使用日记

    1.Live USB制作官方guide:Making a Kali Bootable USB Drive:https://docs.kali.org/downloading/kali-linux-li ...

  2. Linux下 USB设备驱动分析(原创)

    之前做过STM32的usb HID复合设备,闲来看看linux下USB设备驱动是怎么一回事, 参考资料基于韦东山JZ2440开发板,以下,有错误欢迎指出. 1.准备知识 1.1USB相关概念: USB ...

  3. Linux中USB协议栈的框架简介

    文本旨在简单介绍一下Linux中USB协议栈的代码框架: 下图是USB协议栈相关数据结构的关系图: 下面结合上图看一下系统初始化的流程: 1.USB子系统初始化:\drivers\usb\core\u ...

  4. 基于Linux的USB 主/从设备之间通讯的三种方式

    转载:http://archive.eet-china.com/www.eet-china.com/ART_8800323770_617693_TA_eda530e7.HTM 随着简单易用的USB接口 ...

  5. linux控制USB的绑定/解绑

    linux控制USB的绑定/解绑 http://www.jianshu.com/p/57293f9be558 今天工作中遇到一个问题, 要用代码实现USB的enable和disable. 谷歌了一番, ...

  6. Linux操作系统内核编译之NTFS文件系统模块支持案例

    Linux操作系统内核编译之NTFS文件系统模块支持案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.内核编译概述 单内核体系设计.但充分借鉴了微内核设计体系的优点,为内核引 ...

  7. Linux操作基础

    摘要 一.Linux操作系统概述 二.Linux操作系统安装 三.Linux文件系统及文件基础 四.Linux操作系统命令使用基础 五.Linux应用程序的安装与卸载基础 五.用户及进程 六.相关信息 ...

  8. Linux云计算-01_介绍以及Linux操作系统安装

    1 学习目的 兴趣爱好 技能提升 找到满意的工作 2 什么是云计算 云计算(cloud computing)是分布式计算的一种,指的是通过网络"云"将巨大的数据计算处理程序分解成无 ...

  9. Linux操作系统主机名(hostname)简介

    http://www.jb51.net/LINUXjishu/10938.html 摘要:本文是关于Linux操作系统主机名(hostname)的文档,对主要配置文件/etc/hosts进行简要的说明 ...

随机推荐

  1. NIO你真正了解多少?

    解释一下java.io.Serializable接口 类通过实现 Java.io.Serializable 接口以启用其序列化功能.未实现此接口的类将无法使其任何状态序列化或反序列化. IO操作最佳实 ...

  2. delphi FillChar的用法(转)

    delphi FillChar的用法(转) (2012-12-24 15:12:06) 转载▼ 标签: it 分类: delphi7 FillChar的用法(delphi) Fillchar是Turb ...

  3. boa移植 boa交叉编译

    官网:http://www.boa.org/ BOA 服务器是一个小巧高效的web服务器,是一个运行于unix或linux下的,支持CGI的.适合于嵌入式系统的单任务的http服务器,源代码开放.性能 ...

  4. install和cp

    在Makefile里会用到install,其他地方会用cp命令 主要区别: 如果目标文件存在,cp会先清空文件后往里写入新文件,而install则会先删除掉原先的文件然后写入新文件.这是因为往正在 使 ...

  5. Scrum 冲刺第四篇

    我们是这次稳了队,队员分别是温治乾.莫少政.黄思扬.余泽端.江海灵 一.会议 1.1  28号站立式会议照片: 1.2  昨天已完成的事情 团队成员 昨日已完成的任务 黄思扬 活动内容管理页(前端) ...

  6. SqlServer数据库优化之添加主键和自增长

    今天需要给有500万条数据的表添加主键和自增长列,其中最大的难度在于如何UPDATE这500万多条数据,开始吧! 1.先给表添加一个字段叫ID,并允许空 2.查询表,我想到了使用其中的时间列排序来创建 ...

  7. linux中利用speedtest-cli测网速

    [root@elegant-snap-3 you-get]# wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/spe ...

  8. 如何用python编写一个计时器的程序

    python计时器的程序的代码和注释 import time as t #引入time模块 class MyTimer():     def __init__(self): #构造函数         ...

  9. 如何使用jmockit进行单元测试

    1. Jmockit简介 JMockit 是用以帮助开发人员编写测试程序的一组工具和API,它完全基于 Java 5 SE 的 java.lang.instrument 包开发,内部使用 ASM 库来 ...

  10. 快速安装Rainbond——开源企业级Paas平台

    快速安装Rainbond--开源企业级Paas平台 参考:https://www.rainbond.com/docs/user-operations/install/online_install/ R ...