LedInterface.cpp

00001
00002 /***************************************************************************
00003  *  LedInterface.cpp - Fawkes BlackBoard Interface - LedInterface
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/LedInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class LedInterface <interfaces/LedInterface.h>
00034  * LedInterface Fawkes BlackBoard Interface.
00035  * 
00036       This interface provides access to LEDs. The interface controls an
00037       intensity value between 0.0 (off) and 1.0 (on, max intensity). LEDs
00038       that do not support intensity setting can only be set to on and off.
00039     
00040  * @ingroup FawkesInterfaces
00041  */
00042
00043 
00044 /** ON constant */
00045 const float LedInterface::ON = 1.0;
00046 /** OFF constant */
00047 const float LedInterface::OFF = 0.0;
00048 
00049 /** Constructor */
00050 LedInterface::LedInterface() : Interface()
00051 {
00052   data_size = sizeof(LedInterface_data_t);
00053   data_ptr  = malloc(data_size);
00054   data      = (LedInterface_data_t *)data_ptr;
00055   memset(data_ptr, 0, data_size);
00056   add_fieldinfo(IFT_FLOAT, "intensity", 1, &data->intensity);
00057   add_messageinfo("SetIntensityMessage");
00058   add_messageinfo("TurnOnMessage");
00059   add_messageinfo("TurnOffMessage");
00060   unsigned char tmp_hash[] = {0xd, 0x86, 0x60, 0xcd, 0xae, 0x41, 0xa5, 0xa1, 0xbc, 0xb7, 0xf, 0x9, 0x90, 00, 0x4d, 0x40};
00061   set_hash(tmp_hash);
00062 }
00063 
00064 /** Destructor */
00065 LedInterface::~LedInterface()
00066 {
00067   free(data_ptr);
00068 }
00069 /* Methods */
00070 /** Get intensity value.
00071  * Intensity value.
00072  * @return intensity value
00073  */
00074 float
00075 LedInterface::intensity() const
00076 {
00077   return data->intensity;
00078 }
00079 
00080 /** Get maximum length of intensity value.
00081  * @return length of intensity value, can be length of the array or number of 
00082  * maximum number of characters for a string
00083  */
00084 size_t
00085 LedInterface::maxlenof_intensity() const
00086 {
00087   return 1;
00088 }
00089 
00090 /** Set intensity value.
00091  * Intensity value.
00092  * @param new_intensity new intensity value
00093  */
00094 void
00095 LedInterface::set_intensity(const float new_intensity)
00096 {
00097   data->intensity = new_intensity;
00098 }
00099
00100 /* =========== message create =========== */
00101 Message *
00102 LedInterface::create_message(const char *type) const
00103 {
00104   if ( strncmp("SetIntensityMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00105     return new SetIntensityMessage();
00106   } else if ( strncmp("TurnOnMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00107     return new TurnOnMessage();
00108   } else if ( strncmp("TurnOffMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00109     return new TurnOffMessage();
00110   } else {
00111     throw UnknownTypeException("The given type '%s' does not match any known "
00112                                "message type for this interface type.", type);
00113   }
00114 }
00115
00116 
00117 /** Copy values from other interface.
00118  * @param other other interface to copy values from
00119  */
00120 void
00121 LedInterface::copy_values(const Interface *other)
00122 {
00123   const LedInterface *oi = dynamic_cast<const LedInterface *>(other);
00124   if (oi == NULL) {
00125     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00126                                 type(), other->type());
00127   }
00128   memcpy(data, oi->data, sizeof(LedInterface_data_t));
00129 }
00130
00131 /* =========== messages =========== */
00132 /** @class LedInterface::SetIntensityMessage <interfaces/LedInterface.h>
00133  * SetIntensityMessage Fawkes BlackBoard Interface Message.
00134  * 
00135     
00136  */
00137
00138 
00139 /** Constructor with initial values.
00140  * @param ini_time_sec initial value for time_sec
00141  * @param ini_intensity initial value for intensity
00142  */
00143 LedInterface::SetIntensityMessage::SetIntensityMessage(const float ini_time_sec, const float ini_intensity) : Message("SetIntensityMessage")
00144 {
00145   data_size = sizeof(SetIntensityMessage_data_t);
00146   data_ptr  = malloc(data_size);
00147   memset(data_ptr, 0, data_size);
00148   data      = (SetIntensityMessage_data_t *)data_ptr;
00149   data->time_sec = ini_time_sec;
00150   data->intensity = ini_intensity;
00151   add_fieldinfo(IFT_FLOAT, "time_sec", 1, &data->time_sec);
00152   add_fieldinfo(IFT_FLOAT, "intensity", 1, &data->intensity);
00153 }
00154 /** Constructor */
00155 LedInterface::SetIntensityMessage::SetIntensityMessage() : Message("SetIntensityMessage")
00156 {
00157   data_size = sizeof(SetIntensityMessage_data_t);
00158   data_ptr  = malloc(data_size);
00159   memset(data_ptr, 0, data_size);
00160   data      = (SetIntensityMessage_data_t *)data_ptr;
00161   add_fieldinfo(IFT_FLOAT, "time_sec", 1, &data->time_sec);
00162   add_fieldinfo(IFT_FLOAT, "intensity", 1, &data->intensity);
00163 }
00164 
00165 /** Destructor */
00166 LedInterface::SetIntensityMessage::~SetIntensityMessage()
00167 {
00168   free(data_ptr);
00169 }
00170 
00171 /** Copy constructor.
00172  * @param m message to copy from
00173  */
00174 LedInterface::SetIntensityMessage::SetIntensityMessage(const SetIntensityMessage *m) : Message("SetIntensityMessage")
00175 {
00176   data_size = m->data_size;
00177   data_ptr  = malloc(data_size);
00178   memcpy(data_ptr, m->data_ptr, data_size);
00179   data      = (SetIntensityMessage_data_t *)data_ptr;
00180 }
00181
00182 /* Methods */
00183 /** Get time_sec value.
00184  * 
00185       Time in seconds when to reach the intensity.
00186     
00187  * @return time_sec value
00188  */
00189 float
00190 LedInterface::SetIntensityMessage::time_sec() const
00191 {
00192   return data->time_sec;
00193 }
00194 
00195 /** Get maximum length of time_sec value.
00196  * @return length of time_sec value, can be length of the array or number of 
00197  * maximum number of characters for a string
00198  */
00199 size_t
00200 LedInterface::SetIntensityMessage::maxlenof_time_sec() const
00201 {
00202   return 1;
00203 }
00204 
00205 /** Set time_sec value.
00206  * 
00207       Time in seconds when to reach the intensity.
00208     
00209  * @param new_time_sec new time_sec value
00210  */
00211 void
00212 LedInterface::SetIntensityMessage::set_time_sec(const float new_time_sec)
00213 {
00214   data->time_sec = new_time_sec;
00215 }
00216 
00217 /** Get intensity value.
00218  * Intensity value.
00219  * @return intensity value
00220  */
00221 float
00222 LedInterface::SetIntensityMessage::intensity() const
00223 {
00224   return data->intensity;
00225 }
00226 
00227 /** Get maximum length of intensity value.
00228  * @return length of intensity value, can be length of the array or number of 
00229  * maximum number of characters for a string
00230  */
00231 size_t
00232 LedInterface::SetIntensityMessage::maxlenof_intensity() const
00233 {
00234   return 1;
00235 }
00236 
00237 /** Set intensity value.
00238  * Intensity value.
00239  * @param new_intensity new intensity value
00240  */
00241 void
00242 LedInterface::SetIntensityMessage::set_intensity(const float new_intensity)
00243 {
00244   data->intensity = new_intensity;
00245 }
00246 
00247 /** Clone this message.
00248  * Produces a message of the same type as this message and copies the
00249  * data to the new message.
00250  * @return clone of this message
00251  */
00252 Message *
00253 LedInterface::SetIntensityMessage::clone() const
00254 {
00255   return new LedInterface::SetIntensityMessage(this);
00256 }
00257 /** @class LedInterface::TurnOnMessage <interfaces/LedInterface.h>
00258  * TurnOnMessage Fawkes BlackBoard Interface Message.
00259  * 
00260     
00261  */
00262
00263 
00264 /** Constructor */
00265 LedInterface::TurnOnMessage::TurnOnMessage() : Message("TurnOnMessage")
00266 {
00267   data_size = 0;
00268   data_ptr  = NULL;
00269 }
00270 
00271 /** Destructor */
00272 LedInterface::TurnOnMessage::~TurnOnMessage()
00273 {
00274 }
00275 
00276 /** Copy constructor.
00277  * @param m message to copy from
00278  */
00279 LedInterface::TurnOnMessage::TurnOnMessage(const TurnOnMessage *m) : Message("TurnOnMessage")
00280 {
00281   data_size = 0;
00282   data_ptr  = NULL;
00283 }
00284
00285 /* Methods */
00286 /** Clone this message.
00287  * Produces a message of the same type as this message and copies the
00288  * data to the new message.
00289  * @return clone of this message
00290  */
00291 Message *
00292 LedInterface::TurnOnMessage::clone() const
00293 {
00294   return new LedInterface::TurnOnMessage(this);
00295 }
00296 /** @class LedInterface::TurnOffMessage <interfaces/LedInterface.h>
00297  * TurnOffMessage Fawkes BlackBoard Interface Message.
00298  * 
00299     
00300  */
00301
00302 
00303 /** Constructor */
00304 LedInterface::TurnOffMessage::TurnOffMessage() : Message("TurnOffMessage")
00305 {
00306   data_size = 0;
00307   data_ptr  = NULL;
00308 }
00309 
00310 /** Destructor */
00311 LedInterface::TurnOffMessage::~TurnOffMessage()
00312 {
00313 }
00314 
00315 /** Copy constructor.
00316  * @param m message to copy from
00317  */
00318 LedInterface::TurnOffMessage::TurnOffMessage(const TurnOffMessage *m) : Message("TurnOffMessage")
00319 {
00320   data_size = 0;
00321   data_ptr  = NULL;
00322 }
00323
00324 /* Methods */
00325 /** Clone this message.
00326  * Produces a message of the same type as this message and copies the
00327  * data to the new message.
00328  * @return clone of this message
00329  */
00330 Message *
00331 LedInterface::TurnOffMessage::clone() const
00332 {
00333   return new LedInterface::TurnOffMessage(this);
00334 }
00335 /** Check if message is valid and can be enqueued.
00336  * @param message Message to check
00337  */
00338 bool
00339 LedInterface::message_valid(const Message *message) const
00340 {
00341   const SetIntensityMessage *m0 = dynamic_cast<const SetIntensityMessage *>(message);
00342   if ( m0 != NULL ) {
00343     return true;
00344   }
00345   const TurnOnMessage *m1 = dynamic_cast<const TurnOnMessage *>(message);
00346   if ( m1 != NULL ) {
00347     return true;
00348   }
00349   const TurnOffMessage *m2 = dynamic_cast<const TurnOffMessage *>(message);
00350   if ( m2 != NULL ) {
00351     return true;
00352   }
00353   return false;
00354 }
00355 
00356 /// @cond INTERNALS
00357 EXPORT_INTERFACE(LedInterface)
00358 /// @endcond
00359 
00360
00361 } // end namespace fawkes