JoystickInterface.cpp

00001
00002 /***************************************************************************
00003  *  JoystickInterface.cpp - Fawkes BlackBoard Interface - JoystickInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008  Tim Niemueller
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. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <interfaces/JoystickInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class JoystickInterface <interfaces/JoystickInterface.h>
00034  * JoystickInterface Fawkes BlackBoard Interface.
00035  * 
00036       This interface provides access to a joystick. It provides up to
00037       five axes, where each has a X and a Y value between -1.0 and 1.0.
00038       Up to 32 buttons are support via an unsigned int bit field.
00039     
00040  * @ingroup FawkesInterfaces
00041  */
00042
00043 
00044 /** BUTTON_1 constant */
00045 const unsigned int JoystickInterface::BUTTON_1 = 1;
00046 /** BUTTON_2 constant */
00047 const unsigned int JoystickInterface::BUTTON_2 = 2;
00048 /** BUTTON_3 constant */
00049 const unsigned int JoystickInterface::BUTTON_3 = 4;
00050 /** BUTTON_4 constant */
00051 const unsigned int JoystickInterface::BUTTON_4 = 8;
00052 /** BUTTON_5 constant */
00053 const unsigned int JoystickInterface::BUTTON_5 = 16;
00054 /** BUTTON_6 constant */
00055 const unsigned int JoystickInterface::BUTTON_6 = 32;
00056 /** BUTTON_7 constant */
00057 const unsigned int JoystickInterface::BUTTON_7 = 64;
00058 /** BUTTON_8 constant */
00059 const unsigned int JoystickInterface::BUTTON_8 = 128;
00060 /** BUTTON_9 constant */
00061 const unsigned int JoystickInterface::BUTTON_9 = 256;
00062 /** BUTTON_10 constant */
00063 const unsigned int JoystickInterface::BUTTON_10 = 512;
00064 /** BUTTON_11 constant */
00065 const unsigned int JoystickInterface::BUTTON_11 = 1024;
00066 /** BUTTON_12 constant */
00067 const unsigned int JoystickInterface::BUTTON_12 = 2048;
00068 /** BUTTON_13 constant */
00069 const unsigned int JoystickInterface::BUTTON_13 = 4096;
00070 /** BUTTON_14 constant */
00071 const unsigned int JoystickInterface::BUTTON_14 = 8192;
00072 /** BUTTON_15 constant */
00073 const unsigned int JoystickInterface::BUTTON_15 = 16384;
00074 /** BUTTON_16 constant */
00075 const unsigned int JoystickInterface::BUTTON_16 = 32768;
00076 /** BUTTON_17 constant */
00077 const unsigned int JoystickInterface::BUTTON_17 = 65536;
00078 /** BUTTON_18 constant */
00079 const unsigned int JoystickInterface::BUTTON_18 = 131072;
00080 /** BUTTON_19 constant */
00081 const unsigned int JoystickInterface::BUTTON_19 = 262144;
00082 /** BUTTON_20 constant */
00083 const unsigned int JoystickInterface::BUTTON_20 = 524288;
00084 /** BUTTON_21 constant */
00085 const unsigned int JoystickInterface::BUTTON_21 = 1048576;
00086 /** BUTTON_22 constant */
00087 const unsigned int JoystickInterface::BUTTON_22 = 2097152;
00088 /** BUTTON_23 constant */
00089 const unsigned int JoystickInterface::BUTTON_23 = 4194304;
00090 /** BUTTON_24 constant */
00091 const unsigned int JoystickInterface::BUTTON_24 = 8388608;
00092 /** BUTTON_25 constant */
00093 const unsigned int JoystickInterface::BUTTON_25 = 16777216;
00094 /** BUTTON_26 constant */
00095 const unsigned int JoystickInterface::BUTTON_26 = 33554432;
00096 /** BUTTON_27 constant */
00097 const unsigned int JoystickInterface::BUTTON_27 = 67108864;
00098 /** BUTTON_28 constant */
00099 const unsigned int JoystickInterface::BUTTON_28 = 134217728;
00100 /** BUTTON_29 constant */
00101 const unsigned int JoystickInterface::BUTTON_29 = 268435456;
00102 /** BUTTON_30 constant */
00103 const unsigned int JoystickInterface::BUTTON_30 = 536870912;
00104 /** BUTTON_31 constant */
00105 const unsigned int JoystickInterface::BUTTON_31 = 1073741824;
00106 /** BUTTON_32 constant */
00107 const unsigned int JoystickInterface::BUTTON_32 = 2147483648U;
00108 
00109 /** Constructor */
00110 JoystickInterface::JoystickInterface() : Interface()
00111 {
00112   data_size = sizeof(JoystickInterface_data_t);
00113   data_ptr  = malloc(data_size);
00114   data      = (JoystickInterface_data_t *)data_ptr;
00115   memset(data_ptr, 0, data_size);
00116   add_fieldinfo(IFT_BYTE, "num_axes", 1, &data->num_axes);
00117   add_fieldinfo(IFT_BYTE, "num_buttons", 1, &data->num_buttons);
00118   add_fieldinfo(IFT_UINT, "pressed_buttons", 1, &data->pressed_buttons);
00119   add_fieldinfo(IFT_FLOAT, "axis_x", 4, &data->axis_x);
00120   add_fieldinfo(IFT_FLOAT, "axis_y", 4, &data->axis_y);
00121   unsigned char tmp_hash[] = {0xc1, 0x79, 0x75, 0x4e, 0x3e, 0xfa, 0x1, 0x53, 0x18, 0xb0, 0x9d, 0x9b, 0x43, 0x41, 0xa, 0x86};
00122   set_hash(tmp_hash);
00123 }
00124 
00125 /** Destructor */
00126 JoystickInterface::~JoystickInterface()
00127 {
00128   free(data_ptr);
00129 }
00130 /* Methods */
00131 /** Get num_axes value.
00132  * 
00133       The number of axes of this joystick
00134     
00135  * @return num_axes value
00136  */
00137 unsigned char
00138 JoystickInterface::num_axes() const
00139 {
00140   return data->num_axes;
00141 }
00142 
00143 /** Get maximum length of num_axes value.
00144  * @return length of num_axes value, can be length of the array or number of 
00145  * maximum number of characters for a string
00146  */
00147 size_t
00148 JoystickInterface::maxlenof_num_axes() const
00149 {
00150   return 1;
00151 }
00152 
00153 /** Set num_axes value.
00154  * 
00155       The number of axes of this joystick
00156     
00157  * @param new_num_axes new num_axes value
00158  */
00159 void
00160 JoystickInterface::set_num_axes(const unsigned char new_num_axes)
00161 {
00162   data->num_axes = new_num_axes;
00163 }
00164 
00165 /** Get num_buttons value.
00166  * 
00167       The number of buttons of this joystick.
00168     
00169  * @return num_buttons value
00170  */
00171 unsigned char
00172 JoystickInterface::num_buttons() const
00173 {
00174   return data->num_buttons;
00175 }
00176 
00177 /** Get maximum length of num_buttons value.
00178  * @return length of num_buttons value, can be length of the array or number of 
00179  * maximum number of characters for a string
00180  */
00181 size_t
00182 JoystickInterface::maxlenof_num_buttons() const
00183 {
00184   return 1;
00185 }
00186 
00187 /** Set num_buttons value.
00188  * 
00189       The number of buttons of this joystick.
00190     
00191  * @param new_num_buttons new num_buttons value
00192  */
00193 void
00194 JoystickInterface::set_num_buttons(const unsigned char new_num_buttons)
00195 {
00196   data->num_buttons = new_num_buttons;
00197 }
00198 
00199 /** Get pressed_buttons value.
00200  * 
00201       A bit field of enabled buttons. For each currently clicked button the
00202       corresponding bit is set to 1. Use the BUTTON_* constants for bit-wise
00203       comparisons.
00204     
00205  * @return pressed_buttons value
00206  */
00207 unsigned int
00208 JoystickInterface::pressed_buttons() const
00209 {
00210   return data->pressed_buttons;
00211 }
00212 
00213 /** Get maximum length of pressed_buttons value.
00214  * @return length of pressed_buttons value, can be length of the array or number of 
00215  * maximum number of characters for a string
00216  */
00217 size_t
00218 JoystickInterface::maxlenof_pressed_buttons() const
00219 {
00220   return 1;
00221 }
00222 
00223 /** Set pressed_buttons value.
00224  * 
00225       A bit field of enabled buttons. For each currently clicked button the
00226       corresponding bit is set to 1. Use the BUTTON_* constants for bit-wise
00227       comparisons.
00228     
00229  * @param new_pressed_buttons new pressed_buttons value
00230  */
00231 void
00232 JoystickInterface::set_pressed_buttons(const unsigned int new_pressed_buttons)
00233 {
00234   data->pressed_buttons = new_pressed_buttons;
00235 }
00236 
00237 /** Get axis_x value.
00238  * X values of axes
00239  * @return axis_x value
00240  */
00241 float *
00242 JoystickInterface::axis_x() const
00243 {
00244   return data->axis_x;
00245 }
00246 
00247 /** Get axis_x value at given index.
00248  * X values of axes
00249  * @param index index of value
00250  * @return axis_x value
00251  * @exception Exception thrown if index is out of bounds
00252  */
00253 float
00254 JoystickInterface::axis_x(unsigned int index) const
00255 {
00256   if (index > 4) {
00257     throw Exception("Index value %u out of bounds (0..4)", index);
00258   }
00259   return data->axis_x[index];
00260 }
00261 
00262 /** Get maximum length of axis_x value.
00263  * @return length of axis_x value, can be length of the array or number of 
00264  * maximum number of characters for a string
00265  */
00266 size_t
00267 JoystickInterface::maxlenof_axis_x() const
00268 {
00269   return 4;
00270 }
00271 
00272 /** Set axis_x value.
00273  * X values of axes
00274  * @param new_axis_x new axis_x value
00275  */
00276 void
00277 JoystickInterface::set_axis_x(const float * new_axis_x)
00278 {
00279   memcpy(data->axis_x, new_axis_x, sizeof(float) * 4);
00280 }
00281 
00282 /** Set axis_x value at given index.
00283  * X values of axes
00284  * @param new_axis_x new axis_x value
00285  * @param index index for of the value
00286  */
00287 void
00288 JoystickInterface::set_axis_x(unsigned int index, const float new_axis_x)
00289 {
00290   if (index > 4) {
00291     throw Exception("Index value %u out of bounds (0..4)", index);
00292   }
00293   data->axis_x[index] = new_axis_x;
00294 }
00295 /** Get axis_y value.
00296  * Y values of axes
00297  * @return axis_y value
00298  */
00299 float *
00300 JoystickInterface::axis_y() const
00301 {
00302   return data->axis_y;
00303 }
00304 
00305 /** Get axis_y value at given index.
00306  * Y values of axes
00307  * @param index index of value
00308  * @return axis_y value
00309  * @exception Exception thrown if index is out of bounds
00310  */
00311 float
00312 JoystickInterface::axis_y(unsigned int index) const
00313 {
00314   if (index > 4) {
00315     throw Exception("Index value %u out of bounds (0..4)", index);
00316   }
00317   return data->axis_y[index];
00318 }
00319 
00320 /** Get maximum length of axis_y value.
00321  * @return length of axis_y value, can be length of the array or number of 
00322  * maximum number of characters for a string
00323  */
00324 size_t
00325 JoystickInterface::maxlenof_axis_y() const
00326 {
00327   return 4;
00328 }
00329 
00330 /** Set axis_y value.
00331  * Y values of axes
00332  * @param new_axis_y new axis_y value
00333  */
00334 void
00335 JoystickInterface::set_axis_y(const float * new_axis_y)
00336 {
00337   memcpy(data->axis_y, new_axis_y, sizeof(float) * 4);
00338 }
00339 
00340 /** Set axis_y value at given index.
00341  * Y values of axes
00342  * @param new_axis_y new axis_y value
00343  * @param index index for of the value
00344  */
00345 void
00346 JoystickInterface::set_axis_y(unsigned int index, const float new_axis_y)
00347 {
00348   if (index > 4) {
00349     throw Exception("Index value %u out of bounds (0..4)", index);
00350   }
00351   data->axis_y[index] = new_axis_y;
00352 }
00353 /* =========== message create =========== */
00354 Message *
00355 JoystickInterface::create_message(const char *type) const
00356 {
00357   throw UnknownTypeException("The given type '%s' does not match any known "
00358                              "message type for this interface type.", type);
00359 }
00360
00361 
00362 /** Copy values from other interface.
00363  * @param other other interface to copy values from
00364  */
00365 void
00366 JoystickInterface::copy_values(const Interface *other)
00367 {
00368   const JoystickInterface *oi = dynamic_cast<const JoystickInterface *>(other);
00369   if (oi == NULL) {
00370     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00371                                 type(), other->type());
00372   }
00373   memcpy(data, oi->data, sizeof(JoystickInterface_data_t));
00374 }
00375
00376 /* =========== messages =========== */
00377 /** Check if message is valid and can be enqueued.
00378  * @param message Message to check
00379  */
00380 bool
00381 JoystickInterface::message_valid(const Message *message) const
00382 {
00383   return false;
00384 }
00385 
00386 /// @cond INTERNALS
00387 EXPORT_INTERFACE(JoystickInterface)
00388 /// @endcond
00389 
00390
00391 } // end namespace fawkes