36 #include <sys/types.h>
68 integer nButtons,
const std::vector<doublereal>& lc_scale,
69 const std::vector<doublereal>&
v0);
73 virtual std::ostream&
Restart(std::ostream& out)
const;
79 const std::string& sFileName,
80 integer nButtons,
const std::vector<doublereal>& lc_scale,
81 const std::vector<doublereal>&
v0)
82 :
FileDrive(uL, pDH, sFileName, nButtons + lc_scale.size(), v0),
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)
131 FD_SET(
m_fd, &readfds);
132 struct timeval tv = { 0, 0 };
133 int rc = select(
m_fd + 1, &readfds, NULL, NULL, &tv);
136 std::cerr <<
"select=" << rc << std::endl;
141 int save_errno = errno;
142 char *err_msg = strerror(save_errno);
144 silent_cout(
"JoystickDrive(" <<
uLabel <<
", " <<
sFileName <<
"): select failed"
145 <<
" (" << save_errno <<
": " << err_msg <<
")" << std::endl);
153 if (!FD_ISSET(
m_fd, &readfds)) {
154 silent_cout(
"JoystickDrive"
156 "socket " <<
m_fd <<
" reset"
165 ssize_t n2 =
read(
m_fd, (
void *)&buf[0],
sizeof(buf));
169 int save_errno = errno;
170 std::cerr <<
"recv=" << save_errno <<
" " << strerror(save_errno) << std::endl;
175 uint32_t cnt = *((uint32_t *)&buf[0]);
177 uint8_t type = (uint8_t)buf[6];
178 uint8_t idx = (uint8_t)buf[7];
179 int16_t value = *((int16_t *)&buf[4]);
182 std::cerr <<
"n2=" << n2 <<
" cnt=" << cnt <<
" value=" <<
int(value) <<
" type=" << unsigned(type) <<
" idx=" << unsigned(idx) << std::endl;
253 int save_errno = errno;
254 silent_cerr(
"JoystickDrive(" <<
uLabel <<
", " <<
sFileName <<
")::init(): "
255 "unable to open device <" <<
sFileName <<
"> "
256 "(" << save_errno <<
": " << strerror(save_errno) <<
")"
263 uint8_t iBmax = 0, iLCmax = 0;
266 ssize_t n =
read(
m_fd, (
void *)&buf[0],
sizeof(buf));
269 std::cerr <<
"read idx=" << i <<
" n=" << n << std::endl;
273 int save_errno = errno;
274 silent_cerr(
"JoystickDrive(" <<
uLabel <<
", " <<
sFileName <<
")::init(): "
275 "read failed (" << save_errno <<
": " << strerror(save_errno) <<
")"
280 uint8_t type = (uint8_t)buf[6];
281 uint8_t idx = (uint8_t)buf[7];
283 if ((type & 0x7F) == 1) {
285 iBmax = std::max(iBmax, idx);
287 }
else if ((type & 0x7F) == 2) {
289 iLCmax = std::max(iLCmax, idx);
293 "warning, unknown type " <<
int(type & 0x7F) <<
", ignored" << std::endl); }
296 std::cerr <<
" type=" << uint(type) <<
" idx=" << uint(idx) <<
" value=" << value << std::endl;
301 if (nB != m_nButtons) {
303 "inconsistent number of buttons: expected " << m_nButtons <<
", got " << nB << std::endl);
307 if (iBmax >= m_nButtons) {
309 "inconsistent largest button index: expected " << m_nButtons - 1 <<
", got " <<
unsigned(iBmax) << std::endl);
315 "inconsistent number of linear controls: expected " <<
m_nLC <<
", got " << nLC << std::endl);
319 if (iLCmax >=
m_nLC) {
321 "inconsistent largest linear control index: expected " <<
m_nLC - 1 <<
", got " <<
unsigned(iLCmax) << std::endl);
336 return out <<
"# not implemented yet" << std::endl;
358 std::cerr <<
"JoystickDrive(" <<
sFileName <<
")" << std::endl;
360 std::cerr <<
" V[" << i <<
"]=" <<
pdVal[i] << std::endl;
376 "Module: joystick \n"
377 "Author: Pierangelo Masarati <pierangelo.masarati@polimi.it> \n"
378 "Organization: Dipartimento di Scienze e Tecnologie Aerospaziali \n"
379 " Politecnico di Milano \n"
380 " http://www.aero.polimi.it/ \n"
382 " All rights reserved \n"
384 " file: <label> , joystick , \n"
386 " <number_of_buttons> , \n"
387 " <number_of_linear_controls> , \n"
388 " [ scale, <scale_factor> [ , ... ] ] ; \n"
390 " <file> ::= \"/dev/input/js0\" \n"
392 " <scale_factor>'s default to 1.0 \n"
407 std::string sFileName(s);
419 std::vector<doublereal> lc_scale(nLC);
421 for (
integer idx = 0; idx < nLC; idx++) {
426 for (
integer idx = 0; idx < nLC; idx++) {
431 const std::vector<doublereal>
v0;
449 silent_cerr(
"JoystickDrive: "
450 "module_init(" << module_name <<
") "
451 "failed" << std::endl);
bool SetDriveData(const std::string &s, DriveRead *rf)
#define MBDYN_EXCEPT_ARGS
virtual integer GetInt(integer iDefval=0)
JoystickDrive(unsigned int uL, const DriveHandler *pDH, const std::string &sFileName, integer nButtons, const std::vector< doublereal > &lc_scale, const std::vector< doublereal > &v0)
std::vector< doublereal > m_lc_scale
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
const DriveHandler * pGetDrvHdl(void) const
int module_init(const char *module_name, void *pdm, void *php)
This function registers our user defined element for the math parser.
virtual std::ostream & Restart(std::ostream &out) const
virtual bool IsKeyWord(const char *sKeyWord)
virtual Drive * Read(unsigned uLabel, const DataManager *pDM, MBDynParser &HP)
virtual void ServePending(const doublereal &t)
bool set_fd_blocking(bool bBlocking)
static const std::vector< doublereal > v0
static doublereal buf[BUFSIZE]
virtual ~JoystickDrive(void)
static std::stack< const HighParser * > pHP
static void * read(LoadableElem *pEl, DataManager *pDM, MBDynParser &HP)
std::vector< bool > m_b_reset
virtual doublereal GetReal(const doublereal &dDefval=0.0)