- 一. 简介
- 二. 源码
- 三. 源码下载链接
Logitech F310游戏手柄是Logitech公司生产的双模拟摇杆,动式方向按键游戏手柄。
游戏手柄的作用不只是可以用来玩游戏,还可以用于做一些机器人仿真,就比如最近MIT开源的Mini Cheetah四足机器人,其中机器人仿真部分用的控制器就是罗技F310游戏手柄。但是其采用的方法是直接调用QT中的QtGamepad库操作手柄,对于没有安装QT的人没办法直接调用,为此我写了一份罗技F310的通用驱动,驱动也兼容F710,其他型号的游戏手柄还未测试。
代码目前只支持Linux,可以实现读取所有遥感和按键的数值,最终效果如下:
驱动手柄并将各个按键值打印出来:
使用QT将功能做UI显示:
二. 源码
1. 头文件
Logitech_controller.h :
#ifndef Logitech_DRIVER #define Logitech_DRIVER #include#include #include #include #include #include #include #include #include
2. 源文件
Logitech_controller.c :
#include "Logitech_controller.h"
using namespace std;
Logitech::Logitech(char* device)
{
dev = device;
memset(buf, 0, sizeof buf);
}
int Logitech::init()
{
fd = open(dev, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open %s: %s.n", dev, strerror(errno));
return EXIT_FAILURE;
}
Keystate_map[JSKEY_A] =0;
Keystate_map[JSKEY_B] =0;
Keystate_map[JSKEY_X] =0;
Keystate_map[JSKEY_Y] =0;
Keystate_map[JSKEY_LB] =0;
Keystate_map[JSKEY_RB] =0;
Keystate_map[JSKEY_BACK] =0;
Keystate_map[JSKEY_START] =0;
Keystate_map[JSKEY_HOME] =0;
Keystate_map[JSKEY_CROSS_X] =0;
Keystate_map[JSKEY_CROSS_Y] =0;
Keystate_map[JSKEY_LT] =0;
Keystate_map[JSKEY_RT] =0;
Keystate_map[JSKEY_LEFTSTICK_X] =0;
Keystate_map[JSKEY_LEFTSTICK_Y] =0;
Keystate_map[JSKEY_RIGHTSTICK_X] =0;
Keystate_map[JSKEY_RIGHTSTICK_Y] =0;
return 0;
}
void Logitech::listen_input()
{
while (1) {
memset(buf, 0, sizeof buf);
n = read(fd, &buf, sizeof buf);
n = n / sizeof(int);
if (n == (ssize_t)-1) {
if (errno == EINTR)
continue;
else
break;
}
unsigned short btn = buf[1] >> 16;
short val = (short)(buf[1] & 0xffff);
//cout<<"0x"< 3. 测试代码
Logitech_controller_test.cpp :
#include "Logitech_controller.h"
using namespace std;
int main()
{
char path[] = "/dev/input/js0";
Logitech gamepad(path);
gamepad.init();
gamepad.listen_input();
return 0;
}
三. 源码下载链接
以上例程中的代码如下:
- 罗技遥控手柄驱动测试代码下载链接。
- 基于QT的罗技遥控手柄驱动测试下载链接。
开发环境:
- ubuntu18.04
- Qt5.10.0
- Cmake 3.10.2



