Advertisement
Guest User

BeeWi Helipad Controler

a guest
Oct 11th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. /*
  2.     Ultimate BeeWi HeliPad controler
  3.     Used Lib : sfml-1.6, bluez
  4.     Author : ex0ns (ex0ns.hostei.com)
  5.     Langage : C++
  6.     Compilation : g++ controler.cpp -o controler -lbluetooth -lsfml-window -lsfml-system
  7. */
  8. #include <iostream>
  9. #include <sstream>
  10.  
  11. #include <unistd.h>
  12. #include <sys/socket.h>
  13. #include <bluetooth/bluetooth.h>
  14. #include <bluetooth/rfcomm.h>
  15.  
  16. #include <SFML/System.hpp> // SFML
  17. #include <SFML/Window.hpp>
  18. #include <SFML/Network.hpp>
  19.  
  20. typedef long long int lli;
  21.  
  22. const char *intToChar(lli number){
  23.     /* This Function is use to create an hex string from a number), so it can be pass as parameter to the socket */
  24.     std::stringstream ss(std::stringstream::in | std::stringstream::out);
  25.     ss << "0x" << std::hex << number;
  26.     return ss.str().c_str();
  27. }
  28.  
  29. int main()
  30. {
  31.     /* Variables For the Bluetooth Connection */
  32.     struct sockaddr_rc addr = {0};
  33.     char server[18] = "00:11:67:E3:DE:63"; // MAC adress of our Helicoptere
  34.     int s, status; // Two socket handler
  35.    
  36.     /* Variables For the Flying Monitor */
  37.     lli prefixe = 0x150000000000; // See the article for more details
  38.     lli suffixe = 0x0000000000FF;
  39.     lli firstRotorSpeed = 0x0000000000;
  40.     lli trimmerValue = 0x0000;
  41.    
  42.     lli firstRotorSpeedCoeff = 0x0500000000; // Rotor speed increase
  43.     lli trimmerCoeff = 0x0800; // Trimmer Modification
  44.    
  45.     lli command; // Final Command sent to the Heli
  46.     int trimmerPosition = 0; // 0 = Null, 1 = Positif (right), -1 (negative)
  47.    
  48.     /* Handle Bluetooth Connection */
  49.     s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); // See the article for more details
  50.     addr.rc_family = AF_BLUETOOTH;
  51.     addr.rc_channel = (uint8_t)6;
  52.     str2ba(server, &addr.rc_bdaddr);
  53.    
  54.     status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
  55.    
  56.     if(status == 0){
  57.         sf::Window App(sf::VideoMode(800,600,30), "Helipad Controler"); // Just show a black screen I didn't take the time to create a beautiful nice GUI
  58.         sf::Clock clock;
  59.         bool running = true;
  60.         while(App.IsOpened()){
  61.            
  62.             sf::Event Event;
  63.             while(App.GetEvent(Event)){
  64.                 if(Event.Type == sf::Event::Closed) // Hook the "cross" button to quit
  65.                     App.Close();
  66.                    
  67.                 if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::PageUp)){ // Increase First Rotor Velocity
  68.                     if(firstRotorSpeed <= 0xFF00000000-firstRotorSpeedCoeff)
  69.                         firstRotorSpeed+=firstRotorSpeedCoeff;
  70.                 }
  71.                 if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::PageDown)){ // Descrease First Rotor Velocity
  72.                     if(firstRotorSpeed >= firstRotorSpeedCoeff)
  73.                         firstRotorSpeed-=firstRotorSpeedCoeff;
  74.                 }
  75.                
  76.                 if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::Subtract)){ // Reduce trimmer velocity
  77.                     /*
  78.                      * The Subtract key is the '-' on the numPad
  79.                      */
  80.                     if(trimmerValue > trimmerCoeff && trimmerPosition == 1 ){
  81.                         trimmerValue-=trimmerCoeff;
  82.                         prefixe = 0x170000000000;                      
  83.                     }
  84.                     else if(trimmerValue == trimmerCoeff && trimmerPosition == 1){
  85.                         trimmerValue = 0x0000;
  86.                         prefixe = 0x150000000000;
  87.                         trimmerPosition = 0;
  88.                     }
  89.                     else if(trimmerValue <= 0xF800-trimmerCoeff && trimmerPosition != 1){
  90.                         trimmerValue+=trimmerCoeff;
  91.                         prefixe = 0x140000000000;
  92.                         trimmerPosition = -1;
  93.                     }
  94.                 }
  95.                
  96.                 if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::Add)){ // Increase trimmer velocity
  97.                     /*
  98.                      * The Add key is the '+' on numPad
  99.                      */
  100.                     if(trimmerValue > trimmerCoeff && trimmerPosition == -1 ){  
  101.                         trimmerValue-=trimmerCoeff;
  102.                         prefixe = 0x140000000000;                  
  103.                     }
  104.                     else if(trimmerValue == trimmerCoeff && trimmerPosition == -1){
  105.                         trimmerValue = 0x0000;
  106.                         prefixe = 0x150000000000;
  107.                         trimmerPosition = 0;
  108.                     }
  109.                     else if(trimmerValue <= 0xF800-trimmerCoeff && trimmerPosition != -1){
  110.                         trimmerValue+=trimmerCoeff;
  111.                         prefixe = 0x170000000000;
  112.                         trimmerPosition = 1;
  113.                     }
  114.                 }
  115.             }
  116.            
  117.             App.Display();
  118.             if(clock.GetElapsedTime() > 0.5){ // Timer To Avoid Request Spamming
  119.                 command = prefixe + firstRotorSpeed + trimmerValue + suffixe;
  120.                 std::cout << intToChar(command) << std::endl;
  121.                 status = write(s, intToChar(command), 14); // We know that we have to send 14 bytes by analysing the requests
  122.                 clock.Reset(); // To reset timer so GetElapsedTime can work again
  123.             }
  124.         }
  125.     }else{
  126.         std::cout << "Connection Failed" << std::endl;
  127.     }
  128.    
  129.     close(s);
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement