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 <interfaces/JoystickInterface.h>
27 #include <logging/console.h>
28 #include <utils/system/argparser.h>
29 #include <utils/system/getkey.h>
30 #include <utils/time/time.h>
31 
32 #include <cstdio>
33 #include <cstdlib>
34 
35 using namespace fawkes;
36 
37 bool quit = false;
38 
39 void
40 print_usage(const char *program_name)
41 {
42  printf("Usage: %s [-h] [-r host[:port]]\n"
43  " -h This help message\n"
44  " -r host[:port] Remote host (and optionally port) to connect to\n"
45  " -d device Joystick device to use\n"
46  " -l Start in logging mode - print data read from bb\n",
47  program_name);
48 }
49 
50 /** Config tool main.
51  * @param argc argument count
52  * @param argv arguments
53  */
54 int
55 main(int argc, char **argv)
56 {
57  try {
58  ArgumentParser argp(argc, argv, "hr:");
59 
60  if (argp.has_arg("h")) {
61  print_usage(argv[0]);
62  exit(0);
63  }
64  ConsoleLogger logger;
65 
66  char * host = (char *)"localhost";
67  unsigned short int port = 1910;
68  bool free_host = argp.parse_hostport("r", &host, &port);
69 
70  JoystickRemoteBlackBoardPoster jbp(host, port, &logger);
71 
72  jbp.joystick_plugged(3, 10);
73  float axis[3], new_axis[3];
74  unsigned int button, new_button;
75  Time last, now;
76 
77  axis[0] = axis[1] = 0.;
78  axis[2] = 0.5;
79  new_axis[0] = new_axis[1] = new_axis[2] = 0.;
80  button = new_button = 0;
81 
82  last.stamp();
83 
84  char key = 0;
85  int wait_time = 5;
86  while (key != 'q') {
87  key = getkey(wait_time);
88  //printf("Key: %u = %u\n", key, key);
89  if (key != 0) {
90  now.stamp();
91  if ((now - &last) < 0.5) {
92  wait_time = 1;
93  }
94  last.stamp();
95  }
96  if (key == 0) {
97  wait_time = 5;
98  new_axis[0] = new_axis[1] = 0;
99  new_button = 0;
100 
101  } else if (key == 27) {
102  key = getkey();
103  if (key == 0) {
104  // Escape key
105  new_axis[0] = new_axis[1] = 0;
106  new_button = 0;
107  } else {
108  if (key != 91)
109  continue;
110 
111  key = getkey();
112  if (key == 0)
113  continue;
114 
115  switch (key) {
116  case 65: new_axis[0] = +1.; break;
117  case 66: new_axis[0] = -1.; break;
118  case 67: new_axis[1] = -1.; break;
119  case 68: new_axis[1] = +1.; break;
120  default: continue;
121  }
122  }
123  } else if (key == '+') {
124  if ((axis[2] + 0.1) <= 1.0) {
125  new_axis[2] += 0.1;
126  } else {
127  new_axis[2] = 1.;
128  }
129  } else if (key == '-') {
130  if ((axis[2] - 0.1) >= 0.) {
131  new_axis[2] -= 0.1;
132  } else {
133  new_axis[2] = 0.;
134  }
135  } else if (key == '1') {
136  new_button = JoystickInterface::BUTTON_1;
137  } else if (key == ' ') {
138  new_button = JoystickInterface::BUTTON_1;
139  } else if (key == '2') {
140  new_button = JoystickInterface::BUTTON_2;
141  } else if (key == '3') {
142  new_button = JoystickInterface::BUTTON_3;
143  } else if (key == '4') {
144  new_button = JoystickInterface::BUTTON_4;
145  } else if (key == '5') {
146  new_button = JoystickInterface::BUTTON_5;
147  } else if (key == '6') {
148  new_button = JoystickInterface::BUTTON_6;
149  } else if (key == '7') {
150  new_button = JoystickInterface::BUTTON_7;
151  } else if (key == '8') {
152  new_button = JoystickInterface::BUTTON_8;
153  } else if (key == '9') {
154  new_button = JoystickInterface::BUTTON_9;
155  } else if (key == '0') {
156  new_button = JoystickInterface::BUTTON_10;
157  }
158 
159  if ((axis[0] != new_axis[0]) || (axis[1] != new_axis[1]) || (axis[2] != new_axis[2])
160  || (button != new_button)) {
161  axis[0] = new_axis[0];
162  axis[1] = new_axis[1];
163  axis[2] = new_axis[2];
164  button = new_button;
165  jbp.joystick_changed(button, axis);
166  }
167  }
168 
169  jbp.joystick_unplugged();
170 
171  if (free_host)
172  free(host);
173 
174  } catch (UnknownArgumentException &e) {
175  printf("Error: Unknown Argument\n\n");
176  print_usage(argv[0]);
177  exit(0);
178  }
179 
180  return 0;
181 }
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:63
A class for handling time.
Definition: time.h:92
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:68
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:704
static const uint32_t BUTTON_3
BUTTON_3 constant.
Thrown if unknown argument was supplied.
Definition: argparser.h:38
static const uint32_t BUTTON_5
BUTTON_5 constant.