123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #include "joyreadthread.h"
- #include <assert.h>
- #include <math.h>
- #include <algorithm>
- #include <iostream>
- #include <stdexcept>
- #include <stdint.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sstream>
- #include <unistd.h>
- #include <linux/joystick.h>
- #include <QFile>
- extern std::string gstrjoy_path;
- JoyReadThread::JoyReadThread()
- {
- if ((fd = open(gstrjoy_path.data(), O_RDONLY)) < 0)
- {
- mbJoyOK = false;
- std::ostringstream str;
- // str << filename << ": " << strerror(errno);
- // throw std::runtime_error(str.str());
- }
- else
- {
- // ok
- uint8_t num_axis = 0;
- uint8_t num_button = 0;
- ioctl(fd, JSIOCGAXES, &num_axis);
- ioctl(fd, JSIOCGBUTTONS, &num_button);
- axis_count = num_axis;
- button_count = num_button;
- // Get Name
- char name_c_str[1024];
- if (ioctl(fd, JSIOCGNAME(sizeof(name_c_str)), name_c_str) < 0)
- {
- mbJoyOK = false;
- std::ostringstream str;
- // str << filename << ": " << strerror(errno);
- str << "joy:" << ": " << strerror(errno);
- // throw std::runtime_error(str.str());
- }
- else
- {
- orig_name = name_c_str;
- // try {
- // name = Glib::convert_with_fallback(name_c_str, "UTF-8", "ISO-8859-1");
- // } catch(Glib::ConvertError& err) {
- // std::cout << err.what() << std::endl;
- // }
- }
- // axis_state.resize(axis_count);
- }
- }
- void JoyReadThread::run()
- {
- if(mbJoyOK == false)return;
- while(!QThread::isInterruptionRequested())
- {
- struct js_event event;
- ssize_t len = read(fd, &event, sizeof(event));
- if(len < 0)
- {
- // qDebug("fail.");
- mbJoyOK = false;
- break;
- }
- // if(len >0)qDebug("len = %d",len);
- if(len == sizeof(js_event))
- {
- switch (event.number) {
- case 0:
- mfWheel = event.value;
- break;
- case 2:
- mfAcc = event.value;
- break;
- case 3:
- mfBrake = event.value;
- break;
- case 12:
- if(event.value ==1 ) mnShift = 1;
- else mnShift = 0;
- break;
- case 13:
- if(event.value ==1 ) mnShift = 2;
- else mnShift = 0;
- break;
- case 14:
- if(event.value ==1 ) mnShift = 3;
- else mnShift = 0;
- break;
- case 15:
- if(event.value ==1 ) mnShift = 4;
- else mnShift = 0;
- break;
- case 16:
- if(event.value ==1 ) mnShift = 5;
- else mnShift = 0;
- break;
- case 17:
- if(event.value ==1 ) mnShift = 6;
- else mnShift = 0;
- break;
- case 18:
- if(event.value ==1 ) mnShift = -1;
- else mnShift = 0;
- break;
- default:
- break;
- }
- qDebug("shift = %d",mnShift);
- // qDebug("type = %d number = %d value = %d ",event.type,event.number,event.value);
- // qDebug("wheel:%g acc:%g brake:%g",mfWheel,mfAcc,mfBrake);
- }
- }
- close(fd);
- mbJoyOK = false;
- }
- bool JoyReadThread::isOK()
- {
- return mbJoyOK;
- }
- double JoyReadThread::GetAcc()
- {
- return mfAcc;
- }
- double JoyReadThread::GetBrake()
- {
- return mfBrake;
- }
- double JoyReadThread::GetWheel()
- {
- return mfWheel;
- }
- int JoyReadThread::GetShift()
- {
- return mnShift;
- }
|