Fawkes API  Fawkes Development Version
ffkbjoystick.cpp
1 
2 /***************************************************************************
3  * ffkbjoystick.cpp - Keyboard joystick emulation
4  *
5  * Created: Sat Jan 29 12:04:07 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "remote_bb_poster.h"
24 
25 #include <core/exceptions/system.h>
26 #include <utils/system/argparser.h>
27 #include <logging/console.h>
28 #include <utils/system/getkey.h>
29 #include <utils/time/time.h>
30 
31 #include <cstdlib>
32 #include <cstdio>
33 
34 #include <interfaces/JoystickInterface.h>
35 
36 using namespace fawkes;
37 
38 bool quit = false;
39 
40 void
41 print_usage(const char *program_name)
42 {
43  printf("Usage: %s [-h] [-r host[:port]]\n"
44  " -h This help message\n"
45  " -r host[:port] Remote host (and optionally port) to connect to\n"
46  " -d device Joystick device to use\n"
47  " -l Start in logging mode - print data read from bb\n",
48  program_name);
49 }
50 
51 
52 
53 /** Config tool main.
54  * @param argc argument count
55  * @param argv arguments
56  */
57 int
58 main(int argc, char **argv)
59 {
60  try {
61  ArgumentParser argp(argc, argv, "hr:");
62 
63  if ( argp.has_arg("h") ) {
64  print_usage(argv[0]);
65  exit(0);
66  }
67  ConsoleLogger logger;
68 
69  char *host = (char *)"localhost";
70  unsigned short int port = 1910;
71  bool free_host = argp.parse_hostport("r", &host, &port);
72 
73  JoystickRemoteBlackBoardPoster jbp(host, port, &logger);
74 
75  jbp.joystick_plugged(3, 10);
76  float axis[3], new_axis[3];
77  unsigned int button, new_button;
78  Time last, now;
79 
80  axis[0] = axis[1] = 0.;
81  axis[2] = 0.5;
82  new_axis[0] = new_axis[1] = new_axis[2] = 0.;
83  button = new_button = 0;
84 
85  last.stamp();
86 
87  char key = 0;
88  int wait_time = 5;
89  while (key != 'q') {
90  key = getkey(wait_time);
91  //printf("Key: %u = %u\n", key, key);
92  if (key != 0) {
93  now.stamp();
94  if ( (now - &last) < 0.5) {
95  wait_time = 1;
96  }
97  last.stamp();
98  }
99  if (key == 0) {
100  wait_time = 5;
101  new_axis[0] = new_axis[1] = 0;
102  new_button = 0;
103 
104  } else if (key == 27) {
105  key = getkey();
106  if (key == 0) {
107  // Escape key
108  new_axis[0] = new_axis[1] = 0;
109  new_button = 0;
110  } else {
111  if (key != 91) continue;
112 
113  key = getkey();
114  if (key == 0) continue;
115 
116  switch (key) {
117  case 65: new_axis[0] = +1.; break;
118  case 66: new_axis[0] = -1.; break;
119  case 67: new_axis[1] = -1.; break;
120  case 68: new_axis[1] = +1.; break;
121  default: continue;
122  }
123  }
124  } else if (key == '+') {
125  if ((axis[2] + 0.1) <= 1.0) {
126  new_axis[2] += 0.1;
127  } else {
128  new_axis[2] = 1.;
129  }
130  } else if (key == '-') {
131  if ((axis[2] - 0.1) >= 0.) {
132  new_axis[2] -= 0.1;
133  } else {
134  new_axis[2] = 0.;
135  }
136  } else if (key == '1') {
137  new_button = JoystickInterface::BUTTON_1;
138  } else if (key == ' ') {
139  new_button = JoystickInterface::BUTTON_1;
140  } else if (key == '2') {
141  new_button = JoystickInterface::BUTTON_2;
142  } else if (key == '3') {
143  new_button = JoystickInterface::BUTTON_3;
144  } else if (key == '4') {
145  new_button = JoystickInterface::BUTTON_4;
146  } else if (key == '5') {
147  new_button = JoystickInterface::BUTTON_5;
148  } else if (key == '6') {
149  new_button = JoystickInterface::BUTTON_6;
150  } else if (key == '7') {
151  new_button = JoystickInterface::BUTTON_7;
152  } else if (key == '8') {
153  new_button = JoystickInterface::BUTTON_8;
154  } else if (key == '9') {
155  new_button = JoystickInterface::BUTTON_9;
156  } else if (key == '0') {
157  new_button = JoystickInterface::BUTTON_10;
158  }
159 
160  if ((axis[0] != new_axis[0]) || (axis[1] != new_axis[1]) ||
161  (axis[2] != new_axis[2]) || (button != new_button))
162  {
163  axis[0] = new_axis[0];
164  axis[1] = new_axis[1];
165  axis[2] = new_axis[2];
166  button = new_button;
167  jbp.joystick_changed(button, axis);
168  }
169  }
170 
171  jbp.joystick_unplugged();
172 
173  if (free_host) free(host);
174 
175  } catch (UnknownArgumentException e) {
176  printf("Error: Unknown Argument\n\n");
177  print_usage(argv[0]);
178  exit(0);
179  }
180 
181  return 0;
182 }
static const uint32_t BUTTON_4
BUTTON_4 constant.
static const uint32_t BUTTON_1
BUTTON_1 constant.
static const uint32_t BUTTON_7
BUTTON_7 constant.
Interface for logging to stderr.
Definition: console.h:36
Fawkes library namespace.
Parse command line arguments.
Definition: argparser.h:66
A class for handling time.
Definition: time.h:91
static const uint32_t BUTTON_8
BUTTON_8 constant.
char getkey(int timeout_decisecs)
Get value of a single key-press non-blocking.
Definition: getkey.cpp:70
static const uint32_t BUTTON_6
BUTTON_6 constant.
Glue to post new data to a RemoteBlackBoard.
static const uint32_t BUTTON_10
BUTTON_10 constant.
static const uint32_t BUTTON_2
BUTTON_2 constant.
static const uint32_t BUTTON_9
BUTTON_9 constant.
Time & stamp()
Set this time to the current time.
Definition: time.cpp:783
static const uint32_t BUTTON_3
BUTTON_3 constant.
Thrown if unknown argument was supplied.
Definition: argparser.h:40
static const uint32_t BUTTON_5
BUTTON_5 constant.