Fawkes API  Fawkes Development Version
ffkbjoystick.cpp
00001 
00002 /***************************************************************************
00003  *  ffkbjoystick.cpp - Keyboard joystick emulation
00004  *
00005  *  Created: Sat Jan 29 12:04:07 2011
00006  *  Copyright  2006-2011  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "remote_bb_poster.h"
00024 
00025 #include <core/exceptions/system.h>
00026 #include <utils/system/argparser.h>
00027 #include <logging/console.h>
00028 #include <utils/system/getkey.h>
00029 #include <utils/time/time.h>
00030 
00031 #include <cstdlib>
00032 #include <cstdio>
00033 
00034 #include <interfaces/JoystickInterface.h>
00035 
00036 using namespace fawkes;
00037 
00038 bool quit = false;
00039 
00040 void
00041 print_usage(const char *program_name)
00042 {
00043   printf("Usage: %s [-h] [-r host[:port]]\n"
00044          " -h              This help message\n"
00045          " -r host[:port]  Remote host (and optionally port) to connect to\n"
00046          " -d device       Joystick device to use\n"
00047          " -l              Start in logging mode - print data read from bb\n",
00048          program_name);
00049 }
00050 
00051 
00052 
00053 /** Config tool main.
00054  * @param argc argument count
00055  * @param argv arguments
00056  */
00057 int
00058 main(int argc, char **argv)
00059 {
00060   try {
00061     ArgumentParser argp(argc, argv, "hr:");
00062     
00063     if ( argp.has_arg("h") ) {
00064       print_usage(argv[0]);
00065       exit(0);
00066     }
00067     ConsoleLogger logger;
00068 
00069     char *host = (char *)"localhost";
00070     unsigned short int port = 1910;
00071     bool free_host = argp.parse_hostport("r", &host, &port);
00072 
00073     JoystickRemoteBlackBoardPoster jbp(host, port, &logger);
00074 
00075     jbp.joystick_plugged(3, 10);
00076     float axis[3], new_axis[3];
00077     unsigned int button, new_button;
00078     Time last, now;
00079 
00080     axis[0] = axis[1] = 0.;
00081     axis[2] = 0.5;
00082     new_axis[0] = new_axis[1] = new_axis[2] = 0.;
00083     button = new_button = 0;
00084 
00085     last.stamp();
00086 
00087     char key = 0;
00088     int wait_time = 5;
00089     while (key != 'q') {
00090       key = getkey(wait_time);
00091       //printf("Key: %u = %u\n", key, key);
00092       if (key != 0) {
00093         now.stamp();
00094         if ( (now - &last) < 0.5) {
00095           wait_time = 1;
00096         }
00097         last.stamp();
00098       }
00099       if (key == 0) {
00100         wait_time = 5;
00101         new_axis[0] = new_axis[1] = 0;
00102         new_button = 0;
00103 
00104       } else if (key == 27) {
00105         key = getkey();
00106         if (key == 0) {
00107           // Escape key
00108           new_axis[0] = new_axis[1] = 0;
00109           new_button = 0;
00110         } else {
00111           if (key != 91) continue;
00112 
00113           key = getkey();
00114           if (key == 0) continue;
00115 
00116           switch (key) {
00117           case 65: new_axis[0] = +1.; break;
00118           case 66: new_axis[0] = -1.; break;
00119           case 67: new_axis[1] = -1.; break;
00120           case 68: new_axis[1] = +1.; break;
00121           default: continue;
00122           }
00123         }
00124       } else if (key == '+') {
00125         if ((axis[2] + 0.1) <= 1.0) {
00126           new_axis[2] += 0.1;
00127         } else {
00128           new_axis[2]  = 1.;
00129         }
00130       } else if (key == '-') {
00131         if ((axis[2] - 0.1) >= 0.) {
00132           new_axis[2] -= 0.1;
00133         } else {
00134           new_axis[2]  = 0.;
00135         }
00136       } else if (key == '1') {
00137         new_button = JoystickInterface::BUTTON_1;
00138       } else if (key == ' ') {
00139         new_button = JoystickInterface::BUTTON_1;
00140       } else if (key == '2') {
00141         new_button = JoystickInterface::BUTTON_2;
00142       } else if (key == '3') {
00143         new_button = JoystickInterface::BUTTON_3;
00144       } else if (key == '4') {
00145         new_button = JoystickInterface::BUTTON_4;
00146       } else if (key == '5') {
00147         new_button = JoystickInterface::BUTTON_5;
00148       } else if (key == '6') {
00149         new_button = JoystickInterface::BUTTON_6;
00150       } else if (key == '7') {
00151         new_button = JoystickInterface::BUTTON_7;
00152       } else if (key == '8') {
00153         new_button = JoystickInterface::BUTTON_8;
00154       } else if (key == '9') {
00155         new_button = JoystickInterface::BUTTON_9;
00156       } else if (key == '0') {
00157         new_button = JoystickInterface::BUTTON_10;
00158       }
00159 
00160       if ((axis[0] != new_axis[0]) || (axis[1] != new_axis[1]) ||
00161           (axis[2] != new_axis[2]) || (button != new_button))
00162       {
00163         axis[0] = new_axis[0];
00164         axis[1] = new_axis[1];
00165         axis[2] = new_axis[2];
00166         button    = new_button;
00167         jbp.joystick_changed(button, axis);
00168       }
00169     }
00170 
00171     jbp.joystick_unplugged();
00172 
00173     if (free_host)  free(host);
00174 
00175   } catch (UnknownArgumentException e) {
00176     printf("Error: Unknown Argument\n\n");
00177     print_usage(argv[0]);
00178     exit(0);
00179   }
00180 
00181   return 0;
00182 }