MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
JoystickDrive Class Reference
Inheritance diagram for JoystickDrive:
Collaboration diagram for JoystickDrive:

Public Member Functions

 JoystickDrive (unsigned int uL, const DriveHandler *pDH, const std::string &sFileName, integer nButtons, const std::vector< doublereal > &lc_scale, const std::vector< doublereal > &v0)
 
virtual ~JoystickDrive (void)
 
virtual std::ostream & Restart (std::ostream &out) const
 
virtual void ServePending (const doublereal &t)
 
- Public Member Functions inherited from FileDrive
 FileDrive (unsigned int uL, const DriveHandler *pDH, const std::string &s, integer nd, const std::vector< doublereal > &v0)
 
virtual ~FileDrive (void)
 
virtual Drive::Type GetDriveType (void) const
 
virtual integer iGetNumDrives (void) const
 
virtual doublereal dGet (const doublereal &t, int i=1) const
 
- Public Member Functions inherited from Drive
 Drive (unsigned int uL, const DriveHandler *pDH)
 
virtual ~Drive (void)
 
- Public Member Functions inherited from WithLabel
 WithLabel (unsigned int uL=0, const std::string &sN="")
 
virtual ~WithLabel (void)
 
void PutLabel (unsigned int uL)
 
void PutName (const std::string &sN)
 
unsigned int GetLabel (void) const
 
const std::string & GetName (void) const
 

Private Member Functions

bool get_one (void)
 
void init (void)
 
bool get_fd_flags (void)
 
bool set_fd_blocking (bool bBlocking)
 

Private Attributes

int m_fd
 
std::vector< boolm_b_reset
 
integer m_nButtons
 
std::vector< doublerealm_lc_scale
 
integer m_nLC
 
doublerealm_pdLC
 
doublerealm_pdB
 
int m_flags
 

Additional Inherited Members

- Public Types inherited from Drive
enum  Type { UNKNOWN = -1, FILEDRIVE = 0, LASTDRIVETYPE }
 
enum  Bailout { BO_NONE = 0x0, BO_UPPER = 0x1, BO_LOWER = 0x2, BO_ANY = (BO_UPPER | BO_LOWER) }
 
- Protected Attributes inherited from FileDrive
std::string sFileName
 
integer iNumDrives
 
doublerealpdVal
 
- Protected Attributes inherited from Drive
const DriveHandlerpDrvHdl
 
- Protected Attributes inherited from WithLabel
unsigned int uLabel
 
std::string sName
 
- Static Protected Attributes inherited from Drive
static doublereal dReturnValue = 0.
 

Detailed Description

Definition at line 43 of file module-hid.cc.

Constructor & Destructor Documentation

JoystickDrive::JoystickDrive ( unsigned int  uL,
const DriveHandler pDH,
const std::string &  sFileName,
integer  nButtons,
const std::vector< doublereal > &  lc_scale,
const std::vector< doublereal > &  v0 
)

Definition at line 78 of file module-hid.cc.

82 : FileDrive(uL, pDH, sFileName, nButtons + lc_scale.size(), v0),
83 m_fd(-1),
84 m_b_reset(nButtons), m_nButtons(nButtons),
85 m_lc_scale(lc_scale), m_nLC(m_lc_scale.size()),
86 m_pdLC(pdVal + 1), m_pdB(m_pdLC + m_nLC)
87 {
88 }
doublereal * m_pdB
Definition: module-hid.cc:52
doublereal * m_pdLC
Definition: module-hid.cc:52
std::vector< doublereal > m_lc_scale
Definition: module-hid.cc:49
std::string sFileName
Definition: filedrv.h:46
FileDrive(unsigned int uL, const DriveHandler *pDH, const std::string &s, integer nd, const std::vector< doublereal > &v0)
Definition: filedrv.cc:52
doublereal * pdVal
Definition: filedrv.h:48
integer m_nLC
Definition: module-hid.cc:50
integer m_nButtons
Definition: module-hid.cc:48
static const std::vector< doublereal > v0
Definition: fixedstep.cc:45
std::vector< bool > m_b_reset
Definition: module-hid.cc:47
JoystickDrive::~JoystickDrive ( void  )
virtual

Definition at line 90 of file module-hid.cc.

References m_fd.

91 {
92  if (m_fd != -1) {
93  close(m_fd);
94  }
95  m_fd = -1;
96 }

Member Function Documentation

bool JoystickDrive::get_fd_flags ( void  )
private

Definition at line 99 of file module-hid.cc.

References m_fd, and m_flags.

Referenced by init().

100 {
101  m_flags = fcntl(m_fd, F_GETFL, 0);
102  if (m_flags == -1) {
103  return false;
104  }
105 
106  return true;
107 }
bool JoystickDrive::get_one ( void  )
private

Definition at line 127 of file module-hid.cc.

References buf, m_b_reset, m_fd, m_lc_scale, m_nButtons, m_nLC, m_pdB, m_pdLC, MBDYN_EXCEPT_ARGS, read(), set_fd_blocking(), FileDrive::sFileName, and WithLabel::uLabel.

Referenced by ServePending().

128 {
129  fd_set readfds;
130  FD_ZERO(&readfds);
131  FD_SET(m_fd, &readfds);
132  struct timeval tv = { 0, 0 };
133  int rc = select(m_fd + 1, &readfds, NULL, NULL, &tv);
134 
135 #ifdef HID_DEBUG
136  std::cerr << "select=" << rc << std::endl;
137 #endif // HID_DEBUG
138 
139  switch (rc) {
140  case -1: {
141  int save_errno = errno;
142  char *err_msg = strerror(save_errno);
143 
144  silent_cout("JoystickDrive(" << uLabel << ", " << sFileName << "): select failed"
145  << " (" << save_errno << ": " << err_msg << ")" << std::endl);
147  }
148 
149  case 0:
150  return true;
151 
152  default:
153  if (!FD_ISSET(m_fd, &readfds)) {
154  silent_cout("JoystickDrive"
155  "(" << sFileName << "): "
156  "socket " << m_fd << " reset"
157  << std::endl);
159  }
160  }
161 
162  // toggle blocking to make sure we get the whole package
163  char buf[8];
164  set_fd_blocking(true);
165  ssize_t n2 = read(m_fd, (void *)&buf[0], sizeof(buf));
166  set_fd_blocking(false);
167  if (n2 == -1) {
168 #ifdef HID_DEBUG
169  int save_errno = errno;
170  std::cerr << "recv=" << save_errno << " " << strerror(save_errno) << std::endl;
171 #endif // HID_DEBUG
172  }
173 
174 #ifdef HID_DEBUG
175  uint32_t cnt = *((uint32_t *)&buf[0]);
176 #endif // HID_DEBUG
177  uint8_t type = (uint8_t)buf[6];
178  uint8_t idx = (uint8_t)buf[7];
179  int16_t value = *((int16_t *)&buf[4]);
180 
181 #ifdef HID_DEBUG
182  std::cerr << "n2=" << n2 << " cnt=" << cnt << " value=" << int(value) << " type=" << unsigned(type) << " idx=" << unsigned(idx) << std::endl;
183 #endif // HID_DEBUG
184 
185  switch (type) {
186  case 1:
187  // buttons
188  if (idx < m_nButtons) {
189  /*
190  * when a button is pressed, an event
191  * with non-zero value is recorded;
192  * when the button is released, an event
193  * with zero value is recorded.
194  *
195  * when we see an event with non-zero value,
196  * we set the value to 1., and clear
197  * the "reset" flag; when we see an event with
198  * zero value, we set the reset flag.
199  *
200  * if the button was not previously set,
201  * and it is pressed between two calls to
202  * ServePending(), at the end it is set
203  * and not scheduled for clear.
204  *
205  * if the button was set, and it is
206  * released between two calls to ServePending(),
207  * it is scheduled for clear.
208  *
209  * if the button is pressed and released
210  * multiple times between two calls,
211  * it remains set to 1. and scheduled for clear.
212  */
213  if (value) {
214  m_pdB[idx] = 1.;
215  m_b_reset[idx] = false;
216 
217  } else {
218  m_b_reset[idx] = true;
219  }
220  }
221  // else ignore
222  break;
223 
224  case 2:
225  // linear controls
226 
227  /*
228  * linear controls vary either between 0 and UINT16_MAX
229  * or between -INT16_MAX and INT16_MAX.
230  *
231  * FIXME: currently, we assume -INT16_MAX and INT16_MAX.
232  *
233  * we simply record the last value, and scale it.
234  */
235  if (idx < m_nLC) {
236  m_pdLC[idx] = (m_lc_scale[idx]*value)/INT16_MAX;
237  }
238  break;
239 
240  default:
241  // ignore
242  break;
243  }
244 
245  return false;
246 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
doublereal * m_pdB
Definition: module-hid.cc:52
doublereal * m_pdLC
Definition: module-hid.cc:52
std::vector< doublereal > m_lc_scale
Definition: module-hid.cc:49
std::string sFileName
Definition: filedrv.h:46
integer m_nLC
Definition: module-hid.cc:50
unsigned int uLabel
Definition: withlab.h:44
integer m_nButtons
Definition: module-hid.cc:48
bool set_fd_blocking(bool bBlocking)
Definition: module-hid.cc:110
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
static void * read(LoadableElem *pEl, DataManager *pDM, MBDynParser &HP)
std::vector< bool > m_b_reset
Definition: module-hid.cc:47

Here is the call graph for this function:

void JoystickDrive::init ( void  )
private

Definition at line 249 of file module-hid.cc.

References buf, get_fd_flags(), m_fd, m_nButtons, m_nLC, MBDYN_EXCEPT_ARGS, read(), set_fd_blocking(), FileDrive::sFileName, and WithLabel::uLabel.

Referenced by ServePending().

250 {
251  m_fd = open(sFileName.c_str(), O_RDONLY);
252  if (m_fd == -1) {
253  int save_errno = errno;
254  silent_cerr("JoystickDrive(" << uLabel << ", " << sFileName << ")::init(): "
255  "unable to open device <" << sFileName << "> "
256  "(" << save_errno << ": " << strerror(save_errno) << ")"
257  << std::endl);
259  }
260 
261  // this probably contains the description of the channels...
262  integer nB = 0, nLC = 0;
263  uint8_t iBmax = 0, iLCmax = 0;
264  for (int i = 0; i < m_nLC + m_nButtons; i++) {
265  char buf[8];
266  ssize_t n = read(m_fd, (void *)&buf[0], sizeof(buf));
267 
268 #ifdef HID_DEBUG
269  std::cerr << "read idx=" << i << " n=" << n << std::endl;
270 #endif // HID_DEBUG
271 
272  if (n == -1) {
273  int save_errno = errno;
274  silent_cerr("JoystickDrive(" << uLabel << ", " << sFileName << ")::init(): "
275  "read failed (" << save_errno << ": " << strerror(save_errno) << ")"
276  << std::endl);
278  }
279 
280  uint8_t type = (uint8_t)buf[6];
281  uint8_t idx = (uint8_t)buf[7];
282  // int16_t value = *((int16_t *)&buf[4]);
283  if ((type & 0x7F) == 1) {
284  nB++;
285  iBmax = std::max(iBmax, idx);
286 
287  } else if ((type & 0x7F) == 2) {
288  nLC++;
289  iLCmax = std::max(iLCmax, idx);
290 
291  } else {
292  silent_cerr("JoystickDrive(" << uLabel << ", " << sFileName << "): "
293  "warning, unknown type " << int(type & 0x7F) << ", ignored" << std::endl); }
294 
295 #ifdef HID_DEBUG
296  std::cerr << " type=" << uint(type) << " idx=" << uint(idx) << " value=" << value << std::endl;
297 #endif // HID_DEBUG
298  }
299 
300  bool bFail(false);
301  if (nB != m_nButtons) {
302  silent_cerr("JoystickDrive(" << uLabel << ", " << sFileName << "): "
303  "inconsistent number of buttons: expected " << m_nButtons << ", got " << nB << std::endl);
304  bFail = true;
305  }
306 
307  if (iBmax >= m_nButtons) {
308  silent_cerr("JoystickDrive(" << uLabel << ", " << sFileName << "): "
309  "inconsistent largest button index: expected " << m_nButtons - 1 << ", got " << unsigned(iBmax) << std::endl);
310  bFail = true;
311  }
312 
313  if (nLC != m_nLC) {
314  silent_cerr("JoystickDrive(" << uLabel << ", " << sFileName << "): "
315  "inconsistent number of linear controls: expected " << m_nLC << ", got " << nLC << std::endl);
316  bFail = true;
317  }
318 
319  if (iLCmax >= m_nLC) {
320  silent_cerr("JoystickDrive(" << uLabel << ", " << sFileName << "): "
321  "inconsistent largest linear control index: expected " << m_nLC - 1 << ", got " << unsigned(iLCmax) << std::endl);
322  bFail = true;
323  }
324 
325  if (bFail) {
326  // TODO: error
327  }
328 
329  get_fd_flags();
330  set_fd_blocking(false);
331 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
std::string sFileName
Definition: filedrv.h:46
integer m_nLC
Definition: module-hid.cc:50
unsigned int uLabel
Definition: withlab.h:44
integer m_nButtons
Definition: module-hid.cc:48
bool get_fd_flags(void)
Definition: module-hid.cc:99
bool set_fd_blocking(bool bBlocking)
Definition: module-hid.cc:110
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
long int integer
Definition: colamd.c:51
static void * read(LoadableElem *pEl, DataManager *pDM, MBDynParser &HP)

Here is the call graph for this function:

std::ostream & JoystickDrive::Restart ( std::ostream &  out) const
virtual

Implements FileDrive.

Definition at line 334 of file module-hid.cc.

335 {
336  return out << "# not implemented yet" << std::endl;
337 }
void JoystickDrive::ServePending ( const doublereal t)
virtual

Implements Drive.

Definition at line 340 of file module-hid.cc.

References get_one(), init(), m_b_reset, m_fd, m_nButtons, m_nLC, m_pdB, NO_OP, FileDrive::pdVal, and FileDrive::sFileName.

341 {
342  if (m_fd == -1) {
343  init();
344  }
345 
346  // reset if needed
347  for (integer idx = 0; idx < m_nButtons; idx++) {
348  if (m_b_reset[idx]) {
349  m_pdB[idx] = 0.;
350  m_b_reset[idx] = false;
351  }
352  }
353 
354  // flush buffer
355  while (!get_one()) { NO_OP; };
356 
357 #ifdef HID_DEBUG
358  std::cerr << "JoystickDrive(" << sFileName << ")" << std::endl;
359  for (int i = 0; i < m_nLC + m_nButtons; i++) {
360  std::cerr << " V[" << i << "]=" << pdVal[i] << std::endl;
361  }
362 #endif // HID_DEBUG
363 }
bool get_one(void)
Definition: module-hid.cc:127
doublereal * m_pdB
Definition: module-hid.cc:52
std::string sFileName
Definition: filedrv.h:46
#define NO_OP
Definition: myassert.h:74
doublereal * pdVal
Definition: filedrv.h:48
integer m_nLC
Definition: module-hid.cc:50
integer m_nButtons
Definition: module-hid.cc:48
long int integer
Definition: colamd.c:51
void init(void)
Definition: module-hid.cc:249
std::vector< bool > m_b_reset
Definition: module-hid.cc:47

Here is the call graph for this function:

bool JoystickDrive::set_fd_blocking ( bool  bBlocking)
private

Definition at line 110 of file module-hid.cc.

References m_fd, and m_flags.

Referenced by get_one(), and init().

111 {
112  int rc;
113 
114  if (bBlocking) {
115  m_flags &= ~O_NONBLOCK;
116 
117  } else {
118  m_flags |= O_NONBLOCK;
119  }
120 
121  rc = fcntl(m_fd, F_SETFL, m_flags);
122 
123  return (rc != -1);
124 }

Member Data Documentation

std::vector<bool> JoystickDrive::m_b_reset
private

Definition at line 47 of file module-hid.cc.

Referenced by get_one(), and ServePending().

int JoystickDrive::m_fd
private

Definition at line 45 of file module-hid.cc.

Referenced by get_fd_flags(), get_one(), init(), ServePending(), set_fd_blocking(), and ~JoystickDrive().

int JoystickDrive::m_flags
private

Definition at line 54 of file module-hid.cc.

Referenced by get_fd_flags(), and set_fd_blocking().

std::vector<doublereal> JoystickDrive::m_lc_scale
private

Definition at line 49 of file module-hid.cc.

Referenced by get_one().

integer JoystickDrive::m_nButtons
private

Definition at line 48 of file module-hid.cc.

Referenced by get_one(), init(), and ServePending().

integer JoystickDrive::m_nLC
private

Definition at line 50 of file module-hid.cc.

Referenced by get_one(), init(), and ServePending().

doublereal * JoystickDrive::m_pdB
private

Definition at line 52 of file module-hid.cc.

Referenced by get_one(), and ServePending().

doublereal* JoystickDrive::m_pdLC
private

Definition at line 52 of file module-hid.cc.

Referenced by get_one().


The documentation for this class was generated from the following file: