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