Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Ultimate BeeWi HeliPad controler
- Used Lib : sfml-1.6, bluez
- Author : ex0ns (ex0ns.hostei.com)
- Langage : C++
- Compilation : g++ controler.cpp -o controler -lbluetooth -lsfml-window -lsfml-system
- */
- #include <iostream>
- #include <sstream>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <bluetooth/bluetooth.h>
- #include <bluetooth/rfcomm.h>
- #include <SFML/System.hpp> // SFML
- #include <SFML/Window.hpp>
- #include <SFML/Network.hpp>
- typedef long long int lli;
- const char *intToChar(lli number){
- /* This Function is use to create an hex string from a number), so it can be pass as parameter to the socket */
- std::stringstream ss(std::stringstream::in | std::stringstream::out);
- ss << "0x" << std::hex << number;
- return ss.str().c_str();
- }
- int main()
- {
- /* Variables For the Bluetooth Connection */
- struct sockaddr_rc addr = {0};
- char server[18] = "00:11:67:E3:DE:63"; // MAC adress of our Helicoptere
- int s, status; // Two socket handler
- /* Variables For the Flying Monitor */
- lli prefixe = 0x150000000000; // See the article for more details
- lli suffixe = 0x0000000000FF;
- lli firstRotorSpeed = 0x0000000000;
- lli trimmerValue = 0x0000;
- lli firstRotorSpeedCoeff = 0x0500000000; // Rotor speed increase
- lli trimmerCoeff = 0x0800; // Trimmer Modification
- lli command; // Final Command sent to the Heli
- int trimmerPosition = 0; // 0 = Null, 1 = Positif (right), -1 (negative)
- /* Handle Bluetooth Connection */
- s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); // See the article for more details
- addr.rc_family = AF_BLUETOOTH;
- addr.rc_channel = (uint8_t)6;
- str2ba(server, &addr.rc_bdaddr);
- status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
- if(status == 0){
- 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
- sf::Clock clock;
- bool running = true;
- while(App.IsOpened()){
- sf::Event Event;
- while(App.GetEvent(Event)){
- if(Event.Type == sf::Event::Closed) // Hook the "cross" button to quit
- App.Close();
- if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::PageUp)){ // Increase First Rotor Velocity
- if(firstRotorSpeed <= 0xFF00000000-firstRotorSpeedCoeff)
- firstRotorSpeed+=firstRotorSpeedCoeff;
- }
- if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::PageDown)){ // Descrease First Rotor Velocity
- if(firstRotorSpeed >= firstRotorSpeedCoeff)
- firstRotorSpeed-=firstRotorSpeedCoeff;
- }
- if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::Subtract)){ // Reduce trimmer velocity
- /*
- * The Subtract key is the '-' on the numPad
- */
- if(trimmerValue > trimmerCoeff && trimmerPosition == 1 ){
- trimmerValue-=trimmerCoeff;
- prefixe = 0x170000000000;
- }
- else if(trimmerValue == trimmerCoeff && trimmerPosition == 1){
- trimmerValue = 0x0000;
- prefixe = 0x150000000000;
- trimmerPosition = 0;
- }
- else if(trimmerValue <= 0xF800-trimmerCoeff && trimmerPosition != 1){
- trimmerValue+=trimmerCoeff;
- prefixe = 0x140000000000;
- trimmerPosition = -1;
- }
- }
- if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::Add)){ // Increase trimmer velocity
- /*
- * The Add key is the '+' on numPad
- */
- if(trimmerValue > trimmerCoeff && trimmerPosition == -1 ){
- trimmerValue-=trimmerCoeff;
- prefixe = 0x140000000000;
- }
- else if(trimmerValue == trimmerCoeff && trimmerPosition == -1){
- trimmerValue = 0x0000;
- prefixe = 0x150000000000;
- trimmerPosition = 0;
- }
- else if(trimmerValue <= 0xF800-trimmerCoeff && trimmerPosition != -1){
- trimmerValue+=trimmerCoeff;
- prefixe = 0x170000000000;
- trimmerPosition = 1;
- }
- }
- }
- App.Display();
- if(clock.GetElapsedTime() > 0.5){ // Timer To Avoid Request Spamming
- command = prefixe + firstRotorSpeed + trimmerValue + suffixe;
- std::cout << intToChar(command) << std::endl;
- status = write(s, intToChar(command), 14); // We know that we have to send 14 bytes by analysing the requests
- clock.Reset(); // To reset timer so GetElapsedTime can work again
- }
- }
- }else{
- std::cout << "Connection Failed" << std::endl;
- }
- close(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement