Fawkes API  Fawkes Development Version
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 uint32 bit field.
00039     
00040  * @ingroup FawkesInterfaces
00041  */
00042 
00043 
00044 /** BUTTON_1 constant */
00045 const uint32_t JoystickInterface::BUTTON_1 = 1u;
00046 /** BUTTON_2 constant */
00047 const uint32_t JoystickInterface::BUTTON_2 = 2u;
00048 /** BUTTON_3 constant */
00049 const uint32_t JoystickInterface::BUTTON_3 = 4u;
00050 /** BUTTON_4 constant */
00051 const uint32_t JoystickInterface::BUTTON_4 = 8u;
00052 /** BUTTON_5 constant */
00053 const uint32_t JoystickInterface::BUTTON_5 = 16u;
00054 /** BUTTON_6 constant */
00055 const uint32_t JoystickInterface::BUTTON_6 = 32u;
00056 /** BUTTON_7 constant */
00057 const uint32_t JoystickInterface::BUTTON_7 = 64u;
00058 /** BUTTON_8 constant */
00059 const uint32_t JoystickInterface::BUTTON_8 = 128u;
00060 /** BUTTON_9 constant */
00061 const uint32_t JoystickInterface::BUTTON_9 = 256u;
00062 /** BUTTON_10 constant */
00063 const uint32_t JoystickInterface::BUTTON_10 = 512u;
00064 /** BUTTON_11 constant */
00065 const uint32_t JoystickInterface::BUTTON_11 = 1024u;
00066 /** BUTTON_12 constant */
00067 const uint32_t JoystickInterface::BUTTON_12 = 2048u;
00068 /** BUTTON_13 constant */
00069 const uint32_t JoystickInterface::BUTTON_13 = 4096u;
00070 /** BUTTON_14 constant */
00071 const uint32_t JoystickInterface::BUTTON_14 = 8192u;
00072 /** BUTTON_15 constant */
00073 const uint32_t JoystickInterface::BUTTON_15 = 16384u;
00074 /** BUTTON_16 constant */
00075 const uint32_t JoystickInterface::BUTTON_16 = 32768u;
00076 /** BUTTON_17 constant */
00077 const uint32_t JoystickInterface::BUTTON_17 = 65536u;
00078 /** BUTTON_18 constant */
00079 const uint32_t JoystickInterface::BUTTON_18 = 131072u;
00080 /** BUTTON_19 constant */
00081 const uint32_t JoystickInterface::BUTTON_19 = 262144u;
00082 /** BUTTON_20 constant */
00083 const uint32_t JoystickInterface::BUTTON_20 = 524288u;
00084 /** BUTTON_21 constant */
00085 const uint32_t JoystickInterface::BUTTON_21 = 1048576u;
00086 /** BUTTON_22 constant */
00087 const uint32_t JoystickInterface::BUTTON_22 = 2097152u;
00088 /** BUTTON_23 constant */
00089 const uint32_t JoystickInterface::BUTTON_23 = 4194304u;
00090 /** BUTTON_24 constant */
00091 const uint32_t JoystickInterface::BUTTON_24 = 8388608u;
00092 /** BUTTON_25 constant */
00093 const uint32_t JoystickInterface::BUTTON_25 = 16777216u;
00094 /** BUTTON_26 constant */
00095 const uint32_t JoystickInterface::BUTTON_26 = 33554432u;
00096 /** BUTTON_27 constant */
00097 const uint32_t JoystickInterface::BUTTON_27 = 67108864u;
00098 /** BUTTON_28 constant */
00099 const uint32_t JoystickInterface::BUTTON_28 = 134217728u;
00100 /** BUTTON_29 constant */
00101 const uint32_t JoystickInterface::BUTTON_29 = 268435456u;
00102 /** BUTTON_30 constant */
00103 const uint32_t JoystickInterface::BUTTON_30 = 536870912u;
00104 /** BUTTON_31 constant */
00105 const uint32_t JoystickInterface::BUTTON_31 = 1073741824u;
00106 /** BUTTON_32 constant */
00107 const uint32_t 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   data_ts   = (interface_data_ts_t *)data_ptr;
00116   memset(data_ptr, 0, data_size);
00117   add_fieldinfo(IFT_BYTE, "num_axes", 1, &data->num_axes);
00118   add_fieldinfo(IFT_BYTE, "num_buttons", 1, &data->num_buttons);
00119   add_fieldinfo(IFT_UINT32, "pressed_buttons", 1, &data->pressed_buttons);
00120   add_fieldinfo(IFT_FLOAT, "axis_x", 4, &data->axis_x);
00121   add_fieldinfo(IFT_FLOAT, "axis_y", 4, &data->axis_y);
00122   unsigned char tmp_hash[] = {0x20, 0xe5, 0x9c, 0x19, 0x6e, 0xd2, 0xcf, 0xcc, 0xf2, 0x5d, 0x70, 0x88, 0x52, 0x66, 0x7a, 0x1e};
00123   set_hash(tmp_hash);
00124 }
00125 
00126 /** Destructor */
00127 JoystickInterface::~JoystickInterface()
00128 {
00129   free(data_ptr);
00130 }
00131 /* Methods */
00132 /** Get num_axes value.
00133  * 
00134       The number of axes of this joystick
00135     
00136  * @return num_axes value
00137  */
00138 uint8_t
00139 JoystickInterface::num_axes() const
00140 {
00141   return data->num_axes;
00142 }
00143 
00144 /** Get maximum length of num_axes value.
00145  * @return length of num_axes value, can be length of the array or number of 
00146  * maximum number of characters for a string
00147  */
00148 size_t
00149 JoystickInterface::maxlenof_num_axes() const
00150 {
00151   return 1;
00152 }
00153 
00154 /** Set num_axes value.
00155  * 
00156       The number of axes of this joystick
00157     
00158  * @param new_num_axes new num_axes value
00159  */
00160 void
00161 JoystickInterface::set_num_axes(const uint8_t new_num_axes)
00162 {
00163   data->num_axes = new_num_axes;
00164   data_changed = true;
00165 }
00166 
00167 /** Get num_buttons value.
00168  * 
00169       The number of buttons of this joystick.
00170     
00171  * @return num_buttons value
00172  */
00173 uint8_t
00174 JoystickInterface::num_buttons() const
00175 {
00176   return data->num_buttons;
00177 }
00178 
00179 /** Get maximum length of num_buttons value.
00180  * @return length of num_buttons value, can be length of the array or number of 
00181  * maximum number of characters for a string
00182  */
00183 size_t
00184 JoystickInterface::maxlenof_num_buttons() const
00185 {
00186   return 1;
00187 }
00188 
00189 /** Set num_buttons value.
00190  * 
00191       The number of buttons of this joystick.
00192     
00193  * @param new_num_buttons new num_buttons value
00194  */
00195 void
00196 JoystickInterface::set_num_buttons(const uint8_t new_num_buttons)
00197 {
00198   data->num_buttons = new_num_buttons;
00199   data_changed = true;
00200 }
00201 
00202 /** Get pressed_buttons value.
00203  * 
00204       A bit field of enabled buttons. For each currently clicked button the
00205       corresponding bit is set to 1. Use the BUTTON_* constants for bit-wise
00206       comparisons.
00207     
00208  * @return pressed_buttons value
00209  */
00210 uint32_t
00211 JoystickInterface::pressed_buttons() const
00212 {
00213   return data->pressed_buttons;
00214 }
00215 
00216 /** Get maximum length of pressed_buttons value.
00217  * @return length of pressed_buttons value, can be length of the array or number of 
00218  * maximum number of characters for a string
00219  */
00220 size_t
00221 JoystickInterface::maxlenof_pressed_buttons() const
00222 {
00223   return 1;
00224 }
00225 
00226 /** Set pressed_buttons value.
00227  * 
00228       A bit field of enabled buttons. For each currently clicked button the
00229       corresponding bit is set to 1. Use the BUTTON_* constants for bit-wise
00230       comparisons.
00231     
00232  * @param new_pressed_buttons new pressed_buttons value
00233  */
00234 void
00235 JoystickInterface::set_pressed_buttons(const uint32_t new_pressed_buttons)
00236 {
00237   data->pressed_buttons = new_pressed_buttons;
00238   data_changed = true;
00239 }
00240 
00241 /** Get axis_x value.
00242  * X values of axes
00243  * @return axis_x value
00244  */
00245 float *
00246 JoystickInterface::axis_x() const
00247 {
00248   return data->axis_x;
00249 }
00250 
00251 /** Get axis_x value at given index.
00252  * X values of axes
00253  * @param index index of value
00254  * @return axis_x value
00255  * @exception Exception thrown if index is out of bounds
00256  */
00257 float
00258 JoystickInterface::axis_x(unsigned int index) const
00259 {
00260   if (index > 4) {
00261     throw Exception("Index value %u out of bounds (0..4)", index);
00262   }
00263   return data->axis_x[index];
00264 }
00265 
00266 /** Get maximum length of axis_x value.
00267  * @return length of axis_x value, can be length of the array or number of 
00268  * maximum number of characters for a string
00269  */
00270 size_t
00271 JoystickInterface::maxlenof_axis_x() const
00272 {
00273   return 4;
00274 }
00275 
00276 /** Set axis_x value.
00277  * X values of axes
00278  * @param new_axis_x new axis_x value
00279  */
00280 void
00281 JoystickInterface::set_axis_x(const float * new_axis_x)
00282 {
00283   memcpy(data->axis_x, new_axis_x, sizeof(float) * 4);
00284   data_changed = true;
00285 }
00286 
00287 /** Set axis_x value at given index.
00288  * X values of axes
00289  * @param new_axis_x new axis_x value
00290  * @param index index for of the value
00291  */
00292 void
00293 JoystickInterface::set_axis_x(unsigned int index, const float new_axis_x)
00294 {
00295   if (index > 4) {
00296     throw Exception("Index value %u out of bounds (0..4)", index);
00297   }
00298   data->axis_x[index] = new_axis_x;
00299 }
00300 /** Get axis_y value.
00301  * Y values of axes
00302  * @return axis_y value
00303  */
00304 float *
00305 JoystickInterface::axis_y() const
00306 {
00307   return data->axis_y;
00308 }
00309 
00310 /** Get axis_y value at given index.
00311  * Y values of axes
00312  * @param index index of value
00313  * @return axis_y value
00314  * @exception Exception thrown if index is out of bounds
00315  */
00316 float
00317 JoystickInterface::axis_y(unsigned int index) const
00318 {
00319   if (index > 4) {
00320     throw Exception("Index value %u out of bounds (0..4)", index);
00321   }
00322   return data->axis_y[index];
00323 }
00324 
00325 /** Get maximum length of axis_y value.
00326  * @return length of axis_y value, can be length of the array or number of 
00327  * maximum number of characters for a string
00328  */
00329 size_t
00330 JoystickInterface::maxlenof_axis_y() const
00331 {
00332   return 4;
00333 }
00334 
00335 /** Set axis_y value.
00336  * Y values of axes
00337  * @param new_axis_y new axis_y value
00338  */
00339 void
00340 JoystickInterface::set_axis_y(const float * new_axis_y)
00341 {
00342   memcpy(data->axis_y, new_axis_y, sizeof(float) * 4);
00343   data_changed = true;
00344 }
00345 
00346 /** Set axis_y value at given index.
00347  * Y values of axes
00348  * @param new_axis_y new axis_y value
00349  * @param index index for of the value
00350  */
00351 void
00352 JoystickInterface::set_axis_y(unsigned int index, const float new_axis_y)
00353 {
00354   if (index > 4) {
00355     throw Exception("Index value %u out of bounds (0..4)", index);
00356   }
00357   data->axis_y[index] = new_axis_y;
00358 }
00359 /* =========== message create =========== */
00360 Message *
00361 JoystickInterface::create_message(const char *type) const
00362 {
00363   throw UnknownTypeException("The given type '%s' does not match any known "
00364                              "message type for this interface type.", type);
00365 }
00366 
00367 
00368 /** Copy values from other interface.
00369  * @param other other interface to copy values from
00370  */
00371 void
00372 JoystickInterface::copy_values(const Interface *other)
00373 {
00374   const JoystickInterface *oi = dynamic_cast<const JoystickInterface *>(other);
00375   if (oi == NULL) {
00376     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00377                                 type(), other->type());
00378   }
00379   memcpy(data, oi->data, sizeof(JoystickInterface_data_t));
00380 }
00381 
00382 const char *
00383 JoystickInterface::enum_tostring(const char *enumtype, int val) const
00384 {
00385   throw UnknownTypeException("Unknown enum type %s", enumtype);
00386 }
00387 
00388 /* =========== messages =========== */
00389 /** Check if message is valid and can be enqueued.
00390  * @param message Message to check
00391  * @return true if the message is valid, false otherwise.
00392  */
00393 bool
00394 JoystickInterface::message_valid(const Message *message) const
00395 {
00396   return false;
00397 }
00398 
00399 /// @cond INTERNALS
00400 EXPORT_INTERFACE(JoystickInterface)
00401 /// @endcond
00402 
00403 
00404 } // end namespace fawkes