Fawkes API  Fawkes Development Version
SwitchInterface.cpp
1 
2 /***************************************************************************
3  * SwitchInterface.cpp - Fawkes BlackBoard Interface - SwitchInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2008 Tim Niemueller
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/SwitchInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class SwitchInterface <interfaces/SwitchInterface.h>
36  * SwitchInterface Fawkes BlackBoard Interface.
37  *
38  This interface provides access to LEDs. The interface controls
39  an intensity value between 0.0 (off) and 1.0 (on, max
40  intensity). LEDs that do not support intensity setting can only
41  be set to on and off.
42 
43  * @ingroup FawkesInterfaces
44  */
45 
46 
47 
48 /** Constructor */
49 SwitchInterface::SwitchInterface() : Interface()
50 {
51  data_size = sizeof(SwitchInterface_data_t);
52  data_ptr = malloc(data_size);
53  data = (SwitchInterface_data_t *)data_ptr;
54  data_ts = (interface_data_ts_t *)data_ptr;
55  memset(data_ptr, 0, data_size);
56  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
57  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
58  add_fieldinfo(IFT_FLOAT, "history", 1, &data->history);
59  add_fieldinfo(IFT_UINT32, "short_activations", 1, &data->short_activations);
60  add_fieldinfo(IFT_UINT32, "long_activations", 1, &data->long_activations);
61  add_fieldinfo(IFT_UINT32, "activation_count", 1, &data->activation_count);
62  add_messageinfo("SetMessage");
63  add_messageinfo("EnableSwitchMessage");
64  add_messageinfo("DisableSwitchMessage");
65  add_messageinfo("EnableDurationMessage");
66  unsigned char tmp_hash[] = {0xa7, 0xa4, 0xc, 0x19, 0x66, 0xa4, 0x87, 0x6b, 0xa9, 0x32, 0x95, 0x40, 0xc7, 0x82, 0x75, 0x6d};
67  set_hash(tmp_hash);
68 }
69 
70 /** Destructor */
71 SwitchInterface::~SwitchInterface()
72 {
73  free(data_ptr);
74 }
75 /* Methods */
76 /** Get enabled value.
77  *
78  True if the switch is currently enabled.
79 
80  * @return enabled value
81  */
82 bool
84 {
85  return data->enabled;
86 }
87 
88 /** Get maximum length of enabled value.
89  * @return length of enabled value, can be length of the array or number of
90  * maximum number of characters for a string
91  */
92 size_t
94 {
95  return 1;
96 }
97 
98 /** Set enabled value.
99  *
100  True if the switch is currently enabled.
101 
102  * @param new_enabled new enabled value
103  */
104 void
105 SwitchInterface::set_enabled(const bool new_enabled)
106 {
107  data->enabled = new_enabled;
108  data_changed = true;
109 }
110 
111 /** Get value value.
112  *
113  If switches support multiple states these can be indicated with
114  this value. For example for a switch that notes the intensity it
115  could be a value in the valid range.
116 
117  * @return value value
118  */
119 float
121 {
122  return data->value;
123 }
124 
125 /** Get maximum length of value value.
126  * @return length of value value, can be length of the array or number of
127  * maximum number of characters for a string
128  */
129 size_t
131 {
132  return 1;
133 }
134 
135 /** Set value value.
136  *
137  If switches support multiple states these can be indicated with
138  this value. For example for a switch that notes the intensity it
139  could be a value in the valid range.
140 
141  * @param new_value new value value
142  */
143 void
144 SwitchInterface::set_value(const float new_value)
145 {
146  data->value = new_value;
147  data_changed = true;
148 }
149 
150 /** Get history value.
151  *
152  This value records the number of seconds a switch has been
153  enabled continuously -- or not. The time is recorded in
154  seconds. A positive value indicates time the switch was turned
155  on, a negative value indicates the time (when converted to the
156  absolute value) the button has not been pressed. Zero means
157  "just initialized".
158 
159  * @return history value
160  */
161 float
163 {
164  return data->history;
165 }
166 
167 /** Get maximum length of history value.
168  * @return length of history value, can be length of the array or number of
169  * maximum number of characters for a string
170  */
171 size_t
173 {
174  return 1;
175 }
176 
177 /** Set history value.
178  *
179  This value records the number of seconds a switch has been
180  enabled continuously -- or not. The time is recorded in
181  seconds. A positive value indicates time the switch was turned
182  on, a negative value indicates the time (when converted to the
183  absolute value) the button has not been pressed. Zero means
184  "just initialized".
185 
186  * @param new_history new history value
187  */
188 void
189 SwitchInterface::set_history(const float new_history)
190 {
191  data->history = new_history;
192  data_changed = true;
193 }
194 
195 /** Get short_activations value.
196  *
197  Number of consecutive short clicks (turned on). Can be used to recognize
198  patterns of clicks. This is an optional field.
199 
200  * @return short_activations value
201  */
202 uint32_t
204 {
205  return data->short_activations;
206 }
207 
208 /** Get maximum length of short_activations value.
209  * @return length of short_activations value, can be length of the array or number of
210  * maximum number of characters for a string
211  */
212 size_t
214 {
215  return 1;
216 }
217 
218 /** Set short_activations value.
219  *
220  Number of consecutive short clicks (turned on). Can be used to recognize
221  patterns of clicks. This is an optional field.
222 
223  * @param new_short_activations new short_activations value
224  */
225 void
226 SwitchInterface::set_short_activations(const uint32_t new_short_activations)
227 {
228  data->short_activations = new_short_activations;
229  data_changed = true;
230 }
231 
232 /** Get long_activations value.
233  *
234  Number of consecutive short clicks (turned on). Can be used to recognize
235  patterns of clicks. This is an optional field.
236 
237  * @return long_activations value
238  */
239 uint32_t
241 {
242  return data->long_activations;
243 }
244 
245 /** Get maximum length of long_activations value.
246  * @return length of long_activations value, can be length of the array or number of
247  * maximum number of characters for a string
248  */
249 size_t
251 {
252  return 1;
253 }
254 
255 /** Set long_activations value.
256  *
257  Number of consecutive short clicks (turned on). Can be used to recognize
258  patterns of clicks. This is an optional field.
259 
260  * @param new_long_activations new long_activations value
261  */
262 void
263 SwitchInterface::set_long_activations(const uint32_t new_long_activations)
264 {
265  data->long_activations = new_long_activations;
266  data_changed = true;
267 }
268 
269 /** Get activation_count value.
270  *
271  Number that is to be incremented whenever a short or long activation
272  happened. Can be used to decide if a change in status happened.
273 
274  * @return activation_count value
275  */
276 uint32_t
278 {
279  return data->activation_count;
280 }
281 
282 /** Get maximum length of activation_count value.
283  * @return length of activation_count value, can be length of the array or number of
284  * maximum number of characters for a string
285  */
286 size_t
288 {
289  return 1;
290 }
291 
292 /** Set activation_count value.
293  *
294  Number that is to be incremented whenever a short or long activation
295  happened. Can be used to decide if a change in status happened.
296 
297  * @param new_activation_count new activation_count value
298  */
299 void
300 SwitchInterface::set_activation_count(const uint32_t new_activation_count)
301 {
302  data->activation_count = new_activation_count;
303  data_changed = true;
304 }
305 
306 /* =========== message create =========== */
307 Message *
309 {
310  if ( strncmp("SetMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
311  return new SetMessage();
312  } else if ( strncmp("EnableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
313  return new EnableSwitchMessage();
314  } else if ( strncmp("DisableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
315  return new DisableSwitchMessage();
316  } else if ( strncmp("EnableDurationMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
317  return new EnableDurationMessage();
318  } else {
319  throw UnknownTypeException("The given type '%s' does not match any known "
320  "message type for this interface type.", type);
321  }
322 }
323 
324 
325 /** Copy values from other interface.
326  * @param other other interface to copy values from
327  */
328 void
330 {
331  const SwitchInterface *oi = dynamic_cast<const SwitchInterface *>(other);
332  if (oi == NULL) {
333  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
334  type(), other->type());
335  }
336  memcpy(data, oi->data, sizeof(SwitchInterface_data_t));
337 }
338 
339 const char *
340 SwitchInterface::enum_tostring(const char *enumtype, int val) const
341 {
342  throw UnknownTypeException("Unknown enum type %s", enumtype);
343 }
344 
345 /* =========== messages =========== */
346 /** @class SwitchInterface::SetMessage <interfaces/SwitchInterface.h>
347  * SetMessage Fawkes BlackBoard Interface Message.
348  *
349 
350  */
351 
352 
353 /** Constructor with initial values.
354  * @param ini_enabled initial value for enabled
355  * @param ini_value initial value for value
356  */
357 SwitchInterface::SetMessage::SetMessage(const bool ini_enabled, const float ini_value) : Message("SetMessage")
358 {
359  data_size = sizeof(SetMessage_data_t);
360  data_ptr = malloc(data_size);
361  memset(data_ptr, 0, data_size);
362  data = (SetMessage_data_t *)data_ptr;
364  data->enabled = ini_enabled;
365  data->value = ini_value;
366  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
367  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
368 }
369 /** Constructor */
371 {
372  data_size = sizeof(SetMessage_data_t);
373  data_ptr = malloc(data_size);
374  memset(data_ptr, 0, data_size);
375  data = (SetMessage_data_t *)data_ptr;
377  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
378  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
379 }
380 
381 /** Destructor */
383 {
384  free(data_ptr);
385 }
386 
387 /** Copy constructor.
388  * @param m message to copy from
389  */
391 {
392  data_size = m->data_size;
393  data_ptr = malloc(data_size);
394  memcpy(data_ptr, m->data_ptr, data_size);
395  data = (SetMessage_data_t *)data_ptr;
397 }
398 
399 /* Methods */
400 /** Get enabled value.
401  *
402  True if the switch is currently enabled.
403 
404  * @return enabled value
405  */
406 bool
408 {
409  return data->enabled;
410 }
411 
412 /** Get maximum length of enabled value.
413  * @return length of enabled value, can be length of the array or number of
414  * maximum number of characters for a string
415  */
416 size_t
418 {
419  return 1;
420 }
421 
422 /** Set enabled value.
423  *
424  True if the switch is currently enabled.
425 
426  * @param new_enabled new enabled value
427  */
428 void
430 {
431  data->enabled = new_enabled;
432 }
433 
434 /** Get value value.
435  *
436  If switches support multiple states these can be indicated with
437  this value. For example for a switch that notes the intensity it
438  could be a value in the valid range.
439 
440  * @return value value
441  */
442 float
444 {
445  return data->value;
446 }
447 
448 /** Get maximum length of value value.
449  * @return length of value value, can be length of the array or number of
450  * maximum number of characters for a string
451  */
452 size_t
454 {
455  return 1;
456 }
457 
458 /** Set value value.
459  *
460  If switches support multiple states these can be indicated with
461  this value. For example for a switch that notes the intensity it
462  could be a value in the valid range.
463 
464  * @param new_value new value value
465  */
466 void
468 {
469  data->value = new_value;
470 }
471 
472 /** Clone this message.
473  * Produces a message of the same type as this message and copies the
474  * data to the new message.
475  * @return clone of this message
476  */
477 Message *
479 {
480  return new SwitchInterface::SetMessage(this);
481 }
482 /** @class SwitchInterface::EnableSwitchMessage <interfaces/SwitchInterface.h>
483  * EnableSwitchMessage Fawkes BlackBoard Interface Message.
484  *
485 
486  */
487 
488 
489 /** Constructor */
491 {
492  data_size = sizeof(EnableSwitchMessage_data_t);
493  data_ptr = malloc(data_size);
494  memset(data_ptr, 0, data_size);
495  data = (EnableSwitchMessage_data_t *)data_ptr;
497 }
498 
499 /** Destructor */
501 {
502  free(data_ptr);
503 }
504 
505 /** Copy constructor.
506  * @param m message to copy from
507  */
509 {
510  data_size = m->data_size;
511  data_ptr = malloc(data_size);
512  memcpy(data_ptr, m->data_ptr, data_size);
513  data = (EnableSwitchMessage_data_t *)data_ptr;
515 }
516 
517 /* Methods */
518 /** Clone this message.
519  * Produces a message of the same type as this message and copies the
520  * data to the new message.
521  * @return clone of this message
522  */
523 Message *
525 {
526  return new SwitchInterface::EnableSwitchMessage(this);
527 }
528 /** @class SwitchInterface::DisableSwitchMessage <interfaces/SwitchInterface.h>
529  * DisableSwitchMessage Fawkes BlackBoard Interface Message.
530  *
531 
532  */
533 
534 
535 /** Constructor */
537 {
538  data_size = sizeof(DisableSwitchMessage_data_t);
539  data_ptr = malloc(data_size);
540  memset(data_ptr, 0, data_size);
541  data = (DisableSwitchMessage_data_t *)data_ptr;
543 }
544 
545 /** Destructor */
547 {
548  free(data_ptr);
549 }
550 
551 /** Copy constructor.
552  * @param m message to copy from
553  */
555 {
556  data_size = m->data_size;
557  data_ptr = malloc(data_size);
558  memcpy(data_ptr, m->data_ptr, data_size);
559  data = (DisableSwitchMessage_data_t *)data_ptr;
561 }
562 
563 /* Methods */
564 /** Clone this message.
565  * Produces a message of the same type as this message and copies the
566  * data to the new message.
567  * @return clone of this message
568  */
569 Message *
571 {
572  return new SwitchInterface::DisableSwitchMessage(this);
573 }
574 /** @class SwitchInterface::EnableDurationMessage <interfaces/SwitchInterface.h>
575  * EnableDurationMessage Fawkes BlackBoard Interface Message.
576  *
577 
578  */
579 
580 
581 /** Constructor with initial values.
582  * @param ini_duration initial value for duration
583  * @param ini_value initial value for value
584  */
585 SwitchInterface::EnableDurationMessage::EnableDurationMessage(const float ini_duration, const float ini_value) : Message("EnableDurationMessage")
586 {
587  data_size = sizeof(EnableDurationMessage_data_t);
588  data_ptr = malloc(data_size);
589  memset(data_ptr, 0, data_size);
590  data = (EnableDurationMessage_data_t *)data_ptr;
592  data->duration = ini_duration;
593  data->value = ini_value;
594  add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration);
595  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
596 }
597 /** Constructor */
599 {
600  data_size = sizeof(EnableDurationMessage_data_t);
601  data_ptr = malloc(data_size);
602  memset(data_ptr, 0, data_size);
603  data = (EnableDurationMessage_data_t *)data_ptr;
605  add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration);
606  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
607 }
608 
609 /** Destructor */
611 {
612  free(data_ptr);
613 }
614 
615 /** Copy constructor.
616  * @param m message to copy from
617  */
619 {
620  data_size = m->data_size;
621  data_ptr = malloc(data_size);
622  memcpy(data_ptr, m->data_ptr, data_size);
623  data = (EnableDurationMessage_data_t *)data_ptr;
625 }
626 
627 /* Methods */
628 /** Get duration value.
629  * Duration in seconds for which
630  the switch should be enabled.
631  * @return duration value
632  */
633 float
635 {
636  return data->duration;
637 }
638 
639 /** Get maximum length of duration value.
640  * @return length of duration value, can be length of the array or number of
641  * maximum number of characters for a string
642  */
643 size_t
645 {
646  return 1;
647 }
648 
649 /** Set duration value.
650  * Duration in seconds for which
651  the switch should be enabled.
652  * @param new_duration new duration value
653  */
654 void
656 {
657  data->duration = new_duration;
658 }
659 
660 /** Get value value.
661  *
662  If switches support multiple states these can be indicated with
663  this value. For example for a switch that notes the intensity it
664  could be a value in the valid range.
665 
666  * @return value value
667  */
668 float
670 {
671  return data->value;
672 }
673 
674 /** Get maximum length of value value.
675  * @return length of value value, can be length of the array or number of
676  * maximum number of characters for a string
677  */
678 size_t
680 {
681  return 1;
682 }
683 
684 /** Set value value.
685  *
686  If switches support multiple states these can be indicated with
687  this value. For example for a switch that notes the intensity it
688  could be a value in the valid range.
689 
690  * @param new_value new value value
691  */
692 void
694 {
695  data->value = new_value;
696 }
697 
698 /** Clone this message.
699  * Produces a message of the same type as this message and copies the
700  * data to the new message.
701  * @return clone of this message
702  */
703 Message *
705 {
707 }
708 /** Check if message is valid and can be enqueued.
709  * @param message Message to check
710  * @return true if the message is valid, false otherwise.
711  */
712 bool
714 {
715  const SetMessage *m0 = dynamic_cast<const SetMessage *>(message);
716  if ( m0 != NULL ) {
717  return true;
718  }
719  const EnableSwitchMessage *m1 = dynamic_cast<const EnableSwitchMessage *>(message);
720  if ( m1 != NULL ) {
721  return true;
722  }
723  const DisableSwitchMessage *m2 = dynamic_cast<const DisableSwitchMessage *>(message);
724  if ( m2 != NULL ) {
725  return true;
726  }
727  const EnableDurationMessage *m3 = dynamic_cast<const EnableDurationMessage *>(message);
728  if ( m3 != NULL ) {
729  return true;
730  }
731  return false;
732 }
733 
734 /// @cond INTERNALS
735 EXPORT_INTERFACE(SwitchInterface)
736 /// @endcond
737 
738 
739 } // end namespace fawkes
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:124
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
SetMessage Fawkes BlackBoard Interface Message.
void set_value(const float new_value)
Set value value.
size_t maxlenof_long_activations() const
Get maximum length of long_activations value.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:314
void set_enabled(const bool new_enabled)
Set enabled value.
Fawkes library namespace.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:129
size_t maxlenof_history() const
Get maximum length of history value.
uint32_t short_activations() const
Get short_activations value.
uint32_t activation_count() const
Get activation_count value.
float value() const
Get value value.
void set_value(const float new_value)
Set value value.
void set_history(const float new_history)
Set history value.
uint32_t long_activations() const
Get long_activations value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
bool is_enabled() const
Get enabled value.
void set_short_activations(const uint32_t new_short_activations)
Set short_activations value.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
float value() const
Get value value.
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:373
virtual Message * create_message(const char *type) const
Create message based on type name.
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
SwitchInterface Fawkes BlackBoard Interface.
const char * type() const
Get type of interface.
Definition: interface.cpp:651
void set_activation_count(const uint32_t new_activation_count)
Set activation_count value.
void set_enabled(const bool new_enabled)
Set enabled value.
size_t maxlenof_value() const
Get maximum length of value value.
DisableSwitchMessage Fawkes BlackBoard Interface Message.
float history() const
Get history value.
virtual Message * clone() const
Clone this message.
bool is_enabled() const
Get enabled value.
size_t maxlenof_value() const
Get maximum length of value value.
void set_long_activations(const uint32_t new_long_activations)
Set long_activations value.
float field
Definition: types.h:45
size_t maxlenof_activation_count() const
Get maximum length of activation_count value.
float duration() const
Get duration value.
virtual void copy_values(const Interface *other)
Copy values from other interface.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
virtual Message * clone() const
Clone this message.
void set_value(const float new_value)
Set value value.
virtual Message * clone() const
Clone this message.
EnableDurationMessage Fawkes BlackBoard Interface Message.
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
size_t maxlenof_short_activations() const
Get maximum length of short_activations value.
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:436
size_t maxlenof_enabled() const
Get maximum length of enabled value.
boolean field
Definition: types.h:36
void set_duration(const float new_duration)
Set duration value.
size_t maxlenof_enabled() const
Get maximum length of enabled value.
virtual Message * clone() const
Clone this message.
size_t maxlenof_duration() const
Get maximum length of duration value.
const char * type() const
Get message type.
Definition: message.cpp:378
size_t maxlenof_value() const
Get maximum length of value value.
32 bit unsigned integer field
Definition: types.h:42