ControllerHandler.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Created by janco on 5/23/16.
  3. //
  4. #include "ControllerHandler.h"
  5. #include <iostream>
  6. /*
  7. * String split helper functions
  8. */
  9. std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
  10. std::stringstream ss(s);
  11. std::string item;
  12. while (std::getline(ss, item, delim)) {
  13. elems.push_back(item);
  14. }
  15. return elems;
  16. }
  17. std::vector<std::string> split(const std::string &s, char delim) {
  18. std::vector<std::string> elems;
  19. split(s, delim, elems);
  20. return elems;
  21. }
  22. ControllerHandler::ControllerHandler(){
  23. readthread = std::thread(&ControllerHandler::BasestationReader, this);
  24. };
  25. Controller* ControllerHandler::getLeftController(void){
  26. for (int i = 0; i < 4; i++)
  27. {
  28. if(controllers[i] != nullptr && controllers[i]->isConnected()){
  29. return controllers[i];
  30. }
  31. }
  32. return nullptr;
  33. }
  34. Controller* ControllerHandler::getRightController(void){
  35. bool c1found = false;
  36. for (int i = 0; i < 4; i++)
  37. {
  38. if(controllers[i] != nullptr && controllers[i]->isConnected()){
  39. if(c1found){
  40. return controllers[i];
  41. }
  42. c1found = true;
  43. }
  44. }
  45. return nullptr;
  46. }
  47. void ControllerHandler::SearchBasestation(void){
  48. basestationFound = false;
  49. std::vector<serial::PortInfo> devices_found = serial::list_ports();
  50. std::vector<serial::PortInfo>::iterator iter = devices_found.begin();
  51. while( iter != devices_found.end() )
  52. {
  53. serial::PortInfo device = *iter++;
  54. std::cout << device.hardware_id << " "<< device.port << std::endl;
  55. if(device.hardware_id == "USB\\VID_10C4&PID_EA60&REV_0100"){
  56. basestationFound = true;
  57. baseStation = new serial::Serial(device.port, BasestationBaudrate, serial::Timeout::simpleTimeout(1000));
  58. break;
  59. }
  60. }
  61. }
  62. void ControllerHandler::rumble(int idController, int duration, int power){
  63. if (basestationFound)
  64. {
  65. char sendbuffer[16];
  66. sprintf(sendbuffer, "1|%01d|%06d|%03d\n", idController, duration, power);
  67. baseStation->write(sendbuffer);
  68. }
  69. }
  70. /*
  71. * Reading from basestation
  72. */
  73. void ControllerHandler::BasestationReader() {
  74. while(1){
  75. while(!basestationFound){
  76. SearchBasestation();
  77. if(basestationFound){
  78. baseStation ->write("stop\n");
  79. baseStation ->write("clientlist\n");
  80. }
  81. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  82. }
  83. while(baseStation->isOpen()){
  84. std::string basestationMessage = baseStation->readline();
  85. std::vector<std::string> basestationMessageSplit = split(basestationMessage, '|');
  86. if(basestationMessageSplit.size() > 1){ //Valid message
  87. //std::cout << "DEBUG: Message received. Length: " << basestationMessageSplit.size() << " Type: " << basestationMessageSplit[0] << " Message: " << basestationMessage;
  88. int messageId = std::stoi(basestationMessageSplit[0]);
  89. switch(messageId){
  90. //commandList[messageId](basestationMessageSplit);
  91. case 0: commandDebug(basestationMessageSplit); break; //Debug message
  92. case 1: commandControllerData(basestationMessageSplit); break; //Data from controller
  93. case 2: commandControllerEvent(basestationMessageSplit); break; //Controller events
  94. case 3: commandControllerList(basestationMessageSplit); break; //Connected controller list
  95. }
  96. }
  97. }
  98. }
  99. }
  100. void ControllerHandler::commandDebug(std::vector<std::string> data) {
  101. std::cout << "DEBUG: " << data[1];
  102. }
  103. void ControllerHandler::commandControllerData(std::vector<std::string> data) {
  104. if(data.size() == 10){
  105. Controller* c = controllers[std::stoi(data[1])];
  106. if(c != nullptr && c->isConnected()){
  107. c->ypr.x = std::stoi(data[5])/100.0f;
  108. c->ypr.y = std::stoi(data[6])/100.0f;
  109. c->ypr.z = std::stoi(data[7])/100.0f;
  110. c->joystick.x = std::stoi(data[2])/2000.0f;
  111. c->joystick.y = std::stoi(data[3])/2000.0f;
  112. c->lastButton = c->button;
  113. c->lastJoystickButton = c->joystickButton;
  114. c->lastMagetSwitch = c->magnetSwitch;
  115. c->joystickButton = !(data[4] == "0");
  116. c->button = !(data[8] == "0");
  117. c->magnetSwitch = !(data[9] == "0");
  118. }
  119. }
  120. }
  121. void ControllerHandler::commandControllerEvent(std::vector<std::string> data) {
  122. if(data.size() == 3){
  123. if(data[1] == "connected"){
  124. int controllerId = std::stoi(data[2]);
  125. controllers[controllerId] = new Controller(controllerId);
  126. controllers[controllerId]->setConnected(true);
  127. }else if(data[1] == "disconnected"){
  128. // controllers.erase(*controllers[std::stoi(data[2])]);
  129. }
  130. }
  131. }
  132. void ControllerHandler::commandControllerList(std::vector<std::string> data) {
  133. for(unsigned int i = 1; i < data.size() - 1; i++){
  134. int controllerId = std::stoi(data[i]);
  135. controllers[controllerId] = new Controller(controllerId);
  136. rumble(controllerId, 100, 100);
  137. }
  138. if(basestationFound)
  139. baseStation->write("start\n");
  140. }