其他分享
首页 > 其他分享> > 树梅派硬件学习_xbox手柄驱动

树梅派硬件学习_xbox手柄驱动

作者:互联网

joystick手柄驱动

使用xbox手柄

手柄测试:

ls /dev/input #找到一个js0表示支持joystick

cat /dev/input/js0 | hexdump #实时查看手柄的输出, 十六进制显示
00000a0 8bb4 0051 0000 0702 8ee8 0051 8001 0702
00000b0 8f56 0051 0000 0702 9000 0051 8001 0702
00000c0 9046 0051 0000 0702 9456 0051 7fff 0702


sudo apt-get install joystick #下载手柄驱动

 jstest /dev/input/js0 #测试手柄,相当于上一种16进制的解释
Axes:  0:     0  1:     0  2:-32767  3:     0  4:     0  5:-32767  6:     0  7:     0 Buttons:  0:off  1:off  2:off  3:off  4:off  5:off  6:off  7:off  8:off  9:off 10:off 

代码

#include <stdio.h>  
#include <unistd.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <errno.h>  
#include <linux/input.h>  
#include <linux/joystick.h>  
#include <iostream>

using namespace std;

#define XBOX_TYPE_BUTTON    0x01  
#define XBOX_TYPE_AXIS      0x02  

#define XBOX_BUTTON_A       0x00  
#define XBOX_BUTTON_B       0x01  
#define XBOX_BUTTON_X       0x02  
#define XBOX_BUTTON_Y       0x03  
#define XBOX_BUTTON_LB      0x04  
#define XBOX_BUTTON_RB      0x05  
#define XBOX_BUTTON_START   0x06  
#define XBOX_BUTTON_BACK    0x07  
#define XBOX_BUTTON_HOME    0x08  
#define XBOX_BUTTON_LO      0x09    /* 左摇杆按键 */  
#define XBOX_BUTTON_RO      0x0a    /* 右摇杆按键 */  

#define XBOX_BUTTON_ON      0x01  
#define XBOX_BUTTON_OFF     0x00  

#define XBOX_AXIS_LX        0x00    /* 左摇杆X轴 */  
#define XBOX_AXIS_LY        0x01    /* 左摇杆Y轴 */  
#define XBOX_AXIS_RX        0x03    /* 右摇杆X轴 */  
#define XBOX_AXIS_RY        0x04    /* 右摇杆Y轴 */  
#define XBOX_AXIS_LT        0x02  
#define XBOX_AXIS_RT        0x05  
#define XBOX_AXIS_XX        0x06    /* 方向键X轴 */  
#define XBOX_AXIS_YY        0x07    /* 方向键Y轴 */  

#define XBOX_AXIS_VAL_UP        -32767  
#define XBOX_AXIS_VAL_DOWN      32767  
#define XBOX_AXIS_VAL_LEFT      -32767  
#define XBOX_AXIS_VAL_RIGHT     32767  

#define XBOX_AXIS_VAL_MIN       -32767  
#define XBOX_AXIS_VAL_MAX       32767  
#define XBOX_AXIS_VAL_MID       0x00  

class XBox{
	
private:
	int xbox_fd=0;
	const char* file_name = "/dev/input/js0";

public:
	 struct xbox_map 
	{  
		int     time;  
		int     a;  
		int     b;  
		int     x;  
		int     y;  
		int     lb;  
		int     rb;  
		int     start;  
		int     back;  
		int     home;  
		int     lo;  
		int     ro;  

		int     lx;  
		int     ly;  
		int     rx;  
		int     ry;  
		int     lt;  
		int     rt;  
		int     xx;  
		int     yy;  
	}map;  


public:
	XBox(){
		 this->map = {0};
	}
	~XBox(){}

	int xbox_open()  
	{  
		this->xbox_fd = open(this->file_name, O_RDONLY);  
		if (this->xbox_fd < 0)  
		{  
			perror("open");  
			return -1;  
		}  

		return 1;  
	}  

	int xbox_map_read()  
	{  
		int len, type, number, value;  
		struct js_event js;  

		len = read(this->xbox_fd, &js, sizeof(struct js_event));  
		if (len < 0)  
		{  
			perror("read");  
			return -1;  
		}  

		type = js.type;  
		number = js.number;  
		value = js.value;  

		this->map.time = js.time;  

		if (type == JS_EVENT_BUTTON)  
		{  
			switch (number)  
			{  
				case XBOX_BUTTON_A:  
					this->map.a = value;  
					break;  

				case XBOX_BUTTON_B:  
					this->map.b = value;  
					break;  

				case XBOX_BUTTON_X:  
					this->map.x = value;  
					break;  

				case XBOX_BUTTON_Y:  
					this->map.y = value;  
					break;  

				case XBOX_BUTTON_LB:  
					this->map.lb = value;  
					break;  

				case XBOX_BUTTON_RB:  
					this->map.rb = value;  
					break;  

				case XBOX_BUTTON_START:  
					this->map.start = value;  
					break;  

				case XBOX_BUTTON_BACK:  
					this->map.back = value;  
					break;  

				case XBOX_BUTTON_HOME:  
					this->map.home = value;  
					break;  

				case XBOX_BUTTON_LO:  
					this->map.lo = value;  
					break;  

				case XBOX_BUTTON_RO:  
					this->map.ro = value;  
					break;  

				default:  
					break;  
			}  
		}  
		else if (type == JS_EVENT_AXIS)  
		{  
			switch(number)  
			{  
				case XBOX_AXIS_LX:  
					this->map.lx = value;  
					break;  

				case XBOX_AXIS_LY:  
					this->map.ly = value;  
					break;  

				case XBOX_AXIS_RX:  
					this->map.rx = value;  
					break;  

				case XBOX_AXIS_RY:  
					this->map.ry = value;  
					break;  

				case XBOX_AXIS_LT:  
					this->map.lt = value;  
					break;  

				case XBOX_AXIS_RT:  
					this->map.rt = value;  
					break;  

				case XBOX_AXIS_XX:  
					this->map.xx = value;  
					break;  

				case XBOX_AXIS_YY:  
					this->map.yy = value;  
					break;  

				default:  
					break;  
			}  
		}  
		else  
		{  
			/* Init do nothing */  
		}  

		return len;  
	}  

	void xbox_close()  
	{  
		close(this->xbox_fd);  
		return;  
	}  
	
	

};


int main(void)  
{  
    XBox xbox;  
    int len;


    if( xbox.xbox_open() < 0)  
    {  
		printf("打开文件失败!");
        return -1;  
    }  

    while(1)  
    {  
        len = xbox.xbox_map_read();  
        if (len < 0)  
        {  
            usleep(10*1000);  
            continue;  
        }  
		if(xbox.map.a==1)printf("a\t");
		if(xbox.map.b==1)printf("b\t");
		if(xbox.map.xx<0)printf("left\t");
		if(xbox.map.xx>0)printf("right\t");

        printf("\n");
    }  

    xbox.xbox_close();  
    return 0;  
}  


输出:

#全部为:
Time: 6647560 A:0 B:0 X:0 Y:0 LB:0 RB:0 start:0 back:0 home:0 LO:0 RO:0 XX:0      YY:0      LX:0      LY:0      RX:0      RY:0

输出:
right	
b	right	
right	
a	right	
a	
a	left	
left	


标签:map,手柄,int,BUTTON,XBOX,梅派,AXIS,xbox,define
来源: https://blog.csdn.net/weixin_44342705/article/details/119274395