Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9464990
  • 博文数量: 1750
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 20091
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1750)

文章存档

2024年(26)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: 其他平台

2019-04-19 13:37:11




点击(此处)折叠或打开

  1. //my_joystick.c

  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/time.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <linux/joystick.h>
  12. //#include <linux/list.h> /*郁闷,不能直接使用linux自带的list链表,需要单独提取出来,见前面的code*/
  13. #include "listop.h"
  14.   
  15. #if 1
  16. #define LOG_DBG(fmt, ...) fprintf(stdout, fmt, ##__VA_ARGS__)
  17. #else
  18. #define LOG_DBG(fmt, ...)
  19. #endif
  20.   
  21. #define LOG_ERR(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
  22.   
  23.   
  24. typedef struct _joy_stick_ctx {
  25.     struct list_head list;
  26.     int i4_js_fd;
  27.     unsigned int i4_op_block;
  28. } JOYSTICK_CTX_T;
  29.   
  30. LIST_HEAD(_t_js_ctx_head);
  31. /*==> struct list_head _t_js_ctx_head = {&_t_js_ctx_head, &_t_js_ctx_head};*/
  32.   
  33. int joystick_open(char* cp_js_dev_name, int i4_block)
  34. {
  35.     int i4_open_flags = O_RDONLY;
  36.     JOYSTICK_CTX_T* pt_joystick_ctx = NULL;
  37.   
  38.     if (!cp_js_dev_name) {
  39.         LOG_ERR("[%s] js device name is NULL\n", __func__);
  40.         return -1;
  41.     }
  42.   
  43.     pt_joystick_ctx = (JOYSTICK_CTX_T*)calloc(sizeof(JOYSTICK_CTX_T), 1);
  44.     if (!pt_joystick_ctx) {
  45.         LOG_ERR("[%s] no memory!!\n", __func__);
  46.         return -1;
  47.     }
  48.   
  49.     pt_joystick_ctx->i4_op_block = i4_block ? 1 : 0;
  50.   
  51.     if (pt_joystick_ctx->i4_op_block == 0) {
  52.         i4_open_flags |= O_NONBLOCK;
  53.     }
  54.   
  55.     pt_joystick_ctx->i4_js_fd = open(cp_js_dev_name, i4_open_flags);
  56.     if (pt_joystick_ctx->i4_js_fd < 0) {
  57.         LOG_ERR("[%s] open device %s error\n", __func__, cp_js_dev_name);
  58.         free(pt_joystick_ctx);
  59.         return -1;
  60.     }
  61.   
  62.     list_add_tail(&pt_joystick_ctx->list, &_t_js_ctx_head);
  63.   
  64.     return pt_joystick_ctx->i4_js_fd;
  65. }
  66.   
  67. int joystick_close(int i4_fd)
  68. {
  69.     struct list_head* pt_entry;
  70.     struct list_head* pt_next;
  71.     JOYSTICK_CTX_T* pt_node;
  72.   
  73.     if (list_empty(&_t_js_ctx_head)) {
  74.         LOG_ERR("[%s] device not opened\n", __func__);
  75.         return -1;
  76.     }
  77.   
  78.     list_for_each_safe(pt_entry, pt_next, &_t_js_ctx_head) {
  79.         pt_node = list_entry(pt_entry, JOYSTICK_CTX_T, list);
  80.         if (pt_node->i4_js_fd == i4_fd) {
  81.             list_del_init(&pt_node->list);
  82.             free(pt_node);
  83.             return close(i4_fd);
  84.         }
  85.     }
  86.   
  87.     LOG_ERR("[%s] i4_fd=%d invalid\n", __func__, i4_fd);
  88.     return -1;
  89. }
  90.   
  91. int joystick_read_one_event(int i4_fd, struct js_event* tp_jse)
  92. {
  93.     int i4_rd_bytes;
  94.   
  95.     /*do not check i4_fd again*/
  96.   
  97.     i4_rd_bytes = read(i4_fd, tp_jse, sizeof(struct js_event));
  98.   
  99.     if (i4_rd_bytes == -1) {
  100.         if (errno == EAGAIN) { /*when no block, it is not error*/
  101.             return 0;
  102.         }
  103.         else {
  104.             return -1;
  105.         }
  106.     }
  107.   
  108.     return i4_rd_bytes;
  109. }
  110.   
  111. int joystick_read_ready(int i4_fd)
  112. {
  113.     int i4_block = 2;
  114.     struct list_head* pt_entry;
  115.     JOYSTICK_CTX_T* pt_node;
  116.   
  117.     if (list_empty(&_t_js_ctx_head)) {
  118.         LOG_ERR("[%s] device not opened\n", __func__);
  119.         return -1;
  120.     }
  121.   
  122.     list_for_each(pt_entry, &_t_js_ctx_head) {
  123.         pt_node = list_entry(pt_entry, JOYSTICK_CTX_T, list);
  124.         if (pt_node->i4_js_fd == i4_fd) {
  125.             i4_block = pt_node->i4_op_block;
  126.             break;
  127.         }
  128.     }
  129.   
  130.     if (i4_block == 2) {
  131.         LOG_ERR("[%s] i4_fd=%d invalid\n", __func__, i4_fd);
  132.         return 0;
  133.     }
  134.     else if (i4_block == 1) {
  135.         fd_set readfd;
  136.         int i4_ret = 0;
  137.         struct timeval timeout = {0, 0};
  138.         FD_ZERO(&readfd);
  139.         FD_SET(i4_fd, &readfd);
  140.   
  141.         i4_ret = select(i4_fd + 1, &readfd, NULL, NULL, &timeout);
  142.   
  143.         if (i4_ret > 0 && FD_ISSET(i4_fd, &readfd)) {
  144.             return 1;
  145.         }
  146.         else {
  147.             return 0;
  148.         }
  149.   
  150.     }
  151.   
  152.     return 1; /*noblock read, aways ready*/
  153. }
  154.   
  155.   
  156. void debug_list(void)
  157. {
  158.     if (! list_empty(&_t_js_ctx_head)) {
  159.         struct list_head* pt_entry;
  160.         JOYSTICK_CTX_T* pt_node;
  161.   
  162.         list_for_each(pt_entry, &_t_js_ctx_head) {
  163.             pt_node = list_entry(pt_entry, JOYSTICK_CTX_T, list);
  164.             LOG_DBG("fd:%d--block:%d\n", pt_node->i4_js_fd, pt_node->i4_op_block);
  165.         }
  166.     }
  167.     else {
  168.         LOG_DBG("-----------> EMPTY NOW\n");
  169.     }
  170. }
  171.   
  172. #if 1
  173. typedef struct _axes_t {
  174.     int x;
  175.     int y;
  176. } AXES_T;
  177.   
  178. int main(int argc, char* argv[])
  179. {
  180.     int fd, rc;
  181.     int op_times = 0;
  182.     char number_of_axes = 0;
  183.     char number_of_btns = 0;
  184.     char js_name_str[128];
  185.     unsigned int buttons_state = 0;
  186.     AXES_T* tp_axes = NULL;
  187.     int i, print_init_stat = 0;
  188.   
  189.     struct js_event jse;
  190.   
  191.     fd = joystick_open("/dev/input/js0", 1);
  192.     if (fd < 0) {
  193.         LOG_ERR("open failed.\n");
  194.         exit(1);
  195.     }
  196.   
  197.     rc = ioctl(fd, JSIOCGAXES, &number_of_axes);
  198.     if (rc != -1) {
  199.         LOG_DBG("number_of_axes:%d\n", number_of_axes);
  200.         if (number_of_axes > 0) {
  201.             tp_axes = (AXES_T*)calloc(sizeof(AXES_T), 1);
  202.         }
  203.     }
  204.   
  205.     rc = ioctl(fd, JSIOCGBUTTONS, &number_of_btns);
  206.     if (rc != -1) {
  207.         LOG_DBG("number_of_btns:%d\n", number_of_btns);
  208.     }
  209.   
  210.     if (ioctl(fd, JSIOCGNAME(sizeof(js_name_str)), js_name_str) < 0) {
  211.         LOG_DBG(js_name_str, "Unknown", sizeof(js_name_str));
  212.     }
  213.   
  214.     LOG_DBG("joystick Name: %s\n", js_name_str);
  215.   
  216.     for (;;) {
  217.         if (joystick_read_ready(fd)) {
  218.             rc = joystick_read_one_event(fd, &jse);
  219.             if (rc > 0) {
  220.                 if ((jse.type & JS_EVENT_INIT) == JS_EVENT_INIT) {
  221.                     if ((jse.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) {
  222.                         if (jse.value) {
  223.                             buttons_state |= (1 << jse.number);
  224.                         }
  225.                         else {
  226.                             buttons_state &= ~(1 << jse.number);
  227.                         }
  228.                     }
  229.                     else if ((jse.type & ~JS_EVENT_INIT) == JS_EVENT_AXIS) {
  230.                         if (tp_axes) {
  231.                             if ((jse.number & 1) == 0) {
  232.                                 tp_axes[jse.number / 2].x = jse.value;
  233.                             }
  234.                             else {
  235.                                 tp_axes[jse.number / 2].y = jse.value;
  236.                             }
  237.                         }
  238.   
  239.                     }
  240.                 }
  241.                 else {
  242.                     op_times++;
  243.                     if (print_init_stat == 0) {
  244.                         for (i = 0; i < number_of_btns; i++) {
  245.                             LOG_DBG("joystick init state: button %d is %s.\n", i, ((buttons_state & (1 << i)) == (1 << i)) ? "DOWN" : "UP");
  246.                         }
  247.   
  248.                         if (tp_axes)
  249.                             for (i = 0; i < number_of_axes; i++) {
  250.                                 LOG_DBG("joystick init state: axes %d is x=%d y=%d.\n", i, tp_axes[i].x, tp_axes[i].y);
  251.                             }
  252.                         print_init_stat = 1;
  253.                     }
  254.   
  255.                     if (jse.type == JS_EVENT_BUTTON) {
  256.                         if (jse.value) {
  257.                             buttons_state |= (1 << jse.number);
  258.                         }
  259.                         else {
  260.                             buttons_state &= ~(1 << jse.number);
  261.                         }
  262.   
  263.                         LOG_DBG("joystick state: button %d is %s.\n", jse.number, ((buttons_state & (1 << jse.number)) == (1 << jse.number)) ? "DOWN" : "UP");
  264.                     }
  265.                     else if (jse.type == JS_EVENT_AXIS) {
  266.                         if (tp_axes) {
  267.                             if ((jse.number & 1) == 0) {
  268.                                 tp_axes[jse.number / 2].x = jse.value;
  269.                             }
  270.                             else {
  271.                                 tp_axes[jse.number / 2].y = jse.value;
  272.                             }
  273.                             LOG_DBG("joystick state: axes %d is x=%d y=%d.\n", jse.number / 2, tp_axes[jse.number / 2].x, tp_axes[jse.number / 2].y);
  274.                         }
  275.                         else {
  276.                             LOG_DBG("joystick state: axes %d is %s=%d.\n", jse.number / 2, ((jse.number & 1) == 0) ? "x" : "y", jse.value);
  277.                         }
  278.                     }
  279.                 }
  280.   
  281.                 if (op_times >= 18) {
  282.                     break;
  283.                 }
  284.             }
  285.         }
  286.   
  287.         usleep(1000);
  288.     }
  289.   
  290.     joystick_close(fd);
  291.   
  292.     if (tp_axes) {
  293.         free(tp_axes);
  294.     }
  295.   
  296.     return 0;
  297. }
  298. #endif

mayer@mayer-:hid$ ./my_joystick
number_of_axes:2
number_of_btns:10
joystick Name: BETOP GAMEPAD
joystick init state: button 0 is UP.
joystick init state: button 1 is UP.
joystick init state: button 2 is UP.
joystick init state: button 3 is UP.
joystick init state: button 4 is UP.
joystick init state: button 5 is UP.
joystick init state: button 6 is UP.
joystick init state: button 7 is UP.
joystick init state: button 8 is UP.
joystick init state: button 9 is UP.
joystick init state: axes 0 is x=0 y=0.
joystick init state: axes 1 is x=0 y=135129.
joystick state: button 0 is DOWN.
joystick state: button 0 is UP.
joystick state: button 1 is DOWN.
joystick state: button 1 is UP.
joystick state: button 3 is DOWN.
joystick state: button 3 is UP.
joystick state: button 2 is DOWN.
joystick state: button 2 is UP.
joystick state: button 7 is DOWN.
joystick state: button 7 is UP.
joystick state: button 5 is DOWN.
joystick state: button 5 is UP.
joystick state: button 6 is DOWN.
joystick state: button 6 is UP.
joystick state: button 4 is DOWN.
joystick state: button 4 is UP.
joystick state: button 5 is DOWN.
joystick state: button 5 is UP.


不明白的地方可以参考linux kernel中的说明文件:Documentation/input/joystick-api.txt

阅读(15444) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

cjy5132034272019-04-22 14:51:23

ss

cjy5132034272019-04-22 14:51:19

ss