joyreadthread.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "joyreadthread.h"
  2. #include <assert.h>
  3. #include <math.h>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <stdexcept>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <string.h>
  14. #include <sstream>
  15. #include <unistd.h>
  16. #include <linux/joystick.h>
  17. #include <QFile>
  18. extern std::string gstrjoy_path;
  19. JoyReadThread::JoyReadThread()
  20. {
  21. if ((fd = open(gstrjoy_path.data(), O_RDONLY)) < 0)
  22. {
  23. mbJoyOK = false;
  24. std::ostringstream str;
  25. // str << filename << ": " << strerror(errno);
  26. // throw std::runtime_error(str.str());
  27. }
  28. else
  29. {
  30. // ok
  31. uint8_t num_axis = 0;
  32. uint8_t num_button = 0;
  33. ioctl(fd, JSIOCGAXES, &num_axis);
  34. ioctl(fd, JSIOCGBUTTONS, &num_button);
  35. axis_count = num_axis;
  36. button_count = num_button;
  37. // Get Name
  38. char name_c_str[1024];
  39. if (ioctl(fd, JSIOCGNAME(sizeof(name_c_str)), name_c_str) < 0)
  40. {
  41. mbJoyOK = false;
  42. std::ostringstream str;
  43. // str << filename << ": " << strerror(errno);
  44. str << "joy:" << ": " << strerror(errno);
  45. // throw std::runtime_error(str.str());
  46. }
  47. else
  48. {
  49. orig_name = name_c_str;
  50. // try {
  51. // name = Glib::convert_with_fallback(name_c_str, "UTF-8", "ISO-8859-1");
  52. // } catch(Glib::ConvertError& err) {
  53. // std::cout << err.what() << std::endl;
  54. // }
  55. }
  56. // axis_state.resize(axis_count);
  57. }
  58. }
  59. void JoyReadThread::run()
  60. {
  61. if(mbJoyOK == false)return;
  62. while(!QThread::isInterruptionRequested())
  63. {
  64. struct js_event event;
  65. ssize_t len = read(fd, &event, sizeof(event));
  66. if(len < 0)
  67. {
  68. // qDebug("fail.");
  69. mbJoyOK = false;
  70. break;
  71. }
  72. // if(len >0)qDebug("len = %d",len);
  73. if(len == sizeof(js_event))
  74. {
  75. switch (event.number) {
  76. case 0:
  77. mfWheel = event.value;
  78. break;
  79. case 2:
  80. mfAcc = event.value;
  81. break;
  82. case 3:
  83. mfBrake = event.value;
  84. break;
  85. case 12:
  86. if(event.value ==1 ) mnShift = 1;
  87. else mnShift = 0;
  88. break;
  89. case 13:
  90. if(event.value ==1 ) mnShift = 2;
  91. else mnShift = 0;
  92. break;
  93. case 14:
  94. if(event.value ==1 ) mnShift = 3;
  95. else mnShift = 0;
  96. break;
  97. case 15:
  98. if(event.value ==1 ) mnShift = 4;
  99. else mnShift = 0;
  100. break;
  101. case 16:
  102. if(event.value ==1 ) mnShift = 5;
  103. else mnShift = 0;
  104. break;
  105. case 17:
  106. if(event.value ==1 ) mnShift = 6;
  107. else mnShift = 0;
  108. break;
  109. case 18:
  110. if(event.value ==1 ) mnShift = -1;
  111. else mnShift = 0;
  112. break;
  113. default:
  114. break;
  115. }
  116. qDebug("shift = %d",mnShift);
  117. // qDebug("type = %d number = %d value = %d ",event.type,event.number,event.value);
  118. // qDebug("wheel:%g acc:%g brake:%g",mfWheel,mfAcc,mfBrake);
  119. }
  120. }
  121. close(fd);
  122. mbJoyOK = false;
  123. }
  124. bool JoyReadThread::isOK()
  125. {
  126. return mbJoyOK;
  127. }
  128. double JoyReadThread::GetAcc()
  129. {
  130. return mfAcc;
  131. }
  132. double JoyReadThread::GetBrake()
  133. {
  134. return mfBrake;
  135. }
  136. double JoyReadThread::GetWheel()
  137. {
  138. return mfWheel;
  139. }
  140. int JoyReadThread::GetShift()
  141. {
  142. return mnShift;
  143. }