目录[-]


1、查看设备

在项目中需要使用游戏手柄对设备进行一些操作,在此选择了微软xbox手柄,通过在linux下调用设备描述文件来获取游戏手柄的输入数据,使用步骤以及代码描述如下。

• Microsoft X-Box One S pad 游戏手柄在ubuntu下的驱动程序;

sudo apt-get install joystick

• 在linux系统下,向系统中插入硬件时,操作系统回味每一个设备分配一个event号,在开发游戏手柄驱动时,需要首先找到游戏手柄对应的event以及线程,通过以下指令可以获取;

cat /proc/bus/input/devices 

• linux下,查看系统中插入设备对应的所有event以及线程:

ls /dev/input 

• 查看端口输出:

cat /dev/input/js0 | hexdump

2、代码描述

#include "fcntl.h"//Unix 下通用头文件,包含文件的open,close函数
#include "stdio.h"//perror
#include "stdlib.h"
#include "linux/joystick.h"
#include "string.h"//memset
#include <unistd.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <errno.h>  
#include <linux/input.h>  
#include <linux/joystick.h>

#include "../include/microbox.h"

//microbox_acti_type-> 1:button;2:axis

/*==button==*/
#define microbox_type_button 1
//microbox_acti_button_number-> 0:A;	1:B;	2:X;	3:Y
#define microbox_number_button_A 0
#define microbox_number_button_B 1
#define microbox_number_button_X 2
#define microbox_number_button_Y 3
#define microbox_number_button_LB 4
#define microbox_number_button_RB 5
#define microbox_number_button_COPY 6
#define microbox_number_button_FILE 7
#define microbox_number_button_EARTH 8
#define microbox_number_button_LEFTJOY_PRESSED 9
#define microbox_number_button_RIGHTJOY_PRESSED 10
//microbox_acti_button_value-> 0:没有被按下;	1:按钮被按下
#define microbox_value_up 0
#define microbox_value_down 1

/*==axis==*/
#define microbox_type_axis 2
//microbox_acti_axis_number-> 0:左侧摇杆X轴;1:左侧摇杆Y轴;2:左侧摇杆T轴
//3:右侧摇杆X轴;4:右侧摇杆Y轴;5:右侧摇杆T轴;6:左侧+按钮X轴;7:左侧+按钮Y轴
#define microbox_number_axis_lEFTJOY_X 0
#define microbox_number_axis_lEFTJOY_Y 1
#define microbox_number_axis_lEFTJOY_T 2
#define microbox_number_axis_RIGHTJOY_X 3
#define microbox_number_axis_RIGHTJOY_Y 4
#define microbox_number_axis_RIGHTJOY_T 5
#define microbox_number_axis_PLUSJOY_X 6
#define microbox_number_axis_PLUSJOY_Y 7

typedef struct microbox_map
{
	int time;
	int btn_A;
	int btn_B;
	int btn_X;
	int btn_Y;
	int btn_LB;
	int btn_RB;
	int btn_COPY;
	int btn_FILE;
	int btn_EARTH;
	int btn_LFT_JOY_PRESSED;
	int btn_RIT_JOY_PRESSED;

	int axis_LFT_JOY_X;
	int axis_LFT_JOY_Y;
	int axis_LFT_JOY_T;
	int axis_RIT_JOY_X;
	int axis_RIT_JOY_Y;
	int axis_RIT_JOY_T;
	int axis_PLUS_JOY_X;
	int axis_PLUS_JOY_Y;

}microbox_map_t;

//打开手柄对应的文件
int microbox_open(const char *file_name){
	int microbox_fd;
	microbox_fd	=	open(file_name,O_RDONLY);
	if (microbox_fd <0) {
		perror("open");
		return -1;
		usleep(1000*1000);
	}else{
		printf("microbox has been opened:fd=%d\n",microbox_fd);
		return microbox_fd;
	}
}

//读取手柄对应的文件
int microbox_read(int microbox_fd,microbox_map_t *map){
	int len,time,type,number,value;
	struct js_event js;
	len	=	read(microbox_fd,&js,sizeof(struct js_event));
	if (len<0){
		perror("read");
		return -1;
	}
	type 	= js.type;
	number	=	js.number;
	value	=	js.value;
	time	=	js.time;
	map->time =	time;
	if (type == microbox_type_button){
		switch(number){
			case microbox_number_button_A:
				map->btn_A =	value;
				break;
			case microbox_number_button_B:
				map->btn_B = value;
				break;
			case microbox_number_button_X:
				map->btn_X =	value;
				break;
			case microbox_number_button_Y:
				map->btn_Y = 	value;
				break;
			case microbox_number_button_LB:
				map->btn_LB =	value;
				break;
			case microbox_number_button_RB:
				map->btn_RB =	value;
				break;
			case microbox_number_button_EARTH:
				map->btn_EARTH =	value;
				break;
			case microbox_number_button_COPY:
				map->btn_COPY =		value;
				break;
			case microbox_number_button_FILE:
				map->btn_FILE =	value;
				break;
			case microbox_number_button_LEFTJOY_PRESSED:
				map->btn_LFT_JOY_PRESSED = value;
				break;
			case microbox_number_button_RIGHTJOY_PRESSED:
				map->btn_RIT_JOY_PRESSED = value;
				break;
			default:
				break;
		}
	}else if (type == microbox_type_axis){
		switch(number){
			case microbox_number_axis_lEFTJOY_X:
				map->axis_LFT_JOY_X = value;
				break;
			case microbox_number_axis_lEFTJOY_Y:
				map->axis_LFT_JOY_Y = value;
				break;
			case microbox_number_axis_lEFTJOY_T:
				map->axis_LFT_JOY_T = value;
				break;
			case microbox_number_axis_RIGHTJOY_X:
				map->axis_RIT_JOY_X = value;
				break;
			case microbox_number_axis_RIGHTJOY_Y:
				map->axis_RIT_JOY_Y = value;
				break;
			case microbox_number_axis_RIGHTJOY_T:
				map->axis_RIT_JOY_T = value;
				break;
			case microbox_number_axis_PLUSJOY_X:
				map->axis_PLUS_JOY_X = value;
				break;
			case microbox_number_axis_PLUSJOY_Y:
				map->axis_PLUS_JOY_Y = value;
				break;
			default:
				break;
		}
	}else{
		perror("errno");
	}
	 printf("\rTime:%8d A:%d B:%d X:%d Y:%d LB:%d RB:%d COPY:%d FILE:%d EARTH:%d LP:%d RP:%d XX:%-6d XY:%-6d XZ:%-6d RX:%-6d RY:%-6d RZ:%-6d PX:%-6d PY:%-6d",  
                map->time, map->btn_A, map->btn_B, map->btn_X, map->btn_Y, map->btn_LB, map->btn_RB, map->btn_EARTH, map->btn_COPY, map->btn_FILE, map->btn_LFT_JOY_PRESSED, map->btn_RIT_JOY_PRESSED,  
                map->axis_LFT_JOY_X, map->axis_LFT_JOY_Y, map->axis_LFT_JOY_T, map->axis_RIT_JOY_X, map->axis_RIT_JOY_Y, map->axis_RIT_JOY_T, map->axis_PLUS_JOY_X, map->axis_PLUS_JOY_Y); 
}

//关闭手柄对应的文件
 void microbox_close(int microbox_fd){
 	close(microbox_fd);
 	return;
 }


int main(void){
	int len,microbox_fd;
	microbox_map_t microbox_value_map;
	memset(&microbox_value_map,0,sizeof(microbox_map_t));
	microbox_fd	=	microbox_open("/dev/input/js0");
	while(1){
		len = microbox_read(microbox_fd,&microbox_value_map);
		if (len <0){
			memset(&microbox_value_map,0,sizeof(microbox_map_t));
			microbox_fd = microbox_open("/dev/input/js0");
			usleep(1000*1000);
			fflush(stdout);	
			continue;
		}
		fflush(stdout);
		if (microbox_value_map.btn_EARTH == 1){
			microbox_close(microbox_fd);
		}	
	}	
}