Instrument Neutral Distributed Interface INDI  2.0.2
joystickdriver.h
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2013 Jasem Mutlaq. All rights reserved.
3 
4  Based on code by Keith Lantz.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the Free
8  Software Foundation; either version 2 of the License, or (at your option)
9  any later version.
10 
11  This program is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 
21  The full GNU General Public License is included in this distribution in the
22  file called LICENSE.
23 *******************************************************************************/
24 
25 #pragma once
26 
27 #include <iostream>
28 #include <functional>
29 #include <fcntl.h>
30 #include <pthread.h>
31 #include <cmath>
32 #include <linux/joystick.h>
33 #include <vector>
34 #include <unistd.h>
35 
36 #define JOYSTICK_DEV "/dev/input/js0"
37 
38 typedef struct
39 {
40  float theta, r, x, y;
42 
43 typedef struct
44 {
45  std::vector<signed short> button;
46  std::vector<signed short> axis;
48 
67 {
68  public:
71 
72  typedef std::function<void(int joystick_n, double mag, double angle)> joystickFunc;
73  typedef std::function<void(int axis_n, double value)> axisFunc;
74  typedef std::function<void(int button_n, int value)> buttonFunc;
75 
76  bool Connect();
77  bool Disconnect();
78 
79  void setPort(const char *port);
80  void setPoll(int ms);
81 
82  const char *getName();
83  __u32 getVersion();
84  __u8 getNumOfJoysticks();
85  __u8 getNumOfAxes();
86  __u8 getNumrOfButtons();
87 
89  bool buttonPressed(int n);
90 
91  void setJoystickCallback(joystickFunc joystickCallback);
92  void setAxisCallback(axisFunc axisCallback);
93  void setButtonCallback(buttonFunc buttonCallback);
94 
95  protected:
96  static void joystickEvent(int joystick_n, double mag, double angle);
97  static void axisEvent(int axis_n, int value);
98  static void buttonEvent(int button_n, int value);
99 
100  static void *loop(void *obj);
101  void readEv();
102 
106 
107  private:
108  pthread_t thread;
109  bool active;
110  int joystick_fd;
111  js_event *joystick_ev;
112  joystick_state *joystick_st;
113  __u32 version;
114  __u8 axes;
115  __u8 buttons;
116  char name[256];
117  char dev_path[256];
118  int pollMS;
119 };
The JoyStickDriver class provides basic functionality to read events from supported game pads under L...
buttonFunc buttonCallbackFunc
static void buttonEvent(int button_n, int value)
static void * loop(void *obj)
static void axisEvent(int axis_n, int value)
void setButtonCallback(buttonFunc buttonCallback)
axisFunc axisCallbackFunc
joystickFunc joystickCallbackFunc
std::function< void(int axis_n, double value)> axisFunc
std::function< void(int joystick_n, double mag, double angle)> joystickFunc
void setAxisCallback(axisFunc axisCallback)
const char * getName()
static void joystickEvent(int joystick_n, double mag, double angle)
void setPoll(int ms)
void setJoystickCallback(joystickFunc joystickCallback)
bool buttonPressed(int n)
void setPort(const char *port)
std::function< void(int button_n, int value)> buttonFunc
joystick_position joystickPosition(int n)
std::vector< signed short > axis
std::vector< signed short > button