GAMS  1.4.0
LinuxJoystick.h
Go to the documentation of this file.
1 #ifndef _GAMS_UTILITY_LINUXJOYSTICK_H
2 #define _GAMS_UTILITY_LINUXJOYSTICK_H
3 
11 #include <string>
12 #include <stdio.h>
14 
15 namespace gams { namespace utility {
16 
18 {
19  public:
20  uint32_t time;
21  int16_t value;
22  unsigned char type;
23  unsigned char number;
24 };
25 
26 class Joystick
27 {
28  private:
29 
30  // handle to the device
31  FILE * file_ = 0;
32 
33  public:
34 
35  inline bool close_handle(void)
36  {
37  if (file_ != 0)
38  {
39  madara_logger_ptr_log(gams::loggers::global_logger.get(),
41  "gams::utility::Joystick::open: " \
42  "attempting close on existing file %x\n",
43  file_);
44 
45  fclose(file_);
46 
47  // clean up just in case user calls close 2x
48  file_ = 0;
49 
50  return true;
51  }
52  else
53  {
54  madara_logger_ptr_log(gams::loggers::global_logger.get(),
56  "gams::utility::Joystick::open: " \
57  "file is unset\n");
58  }
59 
60  return false;
61  }
62 
63  inline bool open_handle(const std::string & handle)
64  {
65  madara_logger_ptr_log(gams::loggers::global_logger.get(),
67  "gams::utility::Joystick::open: " \
68  "attempting close on file %x\n",
69  file_);
70 
71  close_handle();
72 
73  madara_logger_ptr_log(gams::loggers::global_logger.get(),
75  "gams::utility::Joystick::open: " \
76  "calling fopen on %s\n",
77  handle.c_str());
78 
79  file_ = fopen(handle.c_str(), "rb");
80 
81  madara_logger_ptr_log(gams::loggers::global_logger.get(),
83  "gams::utility::Joystick::open: " \
84  "return of fopen is %x\n",
85  file_);
86 
87  return file_ != 0;
88  }
89 
90  Joystick(const std::string & handle)
91  : file_(0)
92  {
93  if(handle != "")
94  {
95  open_handle(handle);
96  }
97  }
98 
100  : file_(0)
101  {
102 
103  }
104 
106  {
107  close_handle();
108  }
109 
110  inline bool get(JoystickEvent & event)
111  {
112  size_t events = 0;
113 
114  if (file_ != 0)
115  {
116  events = fread(&event, sizeof(event), 1, file_);
117  }
118 
119  madara_logger_ptr_log(gams::loggers::global_logger.get(),
121  "gams::utility::Joystick::open: " \
122  "fread returned %zu events\n",
123  events);
124 
125  return events == 1;
126  }
127 
128  inline bool is_axis(const JoystickEvent & event) const
129  {
130  // we don't want inits
131  return (event.type & 0x02) != 0;
132  }
133 
134  inline bool is_button(const JoystickEvent & event) const
135  {
136  // we don't want inits
137  return (event.type & 0x01) != 0;
138  }
139 
140  inline bool is_x_move(const JoystickEvent & event) const
141  {
142  return event.number == 0;
143  }
144 
145  inline bool is_y_move(const JoystickEvent & event) const
146  {
147  return event.number == 1;
148  }
149 
150  inline bool is_rotate(const JoystickEvent & event) const
151  {
152  return event.number == 3;
153  }
154 
155  inline bool is_z_move(const JoystickEvent & event) const
156  {
157  return event.number == 4;
158  }
159 
160  inline bool is_init(const JoystickEvent & event) const
161  {
162  return (event.type & 0x80) != 0;
163  }
164 
165  inline double to_double(const JoystickEvent & event) const
166  {
167  double result = event.value;
168  result /= 32768;
169 
170  // note that we're cheating here because positives go up to 32767
171  // so, max positive axis will be 0.999969482
172 
173  return result;
174  }
175 };
176 
177 }}
178 
179 #endif // _GAMS_UTILITY_LINUXJOYSTICK_H
bool is_x_move(const JoystickEvent &event) const
bool is_rotate(const JoystickEvent &event) const
bool is_y_move(const JoystickEvent &event) const
bool is_button(const JoystickEvent &event) const
bool open_handle(const std::string &handle)
Definition: LinuxJoystick.h:63
Joystick(const std::string &handle)
Definition: LinuxJoystick.h:90
bool is_z_move(const JoystickEvent &event) const
bool is_axis(const JoystickEvent &event) const
bool is_init(const JoystickEvent &event) const
double to_double(const JoystickEvent &event) const
bool get(JoystickEvent &event)
GAMS_EXPORT madara::utility::Refcounter< madara::logger::Logger > global_logger
Contains all GAMS-related tools, classes and code.