source: BAORadio/libindi/libindi/BAOcontrol/Joystick.cpp @ 689

Last change on this file since 689 was 689, checked in by frichard, 12 years ago
File size: 2.2 KB
Line 
1/**
2 * JOYSTICK.CPP - joystick class
3 *
4 * History
5 *  ver. 0.91 April 2003 - CCDOC
6 *
7 * @author    Eugen Treise
8 * @see:      C++ Coding Standard and CCDOC in help.htm
9 * @version   0.91
10 */
11
12//--------------------------------------------------------------------------
13//                           I N C L U D E
14//--------------------------------------------------------------------------
15
16
17#ifdef WIN32
18
19#include <windows.h>
20#include <mmsystem.h>
21
22#else  // Linux
23
24// Linux version from http://lgdc.sunsite.dk/articles/19.html
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <sys/ioctl.h>
31#include "../communs/const.h"
32
33#endif
34
35#include "Joystick.h"
36
37//--------------------------------------------------------------------------
38//                             Class Joystick
39//--------------------------------------------------------------------------
40
41
42
43// Linux version from http://lgdc.sunsite.dk/articles/19.html
44//#define JOY_DEV "/dev/js0"
45
46
47Joystick::Joystick()
48{
49  axis = 0;
50  button = 0;
51}
52
53Joystick::~Joystick()
54{
55}
56
57bool Joystick::init()
58{
59 
60  joy_fd = open( JOY_DEV , O_RDONLY);
61  if( joy_fd != -1 )
62  {
63    ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
64    ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
65
66    axis = new int[num_of_axis];
67    button = new char[num_of_buttons];
68
69    fcntl( joy_fd, F_SETFL, O_NONBLOCK );       /* use non-blocking mode */
70    return true;
71  }
72  else
73    return false;
74}
75
76bool Joystick::GetStatus(JoystickStatus& js)
77{
78  js.x = js.y = 0;
79  js.button1 = js.button2 = js.button3 = js.button4 = false;
80
81  /* read the joystick state */
82  while( read(joy_fd, &jse, sizeof(struct js_event))>0 )
83  {
84
85    /* see what to do with the event */
86    switch (jse.type & ~JS_EVENT_INIT)
87    {
88      case JS_EVENT_AXIS:
89        axis [ jse.number ] = jse.value;
90        break;
91      case JS_EVENT_BUTTON:
92        button [ jse.number ] = jse.value;
93        break;
94    }
95  }
96 
97  js.x = axis[0];
98  js.y = axis[1];
99  js.button1 = button[0] > 0;
100  js.button2 = button[1] > 0;
101  js.button3 = button[2] > 0;
102  js.button4 = button[3] > 0;
103 
104  return true;
105}
106
107void Joystick::close()
108{
109  if( axis )
110    delete [] axis;
111  if( button )
112    delete [] button;
113
114  ::close( joy_fd );
115}
Note: See TracBrowser for help on using the repository browser.