SwitchInterface.cpp

00001
00002 /***************************************************************************
00003  *  SwitchInterface.cpp - Fawkes BlackBoard Interface - SwitchInterface
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/SwitchInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class SwitchInterface <interfaces/SwitchInterface.h>
00034  * SwitchInterface Fawkes BlackBoard Interface.
00035  * 
00036       This interface provides access to LEDs. The interface controls
00037       an intensity value between 0.0 (off) and 1.0 (on, max
00038       intensity). LEDs that do not support intensity setting can only
00039       be set to on and off.
00040     
00041  * @ingroup FawkesInterfaces
00042  */
00043
00044
00045 
00046 /** Constructor */
00047 SwitchInterface::SwitchInterface() : Interface()
00048 {
00049   data_size = sizeof(SwitchInterface_data_t);
00050   data_ptr  = malloc(data_size);
00051   data      = (SwitchInterface_data_t *)data_ptr;
00052   memset(data_ptr, 0, data_size);
00053   add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00054   add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
00055   add_fieldinfo(IFT_FLOAT, "history", 1, &data->history);
00056   add_fieldinfo(IFT_UINT, "short_activations", 1, &data->short_activations);
00057   add_fieldinfo(IFT_UINT, "long_activations", 1, &data->long_activations);
00058   add_fieldinfo(IFT_UINT, "activation_count", 1, &data->activation_count);
00059   add_messageinfo("SetMessage");
00060   add_messageinfo("EnableSwitchMessage");
00061   add_messageinfo("DisableSwitchMessage");
00062   unsigned char tmp_hash[] = {0xcb, 0xd2, 0x25, 0x8e, 0x3e, 0x18, 0xd6, 0x3a, 0xe0, 0x12, 0x73, 0x27, 0x26, 0x75, 0x95, 0x56};
00063   set_hash(tmp_hash);
00064 }
00065 
00066 /** Destructor */
00067 SwitchInterface::~SwitchInterface()
00068 {
00069   free(data_ptr);
00070 }
00071 /* Methods */
00072 /** Get enabled value.
00073  * 
00074       True if the switch is currently enabled.
00075     
00076  * @return enabled value
00077  */
00078 bool
00079 SwitchInterface::is_enabled() const
00080 {
00081   return data->enabled;
00082 }
00083 
00084 /** Get maximum length of enabled value.
00085  * @return length of enabled value, can be length of the array or number of 
00086  * maximum number of characters for a string
00087  */
00088 size_t
00089 SwitchInterface::maxlenof_enabled() const
00090 {
00091   return 1;
00092 }
00093 
00094 /** Set enabled value.
00095  * 
00096       True if the switch is currently enabled.
00097     
00098  * @param new_enabled new enabled value
00099  */
00100 void
00101 SwitchInterface::set_enabled(const bool new_enabled)
00102 {
00103   data->enabled = new_enabled;
00104 }
00105 
00106 /** Get value value.
00107  * 
00108       If switches support multiple states these can be indicated with
00109       this value. For example for a switch that notes the intensity it
00110       could be a value in the valid range.
00111     
00112  * @return value value
00113  */
00114 float
00115 SwitchInterface::value() const
00116 {
00117   return data->value;
00118 }
00119 
00120 /** Get maximum length of value value.
00121  * @return length of value value, can be length of the array or number of 
00122  * maximum number of characters for a string
00123  */
00124 size_t
00125 SwitchInterface::maxlenof_value() const
00126 {
00127   return 1;
00128 }
00129 
00130 /** Set value value.
00131  * 
00132       If switches support multiple states these can be indicated with
00133       this value. For example for a switch that notes the intensity it
00134       could be a value in the valid range.
00135     
00136  * @param new_value new value value
00137  */
00138 void
00139 SwitchInterface::set_value(const float new_value)
00140 {
00141   data->value = new_value;
00142 }
00143 
00144 /** Get history value.
00145  * 
00146       This value records the number of seconds a switch has been
00147       enabled continuously -- or not. The time is recorded in
00148       seconds. A positive value indicates time the switch was turned
00149       on, a negative value indicates the time (when converted to the
00150       absolute value) the button has not been pressed. Zero means
00151       "just initialized".
00152     
00153  * @return history value
00154  */
00155 float
00156 SwitchInterface::history() const
00157 {
00158   return data->history;
00159 }
00160 
00161 /** Get maximum length of history value.
00162  * @return length of history value, can be length of the array or number of 
00163  * maximum number of characters for a string
00164  */
00165 size_t
00166 SwitchInterface::maxlenof_history() const
00167 {
00168   return 1;
00169 }
00170 
00171 /** Set history value.
00172  * 
00173       This value records the number of seconds a switch has been
00174       enabled continuously -- or not. The time is recorded in
00175       seconds. A positive value indicates time the switch was turned
00176       on, a negative value indicates the time (when converted to the
00177       absolute value) the button has not been pressed. Zero means
00178       "just initialized".
00179     
00180  * @param new_history new history value
00181  */
00182 void
00183 SwitchInterface::set_history(const float new_history)
00184 {
00185   data->history = new_history;
00186 }
00187 
00188 /** Get short_activations value.
00189  * 
00190       Number of consecutive short clicks (turned on). Can be used to recognize
00191       patterns of clicks. This is an optional field.
00192     
00193  * @return short_activations value
00194  */
00195 unsigned int
00196 SwitchInterface::short_activations() const
00197 {
00198   return data->short_activations;
00199 }
00200 
00201 /** Get maximum length of short_activations value.
00202  * @return length of short_activations value, can be length of the array or number of 
00203  * maximum number of characters for a string
00204  */
00205 size_t
00206 SwitchInterface::maxlenof_short_activations() const
00207 {
00208   return 1;
00209 }
00210 
00211 /** Set short_activations value.
00212  * 
00213       Number of consecutive short clicks (turned on). Can be used to recognize
00214       patterns of clicks. This is an optional field.
00215     
00216  * @param new_short_activations new short_activations value
00217  */
00218 void
00219 SwitchInterface::set_short_activations(const unsigned int new_short_activations)
00220 {
00221   data->short_activations = new_short_activations;
00222 }
00223 
00224 /** Get long_activations value.
00225  * 
00226       Number of consecutive short clicks (turned on). Can be used to recognize
00227       patterns of clicks. This is an optional field.
00228     
00229  * @return long_activations value
00230  */
00231 unsigned int
00232 SwitchInterface::long_activations() const
00233 {
00234   return data->long_activations;
00235 }
00236 
00237 /** Get maximum length of long_activations value.
00238  * @return length of long_activations value, can be length of the array or number of 
00239  * maximum number of characters for a string
00240  */
00241 size_t
00242 SwitchInterface::maxlenof_long_activations() const
00243 {
00244   return 1;
00245 }
00246 
00247 /** Set long_activations value.
00248  * 
00249       Number of consecutive short clicks (turned on). Can be used to recognize
00250       patterns of clicks. This is an optional field.
00251     
00252  * @param new_long_activations new long_activations value
00253  */
00254 void
00255 SwitchInterface::set_long_activations(const unsigned int new_long_activations)
00256 {
00257   data->long_activations = new_long_activations;
00258 }
00259 
00260 /** Get activation_count value.
00261  * 
00262       Number that is to be incremented whenever a short or long activation
00263       happened. Can be used to decide if a change in status happened.
00264     
00265  * @return activation_count value
00266  */
00267 unsigned int
00268 SwitchInterface::activation_count() const
00269 {
00270   return data->activation_count;
00271 }
00272 
00273 /** Get maximum length of activation_count value.
00274  * @return length of activation_count value, can be length of the array or number of 
00275  * maximum number of characters for a string
00276  */
00277 size_t
00278 SwitchInterface::maxlenof_activation_count() const
00279 {
00280   return 1;
00281 }
00282 
00283 /** Set activation_count value.
00284  * 
00285       Number that is to be incremented whenever a short or long activation
00286       happened. Can be used to decide if a change in status happened.
00287     
00288  * @param new_activation_count new activation_count value
00289  */
00290 void
00291 SwitchInterface::set_activation_count(const unsigned int new_activation_count)
00292 {
00293   data->activation_count = new_activation_count;
00294 }
00295
00296 /* =========== message create =========== */
00297 Message *
00298 SwitchInterface::create_message(const char *type) const
00299 {
00300   if ( strncmp("SetMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00301     return new SetMessage();
00302   } else if ( strncmp("EnableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00303     return new EnableSwitchMessage();
00304   } else if ( strncmp("DisableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00305     return new DisableSwitchMessage();
00306   } else {
00307     throw UnknownTypeException("The given type '%s' does not match any known "
00308                                "message type for this interface type.", type);
00309   }
00310 }
00311
00312 
00313 /** Copy values from other interface.
00314  * @param other other interface to copy values from
00315  */
00316 void
00317 SwitchInterface::copy_values(const Interface *other)
00318 {
00319   const SwitchInterface *oi = dynamic_cast<const SwitchInterface *>(other);
00320   if (oi == NULL) {
00321     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00322                                 type(), other->type());
00323   }
00324   memcpy(data, oi->data, sizeof(SwitchInterface_data_t));
00325 }
00326
00327 /* =========== messages =========== */
00328 /** @class SwitchInterface::SetMessage <interfaces/SwitchInterface.h>
00329  * SetMessage Fawkes BlackBoard Interface Message.
00330  * 
00331     
00332  */
00333
00334 
00335 /** Constructor with initial values.
00336  * @param ini_enabled initial value for enabled
00337  * @param ini_value initial value for value
00338  */
00339 SwitchInterface::SetMessage::SetMessage(const bool ini_enabled, const float ini_value) : Message("SetMessage")
00340 {
00341   data_size = sizeof(SetMessage_data_t);
00342   data_ptr  = malloc(data_size);
00343   memset(data_ptr, 0, data_size);
00344   data      = (SetMessage_data_t *)data_ptr;
00345   data->enabled = ini_enabled;
00346   data->value = ini_value;
00347   add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00348   add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
00349 }
00350 /** Constructor */
00351 SwitchInterface::SetMessage::SetMessage() : Message("SetMessage")
00352 {
00353   data_size = sizeof(SetMessage_data_t);
00354   data_ptr  = malloc(data_size);
00355   memset(data_ptr, 0, data_size);
00356   data      = (SetMessage_data_t *)data_ptr;
00357   add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00358   add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
00359 }
00360 
00361 /** Destructor */
00362 SwitchInterface::SetMessage::~SetMessage()
00363 {
00364   free(data_ptr);
00365 }
00366 
00367 /** Copy constructor.
00368  * @param m message to copy from
00369  */
00370 SwitchInterface::SetMessage::SetMessage(const SetMessage *m) : Message("SetMessage")
00371 {
00372   data_size = m->data_size;
00373   data_ptr  = malloc(data_size);
00374   memcpy(data_ptr, m->data_ptr, data_size);
00375   data      = (SetMessage_data_t *)data_ptr;
00376 }
00377
00378 /* Methods */
00379 /** Get enabled value.
00380  * 
00381       True if the switch is currently enabled.
00382     
00383  * @return enabled value
00384  */
00385 bool
00386 SwitchInterface::SetMessage::is_enabled() const
00387 {
00388   return data->enabled;
00389 }
00390 
00391 /** Get maximum length of enabled value.
00392  * @return length of enabled value, can be length of the array or number of 
00393  * maximum number of characters for a string
00394  */
00395 size_t
00396 SwitchInterface::SetMessage::maxlenof_enabled() const
00397 {
00398   return 1;
00399 }
00400 
00401 /** Set enabled value.
00402  * 
00403       True if the switch is currently enabled.
00404     
00405  * @param new_enabled new enabled value
00406  */
00407 void
00408 SwitchInterface::SetMessage::set_enabled(const bool new_enabled)
00409 {
00410   data->enabled = new_enabled;
00411 }
00412 
00413 /** Get value value.
00414  * 
00415       If switches support multiple states these can be indicated with
00416       this value. For example for a switch that notes the intensity it
00417       could be a value in the valid range.
00418     
00419  * @return value value
00420  */
00421 float
00422 SwitchInterface::SetMessage::value() const
00423 {
00424   return data->value;
00425 }
00426 
00427 /** Get maximum length of value value.
00428  * @return length of value value, can be length of the array or number of 
00429  * maximum number of characters for a string
00430  */
00431 size_t
00432 SwitchInterface::SetMessage::maxlenof_value() const
00433 {
00434   return 1;
00435 }
00436 
00437 /** Set value value.
00438  * 
00439       If switches support multiple states these can be indicated with
00440       this value. For example for a switch that notes the intensity it
00441       could be a value in the valid range.
00442     
00443  * @param new_value new value value
00444  */
00445 void
00446 SwitchInterface::SetMessage::set_value(const float new_value)
00447 {
00448   data->value = new_value;
00449 }
00450 
00451 /** Clone this message.
00452  * Produces a message of the same type as this message and copies the
00453  * data to the new message.
00454  * @return clone of this message
00455  */
00456 Message *
00457 SwitchInterface::SetMessage::clone() const
00458 {
00459   return new SwitchInterface::SetMessage(this);
00460 }
00461 /** @class SwitchInterface::EnableSwitchMessage <interfaces/SwitchInterface.h>
00462  * EnableSwitchMessage Fawkes BlackBoard Interface Message.
00463  * 
00464     
00465  */
00466
00467 
00468 /** Constructor */
00469 SwitchInterface::EnableSwitchMessage::EnableSwitchMessage() : Message("EnableSwitchMessage")
00470 {
00471   data_size = 0;
00472   data_ptr  = NULL;
00473 }
00474 
00475 /** Destructor */
00476 SwitchInterface::EnableSwitchMessage::~EnableSwitchMessage()
00477 {
00478 }
00479 
00480 /** Copy constructor.
00481  * @param m message to copy from
00482  */
00483 SwitchInterface::EnableSwitchMessage::EnableSwitchMessage(const EnableSwitchMessage *m) : Message("EnableSwitchMessage")
00484 {
00485   data_size = 0;
00486   data_ptr  = NULL;
00487 }
00488
00489 /* Methods */
00490 /** Clone this message.
00491  * Produces a message of the same type as this message and copies the
00492  * data to the new message.
00493  * @return clone of this message
00494  */
00495 Message *
00496 SwitchInterface::EnableSwitchMessage::clone() const
00497 {
00498   return new SwitchInterface::EnableSwitchMessage(this);
00499 }
00500 /** @class SwitchInterface::DisableSwitchMessage <interfaces/SwitchInterface.h>
00501  * DisableSwitchMessage Fawkes BlackBoard Interface Message.
00502  * 
00503     
00504  */
00505
00506 
00507 /** Constructor */
00508 SwitchInterface::DisableSwitchMessage::DisableSwitchMessage() : Message("DisableSwitchMessage")
00509 {
00510   data_size = 0;
00511   data_ptr  = NULL;
00512 }
00513 
00514 /** Destructor */
00515 SwitchInterface::DisableSwitchMessage::~DisableSwitchMessage()
00516 {
00517 }
00518 
00519 /** Copy constructor.
00520  * @param m message to copy from
00521  */
00522 SwitchInterface::DisableSwitchMessage::DisableSwitchMessage(const DisableSwitchMessage *m) : Message("DisableSwitchMessage")
00523 {
00524   data_size = 0;
00525   data_ptr  = NULL;
00526 }
00527
00528 /* Methods */
00529 /** Clone this message.
00530  * Produces a message of the same type as this message and copies the
00531  * data to the new message.
00532  * @return clone of this message
00533  */
00534 Message *
00535 SwitchInterface::DisableSwitchMessage::clone() const
00536 {
00537   return new SwitchInterface::DisableSwitchMessage(this);
00538 }
00539 /** Check if message is valid and can be enqueued.
00540  * @param message Message to check
00541  */
00542 bool
00543 SwitchInterface::message_valid(const Message *message) const
00544 {
00545   const SetMessage *m0 = dynamic_cast<const SetMessage *>(message);
00546   if ( m0 != NULL ) {
00547     return true;
00548   }
00549   const EnableSwitchMessage *m1 = dynamic_cast<const EnableSwitchMessage *>(message);
00550   if ( m1 != NULL ) {
00551     return true;
00552   }
00553   const DisableSwitchMessage *m2 = dynamic_cast<const DisableSwitchMessage *>(message);
00554   if ( m2 != NULL ) {
00555     return true;
00556   }
00557   return false;
00558 }
00559 
00560 /// @cond INTERNALS
00561 EXPORT_INTERFACE(SwitchInterface)
00562 /// @endcond
00563 
00564
00565 } // end namespace fawkes