日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

利用usb远程控制linux,Linux编程控制硬件(5) ---- 操作USB手柄

發(fā)布時間:2024/9/19 linux 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用usb远程控制linux,Linux编程控制硬件(5) ---- 操作USB手柄 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Andrew Haung 轉(zhuǎn)載請注明作者及聯(lián)絡(luò)方式

學(xué)員項目需要用到JoyStick來遠(yuǎn)程控制云臺。以前在用SDL在游戲中很簡單的就可以控制。但是現(xiàn)在需要在Linux C下直接調(diào)用C來控制JoyStick。因此找了一些資料來看。

象如下的USB手柄基本上插入LINUX的USB口即可運行

a

20.0元

Linux控制原理

Linux C控制JoyStick的比較簡單,首先在JoyStick在Linux 安裝好驅(qū)動后會在/dev/input生成js0.對其設(shè)備控制,就是讀取相應(yīng)的結(jié)構(gòu)來判斷用戶輸入哪一些指令.

當(dāng)用戶操作手柄時,驅(qū)動發(fā)送js_event的結(jié)構(gòu)給應(yīng)用程序以通知用戶作了哪一些操作。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 */

};

手柄按鈕分布

當(dāng)按下1-4號鍵會發(fā)送type = 1 (Button),number? = 0- 3的值

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

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

當(dāng)按下左側(cè)四個鍵.有兩種情況,當(dāng)按ANALOG鍵時(即紅燈亮起)。它返回如下四個值

js_event 的type 為2.(AXIS).

上鍵/下鍵:number? = 6,兩者的區(qū)別在于value,上鍵按下value = -32767,松開等于 = 0.

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

左/右鍵:number? = 5兩者的區(qū)別在于value,左鍵按下value = -32767,松開等于 = 0.

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

當(dāng)ANALOG鍵關(guān)閉時(即紅燈滅時).

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

下面的轉(zhuǎn)動控制桿相對比較復(fù)雜。

左側(cè)旋轉(zhuǎn)桿,向上撥 type = 2,number = 1,value = -32767

向下?lián)? type = 2,number = 1,value = 32767

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

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

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

當(dāng)選旋轉(zhuǎn)時,相當(dāng)于向系統(tǒng)傳送一系列的value漸變的jse_event。

右側(cè)轉(zhuǎn)動控制桿是

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

向下?lián)? type = 2,number = 2,value = 32767

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

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

完整的控制代碼

這是從網(wǎng)上下載的演示代碼,這是調(diào)整過后正確誤的

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

#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

#include

#include

#include

#include

#include

#include

#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

總結(jié)

以上是生活随笔為你收集整理的利用usb远程控制linux,Linux编程控制硬件(5) ---- 操作USB手柄的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。