1. Mục đích:
- Lập trình ứng dụng giao tiếp với USB joystick trên máy tính và KIT Arm Linux
2. Chuẩn bị:
- PC Linux (Ubuntu) with arm-linux-gcc
- Bộ KIT thực hành FriendlyArm mini/macro2440
- Dữ liệu: Các ví dụ tham khảo
3. Nội dung thực hành:
3.2. Lập trình giao tiếp USB Joystick
3.2.1. Mô tả:
- Thiết bị USB Joystick đã có sẵn driver và tài liệu đặc tả, hướng dẫn lập trình (Xem chi tiết phụ lục đi kèm. Documentation/input/joystick_api.txt)
- Có thể viết chương trình đọc trạng thái Joystick (các nút bấm điều khiển trên Joystick) từ đó ghép với các xử lý thích hợp.
3.2.2. Mã nguồn tham khảo
(File joystick.h)
#include<stdio.h>
#ifndef __JOYSTICK_H__
#define __JOYSTICK_H__
#define JOYSTICK_DEVNAME "/dev/js1"
#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 {
unsignedint time; /* event timestamp in milliseconds */
short value; /* value */
unsignedchar type; /* event type */
unsignedchar number; /* axis/button number */
};
struct wwvi_js_event {
int button[11];
int stick_x;
int stick_y;
};
externint open_joystick();
externint read_joystick_event(struct js_event *jse);
externvoid set_joystick_y_axis(int axis);
externvoid set_joystick_x_axis(int axis);
externvoid close_joystick();
externint get_joystick_status(struct wwvi_js_event *wjse);
#endif
(File 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"
staticint joystick_fd = -1;
int open_joystick()
{
joystick_fd = open(JOYSTICK_DEVNAME, 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->stick_x = jse.value;
break;
case 1: wjse->stick_y = jse.value;
break;
case 2: wjse->stick_x = jse.value;
break;
case 3: wjse->stick_y = jse.value;
break;
default:
break;
}
} elseif (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();
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
Nguồn https://sites.google.com
Bạn có đam mê ngành thiết kế vi mạch và bạn muốn có mức lương 1000 usd cùng lúc bạn
đang muốn tìm một Trung tâm để học vậy hãy đến với ngành vi mạch tại SEMICON
HotLine: 0972 800 931 Ms Duyên