Fawkes API Fawkes Development Version
|
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 data_ts = (interface_data_ts_t *)data_ptr; 00053 memset(data_ptr, 0, data_size); 00054 add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled); 00055 add_fieldinfo(IFT_FLOAT, "value", 1, &data->value); 00056 add_fieldinfo(IFT_FLOAT, "history", 1, &data->history); 00057 add_fieldinfo(IFT_UINT32, "short_activations", 1, &data->short_activations); 00058 add_fieldinfo(IFT_UINT32, "long_activations", 1, &data->long_activations); 00059 add_fieldinfo(IFT_UINT32, "activation_count", 1, &data->activation_count); 00060 add_messageinfo("SetMessage"); 00061 add_messageinfo("EnableSwitchMessage"); 00062 add_messageinfo("DisableSwitchMessage"); 00063 add_messageinfo("EnableDurationMessage"); 00064 unsigned char tmp_hash[] = {0xa7, 0xa4, 0xc, 0x19, 0x66, 0xa4, 0x87, 0x6b, 0xa9, 0x32, 0x95, 0x40, 0xc7, 0x82, 0x75, 0x6d}; 00065 set_hash(tmp_hash); 00066 } 00067 00068 /** Destructor */ 00069 SwitchInterface::~SwitchInterface() 00070 { 00071 free(data_ptr); 00072 } 00073 /* Methods */ 00074 /** Get enabled value. 00075 * 00076 True if the switch is currently enabled. 00077 00078 * @return enabled value 00079 */ 00080 bool 00081 SwitchInterface::is_enabled() const 00082 { 00083 return data->enabled; 00084 } 00085 00086 /** Get maximum length of enabled value. 00087 * @return length of enabled value, can be length of the array or number of 00088 * maximum number of characters for a string 00089 */ 00090 size_t 00091 SwitchInterface::maxlenof_enabled() const 00092 { 00093 return 1; 00094 } 00095 00096 /** Set enabled value. 00097 * 00098 True if the switch is currently enabled. 00099 00100 * @param new_enabled new enabled value 00101 */ 00102 void 00103 SwitchInterface::set_enabled(const bool new_enabled) 00104 { 00105 data->enabled = new_enabled; 00106 data_changed = true; 00107 } 00108 00109 /** Get value value. 00110 * 00111 If switches support multiple states these can be indicated with 00112 this value. For example for a switch that notes the intensity it 00113 could be a value in the valid range. 00114 00115 * @return value value 00116 */ 00117 float 00118 SwitchInterface::value() const 00119 { 00120 return data->value; 00121 } 00122 00123 /** Get maximum length of value value. 00124 * @return length of value value, can be length of the array or number of 00125 * maximum number of characters for a string 00126 */ 00127 size_t 00128 SwitchInterface::maxlenof_value() const 00129 { 00130 return 1; 00131 } 00132 00133 /** Set value value. 00134 * 00135 If switches support multiple states these can be indicated with 00136 this value. For example for a switch that notes the intensity it 00137 could be a value in the valid range. 00138 00139 * @param new_value new value value 00140 */ 00141 void 00142 SwitchInterface::set_value(const float new_value) 00143 { 00144 data->value = new_value; 00145 data_changed = true; 00146 } 00147 00148 /** Get history value. 00149 * 00150 This value records the number of seconds a switch has been 00151 enabled continuously -- or not. The time is recorded in 00152 seconds. A positive value indicates time the switch was turned 00153 on, a negative value indicates the time (when converted to the 00154 absolute value) the button has not been pressed. Zero means 00155 "just initialized". 00156 00157 * @return history value 00158 */ 00159 float 00160 SwitchInterface::history() const 00161 { 00162 return data->history; 00163 } 00164 00165 /** Get maximum length of history value. 00166 * @return length of history value, can be length of the array or number of 00167 * maximum number of characters for a string 00168 */ 00169 size_t 00170 SwitchInterface::maxlenof_history() const 00171 { 00172 return 1; 00173 } 00174 00175 /** Set history value. 00176 * 00177 This value records the number of seconds a switch has been 00178 enabled continuously -- or not. The time is recorded in 00179 seconds. A positive value indicates time the switch was turned 00180 on, a negative value indicates the time (when converted to the 00181 absolute value) the button has not been pressed. Zero means 00182 "just initialized". 00183 00184 * @param new_history new history value 00185 */ 00186 void 00187 SwitchInterface::set_history(const float new_history) 00188 { 00189 data->history = new_history; 00190 data_changed = true; 00191 } 00192 00193 /** Get short_activations value. 00194 * 00195 Number of consecutive short clicks (turned on). Can be used to recognize 00196 patterns of clicks. This is an optional field. 00197 00198 * @return short_activations value 00199 */ 00200 uint32_t 00201 SwitchInterface::short_activations() const 00202 { 00203 return data->short_activations; 00204 } 00205 00206 /** Get maximum length of short_activations value. 00207 * @return length of short_activations value, can be length of the array or number of 00208 * maximum number of characters for a string 00209 */ 00210 size_t 00211 SwitchInterface::maxlenof_short_activations() const 00212 { 00213 return 1; 00214 } 00215 00216 /** Set short_activations value. 00217 * 00218 Number of consecutive short clicks (turned on). Can be used to recognize 00219 patterns of clicks. This is an optional field. 00220 00221 * @param new_short_activations new short_activations value 00222 */ 00223 void 00224 SwitchInterface::set_short_activations(const uint32_t new_short_activations) 00225 { 00226 data->short_activations = new_short_activations; 00227 data_changed = true; 00228 } 00229 00230 /** Get long_activations value. 00231 * 00232 Number of consecutive short clicks (turned on). Can be used to recognize 00233 patterns of clicks. This is an optional field. 00234 00235 * @return long_activations value 00236 */ 00237 uint32_t 00238 SwitchInterface::long_activations() const 00239 { 00240 return data->long_activations; 00241 } 00242 00243 /** Get maximum length of long_activations value. 00244 * @return length of long_activations value, can be length of the array or number of 00245 * maximum number of characters for a string 00246 */ 00247 size_t 00248 SwitchInterface::maxlenof_long_activations() const 00249 { 00250 return 1; 00251 } 00252 00253 /** Set long_activations value. 00254 * 00255 Number of consecutive short clicks (turned on). Can be used to recognize 00256 patterns of clicks. This is an optional field. 00257 00258 * @param new_long_activations new long_activations value 00259 */ 00260 void 00261 SwitchInterface::set_long_activations(const uint32_t new_long_activations) 00262 { 00263 data->long_activations = new_long_activations; 00264 data_changed = true; 00265 } 00266 00267 /** Get activation_count value. 00268 * 00269 Number that is to be incremented whenever a short or long activation 00270 happened. Can be used to decide if a change in status happened. 00271 00272 * @return activation_count value 00273 */ 00274 uint32_t 00275 SwitchInterface::activation_count() const 00276 { 00277 return data->activation_count; 00278 } 00279 00280 /** Get maximum length of activation_count value. 00281 * @return length of activation_count value, can be length of the array or number of 00282 * maximum number of characters for a string 00283 */ 00284 size_t 00285 SwitchInterface::maxlenof_activation_count() const 00286 { 00287 return 1; 00288 } 00289 00290 /** Set activation_count value. 00291 * 00292 Number that is to be incremented whenever a short or long activation 00293 happened. Can be used to decide if a change in status happened. 00294 00295 * @param new_activation_count new activation_count value 00296 */ 00297 void 00298 SwitchInterface::set_activation_count(const uint32_t new_activation_count) 00299 { 00300 data->activation_count = new_activation_count; 00301 data_changed = true; 00302 } 00303 00304 /* =========== message create =========== */ 00305 Message * 00306 SwitchInterface::create_message(const char *type) const 00307 { 00308 if ( strncmp("SetMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) { 00309 return new SetMessage(); 00310 } else if ( strncmp("EnableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) { 00311 return new EnableSwitchMessage(); 00312 } else if ( strncmp("DisableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) { 00313 return new DisableSwitchMessage(); 00314 } else if ( strncmp("EnableDurationMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) { 00315 return new EnableDurationMessage(); 00316 } else { 00317 throw UnknownTypeException("The given type '%s' does not match any known " 00318 "message type for this interface type.", type); 00319 } 00320 } 00321 00322 00323 /** Copy values from other interface. 00324 * @param other other interface to copy values from 00325 */ 00326 void 00327 SwitchInterface::copy_values(const Interface *other) 00328 { 00329 const SwitchInterface *oi = dynamic_cast<const SwitchInterface *>(other); 00330 if (oi == NULL) { 00331 throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)", 00332 type(), other->type()); 00333 } 00334 memcpy(data, oi->data, sizeof(SwitchInterface_data_t)); 00335 } 00336 00337 const char * 00338 SwitchInterface::enum_tostring(const char *enumtype, int val) const 00339 { 00340 throw UnknownTypeException("Unknown enum type %s", enumtype); 00341 } 00342 00343 /* =========== messages =========== */ 00344 /** @class SwitchInterface::SetMessage <interfaces/SwitchInterface.h> 00345 * SetMessage Fawkes BlackBoard Interface Message. 00346 * 00347 00348 */ 00349 00350 00351 /** Constructor with initial values. 00352 * @param ini_enabled initial value for enabled 00353 * @param ini_value initial value for value 00354 */ 00355 SwitchInterface::SetMessage::SetMessage(const bool ini_enabled, const float ini_value) : Message("SetMessage") 00356 { 00357 data_size = sizeof(SetMessage_data_t); 00358 data_ptr = malloc(data_size); 00359 memset(data_ptr, 0, data_size); 00360 data = (SetMessage_data_t *)data_ptr; 00361 data_ts = (message_data_ts_t *)data_ptr; 00362 data->enabled = ini_enabled; 00363 data->value = ini_value; 00364 add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled); 00365 add_fieldinfo(IFT_FLOAT, "value", 1, &data->value); 00366 } 00367 /** Constructor */ 00368 SwitchInterface::SetMessage::SetMessage() : Message("SetMessage") 00369 { 00370 data_size = sizeof(SetMessage_data_t); 00371 data_ptr = malloc(data_size); 00372 memset(data_ptr, 0, data_size); 00373 data = (SetMessage_data_t *)data_ptr; 00374 data_ts = (message_data_ts_t *)data_ptr; 00375 add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled); 00376 add_fieldinfo(IFT_FLOAT, "value", 1, &data->value); 00377 } 00378 00379 /** Destructor */ 00380 SwitchInterface::SetMessage::~SetMessage() 00381 { 00382 free(data_ptr); 00383 } 00384 00385 /** Copy constructor. 00386 * @param m message to copy from 00387 */ 00388 SwitchInterface::SetMessage::SetMessage(const SetMessage *m) : Message("SetMessage") 00389 { 00390 data_size = m->data_size; 00391 data_ptr = malloc(data_size); 00392 memcpy(data_ptr, m->data_ptr, data_size); 00393 data = (SetMessage_data_t *)data_ptr; 00394 data_ts = (message_data_ts_t *)data_ptr; 00395 } 00396 00397 /* Methods */ 00398 /** Get enabled value. 00399 * 00400 True if the switch is currently enabled. 00401 00402 * @return enabled value 00403 */ 00404 bool 00405 SwitchInterface::SetMessage::is_enabled() const 00406 { 00407 return data->enabled; 00408 } 00409 00410 /** Get maximum length of enabled value. 00411 * @return length of enabled value, can be length of the array or number of 00412 * maximum number of characters for a string 00413 */ 00414 size_t 00415 SwitchInterface::SetMessage::maxlenof_enabled() const 00416 { 00417 return 1; 00418 } 00419 00420 /** Set enabled value. 00421 * 00422 True if the switch is currently enabled. 00423 00424 * @param new_enabled new enabled value 00425 */ 00426 void 00427 SwitchInterface::SetMessage::set_enabled(const bool new_enabled) 00428 { 00429 data->enabled = new_enabled; 00430 } 00431 00432 /** Get value value. 00433 * 00434 If switches support multiple states these can be indicated with 00435 this value. For example for a switch that notes the intensity it 00436 could be a value in the valid range. 00437 00438 * @return value value 00439 */ 00440 float 00441 SwitchInterface::SetMessage::value() const 00442 { 00443 return data->value; 00444 } 00445 00446 /** Get maximum length of value value. 00447 * @return length of value value, can be length of the array or number of 00448 * maximum number of characters for a string 00449 */ 00450 size_t 00451 SwitchInterface::SetMessage::maxlenof_value() const 00452 { 00453 return 1; 00454 } 00455 00456 /** Set value value. 00457 * 00458 If switches support multiple states these can be indicated with 00459 this value. For example for a switch that notes the intensity it 00460 could be a value in the valid range. 00461 00462 * @param new_value new value value 00463 */ 00464 void 00465 SwitchInterface::SetMessage::set_value(const float new_value) 00466 { 00467 data->value = new_value; 00468 } 00469 00470 /** Clone this message. 00471 * Produces a message of the same type as this message and copies the 00472 * data to the new message. 00473 * @return clone of this message 00474 */ 00475 Message * 00476 SwitchInterface::SetMessage::clone() const 00477 { 00478 return new SwitchInterface::SetMessage(this); 00479 } 00480 /** @class SwitchInterface::EnableSwitchMessage <interfaces/SwitchInterface.h> 00481 * EnableSwitchMessage Fawkes BlackBoard Interface Message. 00482 * 00483 00484 */ 00485 00486 00487 /** Constructor */ 00488 SwitchInterface::EnableSwitchMessage::EnableSwitchMessage() : Message("EnableSwitchMessage") 00489 { 00490 data_size = sizeof(EnableSwitchMessage_data_t); 00491 data_ptr = malloc(data_size); 00492 memset(data_ptr, 0, data_size); 00493 data = (EnableSwitchMessage_data_t *)data_ptr; 00494 data_ts = (message_data_ts_t *)data_ptr; 00495 } 00496 00497 /** Destructor */ 00498 SwitchInterface::EnableSwitchMessage::~EnableSwitchMessage() 00499 { 00500 free(data_ptr); 00501 } 00502 00503 /** Copy constructor. 00504 * @param m message to copy from 00505 */ 00506 SwitchInterface::EnableSwitchMessage::EnableSwitchMessage(const EnableSwitchMessage *m) : Message("EnableSwitchMessage") 00507 { 00508 data_size = m->data_size; 00509 data_ptr = malloc(data_size); 00510 memcpy(data_ptr, m->data_ptr, data_size); 00511 data = (EnableSwitchMessage_data_t *)data_ptr; 00512 data_ts = (message_data_ts_t *)data_ptr; 00513 } 00514 00515 /* Methods */ 00516 /** Clone this message. 00517 * Produces a message of the same type as this message and copies the 00518 * data to the new message. 00519 * @return clone of this message 00520 */ 00521 Message * 00522 SwitchInterface::EnableSwitchMessage::clone() const 00523 { 00524 return new SwitchInterface::EnableSwitchMessage(this); 00525 } 00526 /** @class SwitchInterface::DisableSwitchMessage <interfaces/SwitchInterface.h> 00527 * DisableSwitchMessage Fawkes BlackBoard Interface Message. 00528 * 00529 00530 */ 00531 00532 00533 /** Constructor */ 00534 SwitchInterface::DisableSwitchMessage::DisableSwitchMessage() : Message("DisableSwitchMessage") 00535 { 00536 data_size = sizeof(DisableSwitchMessage_data_t); 00537 data_ptr = malloc(data_size); 00538 memset(data_ptr, 0, data_size); 00539 data = (DisableSwitchMessage_data_t *)data_ptr; 00540 data_ts = (message_data_ts_t *)data_ptr; 00541 } 00542 00543 /** Destructor */ 00544 SwitchInterface::DisableSwitchMessage::~DisableSwitchMessage() 00545 { 00546 free(data_ptr); 00547 } 00548 00549 /** Copy constructor. 00550 * @param m message to copy from 00551 */ 00552 SwitchInterface::DisableSwitchMessage::DisableSwitchMessage(const DisableSwitchMessage *m) : Message("DisableSwitchMessage") 00553 { 00554 data_size = m->data_size; 00555 data_ptr = malloc(data_size); 00556 memcpy(data_ptr, m->data_ptr, data_size); 00557 data = (DisableSwitchMessage_data_t *)data_ptr; 00558 data_ts = (message_data_ts_t *)data_ptr; 00559 } 00560 00561 /* Methods */ 00562 /** Clone this message. 00563 * Produces a message of the same type as this message and copies the 00564 * data to the new message. 00565 * @return clone of this message 00566 */ 00567 Message * 00568 SwitchInterface::DisableSwitchMessage::clone() const 00569 { 00570 return new SwitchInterface::DisableSwitchMessage(this); 00571 } 00572 /** @class SwitchInterface::EnableDurationMessage <interfaces/SwitchInterface.h> 00573 * EnableDurationMessage Fawkes BlackBoard Interface Message. 00574 * 00575 00576 */ 00577 00578 00579 /** Constructor with initial values. 00580 * @param ini_duration initial value for duration 00581 * @param ini_value initial value for value 00582 */ 00583 SwitchInterface::EnableDurationMessage::EnableDurationMessage(const float ini_duration, const float ini_value) : Message("EnableDurationMessage") 00584 { 00585 data_size = sizeof(EnableDurationMessage_data_t); 00586 data_ptr = malloc(data_size); 00587 memset(data_ptr, 0, data_size); 00588 data = (EnableDurationMessage_data_t *)data_ptr; 00589 data_ts = (message_data_ts_t *)data_ptr; 00590 data->duration = ini_duration; 00591 data->value = ini_value; 00592 add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration); 00593 add_fieldinfo(IFT_FLOAT, "value", 1, &data->value); 00594 } 00595 /** Constructor */ 00596 SwitchInterface::EnableDurationMessage::EnableDurationMessage() : Message("EnableDurationMessage") 00597 { 00598 data_size = sizeof(EnableDurationMessage_data_t); 00599 data_ptr = malloc(data_size); 00600 memset(data_ptr, 0, data_size); 00601 data = (EnableDurationMessage_data_t *)data_ptr; 00602 data_ts = (message_data_ts_t *)data_ptr; 00603 add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration); 00604 add_fieldinfo(IFT_FLOAT, "value", 1, &data->value); 00605 } 00606 00607 /** Destructor */ 00608 SwitchInterface::EnableDurationMessage::~EnableDurationMessage() 00609 { 00610 free(data_ptr); 00611 } 00612 00613 /** Copy constructor. 00614 * @param m message to copy from 00615 */ 00616 SwitchInterface::EnableDurationMessage::EnableDurationMessage(const EnableDurationMessage *m) : Message("EnableDurationMessage") 00617 { 00618 data_size = m->data_size; 00619 data_ptr = malloc(data_size); 00620 memcpy(data_ptr, m->data_ptr, data_size); 00621 data = (EnableDurationMessage_data_t *)data_ptr; 00622 data_ts = (message_data_ts_t *)data_ptr; 00623 } 00624 00625 /* Methods */ 00626 /** Get duration value. 00627 * Duration in seconds for which 00628 the switch should be enabled. 00629 * @return duration value 00630 */ 00631 float 00632 SwitchInterface::EnableDurationMessage::duration() const 00633 { 00634 return data->duration; 00635 } 00636 00637 /** Get maximum length of duration value. 00638 * @return length of duration value, can be length of the array or number of 00639 * maximum number of characters for a string 00640 */ 00641 size_t 00642 SwitchInterface::EnableDurationMessage::maxlenof_duration() const 00643 { 00644 return 1; 00645 } 00646 00647 /** Set duration value. 00648 * Duration in seconds for which 00649 the switch should be enabled. 00650 * @param new_duration new duration value 00651 */ 00652 void 00653 SwitchInterface::EnableDurationMessage::set_duration(const float new_duration) 00654 { 00655 data->duration = new_duration; 00656 } 00657 00658 /** Get value value. 00659 * 00660 If switches support multiple states these can be indicated with 00661 this value. For example for a switch that notes the intensity it 00662 could be a value in the valid range. 00663 00664 * @return value value 00665 */ 00666 float 00667 SwitchInterface::EnableDurationMessage::value() const 00668 { 00669 return data->value; 00670 } 00671 00672 /** Get maximum length of value value. 00673 * @return length of value value, can be length of the array or number of 00674 * maximum number of characters for a string 00675 */ 00676 size_t 00677 SwitchInterface::EnableDurationMessage::maxlenof_value() const 00678 { 00679 return 1; 00680 } 00681 00682 /** Set value value. 00683 * 00684 If switches support multiple states these can be indicated with 00685 this value. For example for a switch that notes the intensity it 00686 could be a value in the valid range. 00687 00688 * @param new_value new value value 00689 */ 00690 void 00691 SwitchInterface::EnableDurationMessage::set_value(const float new_value) 00692 { 00693 data->value = new_value; 00694 } 00695 00696 /** Clone this message. 00697 * Produces a message of the same type as this message and copies the 00698 * data to the new message. 00699 * @return clone of this message 00700 */ 00701 Message * 00702 SwitchInterface::EnableDurationMessage::clone() const 00703 { 00704 return new SwitchInterface::EnableDurationMessage(this); 00705 } 00706 /** Check if message is valid and can be enqueued. 00707 * @param message Message to check 00708 * @return true if the message is valid, false otherwise. 00709 */ 00710 bool 00711 SwitchInterface::message_valid(const Message *message) const 00712 { 00713 const SetMessage *m0 = dynamic_cast<const SetMessage *>(message); 00714 if ( m0 != NULL ) { 00715 return true; 00716 } 00717 const EnableSwitchMessage *m1 = dynamic_cast<const EnableSwitchMessage *>(message); 00718 if ( m1 != NULL ) { 00719 return true; 00720 } 00721 const DisableSwitchMessage *m2 = dynamic_cast<const DisableSwitchMessage *>(message); 00722 if ( m2 != NULL ) { 00723 return true; 00724 } 00725 const EnableDurationMessage *m3 = dynamic_cast<const EnableDurationMessage *>(message); 00726 if ( m3 != NULL ) { 00727 return true; 00728 } 00729 return false; 00730 } 00731 00732 /// @cond INTERNALS 00733 EXPORT_INTERFACE(SwitchInterface) 00734 /// @endcond 00735 00736 00737 } // end namespace fawkes